Edit report at https://pear.php.net/bugs/bug.php?id=19918&edit=1

ID: 19918
Updated by: pear@nanl.de
Reported By: pear at nanl dot de
Summary: lastInsertID(): Using lastval() breaks triggers in
Status: Open
Type: Bug
Package: MDB2
-Operating System: Linux
+Operating System: irrelevant
Package Version: 2.5.0b5
-PHP Version: 5.3.10
+PHP Version: irrelevant
Roadmap Versions:
New Comment:

-Operating System: Linux
+Operating System: irrelevant
-PHP Version: 5.3.10
+PHP Version: irrelevant

Previous Comments:

[2013-04-29 14:39:45] mirkovogt

The function 'lastInsertID' in /usr/share/php/MDB2/Driver/pgsql.php is
supposed to return the ID of the previous change (mostly 'INSERT') by
the application using this pgsql-connection. This however isn't always

lastInsertID() is either using currval($arg) for a given table/sequence
or lastval() if no parameter got passed.

Using currval($arg) is totally fine, however using lastval() is highly
discouraged and considered to be racy.

lastval() returns the ID of the very last change on this set/connection,
not necessarily done by the application itself.

An example:

I'm having the table 'foo' and a trigger configured on it, which is
causing an INSERT to table 'bar' on any change to 'foo'.
Now I'm doing sth. like:

SELECT lastval();

However in this case 'lastval()' doesn't return the ID of the INSERT my
application caused, but the INSERT the trigger caused.

This isn't just a theoretical issue: I'm using slony for replication of
my postgres tables and the application using lastInsertID() reproducible
get back the the ID of the INSERT caused by the trigger, not my

Since there is no way of using currval() without any argument, I don't
have an ideal solution not changing the API.

So I propose to NOT allow using lastInsertID() anymore without any

Expected result:
lastInsertID() returning the ID of the last change _I_ made

Actual result:
lastInsertID() returning the ID of the last change on this set and
connection (which might be caused by e.g. triggers)


Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppear-bugs @
postedApr 29, '13 at 3:00p
activeApr 29, '13 at 3:00p

1 user in discussion

Pear: 1 post



site design / logo © 2022 Grokbase