Received: from sss.pgh.pa.us (sss.pgh.pa.us [209.114.132.154] (may be forged)) by hub.org (8.10.1/8.10.1) with ESMTP id e8M2HMs64742 for ; Thu, 21 Sep 2000 22:17:22 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.0/8.11.0) with ESMTP id e8M2HDB25810; Thu, 21 Sep 2000 22:17:14 -0400 (EDT) To: Stephan Szabo cc: "Edward Q. Bridges" , "kparker@eudoramail.com" , "pgsql-general@hub.org" Subject: Re: Re: sequences In-reply-to: References: Comments: In-reply-to Stephan Szabo message dated "Thu, 21 Sep 2000 10:06:27 -0700" Date: Thu, 21 Sep 2000 22:17:13 -0400 Message-ID: <25807.969589033@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200009/638 Stephan Szabo writes: > Actually, it looks like currval is defined to give the > value last used in your session. So, the second case > (not in transaction) should still always give the > value of the last nextval, assuming they're part of > the same session. Stephan is correct: you can safely do insert into foo(B) values (Bvalue); -- column A is defaulted select currval('foo_A_seq'); even without a transaction block, and be assured of getting the same value back that was assigned to A. At least, you don't have to worry about other backends messing you up. But it's still possible to shoot yourself in the foot. For example, if you have rules or triggers on insert to foo, and those rules/triggers themselves cause additional calls to nextval('foo_A_seq'), then the eventual currval() will return the latest such result, which might not have been what got inserted into foo. Note that using a transaction block will not protect you from this gaffe. For this reason, and because it just seems cleaner to me, I prefer to solve this problem like so: select nextval('foo_A_seq'); insert into foo(A,B) values (just-returned-value, Bvalue); That gets the same result with about the same amount of work, but seems more understandable and safer to me. It's mostly a matter of taste, though. regards, tom lane