Fixing corrupt table - postgresql

I was trying to upgrade Sentry and a table in my database got corrupt.
After reading about vacuum and reindex I was able to track down the issue to a single table.
Doing a select * from any other table works just fine, but this particular one seems to be problematic. Is there a way I can fix the table, or, worst case scenario, dump all other tables somehow?
pg_dump -T corrupt_table > bkp.sql doesn't work:
bash-4.4# pg_dump -U XXXXXX -T sentry_identityprovider sentry > bkp.sql
pg_dump: [archiver (db)] query failed: ERROR: cache lookup failed for attribute 1 of relation 45941
pg_dump: [archiver (db)] query was: SELECT tableoid, oid, conname, confrelid, pg_catalog.pg_get_constraintdef(oid) AS condef FROM pg_catalog.pg_constraint WHERE conrelid = '45954'::pg_catalog.oid AND contype = 'f'
Please avoid comments like "Well, go get your backups". I'm asking because I don't have a backup.
Also, please avoid comments like "Well, if you don't have backups, shit happens". I'm asking because there was an error in the execution of the backups and none were made.
Also, please avoid any other helpless comments related to backups. Really. You're not helping me that way.

At some stage I have been able to dump and restore an individual table as per below. Note in help for pg_dump that you should be able to do a full dump and exclude the corrupt table, never tried it. Not sure why yours fails, dumping a single good table might give the answer. Hope it works for you.
pg_dump -t good_table old_DB -U youruser -f good_table_BUP.sql
psql -f good_table_BUP.sql new_DB
-t, --table=TABLE dump the named table(s) only
-T, --exclude-table=TABLE do NOT dump the named table(s)

Related

pg_restore throws error when trying to restore a table with constraints

I try dumping tables from a production environment to a dev one. However, when dumping and restoring this table, using the following command:
pg_restore --no-owner --no-acl --clean --if-exists -d database dump_file.dump
I get an error stating that I can't drop that table unless I use something like CASCADE (i.e. dropping all other tables that depend on that one). Is there a way to determine the tables to be dropped? is there a way of maybe state in the pg_dump command to dump the table I'm looking to dump and all related tables ?
Here's the error raised:
pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 4066; 2606 30526 CONSTRAINT table1 pkey user
pg_restore: error: could not execute query: ERROR: cannot drop constraint pkey on table public.table1 because other objects depend on it
DETAIL: constraint id_fkey on table public.dag depends on index public.pkey
constraint id_fkey on table public.dag depends on index public.pkey
HINT: Use DROP ... CASCADE to drop the dependent objects too...
You have a table on the dev database that has a pkey that is dependent and therefore can not be dropped before the restore. This is proper behavior.
I am not seeing dumping/restoring a particular table. You are dumping/restoring the entire database.
If you want recreate the production database as a dev one then do:
pg_restore -C --no-owner --no-acl --clean --if-exists -d postgres dump_file.dump
The -C with --clean will DROP DATABASE db_name and then rebuild it from scratch by connecting to the database postgres to do the DROP/CREATE db_name and then connect to db_name to load the rest of the objects.
This is the best way to clean out cruft and start at a consistent state.
UPDATE
Update your question with the pg_dump command so it is evident what you are doing.
If you want to see whether a particular table has dependencies, in the original database use psql and do \d the_table to see what the dependencies to and from the table are. If you tell pg_dump to dump a single table it will dump just that table. It will not follow dependencies and dump those also. That is up to you to do.
Look into using a schema management tool to do your changes/migrations. I use Sqitch for this.

Dump broken Postgres database

I have database, it's work, but has some problems. I need to migrate database to new Version Postgres, so when I try to make dump with pg_dump or pg_dumpall I got somethink like this:
pg_dump: [archiver (db)] query failed: ERROR: unexpected chunk number 2 (expected 0) for toast value 78482 in pg_toast_2618
pg_dump: [archiver (db)] query was: SELECT pg_catalog.pg_get_viewdef('78478'::pg_catalog.oid) AS viewdef
But, if I make dump only one separate table it works.
I want to make dump by piecemeal. I already got structure of all tables, script for create actual indexes. When I made pg_dumpall of other normal database, I saw in dump-file something like:
ALTER TABLE ONLY schema_name.table_name ALTER COLUMN id_column SET DEFAULT nextval('sequence_name'::regclass);
I need write script which set sequence for each table, where I can to see matching between sequences and tables?
Someone has expirience in such migration? Which problems waits me later? There are special instruments for migration database postgres? Any diferent solutions?

Postgres Dump Excluding Corrupted Page Block

I Tried to take a dump of my database. While Dumping i received the below error. I knew a table in my database is corrupted and i can use exclude option to exclude the corrupted table and take dump of the remaining tables. But I am searching for an option to dump even the corrupted table excluding the invalid page blocks.
The command i used to take dump is :
pg_dump -U postgres -p 5432 -d DBNAME -v -n SCHEMA_NAME -f "OUTFILE.sql"
The error prints:
pg_dump: Dumping the contents of table "TABLE_NAME" failed: PQgetResult() failed.
pg_dump: Error message from server: ERROR: invalid page in block 2145280 of relation base/16384/25464
I was able to access the corrupted table and its data. So i believe the part of the data which i can access can be dumped. Kindly share if there is any possibility to achieve this.
You could set zero_damaged_pages = on, then PostgreSQL should zero (and ignore) such pages.

Error in creating a database dump on PostgreSQL

When trying to make a postgreSQL database dump we got the following error and the process stops immediately.
Command used:
openbravo#master.akluck.com:~
07/26 11:48:11> pg_dump -U tad -h localhost -p 5932 -F c -b -v -f /home/openbravo/dump26072018.dmp openbravo
Output:
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: schema with OID 67046 does not exist
pg_dump: *** aborted because of error
Can anyone guide me how to sort this issue?
Update:
I followed this tutorial
http://www.aukema.org/2011/06/fixing-complex-corruption-in-my-dna.html
And I can see there are objects without a schemaname in the pg_tables.
But I don't know how to update those missing schemanames in the pg_tables. The last part of the tutorial is not quite explanatory. Hope someone can throw some light.
Finally found a way to take the backup by excluding the corrupted table as follows
pg_dump --exclude-table=ad_context_info -h localhost -p 5932 -U postgres > dumpsabnew.dmp

postgresql how to backup and overwrite specific tables

I need to be able to somehow get a set of tables from my dev db into my production db. I've just been creating a dump file from the dev db and using pg_restore on the production db. The problem now is that I need to preserve one table(called users) on the production db while replacing the others
I think I have the dump properly from this command
pg_dump -Fc --no-acl --no-owner -h localhost -U <USER> --exclude-table=users* --data-only <DB NAME> > test.dump
But I can't get the restore part to work. I tried the following command
pg_restore -Fc --no-acl --no-owner -h <PROD HOST> -U <USER> -d <DB NAME> -p <PORT> <FILE LOCATION>
BUt I get the following errors
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 2009; 0 121384 TABLE DATA idx_descs Jason
pg_restore: [archiver (db)] COPY failed for table "idx_descs": ERROR: duplicate key value violates unique constraint "idx_descs_pkey"
DETAIL: Key (id)=(6) already exists.
CONTEXT: COPY idx_descs, line 1
It seems like for the tables I'm trying to overwrite, it is just trying to append the data and running into trouble because there are now duplicate primary keys. Any Ideas how to do this? Thanks
So you need to reassign primary keys?
You could try restoring to a temporary table (say for instance, in failing case: idx_desc_temp), then doing something like:
with t as ( select * from idx_descs_temp )
insert into idx_descs
select id + 100000 [or whatever], [other fields] from t;
Afterwards you need to reset sequences (if applicable -- fill in sequence name....):
select setval( 'idx_descs_id_seq'::regclass, 100000 + [suitable increment]);
If you have a large # of tables you could try to automate using the system catalog.
Note though that you also have to renumber foreign key refs. Possibly less pain would be to move data in production db first. If you are using an ORM, you could also automate via application APIs.