Update columns in the same table in a deferred constraint trigger

From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Update columns in the same table in a deferred constraint trigger
Date: 2014-07-29 09:52:12
Message-ID: VisenaEmail.39.362cb9f24c93a770.14781853e94@tc7-on
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi all.   I have this simple schema:   create table fisk(
    name varchar primary key,
    autofisk varchar
);   I want to update the column "autofisk" on commit based the value of
"name", so I created this trigger:   CREATE OR REPLACE FUNCTION fisk_tf()
returns TRIGGER AS $$
BEGIN
    raise notice 'name %', NEW.name <http://NEW.name>;
    NEW.autofisk = NEW.name <http://NEW.name> || CURRENT_TIMESTAMP::text;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;   CREATE CONSTRAINT TRIGGER fisk_t AFTER INSERT OR
UPDATE ON fisk DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE fisk_tf();   The problem is that "autofisk" is
never populated: andreak=# begin;
BEGIN
andreak=# insert into fisk(name) values ('a');
INSERT 0 1
andreak=# commit;
NOTICE:  name a
COMMIT
andreak=# table fisk;
 name | autofisk
------+----------
 a    |
(1 row)   Is it possible to do what I want, namely to update a column in a
table in an AFTER INSERT OR UPDATE constraint trigger on the same table?  
Thanks.   -- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56
963 andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com> www.visena.com
<https://www.visena.com> <https://www.visena.com>

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2014-07-29 09:56:17 Re: Update columns in the same table in a deferred constraint trigger
Previous Message Vinayak Pokale 2014-07-25 08:59:12 Re: PGsql function timestamp issue