Re: comparing OLD and NEW in update trigger..

From: Richard Huxton <dev(at)archonet(dot)com>
To: Alex Mayrhofer <axelm(at)nona(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: comparing OLD and NEW in update trigger..
Date: 2006-01-26 08:49:01
Message-ID: 43D88CFD.2010405@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alex Mayrhofer wrote:
>
> Hi there,
>
> i'm planning to use the following trigger function to update a timestamp
> of a row when it is UPDATEd:
>
> CREATE OR REPLACE modified_trigger() RETURNS opaque AS $$
> BEGIN
> NEW.modify_timestamp := now();
> END;
> $$ LANGUAGE SQL;

I don't think you can write a trigger function in "SQL" - you'll want
one of the procedural languages: plpgsql / plperl / pltcl etc.

> Since i like to use the same trigger procedure for various tables, i'm
> planning to keep it very generic.
>
> What i'd like to do now is to just update the modify_timestamp column if
> OLD
> and NEW are different. I'd LOOP over the row elements, and compare each
> column of OLD with NEW, and bailing out if there's a difference.
>
> I'd appreciate your help on the following two questions:
>
> - How can i get the column names of NEW/OLD? Is there a set returning
> function for this?

You'll want one of the interpreted languages: pltcl / plperl / plphp
etc. You'll find plpgsql can't cope with the sort of dynamic-typing
required to do this easily.

> - Is there a more efficient way to compare whole rows?

No.

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2006-01-26 08:52:41 Re: 2 instances of postmaster with different data directories
Previous Message Dick Kniep 2006-01-26 08:39:47 Re: Connected user in a triggerfunction