@@ -1798,13 +1798,13 @@ check_index_only(RelOptInfo *rel, IndexOptInfo *index)
17981798 * Check that all needed attributes of the relation are available from the
17991799 * index.
18001800 *
1801- * XXX this is overly conservative for partial indexes, since we will
1802- * consider attributes involved in the index predicate as required even
1803- * though the predicate won't need to be checked at runtime. (The same is
1804- * true for attributes used only in index quals, if we are certain that
1805- * the index is not lossy.) However, it would be quite expensive to
1806- * determine that accurately at this point, so for now we take the easy
1807- * way out.
1801+ * For partial indexes we won't consider attributes involved in clauses
1802+ * implied by the index predicate, as those won't be needed at runtime.
1803+ *
1804+ * XXX The same is true for attributes used only in index quals, if we
1805+ * are certain that the index is not lossy. However, it would be quite
1806+ * expensive to determine that accurately at this point, so for now we
1807+ * take the easy way out.
18081808 */
18091809
18101810 /*
@@ -1819,6 +1819,27 @@ check_index_only(RelOptInfo *rel, IndexOptInfo *index)
18191819 {
18201820 RestrictInfo * rinfo = (RestrictInfo * ) lfirst (lc );
18211821
1822+ /*
1823+ * If the index is partial, we won't consider the clauses that are
1824+ * implied by the index predicate (those are not needed at runtime).
1825+ */
1826+ if (index -> indpred != NIL )
1827+ {
1828+ bool implied = false;
1829+ List * clauses = NIL ;
1830+
1831+ /* need a list for the 'implied_by' call */
1832+ clauses = lappend (clauses , (Node * ) rinfo -> clause );
1833+
1834+ implied = predicate_implied_by (clauses , index -> indpred );
1835+
1836+ /* we generally don't free memory, but well ... */
1837+ list_free (clauses );
1838+
1839+ /* if the clause is implied by index predicate, skip it */
1840+ if (implied )
1841+ continue ;
1842+ }
18221843 pull_varattnos ((Node * ) rinfo -> clause , rel -> relid , & attrs_used );
18231844 }
18241845
0 commit comments