Re: How to lock a table for update and make other connections to wait on it

From: "Karsten Hilbert" <Karsten(dot)Hilbert(at)gmx(dot)net>
To:
Cc: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: How to lock a table for update and make other connections to wait on it
Date: 2015-07-09 22:20:41
Message-ID: trinity-e5f61172-56ce-4b5e-9832-df3f03c1824e-1436480441290@3capp-gmx-bs07
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Now I am facing a new problem. Below is the detailed description of the problem.
 
We have multiple instances of a program which runs on different locations and inserts certain data in the same database.
 
Below are the steps followed by an instance of the program
 
1.       Insert a row to a table , using cursor.execute()
2.       To fetch the row id of the above inserted data, we are using cursor.execute() to execute a sql command which fetches the max ‘id’ from the table (id is a column in the table which gets incremented on each insert)
 
Here the problem is, as we have parallel processes updating the same db and same table, before we execute step 2 there might be one more row inserted  into the table by a different process.
In this case step 2 returns me the wrong id.

You need: INSERT ... RETURNING ...

Karsten

In response to

Browse psycopg by date

  From Date Subject
Next Message Vamsi Krishna Reddy -T (vamsredd - TATA CONSULTANCY SERVICES LIMITED at Cisco) 2015-07-13 14:06:41 Re: How to lock a table for update and make other connections to wait on it
Previous Message Christophe Pettus 2015-07-09 17:25:44 Re: How to lock a table for update and make other connections to wait on it