note 67279 added to function.pg-last-oid
| From: | kevin at stormtide dot ca | Date: | Tue, 06 Jun 2006 22:10:19 +0000 |
| Subject: | note 67279 added to function.pg-last-oid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-113393@lists.php.net to get a copy of this message | ||
I am afraid that the editor is misleading people here.
QUOTE "Editor's Note: If another record is inserted after the nextval is obtained and
before you [sic: execute] the INSERT query this code will fail. This should not be done on busy
sites."
This is not correct. A sequence is a multi-session safe table-like structure in postgresql.
From the postgresql manual for nextval()
"Advance the sequence object to its next value and return that value. This is done atomically:
even if multiple sessions execute nextval concurrently, each will safely receive a distinct sequence
value. "
Since the default for a serial column during an insert is to call nextval(), it will also get a
unique identifier.
Sequences are _not_ defined as being linearly ordered however and may contain holes and return
values out of order (due to cache settings). They will, however, be unique 100% of the time.
The following code is CORRECT and thread safe WITHOUT a transaction on even the most loaded server.
QUOTE (with corrections) "
$res=pg_query("SELECT nextval('foo_key_seq') as key");
$row=pg_fetch_array($res, 0);
$key=$row['key'];
// now we have the serial value in $key, let's do the insert
pg_query("INSERT INTO foo (key, foo) VALUES ($key, 'blah blah')");
"
In this case the value retrieved for $key will never again be retrieved by the database under any
circumstance. Period. Ever.
Hope that clears this up.
----
Server IP: 66.207.199.35
Probable Submitter: 142.179.200.164
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.pg-last-oid.php
Edit -- http://master.php.net/note/edit/67279
Del: integrated -- http://master.php.net/note/delete/67279/integrated
Del: useless -- http://master.php.net/note/delete/67279/useless
Del: bad code -- http://master.php.net/note/delete/67279/bad+code
Del: spam -- http://master.php.net/note/delete/67279/spam
Del: non-english -- http://master.php.net/note/delete/67279/non-english
Del: in docs -- http://master.php.net/note/delete/67279/in+docs
Del: other reasons-- http://master.php.net/note/delete/67279
Reject -- http://master.php.net/note/reject/67279
Search -- http://master.php.net/manage/user-notes.php