Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fa63749

Browse files
committed
Fix oversight in indexscan plan creation. I recently added code to use
predicate_implied_by() to detect redundant filter conditions, but forgot that predicate_implied_by() assumes its first argument contains only immutable functions. Add a check to guarantee that. Also, test to see if filter conditions can be discarded because they are redundant with the predicate of a partial index.
1 parent e9f11c6 commit fa63749

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/backend/optimizer/plan/createplan.c

+35-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.198 2005/09/24 22:54:37 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.199 2005/10/06 16:01:54 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -819,7 +819,9 @@ create_indexscan_plan(PlannerInfo *root,
819819
* (particularly with OR'd index conditions) we may have scan_clauses
820820
* that are not equal to, but are logically implied by, the index quals;
821821
* so we also try a predicate_implied_by() check to see if we can discard
822-
* quals that way.
822+
* quals that way. (predicate_implied_by assumes its first input contains
823+
* only immutable functions, so we have to check that.) We can also
824+
* discard quals that are implied by a partial index's predicate.
823825
*
824826
* While at it, we strip off the RestrictInfos to produce a list of
825827
* plain expressions.
@@ -832,9 +834,15 @@ create_indexscan_plan(PlannerInfo *root,
832834
Assert(IsA(rinfo, RestrictInfo));
833835
if (list_member_ptr(nonlossy_indexquals, rinfo))
834836
continue;
835-
if (predicate_implied_by(list_make1(rinfo->clause),
836-
nonlossy_indexquals))
837-
continue;
837+
if (!contain_mutable_functions((Node *) rinfo->clause))
838+
{
839+
List *clausel = list_make1(rinfo->clause);
840+
841+
if (predicate_implied_by(clausel, nonlossy_indexquals))
842+
continue;
843+
if (predicate_implied_by(clausel, best_path->indexinfo->indpred))
844+
continue;
845+
}
838846
qpqual = lappend(qpqual, rinfo->clause);
839847
}
840848

@@ -916,6 +924,14 @@ create_bitmap_scan_plan(PlannerInfo *root,
916924
* OR'd index conditions) we may have scan_clauses that are not equal to,
917925
* but are logically implied by, the index quals; so we also try a
918926
* predicate_implied_by() check to see if we can discard quals that way.
927+
* (predicate_implied_by assumes its first input contains only immutable
928+
* functions, so we have to check that.) We can also discard quals that
929+
* are implied by a partial index's predicate.
930+
*
931+
* XXX For the moment, we only consider partial index predicates in the
932+
* simple single-index-scan case. Is it worth trying to be smart about
933+
* more complex cases? Perhaps create_bitmap_subplan should be made to
934+
* include predicate info in what it constructs.
919935
*/
920936
qpqual = NIL;
921937
foreach(l, scan_clauses)
@@ -924,9 +940,20 @@ create_bitmap_scan_plan(PlannerInfo *root,
924940

925941
if (list_member(indexquals, clause))
926942
continue;
927-
if (predicate_implied_by(list_make1(clause),
928-
indexquals))
929-
continue;
943+
if (!contain_mutable_functions(clause))
944+
{
945+
List *clausel = list_make1(clause);
946+
947+
if (predicate_implied_by(clausel, indexquals))
948+
continue;
949+
if (IsA(best_path->bitmapqual, IndexPath))
950+
{
951+
IndexPath *ipath = (IndexPath *) best_path->bitmapqual;
952+
953+
if (predicate_implied_by(clausel, ipath->indexinfo->indpred))
954+
continue;
955+
}
956+
}
930957
qpqual = lappend(qpqual, clause);
931958
}
932959

src/backend/optimizer/util/predtest.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/optimizer/util/predtest.c,v 1.2 2005/07/23 21:05:47 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/optimizer/util/predtest.c,v 1.3 2005/10/06 16:01:55 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -47,7 +47,7 @@ static bool btree_predicate_proof(Expr *predicate, Node *clause,
4747
* valid, but no worse consequences will ensue.
4848
*
4949
* We assume the predicate has already been checked to contain only
50-
* immutable functions and operators. (In current use this is true
50+
* immutable functions and operators. (In most current uses this is true
5151
* because the predicate is part of an index predicate that has passed
5252
* CheckPredicate().) We dare not make deductions based on non-immutable
5353
* functions, because they might change answers between the time we make

0 commit comments

Comments
 (0)