dbTalk Databases Forums  

field incrementing in a PL/pgSQL trigger

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss field incrementing in a PL/pgSQL trigger in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Tim Vadnais
 
Posts: n/a

Default field incrementing in a PL/pgSQL trigger - 10-20-2004 , 07:20 PM






Hi,

My boss wants to add some logging functionality to some of our tables on
update/delete/insert. I need to log who, when, table_name, field name,
original value and new value for each record, but only logging modified
fields, and he wants me to do this wing postgres pgSQL triggers.

We are given 10 automatically created variables. Some of which I know I can
use: NEW, OLD, TG_WHEN, TG_OP and TG_RELNAME. I can use these to get
general information for the update, but when the trigger is called, I don't
know how many fields are in the tables that are being updated.

My questions are: Is there a way I can dynamically determine the number of
fields in the row that is being maintained. (a function much like:
PQnfields(const PGresult *); )
Then I need a way to get the name of the field (using a function much like:
PQfname(const PGresult *, int); )

Using the dynamically generated name I could then walk the NEW and OLD rows
to compare the values. (e.g. if (NEW.field != OLD.field) do something;

Can anyone help me with this? Thank you in advance.



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: field incrementing in a PL/pgSQL trigger - 10-23-2004 , 06:42 PM






"Tim Vadnais" <tvadnais (AT) earthlink (DOT) net> writes:
Quote:
My questions are: Is there a way I can dynamically determine the number of
fields in the row that is being maintained. (a function much like:
PQnfields(const PGresult *); )
Then I need a way to get the name of the field (using a function much like:
PQfname(const PGresult *, int); )
plpgsql isn't capable of doing dynamic field access. You could do this
in pltcl, I think, and definitely in C. There are some related examples
in contrib/spi/.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #3  
Old   
Pierre-Frédéric Caillaud
 
Posts: n/a

Default Re: field incrementing in a PL/pgSQL trigger - 10-24-2004 , 01:53 AM





Create a different trigger function for each table, then each trigger can
be customized to know the column names.
You can generate the triggers from a little script which queries the
system tables to get at the column names. It would spit code like 'IF
NEW.fieldname != OLD.fieldname THEN (record modification...) END IF for
each field...
Less elegent than a general solution, but why not.

Quote:
Hi,

My boss wants to add some logging functionality to some of our tables on
update/delete/insert. I need to log who, when, table_name, field name,
original value and new value for each record, but only logging modified
fields, and he wants me to do this wing postgres pgSQL triggers.

We are given 10 automatically created variables. Some of which I know I
can
use: NEW, OLD, TG_WHEN, TG_OP and TG_RELNAME. I can use these to get
general information for the update, but when the trigger is called, I
don't
know how many fields are in the tables that are being updated.

My questions are: Is there a way I can dynamically determine the number
of
fields in the row that is being maintained. (a function much like:
PQnfields(const PGresult *); )
Then I need a way to get the name of the field (using a function much
like:
PQfname(const PGresult *, int); )

Using the dynamically generated name I could then walk the NEW and OLD
rows
to compare the values. (e.g. if (NEW.field != OLD.field) do something;

Can anyone help me with this? Thank you in advance.



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.