postgresql/src/backend/access/common/scankey.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.5 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* scankey.c
* scan key support code
*
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
2010-09-20 22:08:53 +02:00
* src/backend/access/common/scankey.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/skey.h"
#include "catalog/pg_collation.h"
/*
* ScanKeyEntryInitialize
* Initializes a scan key entry given all the field values.
* The target procedure is specified by OID (but can be invalid
* if SK_SEARCHNULL or SK_SEARCHNOTNULL is set).
*
* 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.
*/
void
ScanKeyEntryInitialize(ScanKey entry,
int flags,
AttrNumber attributeNumber,
StrategyNumber strategy,
Oid subtype,
Oid collation,
RegProcedure procedure,
Datum argument)
{
entry->sk_flags = flags;
entry->sk_attno = attributeNumber;
entry->sk_strategy = strategy;
entry->sk_subtype = subtype;
entry->sk_collation = collation;
entry->sk_argument = argument;
if (RegProcedureIsValid(procedure))
{
fmgr_info(procedure, &entry->sk_func);
}
else
{
Assert(flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
MemSet(&entry->sk_func, 0, sizeof(entry->sk_func));
}
}
/*
* ScanKeyInit
* Shorthand version of ScanKeyEntryInitialize: flags and subtype
* 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.
*
Make collation-aware system catalog columns use "C" collation. Up to now we allowed text columns in system catalogs to use collation "default", but that isn't really safe because it might mean something different in template0 than it means in a database cloned from template0. In particular, this could mean that cloned pg_statistic entries for such columns weren't entirely valid, possibly leading to bogus planner estimates, though (probably) not any outright failures. In the wake of commit 5e0928005, a better solution is available: if we label such columns with "C" collation, then their pg_statistic entries will also use that collation and hence will be valid independently of the database collation. This also provides a cleaner solution for indexes on such columns than the hack added by commit 0b28ea79c: the indexes will naturally inherit "C" collation and don't have to be forced to use text_pattern_ops. Also, with the planned improvement of type "name" to be collation-aware, this policy will apply cleanly to both text and name columns. Because of the pg_statistic angle, we should also apply this policy to the tables in information_schema. This patch does that by adjusting information_schema's textual domain types to specify "C" collation. That has the user-visible effect that order-sensitive comparisons to textual information_schema view columns will now use "C" collation by default. The SQL standard says that the collation of those view columns is implementation-defined, so I think this is legal per spec. At some point this might allow for translation of such comparisons into indexable conditions on the underlying "name" columns, although additional work will be needed before that can happen. Discussion: https://postgr.es/m/19346.1544895309@sss.pgh.pa.us
2018-12-18 18:48:15 +01:00
* We set collation to C_COLLATION_OID always. This is the correct value
* for all collation-aware columns in system catalogs, and it will be ignored
* for other column types, 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.
*/
void
ScanKeyInit(ScanKey entry,
AttrNumber attributeNumber,
StrategyNumber strategy,
RegProcedure procedure,
Datum argument)
{
entry->sk_flags = 0;
entry->sk_attno = attributeNumber;
entry->sk_strategy = strategy;
entry->sk_subtype = InvalidOid;
Make collation-aware system catalog columns use "C" collation. Up to now we allowed text columns in system catalogs to use collation "default", but that isn't really safe because it might mean something different in template0 than it means in a database cloned from template0. In particular, this could mean that cloned pg_statistic entries for such columns weren't entirely valid, possibly leading to bogus planner estimates, though (probably) not any outright failures. In the wake of commit 5e0928005, a better solution is available: if we label such columns with "C" collation, then their pg_statistic entries will also use that collation and hence will be valid independently of the database collation. This also provides a cleaner solution for indexes on such columns than the hack added by commit 0b28ea79c: the indexes will naturally inherit "C" collation and don't have to be forced to use text_pattern_ops. Also, with the planned improvement of type "name" to be collation-aware, this policy will apply cleanly to both text and name columns. Because of the pg_statistic angle, we should also apply this policy to the tables in information_schema. This patch does that by adjusting information_schema's textual domain types to specify "C" collation. That has the user-visible effect that order-sensitive comparisons to textual information_schema view columns will now use "C" collation by default. The SQL standard says that the collation of those view columns is implementation-defined, so I think this is legal per spec. At some point this might allow for translation of such comparisons into indexable conditions on the underlying "name" columns, although additional work will be needed before that can happen. Discussion: https://postgr.es/m/19346.1544895309@sss.pgh.pa.us
2018-12-18 18:48:15 +01:00
entry->sk_collation = C_COLLATION_OID;
entry->sk_argument = argument;
fmgr_info(procedure, &entry->sk_func);
}
/*
* ScanKeyEntryInitializeWithInfo
* Initializes a scan key entry using an already-completed FmgrInfo
* function lookup record.
*
* 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.
*/
void
ScanKeyEntryInitializeWithInfo(ScanKey entry,
int flags,
AttrNumber attributeNumber,
StrategyNumber strategy,
Oid subtype,
Oid collation,
FmgrInfo *finfo,
Datum argument)
{
entry->sk_flags = flags;
entry->sk_attno = attributeNumber;
entry->sk_strategy = strategy;
entry->sk_subtype = subtype;
entry->sk_collation = collation;
entry->sk_argument = argument;
fmgr_info_copy(&entry->sk_func, finfo, CurrentMemoryContext);
}