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

Home Posts Topics Members FAQ

Field collection in trigger

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

Josué Maldonado.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 11 '05 #1
4 3656
Josué Maldonado <jo***@lamundia l.hn> writes:
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.


You should be able to do this in a Perl or C trigger. It can't be
done in PL/pgSQL without writing a custom trigger for each table that
hard-codes all the field names.

-Doug

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 11 '05 #2
Hi Doug,

Thanks for anwsers, do you have some sample code or URL where I can get
more info on this. I don't know C or Perl but really need to get this done.

Thanks,

Doug McNaught wrote:
Josué Maldonado <jo***@lamundia l.hn> writes:
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.


You should be able to do this in a Perl or C trigger. It can't be
done in PL/pgSQL without writing a custom trigger for each table that
hard-codes all the field names.

-Doug

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #3
Yes.. I had to do something like that a while back and had to make it
generalized, so I could assume nothing about the input tuples other than
the assertion that they contain the same number of colums, the same set
of data types, and the same mapping of data types to colmns

basically what I did was the following in a trigger (In C, of course. I
don't know if there's a way to do the same thing in PL/Pgsql). I'm
describing it from the viewpoint that you're compairing two tuples,
presumably one selected from a table and the other provided from some
other source (update command? insert? another table? etc).

For one tuple, create a list of columns to check by
looking at its tuple descriptor. Ignore deleted columns
(the tupdesc->attrs[n]->attisdropped in the tuple descriptor will be true if
a column has been previously deleted). This is important in certain
cases if you're dealing with a a table that previously has had deleted
columns.

For the other tuple, do the same procedure then create a mapping between
identical columns in the two tables. (Do not assume that the columns
appear in the same order in either tuple)

Then for each pair of identical columns, one from each tuple:
determine if the value stored is passed by value, by reference, or is
variable length.
- if it is by value, just compare the value
- If it is by reference, do a memcmp over the appropriate length
- If it is variable length object, then detoast it and do a memcmp
over its length (compairing their lengths first, as that's the
easy way to tell if it changed)

You'll have to take a close look at the TupleDesc structure in
access/tupdesc.h and also the Form_pg_attribu te
structure catalog/pg_attribute.h for the fine details, but that's how I
generally went about it. You can take some shortcuts at the expense of
generality it that's appropriate in your case..

-Aaron
On Mon, 1 Sep 2003, =?ISO-8859-1?Q?Josu=E9_Mal donado?= wrote:
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

Josué Maldonado.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly




---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 11 '05 #4
Try the following using pltcl

CREATE OR REPLACE FUNCTION foo() RETURNS TRIGGER AS '

foreach id [array names OLD] {
if { $OLD($id) != $NEW($id) {
# do your logging in here or flags whatever you wish
}
}
return [array get NEW]
' LANGUAGE 'pltcl';

HTH

Darren

On Mon, 1 Sep 2003, Josué Maldonado wrote:
Hi Doug,

Thanks for anwsers, do you have some sample code or URL where I can get
more info on this. I don't know C or Perl but really need to get this done.

Thanks,

Doug McNaught wrote:
Josué Maldonado <jo***@lamundia l.hn> writes:
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.


You should be able to do this in a Perl or C trigger. It can't be
done in PL/pgSQL without writing a custom trigger for each table that
hard-codes all the field names.

-Doug

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


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

Nov 11 '05 #5

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

Similar topics

20
10160
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
6
4259
by: Walt | last post by:
It's easy to make a field required for inserts, just set it to not null and don't give it a default. But how does one make a field required for updates? For instance, we have a table with a field that keeps track of which application last updated the table MYTABLE ------ ID PK
1
1259
by: Steve | last post by:
C# I have a datetime field, when I leave this field I am trying to call some validation on the date enered, namely that the date cant be ahead of today. My validation checks the date entered against todays date. If the user types the following in: 07/9/2005
0
416
by: Josué Maldonado | last post by:
Hello list, First of all, excuse me if this is not the right site 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
4
954
by: alanb | last post by:
I have one object which holds a collection of other objects. I'll make one up as my specific example is too abstract and too much code: Public Class Job Dim CandidateCollection As New Collection
3
4312
by: Justin Clift | last post by:
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.
2
1800
by: Tim Vadnais | last post by:
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...
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
2
1758
by: blackirish | last post by:
Hi everyone, i have a collection editor which i implemented it from System.ComponentModel.Design.CollectionEditor. I need to insert/delete values into the collection by using another editor form at design time. So is there a way to insert values into the collection outside the main CollectionEditor or can i trigger the click events of the add/remove buttons of the collection editor? Any help would be appreciated.
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
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.