Re: Last inserted row id with complex PK

From: Francisco Olarte <folarte(at)peoplecall(dot)com>
To: Nelson Green <nelsongreen84(at)gmail(dot)com>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Last inserted row id with complex PK
Date: 2014-01-08 16:09:22
Message-ID: CA+bJJbyFHG8fDDPGeWpujW=6uiT24E_vkSqN7tsqwzOLgXyBrw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Nelson:

On Wed, Jan 8, 2014 at 4:54 PM, Nelson Green <nelsongreen84(at)gmail(dot)com> wrote:
> I have a projects log table with a three column PK, project_num, person_num,
> and sequence, where each new entry for a project/person combination
> increments the sequence, which is not an auto incrementing sequence. Is
> there any way to retrieve the last entry to the table? For instance, if the
> last entry for person 427 on project 15 was sequence number 125, is that
> information available to me anywhere?

SELECT MAX(sequence) FROM project_log_table WHERE person_num=427 AND
project_num=15

If it's a PK it should have an index and I believe it will be quite fast.

AAMOF, you could even build the above with max+1 into the insert query
for a new entry and have the values returned using a RETURNING clause.

Francisco Olarte.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2014-01-08 16:22:40 Re: Last inserted row id with complex PK
Previous Message Nelson Green 2014-01-08 15:54:37 Last inserted row id with complex PK