pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to completely move a table to another schema?
2+ messages / 2 participants
[nested] [flat]

* How to completely move a table to another schema?
@ 2003-12-10 12:21  Chris Travers <chris@travelamericas.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Chris Travers @ 2003-12-10 12:21 UTC (permalink / raw)
  To: ; +Cc: pgsql-sql

Hi all;

I have a function which moves a table from one schema to another by
updating the relnamespace field of pg_class:

CREATE OR REPLACE FUNCTION move_relation(VARCHAR, VARCHAR, VARCHAR)
RETURNS BOOL
AS '
-- $1 is the table name
-- $2 is the source schema
-- $3 is the destination schema
--
        UPDATE pg_catalog.pg_class
        SET relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
                                WHERE nspname = $3)
        WHERE relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
                                WHERE nspname = $2)
                AND relname = $1;
                                   
	UPDATE pg_catalog.pg_type
        SET typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
                                WHERE nspname = $3)
        WHERE typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
                                WHERE nspname = $2)
                AND typname = $1;
                                                                                
        SELECT TRUE;
' LANGUAGE SQL;
                                                     
Am I missing anything?  I have already had a few problems that led me to discover 
that I needed to put in the second update query.  Just figured I would check.

Best Wishes,
Chris Travers




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: How to completely move a table to another schema?
@ 2003-12-10 16:12  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Chris Travers <chris@travelamericas.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2003-12-10 16:12 UTC (permalink / raw)
  To: Chris Travers <chris@travelamericas.com>; +Cc: pgsql-sql

Chris Travers <chris@travelamericas.com> writes:
> I have a function which moves a table from one schema to another by
> updating the relnamespace field of pg_class:
                                                     
> Am I missing anything?

pg_constraint entries related to the table.
Also you need to recurse for each index of the table.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-12-10 16:12 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-12-10 12:21 How to completely move a table to another schema? Chris Travers <chris@travelamericas.com>
2003-12-10 16:12 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox