Re: Rules on Select - Mailing list pgsql-general

From Tony Wasson
Subject Re: Rules on Select
Date
Msg-id 6d8daee30605051507k1551342ep4979f2b07b064e02@mail.gmail.com
Whole thread Raw
In response to Rules on Select  (Sean Hamilton <sehamilt@chfund.org>)
List pgsql-general
On 5/5/06, Sean Hamilton <sehamilt@chfund.org> wrote:
> I have to log each time a user selects data from a table by inserting
> record in another table.
> For example
> I have table xzy
> user test selects id, name from table xyz.
> I want to insert into table xyz_log
> user, action, fields, timestamp
> test, select, id name, 12/1/05 02:00:21
>
> How can i do this using a Rule or Trigger?

For a small table you could use a SQL set returning function like so.
You can get more granular if you require an argument to the function,
like a certain id or name.

CREATE TABLE log (pguser TEXT, action TEXT, fields TEXT, timestamp
TIMESTAMP DEFAULT NOW());

CREATE TABLE xyz (id INT, name TEXT);

CREATE OR REPLACE FUNCTION xyz() RETURNS SETOF xyz AS
$BODY$
  INSERT INTO log (pguser, action, fields) VALUES (CURRENT_USER,'select','all');
  SELECT id, name FROM xyz;
$BODY$ language sql;

pgsql-general by date:

Previous
From: "Hogan, James F. Jr."
Date:
Subject: Re: Rules on Select
Next
From: Tom Lane
Date:
Subject: Re: Google Summer of Code: Full Disjunctions