Re: Data visibility - Mailing list pgsql-general

From Tom Lane
Subject Re: Data visibility
Date
Msg-id 24765.1160939750@sss.pgh.pa.us
Whole thread Raw
In response to Data visibility  (Rafal Pietrak <rafal@zorro.isa-geek.com>)
Responses Re: Data visibility  (Rafal Pietrak <rafal@zorro.isa-geek.com>)
List pgsql-general
Rafal Pietrak <rafal@zorro.isa-geek.com> writes:
> CREATE TABLE master (id int not null unique, info text, ....);
> CREATE TABLE aux (master int references master(id), info text, ...);
> CREATE FUNCTION adjust() RETURNS "trigger" AS $$ BEGIN
>     new.id := 1000-old.id;
>     INSERT INTO aux (master, info) VALUES (new.id, 'hello world');
>     RETURN new;
> END $$ LANGUAGE plpgsql;
> CREATE TRIGGER pico BEFORE INSERT ON master FOR EACH ROW EXECUTE
> PROCEDURE adjust();

> But in my trigger function "adjust()", executed within a transaction
> opened by "INSERT INTO master", the 'fresh' data (set by "new.id :=
> value") isn't visible to other commands (like: INSERT INTO aux...).

Well, of course not: it's a BEFORE trigger, so the row insertion hasn't
actually happened yet.  I think you need to split this operation into a
BEFORE trigger that changes the ID, and an AFTER trigger that propagates
the data into the other table.

            regards, tom lane

pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Same-page UPDATEs in bloated tables
Next
From: Tom Lane
Date:
Subject: Re: Same-page UPDATEs in bloated tables