<!-- doc/src/sgml/btree.sgml -->
<sect1 id="btree">
<title>B-Tree Indexes</title>
<indexterm>
<primary>index</primary>
<secondary>B-Tree</secondary>
</indexterm>
<sect2 id="btree-intro">
<title>Introduction</title>
<para>
<productname>PostgreSQL</productname> includes an implementation of the
standard <acronym>btree</acronym> (multi-way balanced tree) index data
structure. Any data type that can be sorted into a well-defined linear
order can be indexed by a btree index. The only limitation is that an
index entry cannot exceed approximately one-third of a page (after TOAST
compression, if applicable).
</para>
<para>
Because each btree operator class imposes a sort order on its data type,
btree operator classes (or, really, operator families) have come to be
used as <productname>PostgreSQL</productname>'s general representation
and understanding of sorting semantics. Therefore, they've acquired
some features that go beyond what would be needed just to support btree
indexes, and parts of the system that are quite distant from the
btree <acronym>AM</acronym> make use of them.
</para>
</sect2>
<sect2 id="btree-behavior">
<title>Behavior of B-Tree Operator Classes</title>
<para>
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>.)
</para>
<para>
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.
</para>
<para>
There are some basic assumptions that a btree operator family must
satisfy:
</para>
<itemizedlist>
<listitem>
<para>
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:
<itemizedlist>
<listitem>
<para>
<replaceable>A</replaceable> <literal>=</literal>
<replaceable>A</replaceable> is true
(<firstterm>reflexive law</firstterm>)
</para>
</listitem>
<listitem>
<para>
if <replaceable>A</replaceable> <literal>=</literal>
<replaceable>B</replaceable>,
then <replaceable>B</replaceable> <literal>=</literal>
<replaceable>A</replaceable>
(<firstterm>symmetric law</firstterm>)
</para>
</listitem>
<listitem>
<para>
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>)
</para>
</listitem>
</itemizedlist>