Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9Ctb-0003FX-41 for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Apr 2018 16:58:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f9CtZ-0004kM-5k for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Apr 2018 16:58:53 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9CtY-0004kD-Sq for pgsql-bugs@lists.postgresql.org; Thu, 19 Apr 2018 16:58:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9CtW-0000y6-8E for pgsql-bugs@lists.postgresql.org; Thu, 19 Apr 2018 16:58:51 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w3JGwmtM018364; Thu, 19 Apr 2018 12:58:48 -0400 From: Tom Lane To: Andrew Gierth cc: Andreas Karlsson , pgsql-bugs@lists.postgresql.org Subject: Re: Ignored join clause In-reply-to: <87efjbe8rz.fsf@news-spur.riddles.org.uk> References: <87muy0dk4a.fsf@news-spur.riddles.org.uk> <87efjbe8rz.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Thu, 19 Apr 2018 12:59:40 +0100" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <18297.1524157045.0@sss.pgh.pa.us> Date: Thu, 19 Apr 2018 12:58:47 -0400 Message-ID: <18363.1524157127@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <18297.1524157045.1@sss.pgh.pa.us> Andrew Gierth writes: > I think I was wrong, and that in fact this is a much more general > problem which amounts to a lack of communication between > get_joinrel_parampathinfo and extract_actual_join_clauses. Yeah, I think you're right. The rules about moving clauses down into a parameterized path may require something that had been a regular outer join clause to be moved to a join below its syntactic level. If we'd done that because the clause was actually degenerate (not mentioning the outer join's LHS), we'd have marked it is_pushed_down, but that doesn't seem practical in this situation since the RestrictInfo probably is also referenced in other paths where it's not pushed down. And I don't want to start making multiple RestrictInfos for the same clause --- that'll break some other stuff. So the only practical answer seems to be to teach extract_actual_join_clauses to check the clause's syntactic level along with is_pushed_down, as per the attached patch. Out of curiosity, I put Assert(bms_is_subset(rinfo->required_relids, joinrelids)); in there, and verified that we have no existing regression test that hits the assertion, though of course Andreas' example does. I've been pretty dissatisfied with the squishiness of is_pushed_down for some time (cf comments in initsplan.c around line 1770), and this bug seems like a sufficient reason to write it out of existence entirely. But that's surely not going to lead to a back-patchable change, so it's likely something for v12. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="parameterization-filter-clause-fix.patch"; charset="us-ascii" Content-ID: <18297.1524157045.2@sss.pgh.pa.us> Content-Description: parameterization-filter-clause-fix.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimiz= er/plan/createplan.c index b6ad01b..280f21c 100644 *** a/src/backend/optimizer/plan/createplan.c --- b/src/backend/optimizer/plan/createplan.c *************** create_nestloop_plan(PlannerInfo *root, *** 3802,3807 **** --- 3802,3808 ---- if (IS_OUTER_JOIN(best_path->jointype)) { extract_actual_join_clauses(joinrestrictclauses, + best_path->path.parent->relids, &joinclauses, &otherclauses); } else *************** create_mergejoin_plan(PlannerInfo *root, *** 3917,3922 **** --- 3918,3924 ---- if (IS_OUTER_JOIN(best_path->jpath.jointype)) { extract_actual_join_clauses(joinclauses, + best_path->jpath.path.parent->relids, &joinclauses, &otherclauses); } else *************** create_hashjoin_plan(PlannerInfo *root, *** 4213,4218 **** --- 4215,4221 ---- if (IS_OUTER_JOIN(best_path->jpath.jointype)) { extract_actual_join_clauses(joinclauses, + best_path->jpath.path.parent->relids, &joinclauses, &otherclauses); } else diff --git a/src/backend/optimizer/util/restrictinfo.c b/src/backend/optim= izer/util/restrictinfo.c index 1075dde..65c1abc 100644 *** a/src/backend/optimizer/util/restrictinfo.c --- b/src/backend/optimizer/util/restrictinfo.c *************** extract_actual_clauses(List *restrictinf *** 381,386 **** --- 381,387 ---- */ void extract_actual_join_clauses(List *restrictinfo_list, + Relids joinrelids, List **joinquals, List **otherquals) { *************** extract_actual_join_clauses(List *restri *** 393,399 **** { RestrictInfo *rinfo =3D lfirst_node(RestrictInfo, l); = ! if (rinfo->is_pushed_down) { if (!rinfo->pseudoconstant) *otherquals =3D lappend(*otherquals, rinfo->clause); --- 394,408 ---- { RestrictInfo *rinfo =3D lfirst_node(RestrictInfo, l); = ! /* ! * We must check both is_pushed_down and required_relids, since an ! * outer-join clause that's been pushed down to some lower join level ! * via path parameterization will not be marked is_pushed_down; ! * nonetheless, it must be treated as a filter clause not a join ! * clause so far as the lower join level is concerned. ! */ ! if (rinfo->is_pushed_down || ! !bms_is_subset(rinfo->required_relids, joinrelids)) { if (!rinfo->pseudoconstant) *otherquals =3D lappend(*otherquals, rinfo->clause); diff --git a/src/include/optimizer/restrictinfo.h b/src/include/optimizer/= restrictinfo.h index 9cd874d..a734d79 100644 *** a/src/include/optimizer/restrictinfo.h --- b/src/include/optimizer/restrictinfo.h *************** extern List *get_actual_clauses(List *re *** 36,41 **** --- 36,42 ---- extern List *extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant); extern void extract_actual_join_clauses(List *restrictinfo_list, + Relids joinrelids, List **joinquals, List **otherquals); extern bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *b= aserel); ------- =_aaaaaaaaaa0--