Re: how to determine OID of the row I just inserted???

From: "Jules Alberts" <jules(dot)alberts(at)arbodienst-limburg(dot)nl>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: how to determine OID of the row I just inserted???
Date: 2003-02-06 15:03:12
Message-ID: 20030206150350.4EB941CB1B9@koshin.dsl.systemec.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Op 6 Feb 2003 (14:25), schreef Nigel J. Andrews <nandrews(at)investsystems(dot)co(dot)uk>:
> On Thu, 6 Feb 2003, Jules Alberts wrote:
<snip>
> > But I use PHP or pl/pgsql (others
> > have exactly the same problem with JDBC) and I know of no way to solve
> > this. Something like lastval() IMHO is way too risky. I need something
> > like a return value:
> >
> > catchOID = returnQueryOID('insert into address (street)
> > values ('Penny Lane'));
> > update customer set address = 'select id from address where
> > oid = catchOID' where name = 'X';
>
> Ah, I see, so it wasn't such a mistake as I thought. However, the basic idea is
> the same; forget about oids and use the serial type [sequence] otherwise you
> may as well get rid of it.

Thanks for your reaction Nigel, but my problem remains the same whether
I use OIDs or sequence values. In a more abstract way the problem would
be:

"how do I determine which row was affected by my last INSERT
or UPDATE statement"

If you know the primary key value it's easy, you just do

INSERT INTO customer (id, name) VALUES ('1234', 'Paul');
SELECT * FORM customer WHERE id = '1234';

But the problem is that in my situation there is no way of knowing the
primary key value. Pgsql very politely echoes the OID of the affected
row. Languages like pl/pgsql and PHP AFAIK, don't. Maybe getting the
lastval() of the primary key sequence would be a solution, but what
happens if someone else accesses the sequence between my INSERT and
SELECT?

So no matter if OIDs are lost with a dump / restore, if they will be
dropped in the future or not, my problem remains...

TIA for any other tips!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-02-06 15:08:24 Re: how to determine OID of the row I just inserted???
Previous Message jerome 2003-02-06 15:01:54 Re: Backup scheme...