Re: renaming a table, and its primary key constraint

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Jim <jhefferon(at)smcvt(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: renaming a table, and its primary key constraint
Date: 2005-08-05 09:29:32
Message-ID: 20050805092927.GD9171@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Jul 30, 2005 at 02:02:16PM -0700, Jim wrote:
> Hello,
>
> Forgive me if this is a dopey question. I'm running a web app with a
> dB that takes me a half hour to regenerate. Instead of closing down
> every day, I'd like insert the data into a temp table "stuff_tmp" and
> then rename that to the permanent table "stuff", so the application is
> not off-line for so long.
>
> The table "stuff_tmp" has a primary key constraint. When I rename the
> table, this constraint does not get renamed-- it continues to have the
> name "stuff_tmp_pkey"-- and (you guessed it) the next time I run the
> script pg complains that it can't make "stuff_tmp" because the
> constraint already exists.
>
> That I can see, I can't rename the constraint. Do I have that correct?

Well, you can. Something like:

BEGIN:
ALTER TABLE stuff_tmp_pkey RENAME TO stuff_pkey;
ALTER TABLE stuff ALTER pkey SET DEFAULT nextval('stuff_pkey');
COMMIT;

> Can I simulate (sort of) a primary key constraint by adding a UNIQUE
> index, and a NOT NULL check? That is, if I add those two, do I lose
> anything compared with the original primary key constraint?

Only that foreign key references don't automatically find the right
column by default, but you can still specify directly...

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ben-Nes Yonatan 2005-08-05 09:37:47 Weird lock or bug maybe?
Previous Message Martijn van Oosterhout 2005-08-05 09:24:00 Re: Slow Inserts on 1 table?