Fix thinko in outer-join removal.

If we have a RestrictInfo that mentions both the removal-candidate
relation and the outer join's relid, then that is a pushed-down
condition not a join condition, so it should be grounds for deciding
that we can't remove the outer join.  In commit 2489d76c4, I'd blindly
included the OJ's relid into "joinrelids" as per the new standard
convention, but the checks of attr_needed and ph_needed should only
allow the join's input rels to be mentioned.

Having done that, the check for references in pushed-down quals
a few lines further down should be redundant.  I left it in place
as an Assert, though.

While researching this I happened across a couple of comments that
worried about the effects of update_placeholder_eval_levels.
That's gone as of b448f1c8d, so we can remove some worry.

Per bug #17769 from Robins Tharakan.  The submitted test case
triggers this more or less accidentally because we flatten out
a LATERAL sub-select after we've done join strength reduction;
if we did that in the other order, this problem would be masked
because the outer join would get simplified to an inner join.
To ensure that the committed test case will continue to test
what it means to even if we make that happen someday, use a
test clause involving COALESCE(), which will prevent us from
using it to do join strength reduction.

Patch by me, but thanks to Richard Guo for initial investigation.

Discussion: https://postgr.es/m/17769-e4f7a5c9d84a80a7@postgresql.org
This commit is contained in:
Tom Lane 2023-02-04 15:19:54 -05:00
parent 5840c20272
commit 8538519db1
5 changed files with 39 additions and 29 deletions

View File

@ -164,6 +164,7 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
{ {
int innerrelid; int innerrelid;
RelOptInfo *innerrel; RelOptInfo *innerrel;
Relids inputrelids;
Relids joinrelids; Relids joinrelids;
List *clause_list = NIL; List *clause_list = NIL;
ListCell *l; ListCell *l;
@ -190,17 +191,16 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
return false; return false;
/* Compute the relid set for the join we are considering */ /* Compute the relid set for the join we are considering */
joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand); inputrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
if (sjinfo->ojrelid != 0) Assert(sjinfo->ojrelid != 0);
joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid); joinrelids = bms_copy(inputrelids);
joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid);
/* /*
* We can't remove the join if any inner-rel attributes are used above the * We can't remove the join if any inner-rel attributes are used above the
* join. * join. Here, "above" the join includes pushed-down conditions, so we
* * should reject if attr_needed includes the OJ's own relid; therefore,
* Note that this test only detects use of inner-rel attributes in higher * compare to inputrelids not joinrelids.
* join conditions and the target list. There might be such attributes in
* pushed-down conditions at this join, too. We check that case below.
* *
* As a micro-optimization, it seems better to start with max_attr and * As a micro-optimization, it seems better to start with max_attr and
* count down rather than starting with min_attr and counting up, on the * count down rather than starting with min_attr and counting up, on the
@ -211,7 +211,7 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
attroff >= 0; attroff >= 0;
attroff--) attroff--)
{ {
if (!bms_is_subset(innerrel->attr_needed[attroff], joinrelids)) if (!bms_is_subset(innerrel->attr_needed[attroff], inputrelids))
return false; return false;
} }
@ -230,7 +230,7 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
if (bms_overlap(phinfo->ph_lateral, innerrel->relids)) if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
return false; /* it references innerrel laterally */ return false; /* it references innerrel laterally */
if (bms_is_subset(phinfo->ph_needed, joinrelids)) if (bms_is_subset(phinfo->ph_needed, inputrelids))
continue; /* PHV is not used above the join */ continue; /* PHV is not used above the join */
if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids)) if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
continue; /* it definitely doesn't reference innerrel */ continue; /* it definitely doesn't reference innerrel */
@ -273,13 +273,11 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
{ {
/* /*
* If such a clause actually references the inner rel then join * If such a clause actually references the inner rel then join
* removal has to be disallowed. We have to check this despite * removal has to be disallowed. The previous attr_needed checks
* the previous attr_needed checks because of the possibility of * should have caught this, though.
* pushed-down clauses referencing the rel.
*/ */
if (bms_is_member(innerrelid, restrictinfo->clause_relids)) Assert(!bms_is_member(innerrelid, restrictinfo->clause_relids));
return false; continue; /* ignore; not useful here */
continue; /* else, ignore; not useful here */
} }
/* Ignore if it's not a mergejoinable clause */ /* Ignore if it's not a mergejoinable clause */

View File

@ -494,9 +494,6 @@ extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
* create_lateral_join_info * create_lateral_join_info
* Fill in the per-base-relation direct_lateral_relids, lateral_relids * Fill in the per-base-relation direct_lateral_relids, lateral_relids
* and lateral_referencers sets. * and lateral_referencers sets.
*
* This has to run after deconstruct_jointree, because we need to know the
* final ph_eval_at values for PlaceHolderVars.
*/ */
void void
create_lateral_join_info(PlannerInfo *root) create_lateral_join_info(PlannerInfo *root)
@ -509,6 +506,9 @@ create_lateral_join_info(PlannerInfo *root)
if (!root->hasLateralRTEs) if (!root->hasLateralRTEs)
return; return;
/* We'll need to have the ph_eval_at values for PlaceHolderVars */
Assert(root->placeholdersFrozen);
/* /*
* Examine all baserels (the rel array has been set up by now). * Examine all baserels (the rel array has been set up by now).
*/ */

View File

@ -198,16 +198,6 @@ pull_varnos_walker(Node *node, pull_varnos_context *context)
* fall back to the conservative assumption that the PHV will be * fall back to the conservative assumption that the PHV will be
* evaluated at its syntactic level (phv->phrels). * evaluated at its syntactic level (phv->phrels).
* *
* There is a second hazard: this code is also used to examine
* qual clauses during deconstruct_jointree, when we may have a
* PlaceHolderInfo but its ph_eval_at value is not yet final, so
* that theoretically we could obtain a relid set that's smaller
* than we'd see later on. That should never happen though,
* because we deconstruct the jointree working upwards. Any outer
* join that forces delay of evaluation of a given qual clause
* will be processed before we examine that clause here, so the
* ph_eval_at value should have been updated to include it.
*
* Another problem is that a PlaceHolderVar can appear in quals or * Another problem is that a PlaceHolderVar can appear in quals or
* tlists that have been translated for use in a child appendrel. * tlists that have been translated for use in a child appendrel.
* Typically such a PHV is a parameter expression sourced by some * Typically such a PHV is a parameter expression sourced by some

View File

@ -5150,6 +5150,21 @@ SELECT * FROM
1 | 4567890123456789 | -4567890123456789 | 4567890123456789 1 | 4567890123456789 | -4567890123456789 | 4567890123456789
(5 rows) (5 rows)
-- join removal bug #17769: can't remove if there's a pushed-down reference
EXPLAIN (COSTS OFF)
SELECT q2 FROM
(SELECT *
FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss
WHERE COALESCE(dat1, 0) = q1;
QUERY PLAN
----------------------------------------------------------------
Nested Loop Left Join
Filter: (COALESCE(innertab.dat1, '0'::bigint) = int8_tbl.q1)
-> Seq Scan on int8_tbl
-> Index Scan using innertab_pkey on innertab
Index Cond: (id = int8_tbl.q2)
(5 rows)
rollback; rollback;
-- another join removal bug: we must clean up correctly when removing a PHV -- another join removal bug: we must clean up correctly when removing a PHV
begin; begin;

View File

@ -1860,6 +1860,13 @@ SELECT * FROM
FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss2 FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss2
ON true; ON true;
-- join removal bug #17769: can't remove if there's a pushed-down reference
EXPLAIN (COSTS OFF)
SELECT q2 FROM
(SELECT *
FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss
WHERE COALESCE(dat1, 0) = q1;
rollback; rollback;
-- another join removal bug: we must clean up correctly when removing a PHV -- another join removal bug: we must clean up correctly when removing a PHV