Is there a safe way to modify the table pg_constraint so as no more checking be done ( temporarily )?

旧巷老猫 提交于 2021-02-08 06:11:56

问题


So as to restore a database from a dump, I want to temporarily disable all the constraints in all the tables of my database, which content I have emptied ( postregsql 8.3).

Is there a safe way to modify a field in the pg_constraint table that easily allows to bypass the constraint ?

I don't think so when I look at the documentation pg_constraint.

Is it possible to drop the pg_constraint table content, then refill it again ?

If not, how people do restore/copy databases with tables that are full of constraints and foreign keys ? (my destination database has the schema but all the columns are empty).

Edit: althought Erwin Brandstetter's answer is the good answer (with wise warning) to my precise question... the solution to my general problem for avoiding foreign key errors during restore is to use the flag --disable-triggers when using pg_restore.

finally, my two lines commands are now:

pg_dump.exe  -h %ipAddress% -p 5432 -U postgres -F c -a -v -f %file% mydb
pg_restore.exe -h localhost -p 5432 -U postgres -d mydb -v %file%  --disable-triggers

and it works ok.


回答1:


You can ...

ALTER TABLE tbl DISABLE TRIGGER ALL;

This disables all triggers of the table permanently. So don't forget to later run:

ALTER TABLE tbl ENABLE TRIGGER ALL;

-> 8.3 manual

You can ...

SET CONSTRAINTS ALL DEFERRED;

This makes all deferrable constraints wait until the end of the transaction.
-> 8.3 manual

You should never tinker with tables in the system catalog manually unless you are a hacker and know exactly what you are doing. Mortal humans should use DDL commands exclusively to affect the system catalog.




回答2:


If your constraints are DEFERRABLE, then you can do this:

SET CONSTRAINTS DEFERRED

However this just moves constraint checking on the next COMMIT - in other words its per transaction.

Generally for moving data, deploy some sort of backup strategy.



来源:https://stackoverflow.com/questions/12093654/is-there-a-safe-way-to-modify-the-table-pg-constraint-so-as-no-more-checking-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!