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

Re: Atomic query and update of sequence generators

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeffrey Tenny <jeffrey(dot)tenny(at)comcast(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Atomic query and update of sequence generators
Date: 2004-01-15 03:30:09
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
Jeffrey Tenny <jeffrey(dot)tenny(at)comcast(dot)net> writes:
> If I want to allocate a block of adjacent values from a sequence generator,
> is the following statement atomic with respect to the time between
> when the call to nextval() and setval()?

> SELECT setval('foo', nextval()+20) ...


> The goal is to get a sequence of 20 values that are all +1 from each other.

Do you always want to pull exactly 20 values?  If so you could set the
sequence's "cache" parameter to 20 (see the CREATE SEQUENCE man page for
details).  I don't think there's any way at present to get varying sizes
of consecutively-allocated blocks.  If you need that, it would likely
not be real hard to implement a "next_n_vals(seq, n)" variant of
nextval() to grab N consecutive values and return the first.  But it's
not there at the moment.

			regards, tom lane

In response to

pgsql-sql by date

Next:From: Greg Sabino MullaneDate: 2004-01-15 12:47:39
Subject: Re: Atomic query and update of sequence generators
Previous:From: Stuart BarbeeDate: 2004-01-15 03:17:12
Subject: Re: REPOST[GENERAL] Quoting for a Select Into - Please Help

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