Re: postgres - mysql last_inserted_id

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, June 30, 2005 2:17 pm, Jason Wong said:
> On Friday 01 July 2005 04:06, Richard Lynch wrote:
>
>> > last
>> > record from the tabel, for linking to another tabel.
>>
>> You have to use http://php.net/pg_last_oid to get the PostgreSQL
>> "internal" Object ID (OID) -- You can then use the ubiquitous "oid"
>> column.
>>
>>
>> $query = "insert ...";
>> pg_exec($connection, $query);
>> $oid = pg_last_oid($connection);
>
> I'm pretty sure I read somewhere that the the "last OID" can get messed up
> under some circumstances and the OID that you get is not the OID that you
> want. Can't remember whether this was a php-postgresql thing or simply a
> postgresql thing. But whatever it is, you don't need OIDs to use
> sequences.

There are innumerable on-line forums that (incorrectly) state that an OID
could be returned that is not connection-specific, so two HTTP requests in
parallel would criss-cross OIDs.

This is patently false, and any user of PostgreSQL can demonstrate that in
minutes (okay, an hour for a newbie) of coding.

OIDs *can* get re-used *IF* you end up having more than 32-bits (2 billion
plus) of objects in the lifetime of your application.

For normal usage, that ain't a big problem, honestly...

Though I should have stated it for the record, cuz maybe the OP has a site
where 2 BILLION INSERTs are gonna happen.

The solutions there are the same as for not having OID in the first place
-- Have some other unique identifier you generate yourself in the INSERT,
or use that *with* the OID to be 100% certain you get back the same row
from your 2 billion plus data set.

If there's a reliable, web-safe, connection-dependent way of getting the
sequence ID used in an INSERT, it sure ain't documented, and I've never
seen it discussed on the PostgreSQL list (which I dropped off awhile ago,
so maybe it's something new).

If PostgreSQL guys added some new-fangled thing, sorry!  It should be
documented under http://postgresql.org if you search for OID and sequence,
you'd hope.

You *could* write your application to "SELECT nextval('TABLENAME_id')" and
then provide the ID on the INSERT statement, and then you already have the
ID, but that's kinda hokey, and goes against the grain of just letting the
database do its job... Sort of an elegant and crude solution all at once.

Though that also limits you to 2 billion plus records per table -- which
is better than OID which is shared across all tables in a single database.

If you are dealing in 2 billion object PostgreSQL databases, and you don't
know all this already, you're in DEEP trouble...
Start reading:
http://postgresql.org/
See ya in a couple months. :-)

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux