Skip site navigation (1) Skip section navigation (2)

Re: MySQL LAST_INSERT_ID() to Postgres

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: Russ Brown <pickscrape(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: MySQL LAST_INSERT_ID() to Postgres
Date: 2008-08-29 12:51:39
Message-ID: 20080829125139.GA3983@alvh.no-ip.org (view raw or flat)
Thread:
Lists: pgsql-general
Magnus Hagander escribió:
> Alvaro Herrera wrote:
> > Russ Brown escribió:
> >> Masis, Alexander (US SSA) wrote:
> >>>    "SELECT CURRVAL(
> >>> pg_get_serial_sequence('my_tbl_name','id_col_name'));"
> >> Any reason why you can't just do this?
> >>
> >> CREATE FUNCTION last_insert_id() RETURNS bigint AS $$
> >>   SELECT lastval();
> >> $$ LANGUAGE SQL VOLATILE;
> > 
> > If your table has a trigger that inserts into another table with its own
> > sequence, you're screwed.
> 
> I assume you're equally screwed with MySQL LAST_INSERT_ID() in that case
> - so it'd be bug compatible.

Yeah, which is another reason not to use triggers; more pileups for the
whole "new features are there just for checklist's sake" argument.

The approach proposed by Alexander above does not have such problem,
which is why it is better than the alternative suggested by Russ.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

pgsql-general by date

Next:From: ´gnor gplDate: 2008-08-29 12:56:47
Subject: Full Text Search, plus some General Ideas Beforehand
Previous:From: Roberts, JonDate: 2008-08-29 12:40:53
Subject: Re: temp schemas

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group