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

From Tender Wang
Subject Re: BUG #19689: MERGE INSERT accepts a set-returning function during PREPARE but fails at EXECUTE
Date
Msg-id CAHewXN=t2OW_gehDsso17CLgqRfMfnhzDsby7pXWpzSy_-e9Tg@mail.gmail.com
Whole thread
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.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

pgsql-bugs by date:

Previous
From: Richard Guo
Date:
Subject: Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
Next
From: Manuel Reyes Bravo
Date:
Subject: Re: Detaching a child table makes an expression using it unrestorable