pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Beth Gatewood <beth@vizxlabs.com>
To: pgsql-sql@postgresql.org
Subject: Re: writing a function to mimic mysql last_insert_id
Date: Wed, 11 Sep 2002 10:53:49 -0700
Message-ID: <200209111053.49837.josh@agliodbs.com> (raw)
In-Reply-To: <000901c259ba$c5b7ee70$0c00000a@bethvizx>
References: <000901c259ba$c5b7ee70$0c00000a@bethvizx>


Beth,

> However, we are in the midst of porting a mysql database to pgsql.  The perl
> application that is tied to this mysql database heavily utilizes
> last_insert_id.  In an effort to save the perl developers the pain of having
> to go in and modify a bunch of their SQL to call out to seq.currval, I
> thought I would attempt to mimic this function.
> 
> Perhaps there is another way to approach this than writing this type of
> function?

Not really.   CURRVAL requires a sequence name as a parameter because, in 
Postgres, one can have more than one sequence for a table.   Further, in 
Postgres, you can update more than one table at a time using a function, 
trigger, or rule; how would any function know which sequence id to return?

While it would be relatively easy for a function to look up what seqeunces 
were related to a table in the pg_* tables, you are left with the difficult 
task of determining what statement the user last ran.  This would, I think, 
involve hacking MVCC to some degree.

If you're serious about pursuing this, I suggest posting your question on 
PGSQL-HACKERS to get help with hacking MVCC to determine a connection's last 
action.  I have no idea how easy or difficult this would be.

A second possibility would be writing triggers for all tables that place a 
value into a temporary table that can be read back by your custom function.

You may find it less work, however, to do a search-and-replace on calls to 
last_inser_id().   But if you do write a replacement function, please post it 
on TechDocs!

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco




view thread (9+ messages)  latest in thread

Message-ID: <200209111053.49837.josh@agliodbs.com>
Permalink:  ../200209111053.49837.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200209111053.49837.josh@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, beth@vizxlabs.com
  Subject: Re: writing a function to mimic mysql last_insert_id
  In-Reply-To: <200209111053.49837.josh@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox