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

From Nico Williams
Subject Re: Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction.
Date
Msg-id aLMt00w2HukeP5k0@ubby
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 Sat, Aug 30, 2025 at 01:13:35PM +0530, 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'.

The transaction that loses the race to do the insert (step 3) has to
block somewhere waiting for the winning transaction to commit or
rollback.  The losing transaction could have continued speculatively and
block in the commit (step 4) instead of in the insert, but it still had
to block.

Does it matter that it blocks in the insert instead of the commit?



pgsql-general by date:

Previous
From: Achilleas Mantzios
Date:
Subject: Re: Saw some strange behavior when using `INSERT ON CONFLICT` inside a transaction.
Next
From: Adrian Klaver
Date:
Subject: Re: In-order pg_dump (or in-order COPY TO)