#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
+#include "catalog/pg_collation.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
static char *chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt);
static IndexStmt *generateClonedIndexStmt(CreateStmtContext *cxt,
Relation parent_index, AttrNumber *attmap);
+static List *get_collation(Oid collation, Oid actual_datatype);
static List *get_opclass(Oid opclass, Oid actual_datatype);
static void transformIndexConstraints(CreateStmtContext *cxt);
static IndexStmt *transformIndexConstraint(Constraint *constraint,
Form_pg_class idxrelrec;
Form_pg_index idxrec;
Form_pg_am amrec;
+ oidvector *indcollation;
oidvector *indclass;
IndexStmt *index;
List *indexprs;
/* Fetch pg_am tuple for source index from relcache entry */
amrec = source_idx->rd_am;
+ /* Extract indcollation from the pg_index tuple */
+ datum = SysCacheGetAttr(INDEXRELID, ht_idx,
+ Anum_pg_index_indcollation, &isnull);
+ Assert(!isnull);
+ indcollation = (oidvector *) DatumGetPointer(datum);
+
/* Extract indclass from the pg_index tuple */
datum = SysCacheGetAttr(INDEXRELID, ht_idx,
Anum_pg_index_indclass, &isnull);
/* Copy the original index column name */
iparam->indexcolname = pstrdup(NameStr(attrs[keyno]->attname));
+ /* Add the collation name, if non-default */
+ iparam->collation = get_collation(indcollation->values[keyno], keycoltype);
+
/* Add the operator class name, if non-default */
iparam->opclass = get_opclass(indclass->values[keyno], keycoltype);
}
/*
- * get_opclass - fetch name of an index operator class
+ * get_collation - fetch qualified name of a collation
+ *
+ * If collation is InvalidOid or is the default for the given actual_datatype,
+ * then the return value is NIL.
+ */
+static List *
+get_collation(Oid collation, Oid actual_datatype)
+{
+ List *result;
+ HeapTuple ht_coll;
+ Form_pg_collation coll_rec;
+ char *nsp_name;
+ char *coll_name;
+
+ if (!OidIsValid(collation))
+ return NIL; /* easy case */
+ if (collation == get_typcollation(actual_datatype))
+ return NIL; /* just let it default */
+
+ ht_coll = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
+ if (!HeapTupleIsValid(ht_coll))
+ elog(ERROR, "cache lookup failed for collation %u", collation);
+ coll_rec = (Form_pg_collation) GETSTRUCT(ht_coll);
+
+ /* For simplicity, we always schema-qualify the name */
+ nsp_name = get_namespace_name(coll_rec->collnamespace);
+ coll_name = pstrdup(NameStr(coll_rec->collname));
+ result = list_make2(makeString(nsp_name), makeString(coll_name));
+
+ ReleaseSysCache(ht_coll);
+ return result;
+}
+
+/*
+ * get_opclass - fetch qualified name of an index operator class
*
* If the opclass is the default for the given actual_datatype, then
* the return value is NIL.
static List *
get_opclass(Oid opclass, Oid actual_datatype)
{
+ List *result = NIL;
HeapTuple ht_opc;
Form_pg_opclass opc_rec;
- List *result = NIL;
ht_opc = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
if (!HeapTupleIsValid(ht_opc))
iparam->name = pstrdup(key);
iparam->expr = NULL;
iparam->indexcolname = NULL;
+ iparam->collation = NIL;
iparam->opclass = NIL;
iparam->ordering = SORTBY_DEFAULT;
iparam->nulls_ordering = SORTBY_NULLS_DEFAULT;
* merge join node
*
* The expected ordering of each mergeable column is described by a btree
- * opfamily OID, a direction (BTLessStrategyNumber or BTGreaterStrategyNumber)
- * and a nulls-first flag. Note that the two sides of each mergeclause may
- * be of different datatypes, but they are ordered the same way according to
- * the common opfamily. The operator in each mergeclause must be an equality
- * operator of the indicated opfamily.
+ * opfamily OID, a collation OID, a direction (BTLessStrategyNumber or
+ * BTGreaterStrategyNumber) and a nulls-first flag. Note that the two sides
+ * of each mergeclause may be of different datatypes, but they are ordered the
+ * same way according to the common opfamily and collation. The operator in
+ * each mergeclause must be an equality operator of the indicated opfamily.
* ----------------
*/
typedef struct MergeJoin
* IndexOptInfo
* Per-index information for planning/optimization
*
- * opfamily[], indexkeys[], and opcintype[] each have ncolumns entries.
+ * indexkeys[], indexcollations[], opfamily[], and opcintype[]
+ * each have ncolumns entries.
+ *
* sortopfamily[], reverse_sort[], and nulls_first[] likewise have
* ncolumns entries, if the index is ordered; but if it is unordered,
* those pointers are NULL.
/* index descriptor information */
int ncolumns; /* number of columns in index */
- Oid *opfamily; /* OIDs of operator families for columns */
int *indexkeys; /* column numbers of index's keys, or 0 */
Oid *indexcollations; /* OIDs of collations of index columns */
+ Oid *opfamily; /* OIDs of operator families for columns */
Oid *opcintype; /* OIDs of opclass declared input data types */
Oid *sortopfamily; /* OIDs of btree opfamilies, if orderable */
bool *reverse_sort; /* is sort order descending? */