473,700 Members | 2,801 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 3645
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
10151
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
4255
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
1256
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
952
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
4305
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
2489
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
1753
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
8641
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
9203
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9060
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
8912
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
7797
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...
1
6557
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5897
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();...
1
3082
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
2021
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.