X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 4DCC247634A for ; Sun, 11 May 2003 10:28:37 -0400 (EDT) Received: from mta08ps.bigpond.com (mta08ps.bigpond.com [144.135.25.169]) by postgresql.org (Postfix) with ESMTP id 81A3A476352 for ; Sun, 11 May 2003 10:28:36 -0400 (EDT) Received: from canprint.com.au ([144.135.25.84]) by mta08ps.email.bigpond.com (iPlanet Messaging Server 5.2 HotFix 1.14 (built Mar 18 2003)) with SMTP id <0HEQ001U487RPB@mta08ps.email.bigpond.com> for pgsql-sql@postgresql.org; Mon, 12 May 2003 00:28:40 +1000 (EST) Received: from CPE-203-51-27-213.nsw.bigpond.net.au ([203.51.27.213]) by psmam06bpa.bigpond.com(MailRouter V3.2g 116/11564848); Mon, 12 May 2003 00:28:39 +0000 Date: Mon, 12 May 2003 00:28:33 +1000 From: Matthew Horoschun Subject: Re: Knowing when it is safe to call currval() In-reply-to: <20030511133513.GA22780@wolff.to> To: Bruno Wolff III Cc: pgsql-sql@postgresql.org Message-id: MIME-version: 1.0 X-Mailer: Apple Mail (2.552) Content-type: text/plain; charset=US-ASCII; format=flowed Content-transfer-encoding: 7BIT X-Image-Url: http://www.horoschun.id.au/matthew/uniphoto.jpg X-Spam-Status: No, hits=-16.1 required=5.0 tests=BAYES_01,IN_REP_TO,QUOTED_EMAIL_TEXT,USER_AGENT_APPLEMAIL autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/205 X-Sequence-Number: 13370 Hi Bruno, Thanks for your assistance. >> I have a bunch of SECURITY DEFINER functions that are called from >> TRIGGERS which force the primary key of tables to the next value in a >> sequence. Normal users can't call nextval() or setval() on any >> sequences. The triggers basically do: > > One way out of this using a different approach might be to make > security > definer functions that will let normal users call nextval for the > sequences. You could then use these as the default for the appropiate > columns. > > Along the way you are doing things, instead of checking to see if > the row is not equal to currval, perhaps check to see if it is null. > Then you could supply a value of null when you want it to be rewritten. > I've decided to do something similar to this. I'm now only doing the currval() check if the user-supplied value is not null. IF NEW.a_id IS NOT NULL THEN IF NEW.a_id <> currval( 'a_id_seq' ) THEN RAISE EXCEPTION 'a_id must be NULL or the current a_id_seq value.'; END IF; ELSE NEW.a_id := nextval( 'a_id_seq'::text ); END IF; It is a bit of a kludge, but I painted myself into the corner in the first place! Cheers Matthew.