Re: [SQL] Race condition in resetting a sequence

2007-08-03 Thread Tom Lane
Steve Midgley <[EMAIL PROTECTED]> writes: > The code I provided to reset a primary key sequence is actually part of > Ruby on Rails core library - actually they use something very similar > to what I originally sent: > SELECT setval('#{sequence}', (SELECT COALESCE(MAX(#{pk})+(SELECT > increment

[SQL] Race condition in resetting a sequence

2007-08-03 Thread Steve Midgley
Hi Scott, You've moved into more general territory, so I'm starting a new thread. The code I provided to reset a primary key sequence is actually part of Ruby on Rails core library - actually they use something very similar to what I originally sent: SELECT setval('#{sequence}', (SELECT COAL

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Michael Glaesemann
On Aug 3, 2007, at 15:27 , Erik Jones wrote: Is there actually a requirement that the block of 5000 values not have gaps? Good point. If not, why not make the versioned table's id column default to nextval from the same sequence? Of course, the ids of the two tables could be interleaved

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Michael Glaesemann
On Aug 3, 2007, at 14:28 , Steve Midgley wrote: AIUI, one difference between the solutions Scott and I proposed is that while INCREMENT is set at 5000, each time nextval is called the sequence is incremented by 5000. For example: test=# select nextval('foos_foo_id_seq'); nextval -

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Erik Jones
On Aug 3, 2007, at 11:50 AM, 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 th

Re: [SQL] Foreign Key inter databases

2007-08-03 Thread Erik Jones
On Aug 3, 2007, at 2:24 AM, Dani Castaños wrote: Josh Tolley escribió: On 8/2/07, Dani Castaños <[EMAIL PROTECTED]> wrote: Hi all! Is it possible to have a foreign key where referenced table is in another database? Thank you in advance ---(end of broadcast)--

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Scott Marlowe
Oh, another point. You should run the alter sequence m increment 5000; select nextval('m'); alter sequence m increment 1; one right after the other to reduce the number of 5000 wide holes in your sequence. Or, given the size of bigint, you could just set the increment to 5000 and leave it there

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Scott Marlowe
On 8/3/07, Steve Midgley <[EMAIL PROTECTED]> wrote: > Hi Scott, > > Thanks for this info (and Michael too!). > > Let me see if I understand your suggestion. I would run these three > commands in sequence: > > # select nextval('[my_seq_name]'); > returns => 52 [I believe that the sequence is at 52]

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Steve Midgley
Hi Scott, Thanks for this info (and Michael too!). Let me see if I understand your suggestion. I would run these three commands in sequence: # select nextval('[my_seq_name]'); returns => 52 [I believe that the sequence is at 52] # alter sequence [my_seq_name] increment by 5000; # select nextv

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Scott Marlowe
On 8/3/07, Steve Midgley <[EMAIL PROTECTED]> 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

Re: [SQL] Increment a sequence by more than one

2007-08-03 Thread Michael Glaesemann
On Aug 3, 2007, at 11:50 , Steve Midgley wrote: 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

[SQL] Increment a sequence by more than one

2007-08-03 Thread Steve Midgley
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 dif

Re: [SQL] Foreign Key inter databases

2007-08-03 Thread Dani Castaños
Josh Tolley escribió: On 8/2/07, Dani Castaños <[EMAIL PROTECTED]> wrote: Hi all! Is it possible to have a foreign key where referenced table is in another database? Thank you in advance ---(end of broadcast)--- TIP 1: if posting/reading thro

Re: [SQL] Count of rows

2007-08-03 Thread Christian Kindler
do this via execute in a stored procedure - something like this (written on the flow - untested!) returns setof text declare my_record record; my counter as bigint; begin for my_record in select tablename from pg_tables where schemaname = 'public' loop execute into counter