Re: Recreate primary key without dropping foreign keys? - Mailing list pgsql-admin

From Igor Neyman
Subject Re: Recreate primary key without dropping foreign keys?
Date
Msg-id A76B25F2823E954C9E45E32FA49D70EC03DACFB6@mail.corp.perceptron.com
Whole thread Raw
In response to Re: Recreate primary key without dropping foreign keys?  (Chris Ernst <cernst@zvelo.com>)
Responses Re: Recreate primary key without dropping foreign keys?  (amador alvarez <aalvarez@d2.com>)
Re: Recreate primary key without dropping foreign keys?  (Chris Ernst <cernst@zvelo.com>)
List pgsql-admin
> -----Original Message-----
> From: Chris Ernst [mailto:cernst@zvelo.com]
> Sent: Monday, April 16, 2012 10:55 PM
> To: pgsql-admin@postgresql.org
> Subject: Re: Recreate primary key without dropping foreign keys?
>
> On 04/16/2012 07:02 PM, amador alvarez wrote:
> > How about deferring the FK's while recreating the PK ?
> > or using a temporary parallel table to be pointed by the other tables
> > (FK) and swap it up on the recreation.
>
> Hmm.. Interesting.   But it appears that you have to declare the
> foreign
> key as deferrable at creation.  Is there any way to set an existing
> foreign key as deferrable?
>
>     - Chris

May be this (from the docs) would help:

"ADD table_constraint [ NOT VALID ]

    This form adds a new constraint to a table using the same syntax as CREATE TABLE, plus the option NOT VALID, which
iscurrently only allowed for foreign key constraints. If the constraint is marked NOT VALID, the potentially-lengthy
initialcheck to verify that all rows in the table satisfy the constraint is skipped. The constraint will still be
enforcedagainst subsequent inserts or updates (that is, they'll fail unless there is a matching row in the referenced
table).But the database will not assume that the constraint holds for all rows in the table, until it is validated by
usingthe VALIDATE CONSTRAINT option." 

Using this option you can drop and recreate corresponding FKs in a very short time, and start using them, while
postponingto run "VALIDATE CONSTRAINT" for later. 

It's similar to Oracle's adding FK with "NOCHECK" option, but if IRC there is no need to run "VALIDATE CONSTRAINT"
later.

Regards,
Igor Neyman

pgsql-admin by date:

Previous
From: "Albe Laurenz"
Date:
Subject: Re: invalid byte sequence for encoding "UTF8": 0xf481 - how could this happen?
Next
From: amador alvarez
Date:
Subject: Re: Recreate primary key without dropping foreign keys?