Re: Move table between schemas

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Markus Schaber <schabios(at)logi-track(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Move table between schemas
Date: 2004-11-20 16:36:54
Message-ID: 21309.1100968614@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Markus Schaber <schabios(at)logi-track(dot)com> writes:
> Andrew Sullivan <ajs(at)crankycanuck(dot)ca> wrote:
>> CREATE TABLE newschema.newtable AS SELECT * FROM oldschema.oldtable
>> oughta work.

> - The operation is not atomic, thus there may be inserts and updates
> into the old table that get lost while the "CREATE...SELECT...;DROP
> TABLE...;" runs.

You'd deal with that by taking a lock on the old table. Any sort of
catalog-munging solution would have to do the same (though admittedly it
wouldn't need to hold the lock as long).

> Is there any (possibly ugly, fiddling with system tables) atomic way to
> move a table between schemas?

Offhand:
* update table's pg_class.relnamespace field
* update pg_depend entry that links table to namespace
* repeat for table's rowtype (pg_type entry)
* repeat for each index on the table
* repeat for each constraint on the table

Not sure if that's all the places or not...

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jeff Eckermann 2004-11-20 17:13:39 Re: How to get the max on a char column?
Previous Message Reza Shanbehbazari Mirzaei 2004-11-20 16:35:33 User defined types