Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vXoX2-006U6Q-1e for pgsql-bugs@arkaria.postgresql.org; Mon, 22 Dec 2025 22:37:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vXoX0-00GTqF-1X for pgsql-bugs@arkaria.postgresql.org; Mon, 22 Dec 2025 22:37:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vXoX0-00GTq6-0g for pgsql-bugs@lists.postgresql.org; Mon, 22 Dec 2025 22:37:34 +0000 Received: from mail-oa1-x2d.google.com ([2001:4860:4864:20::2d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vXoWy-002BpR-0n for pgsql-bugs@lists.postgresql.org; Mon, 22 Dec 2025 22:37:34 +0000 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-3e83c40e9dfso2682400fac.1 for ; Mon, 22 Dec 2025 14:37:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766443050; x=1767047850; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=B8tajcMnFxk5io8ltMIvH8Z0LYHoUTDOjtmR4ksPS0E=; b=PixmSHx76lWGT9sAdbGErkTXId7Jn8Jl6biJ/XGba1I5v+TJL7Dp2aiMBuYQNRia+a bjFUgigcC7f6z5oOjwaV3X6d24Bz7vBqHTpscQpzvmiUKEgOBtP3JTjVrpL6WStKE0gj Y4B6sA+vv2TgzSb1jVubCQmjFtru+0B4gMMpCwYAT2O0fXEiHbYEF2ijltZs8j6BaHl1 aqOZBfd5CY2QCmJMs10z12JCd1DAEWY5sajkSNUjTbutPiqwVN/NPnu3gwgqMfC8eSoa KpVCaN44k2sHtoQcqH/hcWnKfTawaWa0890xPrfptiFpopXxlfZPQgPTojQOIYJpf6pV jyuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766443050; x=1767047850; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=B8tajcMnFxk5io8ltMIvH8Z0LYHoUTDOjtmR4ksPS0E=; b=qPOpD5z/e4aihqR59LmXAjebaocC8qqCMKEYK1LvQrWI6R0a0CBhQw3Y9+XktZz2Mh MVQf3m/7FPQbRv9o4dzt4DT+4xnluBhbb/4FzF3KLnf2hulsMseiFRBeUL0PyHX2Lejc z01Hjsfm9QJ8iQk1Wk8WRYI/l2d0F4VJYyJldFa7mn1hq9AJjnSd5cVdrEWbBad2f1E4 96SQ7AHbhIXEt+95HcMG2ffa23DE4gXZUn56cjUkdhyJY7NDosJM6tckZaZqwIJr++br UjUFTlZllZ31BdbvBTj95uU+bYPOXjLplMDtszWVzjVmxcv90esoq+ZQE+i7pZgrzS6y dX9w== X-Forwarded-Encrypted: i=1; AJvYcCVG9kzqDmPOCAUWbr8uBrkBKuDyaUUFbiGiMargmieOEaWXJDedR5nwcEQMzVaFieSR6eRmkQ7rScOY@lists.postgresql.org X-Gm-Message-State: AOJu0YzjKhp9NtFFkqBGmWEuzI1ixN9oeseR0U+a0PZ2O9e2DCk3jF7I LA9MpKhVfm6dCRjbgaQ7WvIo5swN9QA72GtEIJzqD1QgDNUXJ+PlSKpk+izOIsBvCclpHF8VUsC LZH9AmSbzL8N9at0bGN8lYplPiOkRiec= X-Gm-Gg: AY/fxX5d8a3FT6wYitAiV0wIoULUz6R1y2eu24Dv7W8N+eW7pEBmNW8/ZilZz+VFdZj h7JIhZmYV7tYfN9B1WTaH4KtttIciQd7k5r9Ww1oNM7ukej0ARqdUiva8e1g5jeAA2LIXi9PA73 vam4rl4Ig9ggZhRfKnCFRnGPVAF7TpD/Nt/Ql77DFEcD7C3zVWlc9dv8TevD2aNDtG3Z8f0K/g8 bKx7GCsg4Uh+VMx8+rNzTdONJs2H8LBhlKlpkeVZuS6MTdB3Fzs07HQHMhAhQFUiazFGo2T X-Google-Smtp-Source: AGHT+IFlSBjIEW4LZ90vSoctt+y84EXtInfJjAbqiRPUmUWcPxJIyiZSAwOlgqFbvWcWJ3l0Fbxiak5Kyon23Qnql84= X-Received: by 2002:a05:6820:f029:b0:65d:860:4ed1 with SMTP id 006d021491bc7-65d0e94cfa1mr5661931eaf.1.1766443049967; Mon, 22 Dec 2025 14:37:29 -0800 (PST) MIME-Version: 1.0 References: <19355-57d7d52ea4980dc6@postgresql.org> <868ff2a518820c8864b6d28510294b2457a126af.camel@cybertec.at> In-Reply-To: From: Dean Rasheed Date: Mon, 22 Dec 2025 22:37:18 +0000 X-Gm-Features: AQt7F2qine35_gnbN5E-fnqQg-HPDRstLYQhgOuwKiRaAjyVj-Kbf_JT0-h-Igc Message-ID: Subject: Re: BUG #19355: Attempt to insert data unexpectedly during concurrent update To: Bh W Cc: Laurenz Albe , pgsql-bugs@lists.postgresql.org, Amit Langote Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 22 Dec 2025 at 14:51, Bh W wrote: > > The issue is that the MERGE INTO match condition is not updated. > In the MATCHED path of MERGE INTO, when the target row satisfies the matc= h condition and the condition itself has not changed, the system should sti= ll be able to handle concurrent updates to the same target row by relying o= n EvalPlanQual (EPQ) to refetch the latest version of the tuple, and then p= roceed with the intended update. > However, in the current implementation, even though the concurrent update= does not modify any columns relevant to the ON condition, the EPQ recheck = unexpectedly results in a match condition failure, causing the update path = that should remain MATCHED to be treated as NOT MATCHED. I spent a little time looking at this, and managed to reduce the reproducer test case down to this: -- Setup drop table if exists t1,t2; create table t1(a int primary key, b int); create table t2(a int, b int); insert into t1 values(1,0),(2,0); insert into t2 values(1,1),(2,2); -- Session 1 begin; update t1 set b =3D b+1; -- Session 2 merge into t1 using (values(1,1),(2,2)) as t3(a,b) on (t1.a =3D t3.a) when matched then update set b =3D t1.b + 1 when not matched then insert (a,b) values (1,1); -- Session 1 commit; This works fine in PG17, but fails with a PK violation in PG18. Git-bisecting points to this commit: cbc127917e04a978a788b8bc9d35a70244396d5b is the first bad commit commit cbc127917e04a978a788b8bc9d35a70244396d5b Author: Amit Langote Date: Fri Feb 7 17:15:09 2025 +0900 Track unpruned relids to avoid processing pruned relations Doing a little more debugging, it looks like the problem might be this change in InitPlan(): - /* ignore "parent" rowmarks; they are irrelevant at runtime */ - if (rc->isParent) + /* + * Ignore "parent" rowmarks, because they are irrelevant at + * runtime. Also ignore the rowmarks belonging to child tables + * that have been pruned in ExecDoInitialPruning(). + */ + if (rc->isParent || + !bms_is_member(rc->rti, estate->es_unpruned_relids)) continue; which seems to cause it to incorrectly skip a rowmark, which I suspect is what is causing EvalPlanQual() to return the wrong result. Regards, Dean