Re: How to temporarily disable a table's FK constraints? - Mailing list pgsql-general

From Erik Jones
Subject Re: How to temporarily disable a table's FK constraints?
Date
Msg-id D6311235-B987-4C05-AF78-E8E456B76B1B@myemma.com
Whole thread Raw
In response to How to temporarily disable a table's FK constraints?  ("Kynn Jones" <kynnjo@gmail.com>)
Responses Re: How to temporarily disable a table's FK constraints?  ("Kynn Jones" <kynnjo@gmail.com>)
List pgsql-general
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@myemma.com
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



pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: Restore a database
Next
From: Ron Johnson
Date:
Subject: Re: How to temporarily disable a table's FK constraints?