Re: A problem with sequences... - Mailing list pgsql-general

From Tom Lane
Subject Re: A problem with sequences...
Date
Msg-id 2122.1045881201@sss.pgh.pa.us
Whole thread Raw
In response to Re: A problem with sequences...  (Dmitry Tkach <dmitry@openratings.com>)
List pgsql-general
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

pgsql-general by date:

Previous
From: Neil Conway
Date:
Subject: Re: escaping and sql injection
Next
From: Tom Lane
Date:
Subject: Re: Views + Rules + Triggers + 7.3.2 = Upgrade Problems (was: Re: REWRITE_INVOKE_MAX and "query may contain cycles" )