Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kZzlP-0006zn-Dv for pgsql-bugs@arkaria.postgresql.org; Tue, 03 Nov 2020 17:06:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kZzlO-0002ad-A9 for pgsql-bugs@arkaria.postgresql.org; Tue, 03 Nov 2020 17:06:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kZzlO-0002aW-3p for pgsql-bugs@lists.postgresql.org; Tue, 03 Nov 2020 17:06:30 +0000 Received: from mta97aws110.mxout.inova.com.br ([3.223.97.110] helo=mxhero01.a.inova.com.br) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kZzlI-0005IL-Cr for pgsql-bugs@lists.postgresql.org; Tue, 03 Nov 2020 17:06:29 +0000 Received: from ip-10-0-4-78.ec2.internal (localhost [127.0.0.1]) by mxhero01.a.inova.com.br (Postfix) with ESMTP id A1BD11CE6FD for ; Tue, 3 Nov 2020 17:06:22 +0000 (UTC) Received: from corpmoutpolicyd01.a.inova.com.br (puppetmaster.a.inova.com.br [10.0.1.88]) by mxhero01.a.inova.com.br (Postfix) with ESMTP; Tue, 3 Nov 2020 17:06:22 +0000 (UTC) Received: from corpmoutpolicyd01.a.inova.com.br (localhost [127.0.0.1]) by corpmoutpolicyd01.a.inova.com.br (Postfix) with ESMTP id 78A8B22092E; Tue, 3 Nov 2020 15:06:16 -0200 (BRST) Received: from corpmout01.a.inova.com.br (ip-10-0-1-125.ec2.internal [10.0.1.125]) by corpmoutpolicyd01.a.inova.com.br (Postfix) with ESMTP id 55D0BE02D2; Tue, 3 Nov 2020 15:06:08 -0200 (BRST) Received: from corpmbox02.a.inova.com.br (ip-10-0-4-158.ec2.internal [10.0.4.158]) by corpmout01.a.inova.com.br (Postfix) with ESMTP; Tue, 3 Nov 2020 14:06:08 -0300 (-03) Date: Tue, 3 Nov 2020 14:06:08 -0300 (BRT) From: luis.roberto@siscobra.com.br To: Tom Lane Cc: pgsql-bugs , "alan.formagi" , Tomas Vondra , James Coleman Message-ID: <1332064613.40286998.1604423168236.JavaMail.zimbra@siscobra.com.br> In-Reply-To: <924226.1604422326@sss.pgh.pa.us> References: <622580997.37108180.1604080457319.JavaMail.zimbra@siscobra.com.br> <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> <924226.1604422326@sss.pgh.pa.us> Subject: Re: segfault with incremental sort MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_52efb5da-6e0d-481e-81f3-c059b8bf550c" X-Originating-IP: [10.0.1.247] X-Mailer: Zimbra 8.8.15_GA_3968 (ZimbraWebClient - GC86 (Win)/8.8.15_GA_3968) Thread-Topic: segfault with incremental sort Thread-Index: k39v9e8WFPXK0qtBEHcGyVqFBW0Asg== x-mxHero-Origin-Ip: /127.0.0.1:58248 X-mxHero-AttachmentBlock: rule=48;result=false List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_52efb5da-6e0d-481e-81f3-c059b8bf550c Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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 Thanks for confirming it. For now, disabling incremental sort will 'solve' the issue. I'll be keeping an eye on that thread. --=_52efb5da-6e0d-481e-81f3-c059b8bf550c Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Thanks for the test case!  It looks like this issue is somewhat relat= ed
to the "enable_incremental_sort changes query behavior" thread [1].What I see happening is

1. The SELECT DISTINCT gives rise to a sor= t key expression that
contains non-parallel-safe SubPlans.  (It's n= ot immediately apparent
to me why we don't consider these particular sub= queries parallel safe,
but they aren't.  Anyway such a situation su= rely has to be allowed for.)

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

3. ExecSerializePlan not= es that the subplans aren't parallel-safe and
doesn't send them to the w= orkers.

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

So, not only do we need to be = thinking about volatility while checking
whether IncrementalSort is poss= ible, 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 bu= gs in future.  I'm thinking of
committing the attached to at least = reduce the stakes from "core dump"
to "weird error message".

&nbs= p;            &= nbsp;          regards, t= om lane

Than= ks for confirming it. For now, disabling incremental sort will 'solve' the = issue. I'll be keeping an eye on that thread. 
--=_52efb5da-6e0d-481e-81f3-c059b8bf550c Content-Type: text/x-diff; charset=utf-8 Content-Transfer-Encoding: 7bit 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 */ --=_52efb5da-6e0d-481e-81f3-c059b8bf550c--