Concurrent Insert and Update postgres
<p>Is there a way to solve a problem when you receive a concurrent insert and update for a same field over a  multi-threaded environment.</p>

<p>Example </p>

<p>Thread 1 </p>

<pre><code>BEGIN 
insert into users (name,age) values('spiderman',27)
COMMIT
</code></pre>

<p>Thread 2</p>

<pre><code>BEGIN
 update into users set age = 26 where name='spiderman';
COMMIT
</code></pre>

<p>Most of the time update(transaction) is unaware of the insert happening hence it throws and error.</p>

<p>What I see over a here is classical example of race conditions and one way to avoid race conditions is by using effecting locks.</p>

<p>How to apply locks(record) on a record that don't exist in DB yet.</p>

<p>In the above example for insert statement so that Update knows that record is not in DB yet.</p>