Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 4E1099FB431 for ; Fri, 3 Aug 2007 16:22:22 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 70887-05-3 for ; Fri, 3 Aug 2007 16:22:02 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from rv-out-0910.google.com (rv-out-0910.google.com [209.85.198.184]) by postgresql.org (Postfix) with ESMTP id 59C559FB442 for ; Fri, 3 Aug 2007 16:01:40 -0300 (ADT) Received: by rv-out-0910.google.com with SMTP id f1so622835rvb for ; Fri, 03 Aug 2007 12:01:39 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=S0iFIhX3MRqeSbHVzgWkemKgj8nsg2SqH2UKp/Gxj7h0RyQhgTZoKuonKHlsOqmDVncgOkV6n3VIClzzf3RhWLoDPZwIzmDTfrMVmphlOtdThFvSDrEn2TM9cZHdDCc2+ItTpOFeKbSI/sgaB3gUQFYGO5sOYEAG2WQhFmtWFuo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=slXUuYqb4wgDbFQgTsipNd8hHBGgeEqo+JqjoMWm/mtnXRJqFzr3EIBCpYi0DGyh9pc6PCWGCh2FFYElpXAYT1/+6B3x+tDgh+nnD1/VoFuFuMiTWYXsFbM7sDdW7Re/revmGu65+eE10S2H7FCfpPWGeTqhgNxqGqak9bTKUYQ= Received: by 10.141.99.4 with SMTP id b4mr1146087rvm.1186167698882; Fri, 03 Aug 2007 12:01:38 -0700 (PDT) Received: by 10.141.162.12 with HTTP; Fri, 3 Aug 2007 12:01:38 -0700 (PDT) Message-ID: Date: Fri, 3 Aug 2007 14:01:38 -0500 From: "Scott Marlowe" To: "Steve Midgley" Subject: Re: Increment a sequence by more than one Cc: pgsql-sql@postgresql.org In-Reply-To: <20070803165058.020509FBF2F@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20070803165058.020509FBF2F@postgresql.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/21 X-Sequence-Number: 28959 On 8/3/07, Steve Midgley wrote: > > Hi, > > I'm writing an import app in a third party language. It's going to use > "copy to" to move data from STDIN to a postgres (8.2) table. There are some > complexities though: it's going to copy the records to a "versioned" table > first, and then at a later time the records will be copied by a different > procedure to the "live" table. The live table and versioned table are > identical in terms of their field definitions. But there is no sequence > associated with the versioned table (whose primary key is "id" plus > "import_group_id", whereas the live table's pk is just "id"). So all > versioned table entries must already "know" what their id would be in the > live table. (This makes sense for other business process we have, but it's a > bit of a problem in this instance). > > My problem: I'd like to be able to grab a block of id's from the live > table's pk sequence. So let's say my importer has 5,000 new rows to import > and the current max pk in the live table is 540,203. I'd like to be able to > increment the primary key sequence in such a way that I get a block of ids > all to myself and the sequence is reset to 545,203 with a guarantee that all > the id's between 540203 and 545203 are unused. The real danger in doing this is race conditions. Most anything you do involves a possible race condition. As long as the race condition doesn't result in an id getting used twice, you're safe. So: test=# create sequence m; CREATE SEQUENCE test=# select nextval('m'); nextval --------- 1 (1 row) test=# alter sequence m increment by 5000; ALTER SEQUENCE test=# select nextval('m'); nextval --------- 5001 (1 row) test=# alter sequence m increment by 1; ALTER SEQUENCE test=# select nextval('m'); nextval --------- 5002 (1 row) In this method, the only possible race condition is that someone might run a nextval('m') between the time you set the increment to 5000 and 1 again. If that happens, you'd have 5,000 missing ids, but since sequences are designed to prevent dupes, not holes, that's ok. > But since I've seen so much magic on display from people on this list, I'm > going to ask if it's possible to do this solely from PG SQL sent from a > third party language? The main tricky bit seems to be ensuring that > everything is locked so two such increment calls at the same time don't > yield overlapping blocks of ids. Is there a way to "lock" the sequence > generator for the duration of a "nextval" and "setval" call? Avoiding the setval is the real key. It doesn't scale. Missing 5,000 ids is no big deal. repeating them IS a big deal. Not using setval is the best answer.