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

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Craig James <cjames(at)emolecules(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Does setval(nextval()+N) generate unique blocks of IDs?
Date: 2012-08-21 14:12:59
Message-ID: CAHyXU0xHNKwRTZWZkCLqGiEhLZJCTX8M3d2+Zi8xM-rzGTxqhA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, Aug 21, 2012 at 2:45 AM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> wrote:
> sometimes I hate my laptops touchpad. Ran something similar in php
> got similar performance. By comparison, running select 1 instead of
> nextval() took ~0.160s to run.

you're mostly measuring client overhead i think:

postgres=# explain analyze select nextval('s') from
generate_series(1,1000); explain analyze select nextval('s') from
generate_series(1,1000);
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Function Scan on generate_series (cost=0.00..12.50 rows=1000
width=0) (actual time=0.149..1.320 rows=1000 loops=1)
Total runtime: 1.806 ms

postgres=# do
$$
begin
for x in 1..1000 loop
perform nextval('s');
end loop;
end;
$$ language plpgsql;
DO
Time: 4.333 ms

Anyways, the only reason to do advisory locking is if you
a) strictly need contiguous blocks of ids
and
b) are worried about concurrency and the id is fetched early in a
non-trivial transaction

If a) isn't true, it's better to do looped nextval, and if b) isn't
true, IMO it's better to maintain a value in a table and let mvcc
handle things. Being able to grab sequences in a block without manual
locking would be a nice feature but only if it could be done without
adding an iota of overhead to standard usage :-).

merlin

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Craig James 2012-08-21 14:36:41 Re: Does setval(nextval()+N) generate unique blocks of IDs?
Previous Message Scott Marlowe 2012-08-21 07:45:12 Re: Does setval(nextval()+N) generate unique blocks of IDs?