Re: Feature request: smarter use of conditional indexes

From: John Siracusa <siracusa(at)mindspring(dot)com>
To: Postgres Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Feature request: smarter use of conditional indexes
Date: 2004-03-07 02:29:27
Message-ID: 41C473B6-6FDF-11D8-9156-000A95BA4506@mindspring.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

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 ;-)

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
> }

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Larry Rosenman 2004-03-07 02:39:38 Re: Feature request: smarter use of conditional indexes
Previous Message Tom Lane 2004-03-06 06:13:58 Re: [HACKERS] Tablespaces

Browse pgsql-performance by date

  From Date Subject
Next Message Larry Rosenman 2004-03-07 02:39:38 Re: Feature request: smarter use of conditional indexes
Previous Message Mike Nolan 2004-03-07 02:26:30 Re: Fixed width rows faster?