Re: [PERFORM] Feature request: smarter use of conditional indexes

From: John Siracusa <siracusa(at)mindspring(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: Re: [PERFORM] Feature request: smarter use of conditional indexes
Date: 2004-03-07 03:54:45
Message-ID: 2BF3597E-6FEB-11D8-83AC-000A95BA4506@mindspring.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

On 3/3/04 6:53 PM, Tom Lane wrote:
> John Siracusa <siracusa(at)mindspring(dot)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 ;-)

(Originally sent to the wrong list and in the wrong format...sorry :)

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.

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 -c -r1.156 indxpath.c
*** src/backend/optimizer/path/indxpath.c 7 Jan 2004 22:02:48
-0000 1.156
--- src/backend/optimizer/path/indxpath.c 7 Mar 2004 03:49:17 -0000
***************
*** 1030,1036 ****
--- 1030,1060 ----
* the test operator will always be strict.
*/
if (!is_opclause(predicate))
+ {
+ /* 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;
+ }
+
return false;
+ }
leftop = get_leftop(predicate);
rightop = get_rightop(predicate);
if (rightop == NULL)

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Neil Conway 2004-03-07 04:01:52 Re: [PERFORM] Feature request: smarter use of conditional
Previous Message Neil Conway 2004-03-07 01:33:59 minor doc work