Re: Last inserted row id with complex PK - Mailing list pgsql-general

From Nelson Green
Subject Re: Last inserted row id with complex PK
Date
Msg-id CAGo-KZ=b84ZWv9yRPe2aA7x71VqeyBDw6VvKUjXkYbrMu6Sx9w@mail.gmail.com
Whole thread Raw
In response to Re: Last inserted row id with complex PK  (Francisco Olarte <folarte@peoplecall.com>)
Responses Re: Last inserted row id with complex PK  (David Johnston <polobo@yahoo.com>)
Re: Last inserted row id with complex PK  (John R Pierce <pierce@hogranch.com>)
Re: Last inserted row id with complex PK  (Francisco Olarte <folarte@peoplecall.com>)
List pgsql-general
On Wed, Jan 8, 2014 at 10:09 AM, Francisco Olarte <folarte@peoplecall.com> wrote:
Hi Nelson:

On Wed, Jan 8, 2014 at 4:54 PM, Nelson Green <nelsongreen84@gmail.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

Hi Francisco,

My apologies, I was not completely clear. I will not know any of the columns in advance. The most recent insert is the result of user input from a web form, so I won't know what project or what user generated the last insert. That was why I wandered if that information was stored anywhere in the system.
 

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.

pgsql-general by date:

Previous
From: "George Weaver"
Date:
Subject: argument of CASE/WHEN must not return a set
Next
From: Nelson Green
Date:
Subject: Re: Last inserted row id with complex PK