Re: libpq-fe: how to determine unique collision ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marc SCHAEFER <schaefer(at)alphanet(dot)ch>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: libpq-fe: how to determine unique collision ?
Date: 2001-01-04 17:04:25
Message-ID: 22003.978627865@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Marc SCHAEFER <schaefer(at)alphanet(dot)ch> writes:
> No, that right, but do you have a better way of doing the following ? :)

> INSERT INTO some_table(name, surname) VALUES('marc', 'moua');
> -> creates OID 37492374
> SELECT id FROM some_table WHERE oid = 37492374;

This select will get pretty slow as the table gets large, unless you
make an index on its OID. Which you could do, but why pay the price
of maintaining an index just for this?

> Of course you could do:
> BEGIN TRANSACTION
> SELECT next_val('some_table_id_sequence');
> INSERT INTO some_table(id, name, surname)
> VALUES(current_val('some_table_id_sequence'), 'marc', 'moua');
> END TRANSACTION

This is the Right Way To Do It. You do not need the transaction
(because currval() is backend-private anyway). I'd not bother with
currval() at all, but just do

SELECT nextval('some_table_id_sequence');

-- this returns 4242, say

INSERT INTO some_table(id, name, surname)
VALUES(4242, 'marc', 'moua');

Simple, reliable, fast.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-01-04 17:09:13 Re: Time Zone Query
Previous Message Dave Smith 2001-01-04 16:55:14 Re: Doesn't use index, why?