Re: Move table between schemas

From: Markus Schaber <schabios(at)logi-track(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Move table between schemas
Date: 2004-11-20 10:44:11
Message-ID: 20041120114411.28c05fcf@kingfisher.intern.logi-track.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, Andrew,

On Tue, 16 Nov 2004 06:05:38 -0500
Andrew Sullivan <ajs(at)crankycanuck(dot)ca> wrote:

> > Is there an easy way to move a table to another schema in PostgreSQL 7.4?
> >
> > ALTER TABLE and ALTER SCHEMA don't have this options.
>
> CREATE TABLE newschema.newtable AS SELECT * FROM oldschema.oldtable
> oughta work.

This has several drawbacks I can see for now:

- For large tables (some Gigs of data), this needs a long time and
produces heavy I/O load on the server.

- You need twice the disk space until you can delete the old table.

- Indices, triggers, sequences and constraints are not transferred. When
the target schema is first in the search path of the application, this
means that the application works on an incomplete table until I finished
the transition..

- It does not automatically convert views or foreign key constraints
that point to the table.

- 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.

Is there any (possibly ugly, fiddling with system tables) atomic way to
move a table between schemas? It should not be much more difficult
compared to e. G. renaming a table to implement this, so I couuld not
imagine this does not exist until I tried to find out how to do it.

Thanks,
markus
--
markus schaber | dipl. informatiker
logi-track ag | rennweg 14-16 | ch 8001 zürich
phone +41-43-888 62 52 | fax +41-43-888 62 53
mailto:schabios(at)logi-track(dot)com | www.logi-track.com

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rodrigo Carvalhaes 2004-11-20 14:53:55 How to get the max on a char column?
Previous Message Erik Thiele 2004-11-20 09:24:46 Re: get sequence value of insert command