How to completely move a table to another schema?

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
Thread:
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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message DHS Webmaster 2003-12-10 14:20:28 first of month
Previous Message Olivier Hubaut 2003-12-10 09:17:24 Re: Bug in JDBC CREATE FUNCTION syntax?