X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id E0FBE4758E1 for ; Wed, 5 Feb 2003 14:03:45 -0500 (EST) Received: from smtp016.mail.yahoo.com (smtp016.mail.yahoo.com [216.136.174.113]) by postgresql.org (Postfix) with SMTP id 4FF814758E6 for ; Wed, 5 Feb 2003 14:03:43 -0500 (EST) Received: from psc.progress.com (HELO Yahoo.com) (janwieck@192.233.92.200 with plain) by smtp.mail.vip.sc5.yahoo.com with SMTP; 5 Feb 2003 19:03:42 -0000 Message-ID: <3E415F97.62A6E585@Yahoo.com> Date: Wed, 05 Feb 2003 14:01:43 -0500 From: Jan Wieck Organization: Home X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U) X-Accept-Language: en,ru MIME-Version: 1.0 To: Achilleus Mantzios Cc: Neal Lindsay , pgsql-sql@postgresql.org Subject: Re: automatic time/user stamp - rule or trigger? References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/101 X-Sequence-Number: 11499 Achilleus Mantzios wrote: > > On Wed, 5 Feb 2003, Neal Lindsay wrote: > > > I have a table that I want to keep track of the user who last modified > > it and the timestamp of the modification. Should I use a trigger or a rule? > > > > CREATE TABLE stampedtable ( > > stampedtableid SERIAL PRIMARY KEY, > > updatestamp timestamp NOT NULL DEFAULT now(), > > updateuser name NOT NULL DEFAULT current_user, > > sometext text > > ); > > > > I suspect that I want a rule, but all the examples in the documentation > > seem to update a separate table and not override (or add) the > > insert/update to the timestamp and name columns. > > You may want to use rules if you need rewriting. > What you actually need is some sort of driver to a specific table. > You could create a view to that table (to hide the accounting columns), > and then create rules on that view that do the job as you wish. I'm sure you want to use a BEFORE INSERT OR UPDATE trigger that modifies NEW.updatestamp in place just before the row get's written. A rule will not work here because rules cannot cause the same action on the same table they are called for. Jan -- #======================================================================# # It's easier to get forgiveness for being wrong than for being right. # # Let's break this rule - forgive me. # #================================================== JanWieck@Yahoo.com #