row archiving trigger function

From: Louis-David Mitterrand <vindex(at)apartia(dot)ch>
To: pgsql-hackers(at)postgresql(dot)org
Subject: row archiving trigger function
Date: 2001-04-17 16:00:05
Message-ID: 20010417180005.A9229@apartia.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

In our DB schema we have defined a class of tables containing important
data for which we'd like to keep an audit trail of any change. These
tables have the following inheritance structure:

+----> <table> (real, live table with constraints)
<table>_type |
+----> <table>_archive (archive without any constraints)

The parent <table>_type contains no data, is only used to define the
columns common to <table> and <table>_archive.

On each UPDATE or DELETE to any <table> we would like to record the
modified/deleted row as is in the <table>_archive.

Here is the trigger function that I'm working on:

create function archive_row() returns opaque as '
DECLARE
rec RECORD;
/* initialise future query string
*/
att text := ''INSERT INTO '';
BEGIN
/* prepare the query, converting <table> to <table>_archive
*/
att := att || TG_RELNAME || ''_archive VALUES ('';
/* get all column names for trigger <table> through PG system tables
*/
FOR rec IN SELECT a.attname FROM pg_class c, pg_attribute a
WHERE c.relname = TG_RELNAME AND a.attnum > 0
AND a.attrelid = c.oid ORDER BY a.attnum LOOP
/* RAISE NOTICE ''column name for % is %'', TG_RELNAME, rec.attname;*/
att := att || ''OLD.'' || rec.attname || '','';
END LOOP;
/* remove last coma, add closing paren
*/
att := rtrim(att,'','') || '')'';
RAISE NOTICE ''query is %'', att;
EXECUTE att;
RETURN NEW;
END;
' language 'plpgsql';

The EXECUTE gives the following error:

psql:archive.sql:40: ERROR: OLD used in non-rule query

The best solution would be to simply do:

INSERT INTO table_archive SELECT OLD.*;

but it doesn't work.

Is there a clean solution in pl/pgsql or should I directly try in C?

--
THERAMENE: Prends soin après ma mort de ma chère Aricie.
Cher ami, si mon père un jour désabusé
Plaint le malheur d'un fils faussement accusé,
Pour apaiser mon sang et mon ombre plaintive,
Dis-lui qu'avec douceur il traite sa captive,
Qu'il lui rende... A ce mot ce héros expiré
N'a laissé dans mes bras qu'un corps défiguré,
Triste objet, où des Dieux triomphe la colère,
Et que méconnaîtrait l'oeil même de son père.
(Phèdre, J-B Racine, acte 5, scène 6)

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2001-04-17 16:08:37 Re: AW: timeout on lock feature
Previous Message Zeugswetter Andreas SB 2001-04-17 15:37:47 AW: AW: AW: timeout on lock feature