Re: Query inside transaction - Mailing list pgsql-general

From Michal Táborský
Subject Re: Query inside transaction
Date
Msg-id 40DC00BA.8020501@taborsky.cz
Whole thread Raw
List pgsql-general
NMB Webmaster wrote:

> But if someone else runs the same transaction in the same time what
 > value does "currval('sequence')" return? That one of the first
 > transaction or that one of the other transaction? Moreover, field
 > id is a unique primary key, it does not accept duplicates.

That's the beauty of sequences. They are transaction-safe. Co
"currval('sequence')" will always return the same value of the previous
nextval call within that transaction, no matter how many other
transactions picked the numbers in between.

To demonstrate:

Transaction 1                              Transaction 2
BEGIN;                    --
nextval('seq') = 1            BEGIN;
do something..                nextval('seq') = 2
do something else...            COMMIT;
currval('seq') = 1
COMMIT;

--
Michal Taborsky
http://www.taborsky.cz


pgsql-general by date:

Previous
From: Michal Táborský
Date:
Subject: Re:
Next
From: Ago
Date:
Subject: Re: Query inside transaction