Re: BUG #17800: ON CONFLICT DO UPDATE fails to detect incompatible fields that leads to a server crash

From: Andres Freund <andres(at)anarazel(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, exclusion(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17800: ON CONFLICT DO UPDATE fails to detect incompatible fields that leads to a server crash
Date: 2023-02-24 01:54:17
Message-ID: 20230224015417.75yimxbksejpffh3@awork3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On 2023-02-23 15:36:36 -0800, Andres Freund wrote:
> When we build the evaluation step for the Param, we don't yet know that we're
> dealing with a MULTIEXPR (nor do we have a reference to the relevant
> SubPlan)). At the end of the targetlist, we have a special SubPlan, which make
> ExecInitSubPlan() set ParamExecData->execPlan to its SubPlanState for all the
> output parameters, to let ExecEvalParamExec know that the first reference to
> one of the output params needs to evalute the plan. But that means that we
> need to reset execPlan between rows, which is handled by the no-output
> ExecScanSubPlan() invocation at the end of the targetlist. That just seems
> baroque.

There's at least one case in the regression tests where a correlated MULTIEXPR
is in a non-resjunk TLE. I assume due to subquery pushdown. Is there a
problem with that? I don't immediately see any, but though it's worth
mentioning.

>
> ISTM that a saner sequence of expression steps would be:
> - steps to evalute the 1st argument of the MULTIEXPR, targetting SubPlanState->args[0]
> - steps to evalute the 2nd argument of the MULTIEXPR, targetting SubPlanState->args[1]
> ...
> - step to execute the subplan, computing output parameters
> - PARAM_SUBPLAN step referencing one of the outputs
> - steps for another output column
> - PARAM_SUBPLAN step referencing one of the outputs
> ...
>
> That'd completely obviate the need for any use of execPlan and thus remove the
> problem with getting confused about which subplan we need to execute.
>
>
> Unfortunately, we can't easily produce that today, because we don't have easy
> access to the SubPlan[State] at the time we encounter the Params.
>
>
> I am starting to wonder if a cleaner fix wouldn't be to add magic to
> ExecBuildProjectionInfo(), to find the junklist targetlist with the subplan,
> and then generate something like what I described above. Likely skipping the
> optimized/inlined evaluation of the arguments, initially at least.
>
>
> I didn't think of this until just now, but we actually already do a separate
> traversal of the expressions: ExecInitExprSlots(). Obviously the name
> wouldn't fit anymore, but it seems perfectly suited for collecting subplans
> that we'd need to evaluate?
>
>
> Let me try to hack that up.

Here's a rough prototype for that. Certainly would need a good bit more
polish, but I think the approach looks pretty promising?

I didn't do the part about evaluating the 'input' parameters as part of the
outer ExprState. Still think that's a good idea, but it's somewhat orthogonal
to the problems we're trying to fix.

Greetings,

Andres Freund

Attachment Content-Type Size
v2-0001-WIP-generate-explicit-expression-step-to-evaluate.patch text/x-diff 10.8 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message 小杨 2023-02-24 03:22:35 pg_upgrade does not support a table 2 in the original database to inherit from table 1 (field F_Test1 is not empty), and then table 2 modifies F by itself_ Test1 is nullable
Previous Message Michael Paquier 2023-02-24 00:36:50 Re: BUG #17744: Fail Assert while recoverying from pg_basebackup