Re: BUG #19111: Using EXPLAIN ANALYZE with MERGE causes failed assert

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: o(dot)tselebrovskiy(at)postgrespro(dot)ru, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19111: Using EXPLAIN ANALYZE with MERGE causes failed assert
Date: 2025-11-13 15:32:04
Message-ID: CAEZATCWqE70qVHv6fbdEXVrD7=hCQ_quKRKnirmukMqaKO5T=Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, 13 Nov 2025 at 12:18, PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
>
> On PostgreSQL 17+ if you do the following:
> ...
> Then the backend for the second psql crashes. With asserts turned on.

Thanks for the report.

What's happening here is that the MERGE in the second query has both
NOT MATCHED BY SOURCE and NOT MATCHED BY TARGET actions, so it does a
full join between the two tables. Initially there is a single matched
row, but the concurrent update turns that into a not matched pair of
rows and both actions are executed. So the ModifyTable node processes
it as 2 rows, whereas its parent node only outputs 1 row, which is
something the explain code doesn't like (because it computes the
difference, interpreting that as the number of rows skipped).

A possible solution would be something like the attached. It feels a
little ugly, but I don't see any other easy fix.

It's only a rough patch (it should have an isolation test case), but
it fixes the problem by causing the parent (full join) node to report
that it returned 2 rows, which it didn't really, but it would have
done, if the other update had happened before the MERGE, rather than
concurrently.

(Of course we could just drop that Assert, and it wouldn't cause any
harm, but it seems preferable to try to get the row counts right.)

Regards,
Dean

Attachment Content-Type Size
merge-explain-analyze-fix.patch text/x-patch 1.2 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Nathan Bossart 2025-11-13 16:47:14 Re:   Re: Re: Revoke Connect Privilege from Database not working
Previous Message PG Bug reporting form 2025-11-13 10:04:08 BUG #19111: Using EXPLAIN ANALYZE with MERGE causes failed assert