Thread: How to completely move a table to another schema?

How to completely move a table to another schema?

From
Chris Travers
Date:
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)
WHEREtypnamespace = (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



Re: How to completely move a table to another schema?

From
Tom Lane
Date:
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