pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Keith Worthington <KeithW@NarrowPathInc.com>
To: David Hofmann <mor4321@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Rule
Date: Wed, 08 Jun 2005 17:50:20 -0400
Message-ID: <42A7681C.9060007@NarrowPathInc.com> (raw)
In-Reply-To: <BAY23-F1C805B1E550EF619E5F45BBFD0@phx.gbl>
References: <BAY23-F1C805B1E550EF619E5F45BBFD0@phx.gbl>

>>>David wrote:
>>>
>>>What I want to do is setup some kind of rule so that whenever a s_data 
>>>field is updated, that the time_stamp gets update to the current time/date. 
>>
>> Normally you want to do that with a before trigger rather than a rule.
> 
> Ok, I have no knowledge of Tiggers except what I just read in the docs 
> section. Look like I need to make a procudure then call it with a trigger.
> 
> Is there a better location for Tigger/Procudure Examples. The trigger 
> seems fairly, however I got lost in the procudure part.
> 
> David

David,

Here is a trigger function that I wrote for storing audit information. 
Whether or not a query provides the user and/or timestamp this procedure 
sets them.  Naturally you will need to modify them for your data model.

CREATE OR REPLACE FUNCTION interface.tf_audit_data()
   RETURNS "trigger" AS
$BODY$
    BEGIN
--    Set the user name.
       SELECT * FROM session_user INTO NEW.audit_user;
--    Set the timestamp.
       NEW.audit_timestamp := ('now'::text)::timestamp(6) with time zone;
--    Send the modified record down the pipe.
       RETURN NEW;
    END;
$BODY$
   LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION interface.tf_audit_data() OWNER TO postgres;
GRANT EXECUTE ON FUNCTION interface.tf_audit_data() TO postgres;
GRANT EXECUTE ON FUNCTION interface.tf_audit_data() TO public;

CREATE TRIGGER tgr_audit_data
   BEFORE INSERT OR UPDATE
   ON sales_order.tbl_line_item
   FOR EACH ROW
   EXECUTE PROCEDURE interface.tf_audit_data();

-- 
Kind Regards,
Keith



view thread (7+ messages)

Message-ID: <42A7681C.9060007@NarrowPathInc.com>
Permalink:  ../42A7681C.9060007@NarrowPathInc.com/
Also on:    postgresql.org/message-id/42A7681C.9060007@NarrowPathInc.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: KeithW@NarrowPathInc.com, mor4321@hotmail.com
  Subject: Re: Rule
  In-Reply-To: <42A7681C.9060007@NarrowPathInc.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