X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1980E32B0E1 for ; Wed, 20 Oct 2004 16:36:34 +0100 (BST) 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 13831-10 for ; Wed, 20 Oct 2004 15:36:10 +0000 (GMT) Received: from tiberius.peacock.de (tiberius.peacock.de [62.154.214.98]) by svr1.postgresql.org (Postfix) with ESMTP id 64D1E32A002 for ; Wed, 20 Oct 2004 16:36:07 +0100 (BST) Received: from [193.97.247.16] (helo=[193.97.247.16]) by tiberius.peacock.de with esmtp (Exim 4.14) id 1CKHoV-0005BT-2d; Wed, 20 Oct 2004 16:51:43 +0200 Subject: Re: Sequence question From: Tino Wildenhain To: Eric E Cc: pgsql-general@postgresql.org In-Reply-To: <3sydnUgMwqQ80-ncRVn-uA@speakeasy.net> References: <3sydnUgMwqQ80-ncRVn-uA@speakeasy.net> Content-Type: text/plain Message-Id: <1098286552.21062.333.camel@sabrina.peacock.de> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 Date: Wed, 20 Oct 2004 17:35:53 +0200 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/907 X-Sequence-Number: 67430 Hi, On Tue, 2004-10-19 at 01:16, Eric E wrote: > Hi, > I have a question about sequences. I need a field to have values with > no holes in the sequence. However, the values do not need to be in order. > > My users will draw a number or numbers from the sequence and write to > the field. Sometimes, however, these sequence numbers will be discarded > (after a transaction is complete), and thus available for use. During > the transaction, however, any drawn numbers need to be unavailable. > I would like the next user who draws a number to draw the lowest number > she can, starting with the holes in the sequence. > > This continuous sequence is absolutely required by our company, as the > fact that the sequence has no holes is used to check for much more > serious problems. I would recheck this requirement. What should actually be achieved with the check for no holes in the numbering? Remember you can always enumerate using a set returning function or by means of a temporary sequence for a query. > So my question is: > what's the most effective way to get the next available number? There is none. > My present method is to do a query that finds the first and last number > in each of the holes, step through those holes, and then start > generating new numbers. Unfortunately, this involves doing a table scan > each time - before I generate the number, and does not produce the > transaction-safety I want. You cannot eat the cake and keep it - either you have holes or you have transaction security or you have bad performance by locking the whole table on insert. Regards Tino