Question of using trigger's OLD in EXECUTE - Mailing list pgsql-general

From Nim Li
Subject Question of using trigger's OLD in EXECUTE
Date
Msg-id 4AE1CB4F.4020603@sri.utoronto.ca
Whole thread Raw
In response to Incompatible library : Missing Magic Block  (<A.Bhattacharya@sungard.com>)
Responses Re: Question of using trigger's OLD in EXECUTE
List pgsql-general
Hello,

I'm new to PostgreSQL and wonder if anyone can help.

I'm creating an after-update-trigger for all tables, which copy the old
records to a backup table.  In my test, the table with this trigger has
only two columns - one BIGINT and one VARCHAR.

Also I'd like to pass the backup table's name through a parameter
because I may reuse this function for more than one backup tables.

=====
CREATE OR REPLACE FUNCTION cp_tbl() RETURNS TRIGGER AS $proc$
    BEGIN
       EXECUTE 'INSERT INTO ' ||
       TG_ARGV[0]             ||
       ' SELECT '             ||
       OLD;
       RETURN NEW;
    END;
$proc$ LANGUAGE plpgsql;
=====

At run-time, it prompts an error:

====
ERROR:  column "beginning" does not exist
LINE 1: INSERT INTO test_log SELECT (1,BEGINNING)
                                            ^
QUERY:  INSERT INTO test_log SELECT (1,BEGINNING)
CONTEXT:  PL/pgSQL function "cp_tbl" line 2 at EXECUTE statement
====

"beginning" is the actual data I stored in the second column of the table.

I think the issue is related to the use of OLD in the EXECUTE statement.

Does anyone have any idea how to fix it?

Many thanks!!

Nim

pgsql-general by date:

Previous
From: Merlin Moncure
Date:
Subject: Re: Incompatible library : Missing Magic Block
Next
From: Tom Lane
Date:
Subject: Re: pg 8.4 (Auto)-vacuumlo