pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Copying data with triggers
2+ messages / 2 participants
[nested] [flat]

* Copying data with triggers
@ 2000-08-21 14:48 Keith Wong <keith@e-magine.com.au>
  2000-08-21 23:21 ` Re: Copying data with triggers Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Keith Wong @ 2000-08-21 14:48 UTC (permalink / raw)
  To: pgsql-sql

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.




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Copying data with triggers
  2000-08-21 14:48 Copying data with triggers Keith Wong <keith@e-magine.com.au>
@ 2000-08-21 23:21 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2000-08-21 23:21 UTC (permalink / raw)
  To: Keith Wong <keith@e-magine.com.au>; +Cc: pgsql-sql

Keith Wong <keith@e-magine.com.au> writes:
> 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. 

You certainly can --- I have apps that do it all the time.  I suspect
maybe the plpgsql parser gets confused by SQL-style comments?  Your
example seems to work fine if I leave out the comment.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2000-08-21 23:21 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-08-21 14:48 Copying data with triggers Keith Wong <keith@e-magine.com.au>
2000-08-21 23:21 ` Tom Lane <tgl@sss.pgh.pa.us>

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