pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Broersma Jr <rabroersma@yahoo.com>
To: SQL Postgresql List <pgsql-sql@postgresql.org>
Subject: Advice for generalizing trigger functions
Date: Tue, 25 Dec 2007 16:20:53 -0800 (PST)
Message-ID: <781842.52422.qm@web31813.mail.mud.yahoo.com> (raw)

I've created quite a few functions that log modifications to various history tables. (the history table has the same name as the base table but is prefixed by the 'History.' schema.) The only difference between functions I can find is the table name.

Is there any way to generalize these myriad of functions into one?


Below is a sample of a typical logging trigger function.

Regards,
Richard Broersma Jr.


CREATE OR REPLACE FUNCTION "project"."log_managers_ops"()
RETURNS trigger AS
$BODY$
BEGIN


     IF TG_OP IN ( 'UPDATE', 'DELETE' ) THEN
     
          UPDATE History.Managers AS M
             SET endts = now()
           WHERE M.manager_id = OLD.manager_id
             AND now() BETWEEN M.startts AND M.endts;
             
     end IF;
     
     
     IF TG_OP IN ( 'UPDATE', 'INSERT' ) THEN
     
     INSERT INTO History.Managers
          VALUES ( now()::timestamptz, 'INFINITY'::timestamptz, NEW.*);

          RETURN NEW;
          
     END IF;

     RETURN OLD;

END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER;




view thread (7+ messages)  latest in thread

Message-ID: <781842.52422.qm@web31813.mail.mud.yahoo.com>
Permalink:  ../781842.52422.qm@web31813.mail.mud.yahoo.com/
Also on:    postgresql.org/message-id/781842.52422.qm@web31813.mail.mud.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rabroersma@yahoo.com
  Subject: Re: Advice for generalizing trigger functions
  In-Reply-To: <781842.52422.qm@web31813.mail.mud.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox