Re: sequence not restarting in a for loop (actually semi-random numbers)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: sequence not restarting in a for loop (actually semi-random numbers)
Date: 2008-11-12 16:17:03
Message-ID: 17079.1226506623@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it> writes:
> insert into mytop (id,n) select id, nextval('tmp_seq')
> from biglist join mylist on biglist.id=mylist
> order by biglist.something limit 3;

I suspect you are expecting the nextval()s to be done after the LIMIT,
but actually they'll most likely be done before it, ie you're getting
a lot of unintended nextvals happening. Try doing the ORDER BY/LIMIT
in a sub-select.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2008-11-12 16:29:34 Re: [GENERAL] Very slow queries w/ NOT IN preparation (seems like a bug, test case)
Previous Message Alan Hodgson 2008-11-12 16:06:54 Re: merge 2 dumps