#include "postgres.h"
#include "access/skey.h"
+#include "catalog/pg_collation.h"
/*
AttrNumber attributeNumber,
StrategyNumber strategy,
Oid subtype,
+ Oid collation,
RegProcedure procedure,
Datum argument)
{
entry->sk_subtype = subtype;
entry->sk_argument = argument;
if (RegProcedureIsValid(procedure))
+ {
fmgr_info(procedure, &entry->sk_func);
+ entry->sk_func.fn_collation = collation;
+ }
else
{
Assert(flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
/*
* ScanKeyInit
* Shorthand version of ScanKeyEntryInitialize: flags and subtype
- * are assumed to be zero (the usual value).
+ * are assumed to be zero (the usual value), and collation is defaulted.
*
* This is the recommended version for hardwired lookups in system catalogs.
* It cannot handle NULL arguments, unary operators, or nondefault operators,
* but we need none of those features for most hardwired lookups.
*
+ * We set collation to DEFAULT_COLLATION_OID always. This is appropriate
+ * for textual columns in system catalogs, and it will be ignored for
+ * non-textual columns, so it's not worth trying to be more finicky.
+ *
* Note: CurrentMemoryContext at call should be as long-lived as the ScanKey
* itself, because that's what will be used for any subsidiary info attached
* to the ScanKey's FmgrInfo record.
entry->sk_subtype = InvalidOid;
entry->sk_argument = argument;
fmgr_info(procedure, &entry->sk_func);
+ entry->sk_func.fn_collation = DEFAULT_COLLATION_OID;
}
/*
AttrNumber attributeNumber,
StrategyNumber strategy,
Oid subtype,
+ Oid collation,
FmgrInfo *finfo,
Datum argument)
{
entry->sk_subtype = subtype;
entry->sk_argument = argument;
fmgr_info_copy(&entry->sk_func, finfo, CurrentMemoryContext);
-}
-
-/*
- * ScanKeyEntryInitializeCollation
- *
- * Initialize the collation of a scan key. This is just a notational
- * convenience and small abstraction.
- */
-void
-ScanKeyEntryInitializeCollation(ScanKey entry,
- Oid collation)
-{
entry->sk_func.fn_collation = collation;
}
* TupleDescInitEntry
* This function initializes a single attribute structure in
* a previously allocated tuple descriptor.
+ *
+ * Note that attcollation is set to the default for the specified datatype.
+ * If a nondefault collation is needed, insert it afterwards using
+ * TupleDescInitEntryCollation.
*/
void
TupleDescInitEntry(TupleDesc desc,
/*
* TupleDescInitEntryCollation
*
- * Fill in the collation for an attribute in a previously initialized
- * tuple descriptor.
+ * Assign a nondefault collation to a previously initialized tuple descriptor
+ * entry.
*/
void
TupleDescInitEntryCollation(TupleDesc desc,
TupleDescInitEntry(desc, attnum, attname,
atttypid, atttypmod, attdim);
- TupleDescInitEntryCollation(desc, attnum, attcollation);
/* Override TupleDescInitEntry's settings as requested */
+ TupleDescInitEntryCollation(desc, attnum, attcollation);
if (entry->storage)
desc->attrs[attnum - 1]->attstorage = entry->storage;
origTupdesc->attrs[i]->atttypid,
origTupdesc->attrs[i]->atttypmod,
origTupdesc->attrs[i]->attndims);
+ TupleDescInitEntryCollation(state->tupdesc[i], (AttrNumber) 2,
+ origTupdesc->attrs[i]->attcollation);
}
fmgr_info_copy(&(state->compareFn[i]),
* all comparisons. The original operator is passed to the Consistent
* function in the form of its strategy number, which is available
* from the sk_strategy field, and its subtype from the sk_subtype
- * field.
+ * field. Also, preserve sk_func.fn_collation which is the input
+ * collation for the operator.
*
* Next, if any of keys is a NULL and that key is not marked with
* SK_SEARCHNULL/SK_SEARCHNOTNULL then nothing can be found (ie, we
for (i = 0; i < scan->numberOfKeys; i++)
{
ScanKey skey = scan->keyData + i;
+ Oid collation = skey->sk_func.fn_collation;
skey->sk_func = so->giststate->consistentFn[skey->sk_attno - 1];
+ skey->sk_func.fn_collation = collation;
if (skey->sk_flags & SK_ISNULL)
{
* all comparisons. The original operator is passed to the Distance
* function in the form of its strategy number, which is available
* from the sk_strategy field, and its subtype from the sk_subtype
- * field.
+ * field. Also, preserve sk_func.fn_collation which is the input
+ * collation for the operator.
*/
for (i = 0; i < scan->numberOfOrderBys; i++)
{
ScanKey skey = scan->orderByData + i;
+ Oid collation = skey->sk_func.fn_collation;
skey->sk_func = so->giststate->distanceFn[skey->sk_attno - 1];
+ skey->sk_func.fn_collation = collation;
/* Check we actually have a distance function ... */
if (!OidIsValid(skey->sk_func.fn_oid))
cur->sk_attno,
InvalidStrategy,
cur->sk_subtype,
+ cur->sk_func.fn_collation,
procinfo,
cur->sk_argument);
- ScanKeyEntryInitializeCollation(scankeys + i,
- cur->sk_func.fn_collation);
}
else
{
cur->sk_attno,
InvalidStrategy,
cur->sk_subtype,
+ cur->sk_func.fn_collation,
cmp_proc,
cur->sk_argument);
- ScanKeyEntryInitializeCollation(scankeys + i,
- cur->sk_func.fn_collation);
}
}
}
/*
* We can use the cached (default) support procs since no cross-type
- * comparison can be needed.
+ * comparison can be needed. The cached support proc entries have
+ * the right collation for the index, too.
*/
procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
arg = index_getattr(itup, i + 1, itupdesc, &null);
(AttrNumber) (i + 1),
InvalidStrategy,
InvalidOid,
+ procinfo->fn_collation,
procinfo,
arg);
}
/*
* We can use the cached (default) support procs since no cross-type
- * comparison can be needed.
+ * comparison can be needed. The cached support proc entries have
+ * the right collation for the index, too.
*/
procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
flags = SK_ISNULL | (indoption[i] << SK_BT_INDOPTION_SHIFT);
(AttrNumber) (i + 1),
InvalidStrategy,
InvalidOid,
+ procinfo->fn_collation,
procinfo,
(Datum) 0);
}
#include "catalog/catalog.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
-#include "catalog/pg_collation.h"
#include "catalog/pg_seclabel.h"
#include "commands/seclabel.h"
#include "miscadmin.h"
Anum_pg_seclabel_provider,
BTEqualStrategyNumber, F_TEXTEQ,
CStringGetTextDatum(provider));
- ScanKeyEntryInitializeCollation(&keys[3], DEFAULT_COLLATION_OID);
pg_seclabel = heap_open(SecLabelRelationId, AccessShareLock);
Anum_pg_seclabel_provider,
BTEqualStrategyNumber, F_TEXTEQ,
CStringGetTextDatum(provider));
- ScanKeyEntryInitializeCollation(&keys[3], DEFAULT_COLLATION_OID);
pg_seclabel = heap_open(SecLabelRelationId, RowExclusiveLock);
RelationGetRelationName(seqrel))));
tupdesc = CreateTemplateTupleDesc(5, false);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "start_value", INT8OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "minimum_value", INT8OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "maximum_value", INT8OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "increment", INT8OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "cycle_option", BOOLOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 1, "start_value",
+ INT8OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 2, "minimum_value",
+ INT8OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 3, "maximum_value",
+ INT8OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 4, "increment",
+ INT8OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 5, "cycle_option",
+ BOOLOID, -1, 0);
BlessTupleDesc(tupdesc);
varattno, /* attribute number to scan */
op_strategy, /* op's strategy */
op_righttype, /* strategy subtype */
+ ((OpExpr *) clause)->inputcollid, /* collation */
opfuncid, /* reg proc to use */
scanvalue); /* constant */
- ScanKeyEntryInitializeCollation(this_scan_key,
- ((OpExpr *) clause)->inputcollid);
}
else if (IsA(clause, RowCompareExpr))
{
varattno, /* attribute number */
op_strategy, /* op's strategy */
op_righttype, /* strategy subtype */
+ inputcollation, /* collation */
opfuncid, /* reg proc to use */
scanvalue); /* constant */
- ScanKeyEntryInitializeCollation(this_sub_key,
- inputcollation);
n_sub_key++;
}
varattno, /* attribute number to scan */
op_strategy, /* op's strategy */
op_righttype, /* strategy subtype */
+ saop->inputcollid, /* collation */
opfuncid, /* reg proc to use */
(Datum) 0); /* constant */
- ScanKeyEntryInitializeCollation(this_scan_key,
- saop->inputcollid);
}
else if (IsA(clause, NullTest))
{
varattno, /* attribute number to scan */
InvalidStrategy, /* no strategy */
InvalidOid, /* no strategy subtype */
+ InvalidOid, /* no collation */
InvalidOid, /* no reg proc for this */
(Datum) 0); /* constant */
}
{
Oid sortFunction;
bool reverse;
+ int flags;
if (!get_compare_function_for_ordering_op(node->sortOperators[i],
&sortFunction, &reverse))
elog(ERROR, "operator %u is not a valid ordering operator",
node->sortOperators[i]);
+ /* We use btree's conventions for encoding directionality */
+ flags = 0;
+ if (reverse)
+ flags |= SK_BT_DESC;
+ if (node->nullsFirst[i])
+ flags |= SK_BT_NULLS_FIRST;
+
/*
* We needn't fill in sk_strategy or sk_subtype since these scankeys
* will never be passed to an index.
*/
- ScanKeyInit(&mergestate->ms_scankeys[i],
- node->sortColIdx[i],
- InvalidStrategy,
- sortFunction,
- (Datum) 0);
-
- ScanKeyEntryInitializeCollation(&mergestate->ms_scankeys[i],
- node->collations[i]);
-
- /* However, we use btree's conventions for encoding directionality */
- if (reverse)
- mergestate->ms_scankeys[i].sk_flags |= SK_BT_DESC;
- if (node->nullsFirst[i])
- mergestate->ms_scankeys[i].sk_flags |= SK_BT_NULLS_FIRST;
+ ScanKeyEntryInitialize(&mergestate->ms_scankeys[i],
+ flags,
+ node->sortColIdx[i],
+ InvalidStrategy,
+ InvalidOid,
+ node->collations[i],
+ sortFunction,
+ (Datum) 0);
}
/*
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
tupdesc = CreateTemplateTupleDesc(12, false);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "application_name", TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "current_query", TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 6, "waiting", BOOLOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 7, "act_start", TIMESTAMPTZOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 8, "query_start", TIMESTAMPTZOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 9, "backend_start", TIMESTAMPTZOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_addr", INETOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_hostname", TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 12, "client_port", INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid",
+ OIDOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid",
+ INT4OID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid",
+ OIDOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 4, "application_name",
+ TEXTOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 5, "current_query",
+ TEXTOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 6, "waiting",
+ BOOLOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 7, "act_start",
+ TIMESTAMPTZOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 8, "query_start",
+ TIMESTAMPTZOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 9, "backend_start",
+ TIMESTAMPTZOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_addr",
+ INETOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_hostname",
+ TEXTOID, -1, 0);
+ TupleDescInitEntry(tupdesc, (AttrNumber) 12, "client_port",
+ INT4OID, -1, 0);
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
1, /* index col to scan */
InvalidStrategy, /* no strategy */
InvalidOid, /* no strategy subtype */
+ InvalidOid, /* no collation */
InvalidOid, /* no reg proc for this */
(Datum) 0); /* constant */
/* Fill in sk_strategy as well --- always standard equality */
cache->cc_skey[i].sk_strategy = BTEqualStrategyNumber;
cache->cc_skey[i].sk_subtype = InvalidOid;
+ /* Currently, there are no catcaches on collation-aware data types */
+ cache->cc_skey[i].sk_func.fn_collation = InvalidOid;
CACHE4_elog(DEBUG2, "CatalogCacheInitializeCache %s %d %p",
cache->cc_relname,
{
Oid sortFunction;
bool reverse;
+ int flags;
AssertArg(attNums[i] != 0);
AssertArg(sortOperators[i] != 0);
elog(ERROR, "operator %u is not a valid ordering operator",
sortOperators[i]);
+ /* We use btree's conventions for encoding directionality */
+ flags = 0;
+ if (reverse)
+ flags |= SK_BT_DESC;
+ if (nullsFirstFlags[i])
+ flags |= SK_BT_NULLS_FIRST;
+
/*
* We needn't fill in sk_strategy or sk_subtype since these scankeys
* will never be passed to an index.
*/
- ScanKeyInit(&state->scanKeys[i],
- attNums[i],
- InvalidStrategy,
- sortFunction,
- (Datum) 0);
-
- if (collations)
- ScanKeyEntryInitializeCollation(&state->scanKeys[i],
- collations[i]);
-
- /* However, we use btree's conventions for encoding directionality */
- if (reverse)
- state->scanKeys[i].sk_flags |= SK_BT_DESC;
- if (nullsFirstFlags[i])
- state->scanKeys[i].sk_flags |= SK_BT_NULLS_FIRST;
+ ScanKeyEntryInitialize(&state->scanKeys[i],
+ flags,
+ attNums[i],
+ InvalidStrategy,
+ InvalidOid,
+ collations ? collations[i] : InvalidOid,
+ sortFunction,
+ (Datum) 0);
}
MemoryContextSwitchTo(oldcontext);
* the operator. When using a ScanKey in a heap scan, these fields are not
* used and may be set to InvalidStrategy/InvalidOid.
*
+ * If the operator is collation-sensitive, sk_func.fn_collation must be set
+ * correctly as well.
+ *
* A ScanKey can also represent a condition "column IS NULL" or "column
* IS NOT NULL"; these cases are signaled by the SK_SEARCHNULL and
* SK_SEARCHNOTNULL flag bits respectively. The argument is always NULL,
AttrNumber attributeNumber,
StrategyNumber strategy,
Oid subtype,
+ Oid collation,
RegProcedure procedure,
Datum argument);
extern void ScanKeyEntryInitializeWithInfo(ScanKey entry,
AttrNumber attributeNumber,
StrategyNumber strategy,
Oid subtype,
+ Oid collation,
FmgrInfo *finfo,
Datum argument);
-extern void ScanKeyEntryInitializeCollation(ScanKey entry,
- Oid collation);
#endif /* SKEY_H */
PLpgSQL_variable *var = vars[i];
Oid typoid = RECORDOID;
int32 typmod = -1;
+ Oid typcoll = InvalidOid;
switch (var->dtype)
{
case PLPGSQL_DTYPE_VAR:
typoid = ((PLpgSQL_var *) var)->datatype->typoid;
typmod = ((PLpgSQL_var *) var)->datatype->atttypmod;
+ typcoll = ((PLpgSQL_var *) var)->datatype->collation;
break;
case PLPGSQL_DTYPE_REC:
{
typoid = ((PLpgSQL_row *) var)->rowtupdesc->tdtypeid;
typmod = ((PLpgSQL_row *) var)->rowtupdesc->tdtypmod;
+ /* composite types have no collation */
}
break;
var->refname,
typoid, typmod,
0);
+ TupleDescInitEntryCollation(row->rowtupdesc, i + 1, typcoll);
}
return row;