473,750 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_f ields
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 4312
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_sch ema.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
1850
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 initials and sometimes they do not. so i want to determine how many items exist in this array created by the split BEFORE assigning values: name_of_customer = Split(customer_name," ", -1, 1) first_name = name_of_customer(0) middle_initial =...
6
3171
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 rownum) in which I can see the time which the recoird has changed?? (please do not answer with the sollution to add an extra "field" in the table and let a trigger fill that field with the changingdate. It
4
3656
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 value for each field. I need to make an audit table that must contain only the fields changed after and insert/update. Thanks in advance
1
3313
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 access a field in OLD and NEW if I know the name in advance. Is there any way I can access the fields when I only have a variable containing the name of the field?
3
3580
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 triggered at insert. I can't see that it can be made in pure SQL, but Java is not a problem. Any of you that can tell me the way of doing it ?
5
6145
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 if a certain threshold value is reached, some information of which parameter that might be increased/decreased to improve perfomance. Simple Example bp_hit_ratio = (1 - ( (bp data phys rd + bp inx phys rd) / (bp data logic rd + pool index...
1
1606
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 and BILL_DETAIL FORM. Master table is "BILL" and Child table is "BILL_DETAIL". and also CATEGORIES and COMANY tables values are dependent on "BILL and BILL_DETAIL" table.
2
14252
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* same code is run when I do other things (like press the Save button or tab through that control). I have searched every control, involved queries and tables and I can't find anything that looks fishy.
0
2490
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 table "employee" the trigger "tr_view_emp_iu" will not be executed. create or replace view view_employee as select e.emp_id, e.first_Name, e.last_name from employee e
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.