Re: How to best grab a chunk of Ids from a sequence

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Bryan White" <bryan(at)arcamax(dot)com>
Cc: "pgsql-sql" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to best grab a chunk of Ids from a sequence
Date: 2001-11-15 18:25:24
Message-ID: 18275.1005848724@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Bryan White" <bryan(at)arcamax(dot)com> writes:
> My question is is there a way to grab 4 million IDs from a sequence without
> calling nextval once for each ID. Note, this sequence is being actively
> drawn on by other processes.

I can't think of any really clean way, but you could do something like

select setval('seq', nextval('seq') + 4000000 + 100);

and then use the 4m IDs just before the returned value. Because the
nextval and setval aren't an atomic operation, it's possible that
some other processes will get in and do nextval in between. The extra
100 advance of the sequence should provide an adequate buffer for that
... but of course it could theoretically be insufficient...

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jason Earl 2001-11-15 18:38:36 Re: INSERT question
Previous Message Jack Gao 2001-11-15 17:50:00 Re: Is this a bug?