Re: A problem with sequences...

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dmitry Tkach <dmitry(at)openratings(dot)com>
Cc: Doug McNaught <doug(at)mcnaught(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: A problem with sequences...
Date: 2003-02-22 02:33:21
Message-ID: 2122.1045881201@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dmitry Tkach <dmitry(at)openratings(dot)com> writes:
> Doug McNaught wrote:
>> 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.
>>
> You are right ! That's it! It does get called twice.
> There is even a nice comment in ExecLimit():

BTW, in CVS tip ExecLimit has been rewritten to not do this, so the
query will behave as you expect in 7.4. Still, functions with
side-effects are really really dangerous in any but the simplest kind
of SELECT, because the planner is pretty cavalier about rearranging
things. I'd advise doing this instead:

select setval('answer_id_seq', (select id from answer
order by id desc limit 1));

Here, you *know* that the setval will be called exactly once.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dmitry Tkach 2003-02-22 02:43:59 Re: Foreign Key with Constant
Previous Message Neil Conway 2003-02-22 02:13:52 Re: escaping and sql injection