Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 8E938476A5A for ; Wed, 11 Sep 2002 13:53:29 -0400 (EDT) Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (Postfix) with ESMTP id ED267476A59 for ; Wed, 11 Sep 2002 13:53:28 -0400 (EDT) Received: from [66.219.92.2] (HELO chocolate-mousse) by davinci.ethosmedia.com (CommuniGate Pro SMTP 3.5.9) with ESMTP id 1644505; Wed, 11 Sep 2002 10:53:27 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: "Beth Gatewood" , Subject: Re: writing a function to mimic mysql last_insert_id Date: Wed, 11 Sep 2002 10:53:49 -0700 X-Mailer: KMail [version 1.4] References: <000901c259ba$c5b7ee70$0c00000a@bethvizx> In-Reply-To: <000901c259ba$c5b7ee70$0c00000a@bethvizx> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200209111053.49837.josh@agliodbs.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/84 X-Sequence-Number: 9361 Beth, > However, we are in the midst of porting a mysql database to pgsql. The p= erl > application that is tied to this mysql database heavily utilizes > last_insert_id. In an effort to save the perl developers the pain of hav= ing > 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. >=20 > 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= =20 Postgres, one can have more than one sequence for a table. Further, in=20 Postgres, you can update more than one table at a time using a function,=20 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= =20 were related to a table in the pg_* tables, you are left with the difficult= =20 task of determining what statement the user last ran. This would, I think,= =20 involve hacking MVCC to some degree. If you're serious about pursuing this, I suggest posting your question on= =20 PGSQL-HACKERS to get help with hacking MVCC to determine a connection's las= t=20 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= =20 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= =20 last_inser_id(). But if you do write a replacement function, please post = it=20 on TechDocs! --=20 -Josh Berkus Aglio Database Solutions San Francisco