Re: delete column

From: Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)my>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, "Hillensbeck, Preston" <PHillensbeck(at)sfbcic(dot)com>
Cc: "'webmaster'" <webmaster(at)harbornet(dot)com>, "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: delete column
Date: 2002-04-27 00:31:02
Message-ID: 5.1.0.14.1.20020427082032.009d1a50@192.228.128.13
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 04:58 PM 4/26/02 -0400, Bruce Momjian wrote:
>Hillensbeck, Preston wrote:
> > There isn't a DROP COLUMN function yet, but you can do this...
> >
> > SELECT ... -- select all columns but the one you want to remove
> > INTO TABLE new_table
> > FROM old_table;
> > DROP TABLE old_table;
> > ALTER TABLE new_table RENAME TO old_table;
> >
> > This is straight out of Bruce Momjian's book, so you can give him
> credit for
> > this :)
>
>This is from the FAQ, which appears in my book. I think I wrote that
>too, or at least with help from others. Wish we had a cleaner way, but
>right now, that is all we have.

The following variant makes use of Postgresql's advantages:

BEGIN;
create new_table ... -- the way you want it to be
lock table old_table;
SELECT ... -- select all columns but the one you want to remove
INTO TABLE new_table
FROM old_table;
DROP TABLE old_table;
ALTER TABLE new_table RENAME TO old_table;
COMMIT;

I did something similar on a production server (after backing up just in
case and testing on a test db) and it worked well. So 3 cheers for
rollback/commits of drop table :).

Got to be careful to get any sequences right tho (grrr!).

Link.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message rpetike 2002-04-27 00:41:48 large object thx & vacuuming question
Previous Message Risko Peter 2002-04-27 00:23:56 large object thx & vacuuming question