Re: Field collection in trigger

Поиск
Список
Период
Сортировка
От apb18@cornell.edu
Тема Re: Field collection in trigger
Дата
Msg-id Pine.SOL.3.91.1030901153147.23760I-100000@travelers.mail.cornell.edu
обсуждение исходный текст
Ответ на Field collection in trigger  (Josué Maldonado <josue@lamundial.hn>)
Список pgsql-general
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
>









В списке pgsql-general по дате отправления:

Предыдущее
От: Josué Maldonado
Дата:
Сообщение: Re: Field collection in trigger
Следующее
От: Andreas Fromm
Дата:
Сообщение: inserting via "on insert" rule