From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | "woger151" <woger151(at)jqpx37(dot)cotse(dot)net> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Retrieving PK of inserted row |
Date: | 2007-01-30 14:59:50 |
Message-ID: | 1834.1170169190@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
"woger151" <woger151(at)jqpx37(dot)cotse(dot)net> writes:
> Reading around, I've seen the following methods discussed:
> (1) Within a transation, do the INSERT, and then do a SELECT CURVAL
> (2) Not necessarily within a transaction, get a candidate for the pk using
> SELECT NEXTVAL, then INSERT the row.
> (3) Use LASTVAL
You don't need a transaction for any of these; at least not unless
you're using connection-pooling code that might swap you to a different
physical session at transaction boundaries.
Since all of them are going to cost you two round trips to the server,
it's probably about a wash performance-wise. I'd tend to go with (2)
just because it requires no assumptions about anything. Even without
the session-change risk, (1) and especially (3) can bite you in a
sufficiently complex application: some other part of your own code
could touch this or another sequence between the two steps.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Rich Shepard | 2007-01-30 15:08:48 | Re: pg migrator |
Previous Message | Alvaro Herrera | 2007-01-30 14:50:18 | Re: Foreign keys, table inheritance, and TRUNCATE |