Re: Insert with pl/pgsql trigger - Mailing list pgsql-sql

From Tom Lane
Subject Re: Insert with pl/pgsql trigger
Date
Msg-id 7638.1210173861@sss.pgh.pa.us
Whole thread Raw
In response to Insert with pl/pgsql trigger  ("Woody Woodring" <george.woodring@iglass.net>)
List pgsql-sql
"Woody Woodring" <george.woodring@iglass.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


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: how to check if a point is contained in a polygon ?
Next
From: "Matthew T. O'Connor"
Date:
Subject: Joining with result of a plpgsql function