Re: Slow query to get last created row using CURRVAL

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Mathieu De Zutter <mathieu(at)dezutter(dot)org>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Slow query to get last created row using CURRVAL
Date: 2010-12-04 12:35:33
Message-ID: AANLkTi=0Vc44yQwhx2mR2XKBUqBuoj-nPK8--Jwdx2Cg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Sat, Dec 4, 2010 at 13:56, Mathieu De Zutter <mathieu(at)dezutter(dot)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().

Regards,
Marti

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Mathieu De Zutter 2010-12-04 12:49:38 Re: Slow query to get last created row using CURRVAL
Previous Message Mathieu De Zutter 2010-12-04 11:56:50 Slow query to get last created row using CURRVAL