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

Поиск
Список
Период
Сортировка
От Craig James
Тема Re: Does setval(nextval()+N) generate unique blocks of IDs?
Дата
Msg-id CAFwQ8rc3yEZq5q80UYxUzN5ONm7eN17co8nLq4-51Oe7T9bB8g@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Does setval(nextval()+N) generate unique blocks of IDs?  (Scott Marlowe <scott.marlowe@gmail.com>)
Список pgsql-performance
On Mon, Aug 20, 2012 at 6:06 PM, Scott Marlowe <scott.marlowe@gmail.com> wrote:
> On Mon, Aug 20, 2012 at 6:59 PM, Scott Marlowe <scott.marlowe@gmail.com> wrote:
>> On Mon, Aug 20, 2012 at 6:10 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> Craig James <cjames@emolecules.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


В списке pgsql-performance по дате отправления:

Предыдущее
От: Merlin Moncure
Дата:
Сообщение: Re: Does setval(nextval()+N) generate unique blocks of IDs?
Следующее
От: Craig James
Дата:
Сообщение: Re: Does setval(nextval()+N) generate unique blocks of IDs?