Re: segfault with incremental sort - Mailing list pgsql-bugs

From Tom Lane
Subject Re: segfault with incremental sort
Date
Msg-id 924226.1604422326@sss.pgh.pa.us
Whole thread Raw
In response to Re: segfault with incremental sort  (luis.roberto@siscobra.com.br)
Responses Re: segfault with incremental sort
Re: segfault with incremental sort
Re: segfault with incremental sort
List pgsql-bugs
luis.roberto@siscobra.com.br writes:
> It took me some time to make it the same... I managed to simplify the error. It appears to be something related to a
subplanwith a distinct clause and another subplan...  

Thanks for the test case!  It looks like this issue is somewhat related
to the "enable_incremental_sort changes query behavior" thread [1].
What I see happening is

1. The SELECT DISTINCT gives rise to a sort key expression that
contains non-parallel-safe SubPlans.  (It's not immediately apparent
to me why we don't consider these particular subqueries parallel safe,
but they aren't.  Anyway such a situation surely has to be allowed for.)

2. The planner ignores the fact that the sort key isn't parallel-safe
and makes a plan with IncrementalSort below Gather anyway.  (I'm not
certain that this bug is specific to IncrementalSort; but given the lack
of previous complaints, I'm guessing we avoid this somehow for regular
sorts.)

3. ExecSerializePlan notes that the subplans aren't parallel-safe and
doesn't send them to the workers.

4. In the workers, nodeSubplan.c dumps core because the planstate it's
expecting is not there.

So, not only do we need to be thinking about volatility while checking
whether IncrementalSort is possible, but also parallel-safety.

In the meantime, now that I've seen this I don't have a lot of confidence
that we'll never inject similar bugs in future.  I'm thinking of
committing the attached to at least reduce the stakes from "core dump"
to "weird error message".

            regards, tom lane

[1]
https://www.postgresql.org/message-id/flat/CAJGNTeNaxpXgBVcRhJX%2B2vSbq%2BF2kJqGBcvompmpvXb7pq%2BoFA%40mail.gmail.com

diff --git a/src/backend/executor/nodeSubplan.c b/src/backend/executor/nodeSubplan.c
index 9a706df5f0..152c7ae7eb 100644
--- a/src/backend/executor/nodeSubplan.c
+++ b/src/backend/executor/nodeSubplan.c
@@ -797,7 +797,15 @@ ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
     sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
                                                subplan->plan_id - 1);

-    /* ... and to its parent's state */
+    /*
+     * This check can fail if the planner mistakenly puts a parallel-unsafe
+     * subplan into a parallelized subquery; see ExecSerializePlan.
+     */
+    if (sstate->planstate == NULL)
+        elog(ERROR, "subplan \"%s\" was not initialized",
+             subplan->plan_name);
+
+    /* Link to parent's state, too */
     sstate->parent = parent;

     /* Initialize subexpressions */

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: User with BYPASSRLS privilege can't change password
Next
From: luis.roberto@siscobra.com.br
Date:
Subject: Re: segfault with incremental sort