X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 6CA35D1BB4B for ; Tue, 4 Nov 2003 23:14:32 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 01490-05 for ; Tue, 4 Nov 2003 19:14:03 -0400 (AST) Received: from frankenbride.clueinc.net (xd84b4366.ip.e-nt.net [216.75.67.102]) by svr1.postgresql.org (Postfix) with ESMTP id 8E030D1C9FD for ; Tue, 4 Nov 2003 19:14:00 -0400 (AST) Received: by frankenbride.clueinc.net (Postfix, from userid 16220) id 5BA25BA37; Tue, 4 Nov 2003 18:14:03 -0500 (EST) Date: Tue, 4 Nov 2003 18:14:02 -0500 From: Jamie Lawrence To: Tom Lane Cc: pgsql-sql@postgresql.org Subject: Re: Problems with NEW.* in triggers Message-ID: <20031104231402.GB2879@clueinc.net> Mail-Followup-To: Tom Lane , pgsql-sql@postgresql.org References: <20031104223319.GA2879@clueinc.net> <26587.1067986114@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <26587.1067986114@sss.pgh.pa.us> User-Agent: Mutt/1.3.28i Organization: clue inc X-URL: http://www.clueinc.net/ X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/23 X-Sequence-Number: 15838 On Tue, 04 Nov 2003, Tom Lane wrote: > Jamie Lawrence writes: > > I had thought that if moddate isn't included in an insert or update, > > that it would be null in the NEW context, > > No, it would be whatever the value to be assigned to the column would > be, if the trigger were not present. In particular, during an UPDATE > it's going to contain the old value of the field. In an INSERT it would > be whatever the column's default value is. For an insert, the default is null in this case. test=# create table trash (moddate timestamp, message text); CREATE TABLE test=# create or replace function timestamp_test() returns opaque as ' test'# begin test'# NEW.moddate := coalesce(NEW.moddate, now()); test'# return NEW; test'# end test'# ' language 'plpgsql'; CREATE FUNCTION test=# create trigger critter_timestamp_test after insert or update on critter for each row execute procedure timestamp_fn(); CREATE TRIGGER test=# insert into trash (message) values ('hi there'); INSERT 560920 1 test=# insert into trash (message) values ('hi there'); INSERT 560921 1 test=# select * from trash; moddate | message ---------+---------- | hi there | hi there (2 rows) test=# I don't understand why moddate isn't getting set to now() in the above. (Point taken on updates... I was thinking about NEW in slightly the wrong way for an after trigger.) > I am not sure what your intention is here. If you want the trigger to > force the field to current time, it can certainly do that. If you want > the user to control whether the field is updated, why do you need a > trigger at all? Excellent question, sigh. I'm trying to bandaid a bad design choice until the application can be changed. Now that you have me thinking about it, an update rule is probably a better idea. Thanks for the help, I appreciate it - -j -- Jamie Lawrence jal@jal.org "Remember, half-measures can be very effective if all you deal with are half-wits." - Chris Klein