Thread: Currval question and confusion

Currval question and confusion

From
Kumar S
Date:
Dear Group,
 my question:

I am using python scripts to create insert statements
for populating my tables.

First I fill table 1 and the primary key of table1 is
a FK in table2. Table2 PK is FK in Table3

I am filling in the following order:
Table 1

Take currval(table1_pk_seq) of ta table1 and write in
table2

Take currval in table2 and fill in the table3.

Now 3 people have simultaneously issued insert
statements. After some time while the second process
is still filling table 1 and enters table2 the currval
position has been changed. Then will I be assigning a
wrong FK in table2 and table3.

In this case Do i have to write any triggers.?

Please suggest some ideas.

thank you
k




__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/

Re: Currval question and confusion

From
Michael Fuhr
Date:
On Sat, Mar 05, 2005 at 11:40:47AM -0800, Kumar S wrote:

> Now 3 people have simultaneously issued insert
> statements. After some time while the second process
> is still filling table 1 and enters table2 the currval
> position has been changed. Then will I be assigning a
> wrong FK in table2 and table3.

Are you asking if there's a problem, or are you saying you've
observed a problem?  currval() is documented to return the most
recently obtained value *in the current connection*, so if each
process uses a different connection then you shouldn't have a
problem.  Connection pooling could interfere with this, however,
if it allows queries from different processes to be interleaved on
the same connection.  In that case you could call nextval() prior
to the inserts and remember the key values on the client side.

http://www.postgresql.org/docs/8.0/interactive/functions-sequence.html
http://www.postgresql.org/files/documentation/faqs/FAQ.html#4.11.3

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/