Skip site navigation (1) Skip section navigation (2)

Re: mysql's last_insert_id

From: Bo Lorentsen <bl(at)netgroup(dot)dk>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: mysql's last_insert_id
Date: 2003-08-29 06:36:50
Message-ID: 1062139010.25826.19.camel@localhost (view raw or flat)
Thread:
Lists: pgsql-general
On Fri, 2003-08-29 at 04:11, Martijn van Oosterhout wrote:

> OIDs have never beebn unique, it's just that most databases never get big
> enough to experience wraparound. They are also now optional per table and
> may soon no longer be available by default.
Ok, that may break some of my code :-( But as long as oid's are enabled
and are unique for the current table, I'm happy.

I made a general insert (executeUpdate) function to my general DB layer,
that returned the newly inserted row (to be able to use the default
initealized elements). This code will stop working when oid is disabled,
very sad thing.

I will have to push this row awareness to a higher level in the future.

But, how does PG refer to rows internally, and why can't we get this
reference in the application layer. I don't care if its an integer, long
or even a string, but I like to know the unique id of the currently and
newly inserted row, without knowing the datamodel (table layout).

> There are various solutions. Some people use functions to do inserts, I just
> use the string "currval(whatever)" in the application layer which the
> database replaces with the appropriate value.
Hmm, but what happends if more than one connection does this at the same
time ? Then, only one of the concurrent connections will have a insert
on the returned value, and the rest will get a wrong row reference
returned.

The only safe methode would be to do a "select nextval(whatever)", and
aply this number by "hand" to the insert, but that remove the
possibility to make general code even more, but it will be safe.

/BL


In response to

Responses

pgsql-general by date

Next:From: IvarDate: 2003-08-29 06:39:56
Subject: Re: Functions have 32 args limt ???
Previous:From: Anuradha RatnaweeraDate: 2003-08-29 05:50:56
Subject: Unique constraints for a list

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group