Postgresql 中文操作指南
11.10. Operator Classes and Operator Families #
索引定义可以为索引的每列指定一个 operator class。
An index definition can specify an operator class for each column of an index.
CREATE INDEX name ON table (column opclass [ ( opclass_options ) ] [sort options] [, ...]);
运算符类标识运算符将针对该列的索引使用。例如,对 int4 类型执行的 B 树索引将使用 int4_ops 运算符类;此运算符类包括对 int4 类型值进行比较的函数。实践中,列数据类型的默认运算符类通常足够。拥有运算符类的主要原因是,对于某些数据类型,可能存在多个有意义的索引行为。例如,我们可能想要按绝对值或按实部对复数数据类型进行排序。可以通过为数据类型定义两个运算符类,然后在创建索引时选择适当的类来实现此目的。运算符类确定基本排序顺序(然后可以通过添加 COLLATE,ASC/DESC 和/或 NULLS FIRST/NULLS LAST 排序选项来修改此顺序)。
The operator class identifies the operators to be used by the index for that column. For example, a B-tree index on the type int4 would use the int4_ops class; this operator class includes comparison functions for values of type int4. In practice the default operator class for the column’s data type is usually sufficient. The main reason for having operator classes is that for some data types, there could be more than one meaningful index behavior. For example, we might want to sort a complex-number data type either by absolute value or by real part. We could do this by defining two operator classes for the data type and then selecting the proper class when making an index. The operator class determines the basic sort ordering (which can then be modified by adding sort options COLLATE, ASC/DESC and/or NULLS FIRST/NULLS LAST).
除了默认运算符类之外,还有一些内置运算符类:
There are also some built-in operator classes besides the default ones:
以下查询显示所有定义的运算符类:
The following query shows all defined operator classes:
SELECT am.amname AS index_method,
opc.opcname AS opclass_name,
opc.opcintype::regtype AS indexed_type,
opc.opcdefault AS is_default
FROM pg_am am, pg_opclass opc
WHERE opc.opcmethod = am.oid
ORDER BY index_method, opclass_name;
运算符类实际上只是一个称为 operator family 的更大结构的子集。在几种数据类型具有相似行为的情况下,经常可以定义跨数据类型的运算符,并允许它们与索引配合使用。为此,必须将每种类型的运算符类分组到同一个运算符族中。跨类型的运算符是族中的成员,但不与族中的任何单个类关联。
An operator class is actually just a subset of a larger structure called an operator family. In cases where several data types have similar behaviors, it is frequently useful to define cross-data-type operators and allow these to work with indexes. To do this, the operator classes for each of the types must be grouped into the same operator family. The cross-type operators are members of the family, but are not associated with any single class within the family.
该之前的查询的扩展版本显示了每个运算符类所属的运算符族:
This expanded version of the previous query shows the operator family each operator class belongs to:
SELECT am.amname AS index_method,
opc.opcname AS opclass_name,
opf.opfname AS opfamily_name,
opc.opcintype::regtype AS indexed_type,
opc.opcdefault AS is_default
FROM pg_am am, pg_opclass opc, pg_opfamily opf
WHERE opc.opcmethod = am.oid AND
opc.opcfamily = opf.oid
ORDER BY index_method, opclass_name;
此查询显示所有定义的运算符族和每个族中包含的所有运算符:
This query shows all defined operator families and all the operators included in each family:
SELECT am.amname AS index_method,
opf.opfname AS opfamily_name,
amop.amopopr::regoperator AS opfamily_operator
FROM pg_am am, pg_opfamily opf, pg_amop amop
WHERE opf.opfmethod = am.oid AND
amop.amopfamily = opf.oid
ORDER BY index_method, opfamily_name, opfamily_operator;