Re: A problem with sequences...

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Doug McNaught <doug(at)mcnaught(dot)org>
Cc: Dmitry Tkach <dmitry(at)openratings(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: A problem with sequences...
Date: 2003-02-21 19:11:41
Message-ID: 20030221110807.P60361-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 21 Feb 2003, Doug McNaught wrote:

> Dmitry Tkach <dmitry(at)openratings(dot)com> writes:
>
> > select setval('answer_id_seq', id) from answer order by id desc limit 1;
> >
> > Now, for some reason this reports a correct value, but what actually
> > gets set is wrong!
>
> I'm guessing that 'setval' is getting called more than once here.
> Your 'LIMIT 1' controls how many rows are returned to the client, but
> the server is probably generating more rows internally. So this is

I'd totally missed that, it's probably doing a plan of seqscan + sort to
run the query.

> > select setval ('answer_id_seq', max(id)) from answer
>
> is right.

Or possibly:
select setval('answer_id_seq', id) from
(select id from answer order by id desc limit 1) as foo;

which might if the table gets big enough use an index scan.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2003-02-21 19:13:36 Re: Mutating table (urgent)
Previous Message Diogo de Oliveira Biazus 2003-02-21 19:04:52 Problem with functional indexes