1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Change build_index_pathkeys() so that the expressions it builds to represent

index key columns always have the type expected by the index's associated
operators, ie, we add RelabelType nodes when dealing with binary-compatible
index opclasses.  This is needed to get varchar indexes to play nicely with
the new EquivalenceClass machinery, as per recent gripe from Josh Berkus that
CVS HEAD was failing to match a varchar index column to a constant restriction
in the query.

It seems likely that this change will allow removal of a lot of ugly ad-hoc
RelabelType-stripping that the planner has traditionally done while matching
expressions to other expressions, but I'll worry about that some other day.
This commit is contained in:
Tom Lane
2007-05-31 16:57:34 +00:00
parent 7ce9b3683e
commit 10f719af33
3 changed files with 41 additions and 14 deletions

View File

@ -11,13 +11,14 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.84 2007/04/15 20:09:28 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.85 2007/05/31 16:57:34 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/skey.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/plannodes.h"
#include "optimizer/clauses.h"
@ -493,6 +494,27 @@ build_index_pathkeys(PlannerInfo *root,
indexprs_item = lnext(indexprs_item);
}
/*
* When dealing with binary-compatible indexes, we have to ensure that
* the exposed type of the expression tree matches the declared input
* type of the opclass, except when that is a polymorphic type
* (compare the behavior of parse_coerce.c). This ensures that we can
* correctly match the indexkey expression to expressions we find in
* the query, because arguments of operators that could match the
* index will be cast likewise.
*/
if (exprType((Node *) indexkey) != index->opcintype[i] &&
!IsPolymorphicType(index->opcintype[i]))
{
/* Strip any existing RelabelType, and add a new one */
while (indexkey && IsA(indexkey, RelabelType))
indexkey = (Expr *) ((RelabelType *) indexkey)->arg;
indexkey = (Expr *) makeRelabelType(indexkey,
index->opcintype[i],
-1,
COERCE_DONTCARE);
}
/* OK, make a canonical pathkey for this sort key */
cpathkey = make_pathkey_from_sortinfo(root,
indexkey,