Re: Triggers and Multiple Schemas.

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Paul Newman <paul(dot)newman(at)tripoint(dot)co(dot)uk>
Cc: Louis Gonzales <louis(dot)gonzales(at)linuxlouis(dot)net>, Scott Marlowe <smarlowe(at)g2switchworks(dot)com>, pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Triggers and Multiple Schemas.
Date: 2006-03-08 23:48:06
Message-ID: 20060308234805.GA58186@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Mar 08, 2006 at 11:16:55PM -0000, Paul Newman wrote:
> So how can I get the schema name of the calling table trigger and use it
> in the form of set Search_path at the beginning of the function ?

Here's an example:

CREATE FUNCTION trigfunc() RETURNS trigger AS $$
DECLARE
schemaname text;
oldpath text;
BEGIN
SELECT INTO schemaname n.nspname
FROM pg_namespace AS n
JOIN pg_class AS c ON c.relnamespace = n.oid
WHERE c.oid = TG_RELID;

oldpath := current_setting('search_path');

PERFORM set_config('search_path', schemaname, true);
RAISE INFO 'schema = % oldpath = %', schemaname, oldpath;
PERFORM set_config('search_path', oldpath, false);

RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE SCHEMA foo;
CREATE SCHEMA bar;

CREATE TABLE foo.tablename (id integer);
CREATE TABLE bar.tablename (id integer);

CREATE TRIGGER footrig BEFORE INSERT OR UPDATE ON foo.tablename
FOR EACH ROW EXECUTE PROCEDURE trigfunc();

CREATE TRIGGER bartrig BEFORE INSERT OR UPDATE ON bar.tablename
FOR EACH ROW EXECUTE PROCEDURE trigfunc();

Now let's insert some records:

test=> INSERT INTO foo.tablename VALUES (1);
INFO: schema = foo oldpath = public
INSERT 0 1

test=> INSERT INTO bar.tablename VALUES (2);
INFO: schema = bar oldpath = public
INSERT 0 1

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Noel Faux 2006-03-09 00:13:40 Re: Data corruption zero a file - help!!
Previous Message Paul Newman 2006-03-08 23:19:33 Re: database/schema level triggers?