note 120354 added to pdo.lastinsertid

From: Date: Tue, 20 Dec 2016 11:23:21 +0000
Subject: note 120354 added to pdo.lastinsertid
Groups: php.notes 
Request: Send a blank email to php-notes+get-208672@lists.php.net to get a copy of this message
WARNING for PostgreSQL users! In response to the comment by ed at hicklinslade dot com, who wrote: ... $last_insert_id = $objPDO->lastInsertId("$strTable_id_seq); This does appear to function as expected. What is a little unclear to me is whether this simply returns the current value of the sequence; if it does, this isn't a particularly reliable indicator as to the id of the record your code just inserted, especially if your site or application is especially high traffic. ... NEVER ever use lastInsertId() with PostgreSQL sequences, ESPECIALLY when your application's insert/update load is high. PostgreSQL sequences are non-transactional (a natural design feature to avoid exclusive locking which otherwise produces unacceptable performance). This means that any concurrent transaction incrementing the same sequence will render the value returned by lastInsertId() invalid with respect to the last insert by your transaction. Example: Transaction 1 inserts with nextval('some_seq') yielding 100; Concurrent transaction 2 inserts with nextval('some_seq') yielding 101; Transaction 1 calls lastInsertId(), expecting 100, BUT GETS 101. This PDO method is braindead for PostgreSQL, always use INSERT ... RETURNING instead. Regards. ---- Server IP: 72.52.91.14 Probable Submitter: 213.141.136.31 ---- Manual Page -- http://php.net/manual/en/pdo.lastinsertid.php Edit -- https://master.php.net/note/edit/120354 Del: integrated -- https://master.php.net/note/delete/120354/integrated Del: useless -- https://master.php.net/note/delete/120354/useless Del: bad code -- https://master.php.net/note/delete/120354/bad+code Del: spam -- https://master.php.net/note/delete/120354/spam Del: non-english -- https://master.php.net/note/delete/120354/non-english Del: in docs -- https://master.php.net/note/delete/120354/in+docs Del: other reasons-- https://master.php.net/note/delete/120354 Reject -- https://master.php.net/note/reject/120354 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208672) next »