Re: Feature request: smarter use of conditional indexes - Mailing list pgsql-performance

From John Siracusa
Subject Re: Feature request: smarter use of conditional indexes
Date
Msg-id 41C473B6-6FDF-11D8-9156-000A95BA4506@mindspring.com
Whole thread Raw
In response to Feature request: smarter use of conditional indexes  (John Siracusa <siracusa@mindspring.com>)
Responses Re: Feature request: smarter use of conditional indexes
List pgsql-performance
On 3/3/04 6:53 PM, Tom Lane wrote:
>  John Siracusa <siracusa@mindspring.com> writes:
>>  Given an index like this:
>>      CREATE UNIQUE INDEX i1 ON t1 (c1) WHERE c1 IS NOT NULL;
>>  and a query like this:
>>      SELECT * FROM t1 WHERE c1 = 123;
>>  I'd like the planner to be smart enough to use an index scan using
>> i1.
>
>  Send a patch ;-)

How does this look?  It seems to do what I want without horribly
breaking anything as far as I can tell.  I ran "make check" and got the
same result as I did before my changes (5 failures in OS X 10.3.2).
But then, I also got the same result when I wasn't even checking to
make sure that both clauses were looking at the same variable :)  I'm
not sure how to add a test for this particular change either.

% cvs diff src/backend/optimizer/path/indxpath.c
Index: src/backend/optimizer/path/indxpath.c
===================================================================
RCS file:
/projects/cvsroot/pgsql-server/src/backend/optimizer/path/indxpath.c,v
retrieving revision 1.156
diff -r1.156 indxpath.c
1032a1033,1055
 >    {
 >        /* One last chance: "var = const" or "const = var" implies "var is
not null" */
 >        if (IsA(predicate, NullTest) &&
 >            ((NullTest *) predicate)->nulltesttype == IS_NOT_NULL &&
 >            is_opclause(clause) && op_strict(((OpExpr *) clause)->opno) &&
 >            length(((OpExpr *) clause)->args) == 2)
 >        {
 >            leftop = get_leftop((Expr *) clause);
 >            rightop = get_rightop((Expr *) clause);
 >
 >            /* One of the two arguments must be a constant */
 >            if (IsA(rightop, Const))
 >                clause_var = leftop;
 >            else if (IsA(leftop, Const))
 >                clause_var = rightop;
 >            else
 >                return false;
 >
 >            /* Finally, make sure "var" is the same var in both clauses */
 >            if (equal(((NullTest *) predicate)->arg, clause_var))
 >                return true;
 >        }
 >
1033a1057
 >    }


pgsql-performance by date:

Previous
From: Mike Nolan
Date:
Subject: Re: Fixed width rows faster?
Next
From: Larry Rosenman
Date:
Subject: Re: Feature request: smarter use of conditional indexes