Re: Emulating trigger BEFORE SELECT behavior

From: Fabrízio de Royes Mello <fabriziomello(at)gmail(dot)com>
To: Atri Sharma <atri(dot)jiit(at)gmail(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>, Alfonso Afonso <aafonsoc(at)gmail(dot)com>, PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Emulating trigger BEFORE SELECT behavior
Date: 2013-04-18 14:31:38
Message-ID: CAFcNs+rrRxsO1W5N7UN_p5MJreh7n61gLm1UqAREEm8D534o3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 18, 2013 at 11:02 AM, Atri Sharma <atri(dot)jiit(at)gmail(dot)com> wrote:

>
> [...]
>
> One way I was thinking of was creating an updatable view, which is
> initialized to NULL. As SELECT queries take place, I can update the
> view to include the new rows.
>
>
Why you just create your track function and a view to call it?

Example:

BEGIN;

CREATE TABLE foo (id SERIAL PRIMARY KEY, data TEXT);
CREATE TABLE foo_track(tracktime TIMESTAMP DEFAULT now(), foo_row foo);

INSERT INTO foo (data) SELECT 'Some Data'||id FROM generate_series(1,10) AS
id;

CREATE OR REPLACE FUNCTION foo_track_func(foo) RETURNS integer AS
$$
INSERT INTO foo_track(foo_row) VALUES ($1) RETURNING (foo_row).id
$$
LANGUAGE sql;

CREATE VIEW v_foo AS SELECT foo.*, foo_track_func(foo.*) FROM foo;

SELECT * FROM v_foo;
SELECT * FROM foo_track;

COMMIT;

Regards,

--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Atri Sharma 2013-04-18 14:38:49 Re: Emulating trigger BEFORE SELECT behavior
Previous Message Adrian Klaver 2013-04-18 14:30:19 Re: Emulating trigger BEFORE SELECT behavior