X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1F2A652A20 for ; Thu, 7 Jul 2005 19:45:12 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15805-01 for ; Thu, 7 Jul 2005 22:45:03 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id B8A6952A1B for ; Thu, 7 Jul 2005 19:44:59 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j67MirfZ098233 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 7 Jul 2005 16:44:55 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j67MirXq050718; Thu, 7 Jul 2005 16:44:53 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j67MirKe050717; Thu, 7 Jul 2005 16:44:53 -0600 (MDT) (envelope-from mfuhr) Date: Thu, 7 Jul 2005 16:44:52 -0600 From: Michael Fuhr To: PFC Cc: lmeadors@apache.org, pgsql-sql@postgresql.org Subject: Re: getting back autonumber just inserted Message-ID: <20050707224452.GA50696@winnie.fuhr.org> References: <42CD6B58.6060508@techevolution.be> <20050707201433.94901.qmail@web41013.mail.yahoo.com> <20050707203715.GA26528@winnie.fuhr.org> <20050707205725.GA28200@winnie.fuhr.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.109 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200507/50 X-Sequence-Number: 22028 On Fri, Jul 08, 2005 at 12:26:30AM +0200, PFC wrote: > > >That's a different issue than whether currval() is subject to > >interference from other transactions. And just wait until PostgreSQL > >8.1 comes out and people start using lastval() -- then it could get > >*really* confusing which sequence value you're getting. > > What happens if an INSERT trigger inserts something into another > table which also has a sequence ? Do you mean with lastval()? Here's what happens: CREATE FUNCTION trigfunc() RETURNS trigger AS $$ BEGIN INSERT INTO bar (x) VALUES (NEW.x); RETURN NEW; END; $$ LANGUAGE plpgsql VOLATILE; CREATE TABLE foo (id serial PRIMARY KEY, x integer); CREATE TABLE bar (id serial PRIMARY KEY, x integer); CREATE TRIGGER footrig BEFORE INSERT ON foo FOR EACH ROW EXECUTE PROCEDURE trigfunc(); ALTER SEQUENCE bar_id_seq RESTART WITH 50; INSERT INTO foo (x) VALUES (12345); SELECT lastval(); lastval --------- 50 (1 row) SELECT * FROM foo; id | x ----+------- 1 | 12345 (1 row) SELECT * FROM bar; id | x ----+------- 50 | 12345 (1 row) -- Michael Fuhr http://www.fuhr.org/~mfuhr/