Re: Audit Trigger puzzler

From: Adam Rich <adam(dot)r(at)sbcglobal(dot)net>
To: dmk(at)mr-paradox(dot)net, postgresql Forums <pgsql-general(at)postgresql(dot)org>
Subject: Re: Audit Trigger puzzler
Date: 2009-09-03 04:44:20
Message-ID: 4A9F49A4.5040805@sbcglobal.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> Most of the time, my application will set the edited_by field to
> reflect an application username (i.e., the application logs into the
> database as a database user, and that's not going to be the
> application user) So I log into my application as "Dave", but the
> application connects to the database as "dbuser".

> If the app doesn't specifically send an "edited_by" value in it's
> update, then I want to default that value to the database user.

> This would also be good for auditing any manual data changes that
> could happen at the psql level.

In Oracle, the way we handle audit triggers is by using Package
Variables. We emulate some of that functionality in postgresql by
adding a custom variable to the configuration file:

custom_variable_classes = 'mysess'

Then, whenever a user logs into the application, my login procedure
calls this function:

CREATE OR REPLACE FUNCTION begin_sess(staffid character varying)
RETURNS void AS $BODY$ BEGIN
PERFORM set_config('mysess.curr_user', coalesce(staffid,''), false);
END; $BODY$ LANGUAGE 'plpgsql' VOLATILE;

This makes the current application user automatically available to every
function, including triggers. Then, in your triggers, you can do
this:

DECLARE
curr_user staff.staff_id%TYPE;
BEGIN
SELECT current_setting('mysess.curr_user') INTO curr_user;

In your trigger, you could check that this variable was unset, and fall
back to the database user.

HTH.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message edisan 2009-09-03 04:47:44 handle audiofiles in postgres
Previous Message kalyan s 2009-09-03 04:10:40 Re: print/return only the first X chars of a varchar column?