@@ -837,49 +837,62 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
837837attcollation = attform -> attcollation ;
838838ReleaseSysCache (atttuple );
839839}
840- else if (attribute -> expr && IsA (attribute -> expr ,Var )&&
841- ((Var * )attribute -> expr )-> varattno != InvalidAttrNumber )
842- {
843- /* Tricky tricky, he wrote (column) ... treat as simple attr */
844- Var * var = (Var * )attribute -> expr ;
845-
846- indexInfo -> ii_KeyAttrNumbers [attn ]= var -> varattno ;
847- atttype = get_atttype (relId ,var -> varattno );
848- attcollation = var -> varcollid ;
849- }
850840else
851841{
852842/* Index expression */
853- Assert (attribute -> expr != NULL );
854- indexInfo -> ii_KeyAttrNumbers [attn ]= 0 ;/* marks expression */
855- indexInfo -> ii_Expressions = lappend (indexInfo -> ii_Expressions ,
856- attribute -> expr );
857- atttype = exprType (attribute -> expr );
858- attcollation = exprCollation (attribute -> expr );
843+ Node * expr = attribute -> expr ;
859844
860- /*
861- * We don't currently support generation of an actual query plan
862- * for an index expression, only simple scalar expressions; hence
863- * these restrictions.
864- */
865- if (contain_subplans (attribute -> expr ))
866- ereport (ERROR ,
867- (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
868- errmsg ("cannot use subquery in index expression" )));
869- if (contain_agg_clause (attribute -> expr ))
870- ereport (ERROR ,
871- (errcode (ERRCODE_GROUPING_ERROR ),
872- errmsg ("cannot use aggregate function in index expression" )));
845+ Assert (expr != NULL );
846+ atttype = exprType (expr );
847+ attcollation = exprCollation (expr );
873848
874849/*
875- * A expression using mutable functions is probably wrong, since
876- * if you aren't going to get the same result for the same data
877- * every time, it's not clear what the index entries mean at all.
850+ * Strip any top-level COLLATE clause. This ensures that we treat
851+ * "x COLLATE y" and "(x COLLATE y)" alike.
878852 */
879- if (CheckMutability ((Expr * )attribute -> expr ))
880- ereport (ERROR ,
881- (errcode (ERRCODE_INVALID_OBJECT_DEFINITION ),
882- errmsg ("functions in index expression must be marked IMMUTABLE" )));
853+ while (IsA (expr ,CollateExpr ))
854+ expr = (Node * ) ((CollateExpr * )expr )-> arg ;
855+
856+ if (IsA (expr ,Var )&&
857+ ((Var * )expr )-> varattno != InvalidAttrNumber )
858+ {
859+ /*
860+ * User wrote "(column)" or "(column COLLATE something)".
861+ * Treat it like simple attribute anyway.
862+ */
863+ indexInfo -> ii_KeyAttrNumbers [attn ]= ((Var * )expr )-> varattno ;
864+ }
865+ else
866+ {
867+ indexInfo -> ii_KeyAttrNumbers [attn ]= 0 ;/* marks expression */
868+ indexInfo -> ii_Expressions = lappend (indexInfo -> ii_Expressions ,
869+ expr );
870+
871+ /*
872+ * We don't currently support generation of an actual query
873+ * plan for an index expression, only simple scalar
874+ * expressions; hence these restrictions.
875+ */
876+ if (contain_subplans (expr ))
877+ ereport (ERROR ,
878+ (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
879+ errmsg ("cannot use subquery in index expression" )));
880+ if (contain_agg_clause (expr ))
881+ ereport (ERROR ,
882+ (errcode (ERRCODE_GROUPING_ERROR ),
883+ errmsg ("cannot use aggregate function in index expression" )));
884+
885+ /*
886+ * A expression using mutable functions is probably wrong,
887+ * since if you aren't going to get the same result for the
888+ * same data every time, it's not clear what the index entries
889+ * mean at all.
890+ */
891+ if (CheckMutability ((Expr * )expr ))
892+ ereport (ERROR ,
893+ (errcode (ERRCODE_INVALID_OBJECT_DEFINITION ),
894+ errmsg ("functions in index expression must be marked IMMUTABLE" )));
895+ }
883896}
884897
885898/*