Re: Insert with pl/pgsql trigger

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Woody Woodring" <george(dot)woodring(at)iglass(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Insert with pl/pgsql trigger
Date: 2008-05-07 15:24:21
Message-ID: 7638.1210173861@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Woody Woodring" <george(dot)woodring(at)iglass(dot)net> writes:
> My trigger is :
> CREATE OR REPLACE FUNCTION log_cpe_health() RETURNS trigger AS '
> DECLARE
> BEGIN
> -- Update last outage before inserting
> EXECUTE ''INSERT INTO cpe_health_history VALUES '' || NEW;
> END;
> ' LANGUAGE plpgsql;

That's never going to work because of quoting issues, and it wouldn't be
an efficient way if it did work (because of having to re-parse and
re-plan the INSERT each time). And if it did act the way you are
imagining, it still wouldn't be a good way because you typically want
some additional columns in the log table, such as a timestamp.

In recent releases you can do it like this:

INSERT INTO cpe_health_history VALUES (NEW.*);

which can be extended to, eg,

INSERT INTO cpe_health_history VALUES (NEW.*, now());

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Julien Cigar 2008-05-07 16:25:36 how to check if a point is contained in a polygon ?
Previous Message Tom Lane 2008-05-07 15:12:29 Re: how to check if a point is contained in a polygon ?