How to completely move a table to another schema?

Lists: pgsql-sql
From: Chris Travers <chris(at)travelamericas(dot)com>
To:
Cc: pgsql-sql(at)postgresql(dot)org
Subject: How to completely move a table to another schema?
Date: 2003-12-10 12:21:08
Message-ID: 1071041216.2212.73.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: 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


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris Travers <chris(at)travelamericas(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to completely move a table to another schema?
Date: 2003-12-10 16:12:16
Message-ID: 6768.1071072736@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Chris Travers <chris(at)travelamericas(dot)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