Dmitry Tkach <dmitry@openratings.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