field incrementing in a PL/pgSQL trigger - Mailing list pgsql-general

From Tim Vadnais
Subject field incrementing in a PL/pgSQL trigger
Date
Msg-id GNECLEMNHALMMLOCGFHFIENFCBAA.tvadnais@earthlink.net
Whole thread Raw
Responses Re: field incrementing in a PL/pgSQL trigger  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: field incrementing in a PL/pgSQL trigger  (Pierre-Frédéric Caillaud<lists@boutiquenumerique.com>)
List pgsql-general
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.



pgsql-general by date:

Previous
From: "Vassilev, Lubomir G."
Date:
Subject: how to port Oracle database to PostgreSQL?
Next
From: Karim Nassar
Date:
Subject: Duplicating a database