Search Postgresql Archives

Re: How to temporarily disable a table's FK constraints?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Nov 5, 2007, at 10:50 AM, Kynn Jones wrote:

Hi, everyone.

Is there a standard way to disable a table foreign-key constraint temporarily?

I thought that this would be a fairly common thing to want to do, but
I only found this snippet online:

-- to disable
UPDATE pg_class SET reltriggers=0 WHERE relname = 'your_table';

-- to re-enable
UPDATE pg_class SET reltriggers = count( * )
FROM pg_trigger WHERE pg_class.oid=tgrelid AND relname = 'your_table';

and it appears that one needs to be root to execute these statements.

Is there any other way for non-root users?

Can you explain what it is you're actually trying to do? As in, what's your use case for needing to do this? While there isn't any specific 'disable foreign key' functionality, there are different solutions to what use cases where people think they need this. The one you listed is actually a total hack and should really be avoided unless your really know what you're doing as what you're doing is forcing an inconsistency in the catalogs and if you forget to restore them with that second query, well, good luck to the next guy trying to figure out what you did. You'd be better off just dropping the foreign key than going that route.

I think a good addition to the pieces of advice that get tacked on to the end of the list messages would be something along the lines of: "Don't edit the catalogs unless you absolutely, positively know what you're doing and even then, think again."

Erik Jones

Software Developer | Emma®
erik@xxxxxxxxxx
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com



---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux