As shown in <xref linkend="xindex-btree-strat-table"/>, a btree operator
class must provide five comparison operators,
<literal><</literal>,
<literal><=</literal>,
<literal>=</literal>,
<literal>>=</literal> and
<literal>></literal>.
One might expect that <literal><></literal> should also be part of
the operator class, but it is not, because it would almost never be
useful to use a <literal><></literal> WHERE clause in an index
search. (For some purposes, the planner treats <literal><></literal>
as associated with a btree operator class; but it finds that operator via
the <literal>=</literal> operator's negator link, rather than
from <structname>pg_amop</structname>.)
表 38.3で示すように、btree演算子クラスは次の5つの比較演算子を提供しなければなりません。
<
、<=
、=
、>=
、そして>
です。
<>
も演算子クラスの一部であると期待する方もいるかもしれませんが、そうではありません。
インデックス検索のWHERE句で<>
を使うのは、ほとんど常に役に立たないからです。
(ある種の目的のためにプランナは<>
をbtree演算子クラスに関連しているものとして扱います。
しかし、プランナはpg_amop
から検索するのではなく=
の否定子リンクから検索します。)
When several data types share near-identical sorting semantics, their operator classes can be grouped into an operator family. Doing so is advantageous because it allows the planner to make deductions about cross-type comparisons. Each operator class within the family should contain the single-type operators (and associated support functions) for its input data type, while cross-type comparison operators and support functions are <quote>loose</quote> in the family. It is recommendable that a complete set of cross-type operators be included in the family, thus ensuring that the planner can represent any comparison conditions that it deduces from transitivity. 複数のデータ型がほとんど同じソートセマンティクスを共有している場合、それらの演算子クラスは演算子族にまとめることができます。 そうすることによりプランナが型をまたがる比較を推論できるので、これはメリットがあります。 ファミリー中の演算子クラスには、入力データ型のための単一型演算子(および関連するサポート関数)が含まれます。 一方型をまたがる比較演算子とサポート関数は演算子族中で「ゆるやか」です。 プランナが推移関係から推論するすべての比較条件を提示できるように、型をまたがる演算子の完全な集合を演算子族に入れておくことをお勧めします。
There are some basic assumptions that a btree operator family must satisfy: btree演算子族が満たさなければならない基本的な前提条件があります。
An <literal>=</literal> operator must be an equivalence relation; that
is, for all non-null values <replaceable>A</replaceable>,
<replaceable>B</replaceable>, <replaceable>C</replaceable> of the
data type:
=
演算子は等号関係でなければなりません。
つまり、そのデータ型のすべての非NULL値A
、B
、C
について、
<replaceable>A</replaceable> <literal>=</literal>
<replaceable>A</replaceable> is true
(<firstterm>reflexive law</firstterm>)
A
=
A
が真である(反射律)
if <replaceable>A</replaceable> <literal>=</literal>
<replaceable>B</replaceable>,
then <replaceable>B</replaceable> <literal>=</literal>
<replaceable>A</replaceable>
(<firstterm>symmetric law</firstterm>)
A
=
B
なら、B
=
A
である(対称律)
if <replaceable>A</replaceable> <literal>=</literal>
<replaceable>B</replaceable> and <replaceable>B</replaceable>
<literal>=</literal> <replaceable>C</replaceable>,
then <replaceable>A</replaceable> <literal>=</literal>
<replaceable>C</replaceable>
(<firstterm>transitive law</firstterm>)
A
=
B
かつB
=
C
なら、A
=
C
である(推移律)
A <literal><</literal> operator must be a strong ordering relation;
that is, for all non-null values <replaceable>A</replaceable>,
<replaceable>B</replaceable>, <replaceable>C</replaceable>:
<
は強順序関係でなければなりません。つまり、すべての非NULL値A
、B
、C
に対して、
<replaceable>A</replaceable> <literal><</literal>
<replaceable>A</replaceable> is false
(<firstterm>irreflexive law</firstterm>)
A
<
A
は偽である(非反射律)
if <replaceable>A</replaceable> <literal><</literal>
<replaceable>B</replaceable>
and <replaceable>B</replaceable> <literal><</literal>
<replaceable>C</replaceable>,
then <replaceable>A</replaceable> <literal><</literal>
<replaceable>C</replaceable>
(<firstterm>transitive law</firstterm>)
A
<
B
かつB
<
C
なら、A
<
C
である(推移律)
Furthermore, the ordering is total; that is, for all non-null
values <replaceable>A</replaceable>, <replaceable>B</replaceable>:
更に、順序は全である。すなわち、すべての非NULL値A
、B
に対して、
exactly one of <replaceable>A</replaceable> <literal><</literal>
<replaceable>B</replaceable>, <replaceable>A</replaceable>
<literal>=</literal> <replaceable>B</replaceable>, and
<replaceable>B</replaceable> <literal><</literal>
<replaceable>A</replaceable> is true
(<firstterm>trichotomy law</firstterm>)
厳密にA
<
B
、A
=
B
、B
<
A
のうちどれか一つが真(三分律)
(The trichotomy law justifies the definition of the comparison support function, of course.) (もちろん、三分律は比較サポート関数の定義を正当化します。)
The other three operators are defined in terms of <literal>=</literal>
and <literal><</literal> in the obvious way, and must act consistently
with them.
他の3つの演算子は=
と<
に沿って自明に定義され、それらと一貫していなければなりません。
For an operator family supporting multiple data types, the above laws must
hold when <replaceable>A</replaceable>, <replaceable>B</replaceable>,
<replaceable>C</replaceable> are taken from any data types in the family.
The transitive laws are the trickiest to ensure, as in cross-type
situations they represent statements that the behaviors of two or three
different operators are consistent.
As an example, it would not work to put <type>float8</type>
and <type>numeric</type> into the same operator family, at least not with
the current semantics that <type>numeric</type> values are converted
to <type>float8</type> for comparison to a <type>float8</type>. Because
of the limited accuracy of <type>float8</type>, this means there are
distinct <type>numeric</type> values that will compare equal to the
same <type>float8</type> value, and thus the transitive law would fail.
複数のデータ型をサポートする演算子族について、演算子族中のデータ型であるどんなA
、B
、C
も上記の法則を満たさなければなりません。
型をまたがる際に2つあるいは3つの異なる演算子が一貫していることを表明することになるので、推移律を満たすことはもっとも困難です。
例をあげると、少なくともfloat8
と比較するためにnumeric
値をfloat8
に変換する現在の意味論のもとでは、float8
とnumeric
を同じ演算子族に加えるのはうまくいかないでしょう。
float8
の精度に限りがあるからです。
これは同じfloat8
値に対して等号比較する複数の異なるnumeric
値が存在することを意味し、したがって推移律は満たされません。
Another requirement for a multiple-data-type family is that any implicit or binary-coercion casts that are defined between data types included in the operator family must not change the associated sort ordering. 複数データ型ファミリーに関する別な要件は、演算子族に含まれるデータ型間に定義される暗黙的あるいは二値型強制(binary-coercion)キャストは、関係するソート順を変更してはならないことです。
It should be fairly clear why a btree index requires these laws to hold within a single data type: without them there is no ordering to arrange the keys with. Also, index searches using a comparison key of a different data type require comparisons to behave sanely across two data types. The extensions to three or more data types within a family are not strictly required by the btree index mechanism itself, but the planner relies on them for optimization purposes. 単一のデータ型において、btreeインデックスがこれらの法則を守ることを要求するのはかなり明確です。 これらの法則なしにはキー並べる順序がなくなってしまうからです。 また、異なるデータ型の比較キーを使うインデックス検索では、2つのデータ型またがる比較が正常に動作することが必要です。 演算子族中で3つ以上のデータ型に対する拡張はbtreeインデックスの機構自体では要求されませんが、プランナは最適化の目的でそれらに依存します。