Re: [GENERAL] The value returned by autoinc ?

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Silvio Emanuel Barbosa de Macedo <smacedo(at)inescn(dot)pt>
Cc: pgsql general mailing list <pgsql-general(at)postgreSQL(dot)org>
Subject: Re: [GENERAL] The value returned by autoinc ?
Date: 1999-03-15 10:15:34
Message-ID: l03110704b3128cb1b3e5@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 11:59 +0200 on 15/3/99, Silvio Emanuel Barbosa de Macedo wrote:

> If there is an insert between my INSERT and SELECT, won't the counter be
> increased ? The only way I can understand this is the transaction locks
> inserts... so, in fact there could not exist another insert...

The counter is increased - but you get the last value *you* used, not the
other. It's more or less like this:

counter = 5; Process1 hidden variable = null; Process2 hidden var = null;

-- Process1 inserts a tuple, calling nextval.

counter = 6; Process1 hidden variable = 6;

-- Second process inserts a tuple.

counter = 7; Process1 hidden variable = 6. Process2 hidden var = 7;

-- Process1 now wants to know which number it entered, calling currval.
-- Currval takes the value in the hidden variable. It's 6.

-- Now suppose process1 makes another insertion. Then:

counter = 8; Process1 hidden variable = 8. Process2 hidden var = 8;

Do you understand? Whenever you make a call to currval, your process gets
the value thus retrieved and keeps it. The two operations are done
atomically (uncrementing and checking what value was taken), so it doesn't
actually matter when you make the call to currval - the correct value is
already available to you.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Petr Vanicek 1999-03-15 10:41:31 fulltextindex function problem
Previous Message Clark Evans 1999-03-15 10:10:53 Re: [GENERAL] The value returned by autoinc ?