pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Travers <chris@travelamericas.com>
Cc: pgsql-sql@postgresql.org
Subject: How to completely move a table to another schema?
Date: 10 Dec 2003 20:21:08 +0800
Message-ID: <1071041216.2212.73.camel@localhost.localdomain> (raw)

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




view thread (2+ messages)  latest in thread

Message-ID: <1071041216.2212.73.camel@localhost.localdomain>
Permalink:  ../1071041216.2212.73.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1071041216.2212.73.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: chris@travelamericas.com
  Subject: Re: How to completely move a table to another schema?
  In-Reply-To: <1071041216.2212.73.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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