From: Tom Lane <tgl@sss.pgh.pa.us>
To: luis.roberto@siscobra.com.br
Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Cc: alan.formagi <alan.formagi@siscobra.com.br>
Cc: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: James Coleman <jtc331@gmail.com>
Subject: Re: segfault with incremental sort
Date: Tue, 03 Nov 2020 11:52:06 -0500
Message-ID: <924226.1604422326@sss.pgh.pa.us> (raw)
In-Reply-To: <1779929796.39206557.1604407053112.JavaMail.zimbra@siscobra.com.br>
References: <622580997.37108180.1604080457319.JavaMail.zimbra@siscobra.com.br>
<102089.1604082122@sss.pgh.pa.us>
<541187747.37582276.1604086719237.JavaMail.zimbra@siscobra.com.br>
<107029.1604087483@sss.pgh.pa.us>
<1673261949.37800156.1604090102715.JavaMail.zimbra@siscobra.com.br>
<1807748588.37862034.1604091799640.JavaMail.zimbra@siscobra.com.br>
<136656.1604092024@sss.pgh.pa.us>
<1779929796.39206557.1604407053112.JavaMail.zimbra@siscobra.com.br>
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 subplan with 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...Attachments:more-paranoia-in-ExecInitSubPlan.patch (text/x-diff, 789B) downloaddiff --git a/src/backend/executor/nodeSubplan.c b/src/backend/executor/nodeSubplan.cindex 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 */
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, luis.roberto@siscobra.com.br, pgsql-bugs@lists.postgresql.org, alan.formagi@siscobra.com.br, tomas.vondra@2ndquadrant.com, jtc331@gmail.com
Subject: Re: segfault with incremental sort
In-Reply-To: <924226.1604422326@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox