Skip site navigation (1) Skip section navigation (2)

Re: Does setval(nextval()+N) generate unique blocks of IDs?

From: Craig James <cjames(at)emolecules(dot)com>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Does setval(nextval()+N) generate unique blocks of IDs?
Date: 2012-08-21 14:36:41
Message-ID: CAFwQ8rc3yEZq5q80UYxUzN5ONm7eN17co8nLq4-51Oe7T9bB8g@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-performance
On Mon, Aug 20, 2012 at 6:06 PM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> wrote:
> On Mon, Aug 20, 2012 at 6:59 PM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> wrote:
>> On Mon, Aug 20, 2012 at 6:10 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> Craig James <cjames(at)emolecules(dot)com> writes:
>>>> I want to do this:
>>>
>>>>     select setval('object_id_seq', nextval('object_id_seq') + 1000, false);
>>>
>>>> Now suppose two processes do this simultaneously.  Maybe they're in
>>>> transactions, maybe they're not.  Are they guaranteed to get distinct
>>>> blocks of IDs?
>>>
>>> No, because the setval and the nextval are not indivisible.
>>>
>>>> Or is it possible that each will execute nextval() and
>>>> get N and N+1 respectively, and then do setval() to N+1000 and N+1001,
>>>> resulting in two overlapping blocks.
>>>
>>> Exactly.
>>>
>>>> If the answer is, "This won't work," then what's a better way to do this?

--- snip ---

>> If the OP could live with large gaps in his sequence, he could set it
>> to advance by say 1000 at a time, and then use the numbers in that gap
>> freely.  Just a thought.
>
> Better yet set cache = 1000;  here's an example:
>
> create sequence a cache 1000;
> T1: select nextval('a');
> 1
> T2: select nextval('a');
> 1001
> T1: select nextval('a');
> 2
> T2: select nextval('a');
> 1002
>
> and so on.
>
> Now can he just select nextval('a'); 1000 times in a loop?  Or would
> he prefer another method.
>
> I guess I'm kind of wondering which problem he's trying to solve.

I thought of that, but I can't live with large gaps in the sequence.
It's used for 32-bit keys, and at a maximum rate of use we have about
30 years before we run out of numbers.  If I start using 1000-item
blocks, we could run out in a few months even at today's usage.
Besides which, it doesn't solve the problem, because what do I do when
an application asks for a block of 1001 items?

It's also inefficient to call nextval() 10, 100 (or 10000, or 100000)
times in a row just to get a guaranteed-unique block of identifiers.

Thanks,
Craig


In response to

pgsql-performance by date

Next:From: Craig JamesDate: 2012-08-21 15:32:47
Subject: Re: Does setval(nextval()+N) generate unique blocks of IDs?
Previous:From: Merlin MoncureDate: 2012-08-21 14:12:59
Subject: Re: Does setval(nextval()+N) generate unique blocks of IDs?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group