Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 030BCB5DC0F for ; Mon, 17 Oct 2011 16:43:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 62406-01-2 for ; Mon, 17 Oct 2011 19:43:10 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 84F86B5DC19 for ; Mon, 17 Oct 2011 16:43:10 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p9HJh9T0018369; Mon, 17 Oct 2011 15:43:09 -0400 (EDT) To: Laerson keler cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6258: Lock Sequence In-reply-to: References: <201110171308.p9HD81vP028182@wwwmaster.postgresql.org> <14643.1318866768@sss.pgh.pa.us> Comments: In-reply-to Laerson keler message dated "Mon, 17 Oct 2011 16:03:54 -0200" Date: Mon, 17 Oct 2011 15:43:09 -0400 Message-ID: <18368.1318880589@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/147 X-Sequence-Number: 31417 Laerson keler writes: > 2011/10/17 Tom Lane >> "Laerson Keler" writes: >> Why did you do that, that is what were you trying to accomplish? It >> never did block nextval() on the sequence, for example. > Tom Lane, good afternoon, I block the sequence not to miss the sequel, for > it not to be skipped if the insert to fail. My logic involves two triggers, > one before and one after. I give the first one in last_value select for > update in the sequence and insert after I run a select next_val ('sequence') > to place in the next issue, so the sequence in my table is no failure. Well, that's a cute idea, but the fact is that it was always quite unsafe because it had no interlock against nextval(). Moreover, you still did not have a guarantee of no holes in the assigned ID values, because the transaction could still fail after the AFTER trigger runs. There really is not any way to generate guaranteed-hole-free sequences using sequence objects. If you have to have that, I'd suggest locking the table against other writes and then fetching MAX(id) + 1. It's not very fast, and it's not at all concurrent, but that's the price of ensuring no holes. Personally I'd rethink how badly you need that property. regards, tom lane