Skip to content

Commit f0461cd

Browse files
committed
Improve check_partial_indexes() to consider join clauses in proof attempts.
Traditionally check_partial_indexes() has only looked at restriction clauses while trying to prove partial indexes usable in queries. However, join clauses can also be used in some cases; mainly, that a strict operator on "x" proves an "x IS NOT NULL" index predicate, even if the operator is in a join clause rather than a restriction clause. Adding this code fixes a regression in 9.2, because previously we would take join clauses into account when considering whether a partial index could be used in a nestloop inner indexscan path. 9.2 doesn't handle nestloop inner indexscans in the same way, and this consideration was overlooked in the rewrite. Moving the work to check_partial_indexes() is a better solution anyway, since the proof applies whether or not we actually use the index in that particular way, and we don't have to do it over again for each possible outer relation. Per report from Dave Cramer.
1 parent 3b4db79 commit f0461cd

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,21 +2549,91 @@ match_clause_to_ordering_op(IndexOptInfo *index,
25492549
void
25502550
check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
25512551
{
2552-
List *restrictinfo_list = rel->baserestrictinfo;
2553-
ListCell *ilist;
2552+
List *clauselist;
2553+
bool have_partial;
2554+
Relids otherrels;
2555+
ListCell *lc;
25542556

2555-
foreach(ilist, rel->indexlist)
2557+
/*
2558+
* Frequently, there will be no partial indexes, so first check to make
2559+
* sure there's something useful to do here.
2560+
*/
2561+
have_partial = false;
2562+
foreach(lc, rel->indexlist)
25562563
{
2557-
IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
2564+
IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
2565+
2566+
if (index->indpred == NIL)
2567+
continue; /* ignore non-partial indexes */
2568+
2569+
if (index->predOK)
2570+
continue; /* don't repeat work if already proven OK */
2571+
2572+
have_partial = true;
2573+
break;
2574+
}
2575+
if (!have_partial)
2576+
return;
2577+
2578+
/*
2579+
* Construct a list of clauses that we can assume true for the purpose
2580+
* of proving the index(es) usable. Restriction clauses for the rel are
2581+
* always usable, and so are any join clauses that are "movable to" this
2582+
* rel. Also, we can consider any EC-derivable join clauses (which must
2583+
* be "movable to" this rel, by definition).
2584+
*/
2585+
clauselist = list_copy(rel->baserestrictinfo);
2586+
2587+
/* Scan the rel's join clauses */
2588+
foreach(lc, rel->joininfo)
2589+
{
2590+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2591+
2592+
/* Check if clause can be moved to this rel */
2593+
if (!join_clause_is_movable_to(rinfo, rel->relid))
2594+
continue;
2595+
2596+
clauselist = lappend(clauselist, rinfo);
2597+
}
2598+
2599+
/*
2600+
* Add on any equivalence-derivable join clauses. Computing the correct
2601+
* relid sets for generate_join_implied_equalities is slightly tricky
2602+
* because the rel could be a child rel rather than a true baserel, and
2603+
* in that case we must remove its parent's relid from all_baserels.
2604+
*/
2605+
if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
2606+
{
2607+
/* Lookup parent->child translation data */
2608+
AppendRelInfo *appinfo = find_childrel_appendrelinfo(root, rel);
2609+
2610+
otherrels = bms_difference(root->all_baserels,
2611+
bms_make_singleton(appinfo->parent_relid));
2612+
}
2613+
else
2614+
otherrels = bms_difference(root->all_baserels, rel->relids);
2615+
2616+
if (!bms_is_empty(otherrels))
2617+
clauselist =
2618+
list_concat(clauselist,
2619+
generate_join_implied_equalities(root,
2620+
bms_union(rel->relids,
2621+
otherrels),
2622+
otherrels,
2623+
rel));
2624+
2625+
/* Now try to prove each index predicate true */
2626+
foreach(lc, rel->indexlist)
2627+
{
2628+
IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
25582629

25592630
if (index->indpred == NIL)
25602631
continue; /* ignore non-partial indexes */
25612632

25622633
if (index->predOK)
25632634
continue; /* don't repeat work if already proven OK */
25642635

2565-
index->predOK = predicate_implied_by(index->indpred,
2566-
restrictinfo_list);
2636+
index->predOK = predicate_implied_by(index->indpred, clauselist);
25672637
}
25682638
}
25692639

0 commit comments

Comments
 (0)