From: Keith Wong <keith@e-magine.com.au>
To: pgSQL <pgsql-sql@postgresql.org>
Subject: Copying data with triggers
Date: Tue, 22 Aug 2000 00:48:27 +1000
Message-ID: <4.3.2.7.0.20000822003607.00b0d6b0@mail.e-magine.com.au> (raw)
In-Reply-To: <Pine.BSF.4.21.0008201949120.801-100000@thelab.hub.org>
References: <002f01c00af3$4efce0a0$0b01a8c0@ivsol>
Hi all,
In the database I'm designing, I want to have audit tables that keep a log
of all inserts, updates and deletes that occur
on any table.
e.g. If i had a table Info,
create table Info ( info_id SERIAL,
some_data text
)
I would also have a corresponding audit table
create table AudInfo (aud_key_id SERIAL,
info_id int4,
some_data text,
aud_operation_type,
aud_log_time timestamp default now()
)
now I tried creating a trigger on Info, so that whenever an insert occurs,
the records are copied to the audit table.
create function tp_info () returns opaque as '
begin
-- insert into audit table
insert into AudInfo (info_id, some_data, aud_operation_type) values
(new.info_id, new.some_data, ''i'');
return new;
end;
' language 'plpgsql';
create trigger tp_info before insert on Info
for each row execute procedure tp_info();
This doesn't work however. A parse error occurs in the first line.
I suspect that I cannot insert a record into another table in a trigger.
I'm not sure why though.
Anybody else done similar operations within a trigger procedure? Or know of
a work around?
Cheers,
Keith.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: keith@e-magine.com.au
Subject: Re: Copying data with triggers
In-Reply-To: <4.3.2.7.0.20000822003607.00b0d6b0@mail.e-magine.com.au>
References: <002f01c00af3$4efce0a0$0b01a8c0@ivsol> <4.3.2.7.0.20000822003607.00b0d6b0@mail.e-magine.com.au>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox