Postgresql 中文操作指南
71.3. Extensibility #
BRIN 接口具有很高的抽象级别,只需求访问方法实现者仅实现正在访问的数据类型的语义。BRIN 层本身负责并发性、日志记录和搜索索引结构。
The BRIN interface has a high level of abstraction, requiring the access method implementer only to implement the semantics of the data type being accessed. The BRIN layer itself takes care of concurrency, logging and searching the index structure.
建立一个 BRIN 访问方法所需要的全部是实现一些用户定义的方法,这些方法定义了存储在索引中的摘要值的行为以及它们与扫描键交互的方式。简而言之,BRIN 将可扩展性与通用性、代码重用性和一个干净的接口结合起来。
All it takes to get a BRIN access method working is to implement a few user-defined methods, which define the behavior of summary values stored in the index and the way they interact with scan keys. In short, BRIN combines extensibility with generality, code reuse, and a clean interface.
BRIN 的操作符类必须提供四种方法:
There are four methods that an operator class for BRIN must provide:
-
BrinOpcInfo *opcInfo(Oid type_oid)
-
Returns internal information about the indexed columns' summary data. The return value must point to a palloc’d BrinOpcInfo, which has this definition:
-
typedef struct BrinOpcInfo
{
/* Number of columns stored in an index column of this opclass */
uint16 oi_nstored;
/* Opaque pointer for the opclass' private use */
void *oi_opaque;
/* Type cache entries of the stored columns */
TypeCacheEntry *oi_typcache[FLEXIBLE_ARRAY_MEMBER];
} BrinOpcInfo;
-
BrinOpcInfo.oi_opaque can be used by the operator class routines to pass information between support functions during an index scan.
-
bool consistent(BrinDesc *bdesc, BrinValues *column, ScanKey *keys, int nkeys)
-
-
Returns whether all the ScanKey entries are consistent with the given indexed values for a range. The attribute number to use is passed as part of the scan key. Multiple scan keys for the same attribute may be passed at once; the number of entries is determined by the nkeys parameter.
-
bool consistent(BrinDesc *bdesc, BrinValues *column, ScanKey key)
-
-
Returns whether the ScanKey is consistent with the given indexed values for a range. The attribute number to use is passed as part of the scan key. This is an older backward-compatible variant of the consistent function.
-
bool addValue(BrinDesc *bdesc, BrinValues *column, Datum newval, bool isnull)
-
-
Given an index tuple and an indexed value, modifies the indicated attribute of the tuple so that it additionally represents the new value. If any modification was done to the tuple, true is returned.
-
bool unionTuples(BrinDesc *bdesc, BrinValues *a, BrinValues *b)
-
-
Consolidates two index tuples. Given two index tuples, modifies the indicated attribute of the first of them so that it represents both tuples. The second tuple is not modified.
BRIN 的操作符类可以(选择)指定以下方法:
An operator class for BRIN can optionally specify the following method:
-
void options(local_relopts *relopts)
-
Defines a set of user-visible parameters that control operator class behavior.
-
The options function is passed a pointer to a local_relopts struct, which needs to be filled with a set of operator class specific options. The options can be accessed from other support functions using the PG_HAS_OPCLASS_OPTIONS() and PG_GET_OPCLASS_OPTIONS() macros.
-
Since both key extraction of indexed values and representation of the key in BRIN are flexible, they may depend on user-specified parameters.
-
核心发行版支持四种类型的操作符类:minmax、minmax-multi、inclusion 和 bloom。使用它们的算术子类定义根据需要附带用于核心数据类型。用户可以使用等效定义为其他数据类型定义其他操作符类,而无需编写任何源代码;申报适当的目录条目就足够了。请注意,有关操作符策略语义的假设已嵌入在支持函数的源代码中。
The core distribution includes support for four types of operator classes: minmax, minmax-multi, inclusion and bloom. Operator class definitions using them are shipped for in-core data types as appropriate. Additional operator classes can be defined by the user for other data types using equivalent definitions, without having to write any source code; appropriate catalog entries being declared is enough. Note that assumptions about the semantics of operator strategies are embedded in the support functions' source code.
也可能实现完全不同语义的操作符类,条件是编写了上面描述的四个主要支持函数的实现。请注意,跨主要发行版向后兼容性不受保证:例如,在以后的版本中可能需要其他支持函数。
Operator classes that implement completely different semantics are also possible, provided implementations of the four main support functions described above are written. Note that backwards compatibility across major releases is not guaranteed: for example, additional support functions might be required in later releases.
要为实现完全有序集的数据类型编写操作员类,可以在 Table 71.2 中所示的方式将 minmax 支持函数与相应操作员一起使用。所有操作员类成员(函数和操作员)都是必需的。
To write an operator class for a data type that implements a totally ordered set, it is possible to use the minmax support functions alongside the corresponding operators, as shown in Table 71.2. All operator class members (functions and operators) are mandatory.
Table 71.2. Function and Support Numbers for Minmax Operator Classes
Operator class member |
Object |
Support Function 1 |
internal function brin_minmax_opcinfo() |
Support Function 2 |
internal function brin_minmax_add_value() |
Support Function 3 |
internal function brin_minmax_consistent() |
Support Function 4 |
internal function brin_minmax_union() |
Operator Strategy 1 |
operator less-than |
Operator Strategy 2 |
operator less-than-or-equal-to |
Operator Strategy 3 |
operator equal-to |
Operator Strategy 4 |
operator greater-than-or-equal-to |
Operator Strategy 5 |
operator greater-than |
要为包含在另一个类型中的值的复杂数据类型编写操作员类,可以在 Table 71.3 中所示的方式将包含支持函数与相应操作员一起使用。它只需要一个附加函数,可以使用任何语言编写。可以定义更多函数以获得其他功能。所有操作员都是可选的。某些操作员需要其他操作员,如表中的依赖项所示。
To write an operator class for a complex data type which has values included within another type, it’s possible to use the inclusion support functions alongside the corresponding operators, as shown in Table 71.3. It requires only a single additional function, which can be written in any language. More functions can be defined for additional functionality. All operators are optional. Some operators require other operators, as shown as dependencies on the table.
Table 71.3. Function and Support Numbers for Inclusion Operator Classes
Operator class member |
Object |
Dependency |
Support Function 1 |
internal function brin_inclusion_opcinfo() |
|
Support Function 2 |
internal function brin_inclusion_add_value() |
|
Support Function 3 |
internal function brin_inclusion_consistent() |
|
Support Function 4 |
internal function brin_inclusion_union() |
|
Support Function 11 |
function to merge two elements |
|
Support Function 12 |
optional function to check whether two elements are mergeable |
|
Support Function 13 |
optional function to check if an element is contained within another |
|
Support Function 14 |
optional function to check whether an element is empty |
|
Operator Strategy 1 |
operator left-of |
Operator Strategy 4 |
Operator Strategy 2 |
operator does-not-extend-to-the-right-of |
Operator Strategy 5 |
Operator Strategy 3 |
operator overlaps |
|
Operator Strategy 4 |
operator does-not-extend-to-the-left-of |
Operator Strategy 1 |
Operator Strategy 5 |
operator right-of |
Operator Strategy 2 |
Operator Strategy 6, 18 |
operator same-as-or-equal-to |
Operator Strategy 7 |
Operator Strategy 7, 16, 24, 25 |
operator contains-or-equal-to |
|
Operator Strategy 8, 26, 27 |
operator is-contained-by-or-equal-to |
Operator Strategy 3 |
Operator Strategy 9 |
operator does-not-extend-above |
Operator Strategy 11 |
Operator Strategy 10 |
operator is-below |
Operator Strategy 12 |
Operator Strategy 11 |
operator is-above |
Operator Strategy 9 |
Operator Strategy 12 |
operator does-not-extend-below |
Operator Strategy 10 |
Operator Strategy 20 |
operator less-than |
Operator Strategy 5 |
Operator Strategy 21 |
operator less-than-or-equal-to |
Operator Strategy 5 |
Operator Strategy 22 |
operator greater-than |
Operator Strategy 1 |
Operator Strategy 23 |
operator greater-than-or-equal-to |
Operator Strategy 1 |
支持函数编号 1 到 10 保留给 BRIN 内部函数,因此 SQL 级别的函数从编号 11 开始。支持函数编号 11 是构建索引所需的主要函数。它应接受两个与操作符类相同数据类型参数,并返回它们的并集。如果 inclusion 操作符类是用 @{6} 参数定义的,则它可以存储有不同数据类型的并集值。并集函数的返回值应与 @{7} 数据类型匹配。
Support function numbers 1 through 10 are reserved for the BRIN internal functions, so the SQL level functions start with number 11. Support function number 11 is the main function required to build the index. It should accept two arguments with the same data type as the operator class, and return the union of them. The inclusion operator class can store union values with different data types if it is defined with the STORAGE parameter. The return value of the union function should match the STORAGE data type.
支持函数编号 12 和 14 用于支持内置数据类型的非常规性。函数编号 12 用于支持不可合并的不同类的网络地址。函数编号 14 用于支持空范围。函数编号 13 是可选的,但推荐的,它允许在将新值传递给并集函数之前对其进行检查。由于 BRIN 框架可以在并集未更改时对某些操作进行短路,因此使用这个函数可以提高索引性能。
Support function numbers 12 and 14 are provided to support irregularities of built-in data types. Function number 12 is used to support network addresses from different families which are not mergeable. Function number 14 is used to support empty ranges. Function number 13 is an optional but recommended one, which allows the new value to be checked before it is passed to the union function. As the BRIN framework can shortcut some operations when the union is not changed, using this function can improve index performance.
要为仅实现等式操作符并支持哈希的数据类型编写操作员类,可以在 Table 71.4 中所示的方式将 bloom 支持过程与相应操作员一起使用。所有操作员类成员(过程和操作员)都是必需的。
To write an operator class for a data type that implements only an equality operator and supports hashing, it is possible to use the bloom support procedures alongside the corresponding operators, as shown in Table 71.4. All operator class members (procedures and operators) are mandatory.
Table 71.4. Procedure and Support Numbers for Bloom Operator Classes
Operator class member |
Object |
Support Procedure 1 |
internal function brin_bloom_opcinfo() |
Support Procedure 2 |
internal function brin_bloom_add_value() |
Support Procedure 3 |
internal function brin_bloom_consistent() |
Support Procedure 4 |
internal function brin_bloom_union() |
Support Procedure 5 |
internal function brin_bloom_options() |
Support Procedure 11 |
function to compute hash of an element |
Operator Strategy 1 |
operator equal-to |
过程支持编号 1-10 保留给 BRIN 内部函数,因此 SQL 级别的函数从编号 11 开始。支持函数编号 11 是构建索引所需的主要函数。它应接受与操作符类相同数据类型的一个参数,并返回该值的哈希值。
Support procedure numbers 1-10 are reserved for the BRIN internal functions, so the SQL level functions start with number 11. Support function number 11 is the main function required to build the index. It should accept one argument with the same data type as the operator class, and return a hash of the value.
minmax-multi 操作符类也适用于实现完全有序集的数据类型,并且可以被视为 minmax 操作符类的简单扩展。虽然 minmax 操作符类将每个块范围的值归纳为一个连续区间,但 minmax-multi 允许归纳为多个较小的区间,以改善处理异常值。可以在 Table 71.5 中所示的方式将 minmax-multi 支持过程与相应操作员一起使用。所有操作员类成员(过程和操作员)都是必需的。
The minmax-multi operator class is also intended for data types implementing a totally ordered set, and may be seen as a simple extension of the minmax operator class. While minmax operator class summarizes values from each block range into a single contiguous interval, minmax-multi allows summarization into multiple smaller intervals to improve handling of outlier values. It is possible to use the minmax-multi support procedures alongside the corresponding operators, as shown in Table 71.5. All operator class members (procedures and operators) are mandatory.
Table 71.5. Procedure and Support Numbers for minmax-multi Operator Classes
Operator class member |
Object |
Support Procedure 1 |
internal function brin_minmax_multi_opcinfo() |
Support Procedure 2 |
internal function brin_minmax_multi_add_value() |
Support Procedure 3 |
internal function brin_minmax_multi_consistent() |
Support Procedure 4 |
internal function brin_minmax_multi_union() |
Support Procedure 5 |
internal function brin_minmax_multi_options() |
Support Procedure 11 |
function to compute distance between two values (length of a range) |
Operator Strategy 1 |
operator less-than |
Operator Strategy 2 |
operator less-than-or-equal-to |
Operator Strategy 3 |
operator equal-to |
Operator Strategy 4 |
operator greater-than-or-equal-to |
Operator Strategy 5 |
operator greater-than |
minmax 和包含操作员类都支持跨数据类型操作符,但使用这些操作符时,依赖关系会变得更加复杂。minmax 操作符类要求定义一组完整的操作符,其两个参数具有相同的数据类型。它允许通过定义额外的操作符集来支持其他数据类型。包含操作员类操作符策略依赖于另一个操作员策略(如 Table 71.3 中所示),或依赖于它们自己的相同操作员策略。它们要求将依赖操作符定义为使用 STORAGE 数据类型作为左手边的参数,并将另一个受支持的数据类型作为受支持操作符的右手边的参数。参见 float4_minmax_ops 中的 minmax 示例和 box_inclusion_ops 中的包含示例。
Both minmax and inclusion operator classes support cross-data-type operators, though with these the dependencies become more complicated. The minmax operator class requires a full set of operators to be defined with both arguments having the same data type. It allows additional data types to be supported by defining extra sets of operators. Inclusion operator class operator strategies are dependent on another operator strategy as shown in Table 71.3, or the same operator strategy as themselves. They require the dependency operator to be defined with the STORAGE data type as the left-hand-side argument and the other supported data type to be the right-hand-side argument of the supported operator. See float4_minmax_ops as an example of minmax, and box_inclusion_ops as an example of inclusion.