Обсуждение: 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.
Josué Maldonado <josue@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
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 <josue@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 majordomo@postgresql.org so that your > message can get through to the mailing list cleanly > > >
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 majordomo@postgresql.org so that your > message can get through to the mailing list cleanly >
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 <josue@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 majordomo@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