BEFORE UPDATE Triggers

From: Chris Anderson <cva(at)pobox(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: BEFORE UPDATE Triggers
Date: 2003-08-28 21:48:51
Message-ID: 69957C94-D9A1-11D7-8266-000393D3B384@pobox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

PostgreSQL Version: 7.2.3
Procedural Language: PL/pgSQL

I have a table which contains a field for the user who last modified
the record. Whenever a row in this table is updated, I want to have an
UPDATE trigger do the following things:

1) Ensure the UPDATE query supplied a value for the action_user column
2) Log the record to an audit table so I can retrieve a change log

Part 2 was trivial, however it seemed natural that if I had the
following conditional in the trigger function:

IF NEW.action_user ISNULL THEN ...

I could raise an exception if that field was not supplied. (which would
be the case if the function were triggered on an INSERT)

Unfortunately it seems this is not the case. The NEW record contains
values representing both the values explicitly provided with the UPDATE
as well as the existing values which were not stipulated in the query.

Is there any clever way around this limitation? It isn't the end of the
world if I cannot verify this constraint in postgres, however it would
have made it easier to ensure no one is making mistakes.

Oh, and I am aware of the current_user variable. In my case this is
useless as I don't care about the user at the database layer but rather
at the application layer.

Thanks in advance,

cva

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael A Nachbaur 2003-08-28 22:47:51 Forcing a trigger to run
Previous Message Joe Conway 2003-08-28 21:27:32 Re: length of array