Re: Slow query to get last created row using CURRVAL - Mailing list pgsql-performance

From Mathieu De Zutter
Subject Re: Slow query to get last created row using CURRVAL
Date
Msg-id AANLkTimJKHJo7pwmLcWJoQM36C56i1x=geVG_RAR2AAj@mail.gmail.com
Whole thread Raw
In response to Re: Slow query to get last created row using CURRVAL  (Marti Raudsepp <marti@juffo.org>)
List pgsql-performance
On Sat, Dec 4, 2010 at 1:35 PM, Marti Raudsepp <marti@juffo.org> wrote:
> On Sat, Dec 4, 2010 at 13:56, Mathieu De Zutter <mathieu@dezutter.org> wrote:
>> I have no idea why in some cases the index scan is not considered.
>> Does anyone have an idea?
>
> I guess that it's because the currval() function is volatile -- its
> value has to be tested for again each row.
>
> Try this instead:
> SELECT user_id FROM log_event WHERE id = (SELECT CURRVAL('log_event_id_seq'));
>
> This will assure that there's only one call to currval().

OK, that makes a lot of sense. Your suggestion solves my problem.

Thanks!

Mathieu

pgsql-performance by date:

Previous
From: Marti Raudsepp
Date:
Subject: Re: Slow query to get last created row using CURRVAL
Next
From: "Kevin Grittner"
Date:
Subject: Re: problem with from_collapse_limit and joined views