X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 5FB0D310CC8 for ; Tue, 1 Jul 2003 19:17:33 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 11488-03 for ; Tue, 1 Jul 2003 16:17:23 -0300 (ADT) Received: from asus.troels.arvin.dk (62.79.119.132.adsl.vbr.worldonline.dk [62.79.119.132]) by svr1.postgresql.org (Postfix) with ESMTP id 2F775310C11 for ; Tue, 1 Jul 2003 16:17:19 -0300 (ADT) Received: by asus.troels.arvin.dk (Postfix, from userid 500) id 1FA29F7F39; Tue, 1 Jul 2003 21:17:11 +0200 (CEST) Subject: Re: Immutable attributes? From: Troels Arvin To: pgsql-sql@postgresql.org In-Reply-To: <1057076907.11365.411.camel@camel> References: <1057076907.11365.411.camel@camel> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1057087030.15874.38.camel@localhost> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.0 (1.4.0-2) Date: 01 Jul 2003 21:17:10 +0200 X-Archive-Number: 200307/15 X-Sequence-Number: 14129 Hello, On Tue, 2003-07-01 at 18:28, Robert Treat wrote: > > want to make sure that the "crated" attribut for a tuple is > > not changed once it has been set. > > > > I'm thinking about implementing it through a trigger, but is there a > > better way to create such "immutable" attributes? > > I don't know if it's "better", but this is one of the things people find > the RULE system really handy for. I thought about using the rule system for that. However: - I would like to be able to throw an exception if an immutable attribute is changed; it seems that can't be done with the rule system(?) - it seems that RULEs are a PostgreSQL-only phenomenon; I try to keep my SQL more portable than that > Check the docs, I believe there are examples of this. I haven't been able to find any related examples. Anyways, I have now found a way to implement my immutable timestamp fields using a stored procedure and a trigger: create function create_time_unchanged() returns trigger as ' begin if old.time_created <> new.time_created then raise exception ''time_created may not be changed: % <> %'', old.time_created, new.time_created ; end if; return new; end;' language 'plpgsql'; create trigger ensure_create_time_unchanged before update on transaction_pbs for each row execute procedure create_time_unchanged(); Now, let's say that the "transaction" relation has a field "time_created" of type timestamp with time zone and that a record with time_created=2003-07-01 20:56:11.393664+02 : => update transaction => set time_created='2003-07-01 20:56:11.393664+02'::timestamptz => where order_id=1000; -- NOTE: No change. UPDATE 1 => update transaction => set time_created='2003-07-01 20:56:00+02'::timestamptz => where order_id=1000; -- NOTE: Changed. ERROR: time_created may not be changed: 2003-07-01 20:56:11.393664+02 <> 2003-07-01 20:56:00+02 So things work. -- Troels Arvin