Search code examples
postgresqloid

How to find the OID of the rows in a table in postgres?


I have a problem encountered lately in our Postgres database, when I query: select * from myTable, it results to, 'could not open relation with OID 892600370'. And it's front end application can't run properly anymore. Base on my research, I determined the column that has an error but I want exactly to locate the rows OID of the column so that I can modify it. Please help.

Thank you in advance.


Solution

  • You've got a corrupted database. Might be a bug, but more likely bad hardware. If you have a recent backup, just use that. I'm guessing you don't though.

    1. Make sure you locate any backups of either the database or its file tree and keep them safe.

    2. Stop the PostgreSQL server and take a file backup of the entire database tree (base, global, pg_xlog - everything at that level). It is now safe to start fiddling...

    3. Now, start the database server again and dump tables one at a time. If a table won't dump, try dropping any indexes and foreign-key constraints and give it another go.

    4. For a table that won't dump, it might be just certain rows. Drop any indexes and dump a range of rows using COPY ... SELECT. That should let you narrow down any corrupted rows and get the rest.

    5. Now you have a mostly-recovered database, restore it on another machine and take whatever steps are needed to establish what is damaged/lost and what needs to be done.

    6. Run a full set of tests on the old machine and see if anything needs replacement. Consider whether your monitoring needs improvement.

    Then - make sure you keep proper backups next time, that way you won't have to do all this, you'll just use them instead.