Re: Sequences without blank holes

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: MaRcElO PeReIrA <gandalf_mp(at)yahoo(dot)com(dot)br>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Sequences without blank holes
Date: 2003-11-06 08:58:41
Message-ID: Pine.LNX.4.44.0311060956080.9763-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

MaRcElO PeReIrA writes:

> How can I assure a ''sequence WITHOUT holes''?

> $ SELECT max(prod_id)+1 FROM products;

You can do that, but

SELECT prod_id FROM products ORDER BY prod_id DESC LIMIT 1;

will be faster. In fact, if you have a B-tree index on prod_id (which you
should), it will be nearly constant time.

Also, make sure if you do a SELECT, then some client application logic,
then an UPDATE, to do it in one transaction and use the appropriate
isolation level, locking, etc.

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2003-11-06 09:01:06 Re: PostgreSQL v7.4 Release Candidate 1 compile errors
Previous Message MaRcElO PeReIrA 2003-11-06 08:01:54 Sequences without blank holes