473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to determine field names in a trigger?

Hi all,

I'm creating a centralised table to keep a log of changes in other tables.

In thinking about the PL/pgSQL trigger to write and attach to the
monitored tables (probably a row level AFTER trigger), I can see two
approaches:

a) Write a separate PL/pgSQL function for each table, with the hard
coded field names in the function.

or

b) Write one PL/pgSQL function that can be used in the triggers for
all of the monitored tables.

It sounds like b) would be most time effective to write and maintain,
but in looking through the PG docs I haven't seen anything that says how
to determine the field names in the OLD nor NEW records, nor how many
fields there are:

http://www.postgresql.org/docs/7.4/s...l-trigger.html

For example, let's say I have two tables. One is called table_a, and
has two columns, and the other is table_b and has three columns. The
advantage of having the PL/pgSQL trigger knowing the number of fields
and their names in the OLD or NEW rows would be in being able to do this:

(pseudo code)

LOOP
-- If we've processed all the fields, then exit
IF i > OLD.number_of_fields
EXIT;
END IF;

-- Check if the next field was changed, and if so, record it
IF OLD.nextfield <> NEW.nextfield
INSERT INTO log_table (table, field, old_val, new_val)
VALUES (TG_RELNAME, nextfield, OLD.nextfield,
NEW.nextfield);
END IF;

-- Increment the loop counter
i := i + 1;
END LOOP

Are there any way to do this kind of thing for triggers with PL/pgSQL at
present?

Regards and best wishes,

Justin Clift
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
3 4283
On Tue, Jun 29, 2004 at 01:59:11PM +1000, Justin Clift wrote:

Justin,
I'm creating a centralised table to keep a log of changes in other tables.

In thinking about the PL/pgSQL trigger to write and attach to the
monitored tables (probably a row level AFTER trigger), I can see two
approaches:


I think you can do this very easily with PL/Tcl. For a somewhat
unrelated example, see General Bits issue #47,

http://www.varlena.com/GeneralBits/47.php

_I think_ there are examples closer to what you want to achieve in the
archives. The array of column names in a trigger is $TG_relatts.

Hope this helps,

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"La libertad es como el dinero; el que no la sabe emplear la pierde" (Alvarez)
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2
Alvaro Herrera wrote:
<snip>
I think you can do this very easily with PL/Tcl. For a somewhat
unrelated example, see General Bits issue #47,

http://www.varlena.com/GeneralBits/47.php

_I think_ there are examples closer to what you want to achieve in the
archives. The array of column names in a trigger is $TG_relatts.
Thanks Alvaro.

Didn't find any good examples in the archives (looked there before
asking here), even though there are a few people over time asking the
same question.

In PL/pgSQL at present (PG 7.4.3), I'm thinking the only approach may be
to get the name of the table (TG_RELNAME) and then go and look up it's
structure in pg_attribute or something (the same as shown by
information_schema.columns). Not sure how to do that either yet, and a
better solution would be nifty.

PL/TCL does seem to provide that extra bit of info to functions/triggers
that I need, whereas PL/pgSQL doesn't. I'd prefer not to have to load
further PL's into the database, but it's worth looking at anyway.

:)

Regards and best wisehs,

Justin Clift
Hope this helps,


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3
Justin Clift <jc@telstra.net> writes:
PL/TCL does seem to provide that extra bit of info to functions/triggers
that I need, whereas PL/pgSQL doesn't.
It's not so much that there's more info available as that pltcl is
better suited for working with dynamically generated queries. plpgsql
is handicapped by the fact that it wants to precompute and cache query
plans. That's a performance win when you're working with known tables
but it really gets in the way for dynamically addressing columns.
I'd prefer not to have to load
further PL's into the database, but it's worth looking at anyway.


Different needs, different tools.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: JT | last post by:
how do i determine how many items are in an array? the following code creates an array of values each time a space is found in a name field. the problem is that sometimes names have middle...
6
by: Andre | last post by:
Hello all, In my database I have a table with records. (Yes I have!!) Last week a record is changed, but I want to know on which date/time that was. Is there a oracle field in the table (like...
4
by: Josué Maldonado | last post by:
Hello list, First of all, excuse me if this is not the right place to ask my question. Is there a way in postgresql to loop to all the fields of a given table and compare the OLD and NEW...
1
by: Alistair Hopkins | last post by:
Hi, I am trying to write a generic audit-trail trigger function which will record changes on a field-by-field basis to a single table for all audited tables. However, I find that I can only...
3
by: Poul Møller Hansen | last post by:
Hi, I need an auto incrementing field that will contain values like N000001, N000002, N000003 etc. I think the way is to use the value from an identity field in a stored procedure that is...
5
by: lennart | last post by:
I'm trying to put together a simple util that will help me during performance tuning, It will grab a bunch of values from different snapshots and the db/dbm cfg, and print out different values, and...
1
by: soahil | last post by:
Hi and thanks to pragatiswain for reply. i tell u about tables in the database. TABLE NAMES are "SUB_HEAD","CATEGORY","COMPANY","BILL","BILL_DETAIL. I've a master detail relationship b/w BILL...
2
by: PW | last post by:
Hi, What the heck is that supposed to mean? I am getting this error on a "Me.Requery" line in a subroutine on a form, but only when I select something from a combo/dropdown box. The *exact*...
0
by: marieoutayek | last post by:
Hi, I have a problem and I hope that you help me : I have a trigger "tr_view_emp_iu" INSTEAD OF a view "view_employee" the problem is if the changes is done on the field "first_name" in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.