Re: Index not being used in MAX function (7.2.3) - Mailing list pgsql-general

From Ernest E Vogelsinger
Subject Re: Index not being used in MAX function (7.2.3)
Date
Msg-id 5.1.1.6.2.20030610234755.03b44698@mail.vogelsinger.at
Whole thread Raw
In response to Index not being used in MAX function (7.2.3)  (Paulo Jan <admin@digital.ddnet.es>)
List pgsql-general
At 18:39 10.06.2003, Paulo Jan said:
--------------------[snip]--------------------
>Hi all:
>
>       I have here a table belonging to a message board (Phorum 3.3), and
>there's an index in it that is not being used for reasons that I don't
>understand. The table is:
>
> ...
>
> select max(id) from todocinetv;
--------------------[snip]--------------------

Your question has already been answered, but:

I suspect that you're doing this to retrieve the row ID of a newly inserted
row. This may not be foolproof since others could already have inserted
rows in between your insertion and this ID lookup.

Assuming that you're using a sequence to provide the primary key (which you
should) you may safely query its current value:
    SELECT currval('todocinetv_id_seq') as "newid"

This is guaranteed to return the last value **for your connection** only,
regardless if the sequence has actually been incremented by others or not.
And it's lightning fast.

Just my 2c :)


--
   >O     Ernest E. Vogelsinger
   (\)    ICQ #13394035
    ^     http://www.vogelsinger.at/



pgsql-general by date:

Previous
From: Forest Wilkinson
Date:
Subject: How to enumerate foreign key constraints after migrating from 7.1.3?
Next
From: Dmitry Tkach
Date:
Subject: Re: Weird postmaster crashes