Re: Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction. - Mailing list pgsql-general

From Achilleas Mantzios
Subject Re: Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction.
Date
Msg-id 771eb8cf-42c7-468b-98ce-d0e8f86443d3@cloud.gatewaynet.com
Whole thread Raw
In response to Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction.  (Sagar Tiwari <iaansagar@gmail.com>)
List pgsql-general


On 8/30/25 10:43, Sagar Tiwari wrote:
Repro steps:
I created the table:
```
create table t (col TEXT primary key);
```

Open two database consoles and use the following queries in them:

```
1 begin;
2 select txid_current();
3 insert into t (col) values ('test') on conflict (col) do nothing;
4 commit;
```

I ran 1 and 2 in both. It worked for both.
And then I ran query 3 in the first console. It worked.
After that I ran query 3 in the second console. It got stuck!
When I commit the tx in first console, the stuck query in second console finishes
The transaction isolation level is 'read committed'. 

This is absolutely as it should be. 

When you issue the statement 3 in the second transaction it cannot know if the first transaction will be committed, rollbacked or aborted. If the first transaction commits, then there will be a conflict on the insertion in the  second xaction and it should do nothing, so this statement will not insert anything and return successfully, then the commit should work as well. But if the first transaction aborts or rollbacks, then the insert on the 2nd xaction should be attempted and most probably succeed. That's why the second xaction blocks. You can view the locks as you experiment via the pg_locks view.


--
Best Regards
Sagar Tiwari

pgsql-general by date:

Previous
From: Sagar Tiwari
Date:
Subject: Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction.
Next
From: Nico Williams
Date:
Subject: Re: Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction.