Commit 3785f7ee authored by Tom Lane's avatar Tom Lane

Doc: move info for btree opclass implementors into main documentation.

Up to now, useful info for writing a new btree opclass has been buried
in the backend's nbtree/README file.  Let's move it into the SGML docs,
in preparation for extending it with info about "in_range" functions
in the upcoming window RANGE patch.

To do this, I chose to create a new chapter for btree indexes in Part VII
(Internals), parallel to the chapters that exist for the newer index AMs.
This is a pretty short chapter as-is.  At some point somebody might care
to flesh it out with more detail about btree internals, but that is
beyond the scope of my ambition for today.

Discussion: https://postgr.es/m/23141.1517874668@sss.pgh.pa.us
parent f069c91a
This diff is collapsed.
......@@ -83,6 +83,7 @@
<!ENTITY bki SYSTEM "bki.sgml">
<!ENTITY catalogs SYSTEM "catalogs.sgml">
<!ENTITY geqo SYSTEM "geqo.sgml">
<!ENTITY btree SYSTEM "btree.sgml">
<!ENTITY gist SYSTEM "gist.sgml">
<!ENTITY spgist SYSTEM "spgist.sgml">
<!ENTITY gin SYSTEM "gin.sgml">
......
......@@ -252,6 +252,7 @@
&geqo;
&indexam;
&generic-wal;
&btree;
&gist;
&spgist;
&gin;
......
......@@ -35,7 +35,7 @@
<productname>PostgreSQL</productname>, but all index methods are
described in <classname>pg_am</classname>. It is possible to add a
new index access method by writing the necessary code and
then creating a row in <classname>pg_am</classname> &mdash; but that is
then creating an entry in <classname>pg_am</classname> &mdash; but that is
beyond the scope of this chapter (see <xref linkend="indexam"/>).
</para>
......@@ -404,6 +404,8 @@
B-trees require a single support function, and allow a second one to be
supplied at the operator class author's option, as shown in <xref
linkend="xindex-btree-support-table"/>.
The requirements for these support functions are explained further in
<xref linkend="btree-support-funcs"/>.
</para>
<table tocentry="1" id="xindex-btree-support-table">
......@@ -426,8 +428,8 @@
</row>
<row>
<entry>
Return the addresses of C-callable sort support function(s),
as documented in <filename>utils/sortsupport.h</filename> (optional)
Return the addresses of C-callable sort support function(s)
(optional)
</entry>
<entry>2</entry>
</row>
......@@ -1056,11 +1058,8 @@ ALTER OPERATOR FAMILY integer_ops USING btree ADD
<para>
In a B-tree operator family, all the operators in the family must sort
compatibly, meaning that the transitive laws hold across all the data types
supported by the family: <quote>if A = B and B = C, then A = C</quote>,
and <quote>if A &lt; B and B &lt; C, then A &lt; C</quote>. Moreover, implicit
or binary coercion casts between types represented in the operator family
must not change the associated sort ordering. For each
compatibly, as is specified in detail in <xref linkend="btree-behavior"/>.
For each
operator in the family there must be a support function having the same
two input data types as the operator. It is recommended that a family be
complete, i.e., for each combination of data types, all operators are
......
......@@ -623,56 +623,3 @@ routines must treat it accordingly. The actual key stored in the
item is irrelevant, and need not be stored at all. This arrangement
corresponds to the fact that an L&Y non-leaf page has one more pointer
than key.
Notes to Operator Class Implementors
------------------------------------
With this implementation, we require each supported combination of
datatypes to supply us with a comparison procedure via pg_amproc.
This procedure must take two nonnull values A and B and return an int32 < 0,
0, or > 0 if A < B, A = B, or A > B, respectively. The procedure must
not return INT_MIN for "A < B", since the value may be negated before
being tested for sign. A null result is disallowed, too. See nbtcompare.c
for examples.
There are some basic assumptions that a btree operator family must satisfy:
An = operator must be an equivalence relation; that is, for all non-null
values A,B,C of the datatype:
A = A is true reflexive law
if A = B, then B = A symmetric law
if A = B and B = C, then A = C transitive law
A < operator must be a strong ordering relation; that is, for all non-null
values A,B,C:
A < A is false irreflexive law
if A < B and B < C, then A < C transitive law
Furthermore, the ordering is total; that is, for all non-null values A,B:
exactly one of A < B, A = B, and B < A is true trichotomy law
(The trichotomy law justifies the definition of the comparison support
procedure, of course.)
The other three operators are defined in terms of these two in the obvious way,
and must act consistently with them.
For an operator family supporting multiple datatypes, the above laws must hold
when A,B,C are taken from any datatypes 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 float8 and numeric into
an opfamily, at least not with the current semantics that numerics are
converted to float8 for comparison to a float8. Because of the limited
accuracy of float8, this means there are distinct numeric values that will
compare equal to the same float8 value, and thus the transitive law fails.
It should be fairly clear why a btree index requires these laws to hold within
a single datatype: without them there is no ordering to arrange the keys with.
Also, index searches using a key of a different datatype require comparisons
to behave sanely across two datatypes. The extensions to three or more
datatypes within a family are not strictly required by the btree index
mechanism itself, but the planner relies on them for optimization purposes.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment