ScanKeyData
翻译
ScanKey描述了表或者索引以及常量之间比较操作的应用。 当它是ScanKeys的一部分时,它默认为AND。如果是一个二进制操作时,索引列为左操作数
(数据结构也可以支持一元索引操作符,在这种情况下,sk_argument会被省略,但是它现在没有被实现)
对于一个索引扫描来说,sk_strategy和sk_subtype必须被设置为正确。但是在一个heap扫描中使用ScanKye时,这两个参数不会被使用,他们可能会被设置为InvalidStrategy和InvalidOid。
如果操作运算是collation-sensitive时,sk_collation 必须被正确设置
在 “column op ANY(ARRAY[…])”这种情况下,ScanKey也可以描述为ScalarArrayOpExpr,它被SK_SEARCHARRAY标识符标识
sk_argument 不是运算符的右侧参数类型的值,而是这样值的数组,并且每个元素比较要用OR连接起来。
ScanKey也可以描述 “column IS NULL” 或者 “column IS NOT NULL”,
这些情况下会通过 SK_SEARCHNULL 和SK_SEARCHNOTNULL 各自标识
参数为null时 sk_strategy, sk_subtype, sk_collation, 以及sk_func 经常不被使用(除非被 index AM设置)。
只有在索引扫描情况下 SK_SEARCHARRAY, SK_SEARCHNULL 和 SK_SEARCHNOTNULL 才被支持
而且并不是所有的 index AMs 都支持他们,只有分别设置amsearcharray 或者 amsearchnulls时。.
ScanKey 也可以描述一个顺序操作符,例如 “ORDER BY indexedcol op constant”的调用,除了(通常)不产生bool运算符,它看起来跟比较运算符是相同的。我们会标识ScanKeys 为SK_ORDER_BY,但是这里不能使用 SK_SEARCHARRAY, SK_SEARCHNULL, SK_SEARCHNOTNULL
注意:
一些情况下,ScanKeys 被用来方便的调用程序支持的函数,在这种情况下sk_strategy/sk_subtype 是没有意义的,(但是sk_collation 可以有)
并且 sk_func 可能会引用一些函数,返回不是bool的其他值。
typedef struct ScanKeyData
{
int sk_flags; /* 标识符 */
AttrNumber sk_attno; /* 表或者索引列 */
StrategyNumber sk_strategy; /* 操作符策略值 */
Oid sk_subtype; /* strategy subtype */
Oid sk_collation; /* 如果需要,会使用校验值 */
FmgrInfo sk_func; /* 查找函数调用 */
Datum sk_argument; /* 比较的数据 */
} ScanKeyData;
typedef ScanKeyData *ScanKey;
原版
/*
* A ScanKey represents the application of a comparison operator between
* a table or index column and a constant. When it's part of an array of
* ScanKeys, the comparison conditions are implicitly ANDed. The index
* column is the left argument of the operator, if it's a binary operator.
* (The data structure can support unary indexable operators too; in that
* case sk_argument would go unused. This is not currently implemented.)
*
* For an index scan, sk_strategy and sk_subtype must be set correctly for
* 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_collation must be set
* correctly as well.
*
* A ScanKey can also represent a ScalarArrayOpExpr, that is a condition
* "column op ANY(ARRAY[...])". This is signaled by the SK_SEARCHARRAY
* flag bit. The sk_argument is not a value of the operator's right-hand
* argument type, but rather an array of such values, and the per-element
* comparisons are to be ORed together.
*
* 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,
* and the sk_strategy, sk_subtype, sk_collation, and sk_func fields are
* not used (unless set by the index AM).
*
* SK_SEARCHARRAY, SK_SEARCHNULL and SK_SEARCHNOTNULL are supported only
* for index scans, not heap scans; and not all index AMs support them,
* only those that set amsearcharray or amsearchnulls respectively.
*
* A ScanKey can also represent an ordering operator invocation, that is
* an ordering requirement "ORDER BY indexedcol op constant". This looks
* the same as a comparison operator, except that the operator doesn't
* (usually) yield boolean. We mark such ScanKeys with SK_ORDER_BY.
* SK_SEARCHARRAY, SK_SEARCHNULL, SK_SEARCHNOTNULL cannot be used here.
*
* Note: in some places, ScanKeys are used as a convenient representation
* for the invocation of an access method support procedure. In this case
* sk_strategy/sk_subtype are not meaningful (but sk_collation can be); and
* sk_func may refer to a function that returns something other than boolean.
*/
typedef struct ScanKeyData
{
int sk_flags; /* flags, see below */
AttrNumber sk_attno; /* table or index column number */
StrategyNumber sk_strategy; /* operator strategy number */
Oid sk_subtype; /* strategy subtype */
Oid sk_collation; /* collation to use, if needed */
FmgrInfo sk_func; /* lookup info for function to call */
Datum sk_argument; /* data to compare */
} ScanKeyData;
typedef ScanKeyData *ScanKey;