473,386 Members | 1,804 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.

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*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 11 '05 #1
4 3621
Josué Maldonado <jo***@lamundial.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*******@postgresql.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***@lamundial.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*******@postgresql.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_attribute
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_Maldonado?= 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*******@postgresql.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*******@postgresql.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***@lamundial.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*******@postgresql.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
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
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...
1
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...
0
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...
4
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...
3
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...
2
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,...
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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.