Re: BUG #19689: MERGE INSERT accepts a set-returning function during PREPARE but fails at EXECUTE

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: imchifan(at)163(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19689: MERGE INSERT accepts a set-returning function during PREPARE but fails at EXECUTE
Date: 2026-09-16 02:56:34
Message-ID: CAHewXN=t2OW_gehDsso17CLgqRfMfnhzDsby7pXWpzSy_-e9Tg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> 于2026年9月15日周二 17:36写道:
>
> Additional information
> ----------------------
> The issue was reproduced on PostgreSQL 20devel, PostgreSQL 18.6, and
> PostgreSQL 17.11. Inference: the MERGE INSERT action's specialized
> expression handling does not propagate or enforce the restriction on
> set-returning functions during analysis, leaving the executor to detect the
> unsupported context.

I looked into this issue.

MERGE INSERT values are transformed using EXPR_KIND_VALUES_SINGLE,
which permits set-returning functions and sets
pstate->p_hasTargetSRFs. However, transformMergeStmt() later
unconditionally sets qry->hasTargetSRFs to false, and a MERGE action
target list is not expanded using a ProjectSet plan node.

Consequently, the SRF is accepted during PREPARE, but later fails
during executor expression initialization with:

ERROR: set-valued function called in context that cannot accept a set

The attached patch is one possible fix. It checks
pstate->p_hasTargetSRFs immediately after transforming the MERGE
INSERT values and reports an error during parse analysis. The check
is placed specifically in the CMD_INSERT case, so it does not affect
the other MERGE actions or set-returning functions used as the MERGE
data source.

I have not included a regression test in this version, since I would
first like to confirm whether rejecting SRFs in MERGE INSERT actions
is the intended behavior. If so, I can add a test in the next
version.

Thoughts?

--
Thanks,
Tender Wang

Attachment Content-Type Size
0001-Reject-set-returning-functions-in-MERGE-INSERT-actio.patch text/plain 1.9 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Manuel Reyes Bravo 2026-09-16 07:14:40 Re: Detaching a child table makes an expression using it unrestorable
Previous Message Richard Guo 2026-09-16 00:19:39 Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation