Re: MERGE behavior with REPEATABLE READ isolation level

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: MERGE behavior with REPEATABLE READ isolation level
Date: 2026-02-24 03:12:20
Message-ID: CAHewXNmtg=-WRczRUmd0tyNaP57WYOg74=O2pMssq9hWS3YoOw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Alexander,

Alexander Korotkov <aekorotkov(at)gmail(dot)com> 于2026年2月15日周日 09:23写道:
> Oh, sorry I missed the begin statement for s1. The complete case should look like this.
>
> s1# create table test (id int primary key, val int);
> s1# insert into test values (1,0);
>
> s2# begin;
> s2# update test set val = val + 100;
>
> s1# begin isolation level repeatable read;
> s1# MERGE INTO test t USING (VALUES (1, 100)) AS s (id, inc)
> ON t.id = s.id
> WHEN MATCHED THEN
> UPDATE SET val = t.val + s.inc
> WHEN NOT MATCHED THEN
> INSERT (id, val) VALUES (s.id, s.inc);
> (waiting ...)
>
> s2# commit;
>
> s1# MERGE 1
> s1# select * from test;
> id | val
> ----+-----
> 1 | 200
> (1 row)
>

I tried "update test set val = val + 100;" but the SQL reported a
"could not serialize access due to concurrent update" error.
It seems that the MERGE command should behave identically to UPDATE
when performing a match action.

I wrote a fix patch and attached it, and added your test case, too.

--
Thanks,
Tender Wang

Attachment Content-Type Size
0001-Fix-MERGE-match-do-update-in-RR-isolation-level.patch text/plain 3.7 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-02-24 03:25:13 Re: DOCS - pg_waldump synopsis sgml markup
Previous Message Chao Li 2026-02-24 03:11:08 Re: DOCS - pg_walsummary typo?