|
| 1 | +<!-- doc/src/sgml/btree.sgml --> |
| 2 | + |
| 3 | +<chapter id="btree"> |
| 4 | +<title>B-Tree Indexes</title> |
| 5 | + |
| 6 | + <indexterm> |
| 7 | + <primary>index</primary> |
| 8 | + <secondary>B-Tree</secondary> |
| 9 | + </indexterm> |
| 10 | + |
| 11 | +<sect1 id="btree-intro"> |
| 12 | + <title>Introduction</title> |
| 13 | + |
| 14 | + <para> |
| 15 | + <productname>PostgreSQL</productname> includes an implementation of the |
| 16 | + standard <acronym>btree</acronym> (multi-way binary tree) index data |
| 17 | + structure. Any data type that can be sorted into a well-defined linear |
| 18 | + order can be indexed by a btree index. The only limitation is that an |
| 19 | + index entry cannot exceed approximately one-third of a page (after TOAST |
| 20 | + compression, if applicable). |
| 21 | + </para> |
| 22 | + |
| 23 | + <para> |
| 24 | + Because each btree operator class imposes a sort order on its data type, |
| 25 | + btree operator classes (or, really, operator families) have come to be |
| 26 | + used as <productname>PostgreSQL</productname>'s general representation |
| 27 | + and understanding of sorting semantics. Therefore, they've acquired |
| 28 | + some features that go beyond what would be needed just to support btree |
| 29 | + indexes, and parts of the system that are quite distant from the |
| 30 | + btree AM make use of them. |
| 31 | + </para> |
| 32 | + |
| 33 | +</sect1> |
| 34 | + |
| 35 | +<sect1 id="btree-behavior"> |
| 36 | + <title>Behavior of B-Tree Operator Classes</title> |
| 37 | + |
| 38 | + <para> |
| 39 | + As shown in <xref linkend="xindex-btree-strat-table"/>, a btree operator |
| 40 | + class must provide five comparison operators, |
| 41 | + <literal><</literal>, |
| 42 | + <literal><=</literal>, |
| 43 | + <literal>=</literal>, |
| 44 | + <literal>>=</literal> and |
| 45 | + <literal>></literal>. |
| 46 | + One might expect that <literal><></literal> should also be part of |
| 47 | + the operator class, but it is not, because it would almost never be |
| 48 | + useful to use a <literal><></literal> WHERE clause in an index |
| 49 | + search. (For some purposes, the planner treats <literal><></literal> |
| 50 | + as associated with a btree operator class; but it finds that operator via |
| 51 | + the <literal>=</literal> operator's negator link, rather than |
| 52 | + from <structname>pg_amop</structname>.) |
| 53 | + </para> |
| 54 | + |
| 55 | + <para> |
| 56 | + When several data types share near-identical sorting semantics, their |
| 57 | + operator classes can be grouped into an operator family. Doing so is |
| 58 | + advantageous because it allows the planner to make deductions about |
| 59 | + cross-type comparisons. Each operator class within the family should |
| 60 | + contain the single-type operators (and associated support functions) |
| 61 | + for its input data type, while cross-type comparison operators and |
| 62 | + support functions are <quote>loose</quote> in the family. It is |
| 63 | + recommendable that a complete set of cross-type operators be included |
| 64 | + in the family, thus ensuring that the planner can represent any |
| 65 | + comparison conditions that it deduces from transitivity. |
| 66 | + </para> |
| 67 | + |
| 68 | + <para> |
| 69 | + There are some basic assumptions that a btree operator family must |
| 70 | + satisfy: |
| 71 | + </para> |
| 72 | + |
| 73 | + <itemizedlist> |
| 74 | + <listitem> |
| 75 | + <para> |
| 76 | + An <literal>=</literal> operator must be an equivalence relation; that |
| 77 | + is, for all non-null values <replaceable>A</replaceable>, |
| 78 | + <replaceable>B</replaceable>, <replaceable>C</replaceable> of the |
| 79 | + data type: |
| 80 | + |
| 81 | + <itemizedlist> |
| 82 | + <listitem> |
| 83 | + <para> |
| 84 | + <replaceable>A</replaceable> <literal>=</literal> |
| 85 | + <replaceable>A</replaceable> is true |
| 86 | + (<firstterm>reflexive law</firstterm>) |
| 87 | + </para> |
| 88 | + </listitem> |
| 89 | + <listitem> |
| 90 | + <para> |
| 91 | + if <replaceable>A</replaceable> <literal>=</literal> |
| 92 | + <replaceable>B</replaceable>, |
| 93 | + then <replaceable>B</replaceable> <literal>=</literal> |
| 94 | + <replaceable>A</replaceable> |
| 95 | + (<firstterm>symmetric law</firstterm>) |
| 96 | + </para> |
| 97 | + </listitem> |
| 98 | + <listitem> |
| 99 | + <para> |
| 100 | + if <replaceable>A</replaceable> <literal>=</literal> |
| 101 | + <replaceable>B</replaceable> and <replaceable>B</replaceable> |
| 102 | + <literal>=</literal> <replaceable>C</replaceable>, |
| 103 | + then <replaceable>A</replaceable> <literal>=</literal> |
| 104 | + <replaceable>C</replaceable> |
| 105 | + (<firstterm>transitive law</firstterm>) |
| 106 | + </para> |
| 107 | + </listitem> |
| 108 | + </itemizedlist> |
| 109 | + </para> |
| 110 | + </listitem> |
| 111 | + |
| 112 | + <listitem> |
| 113 | + <para> |
| 114 | + A <literal><</literal> operator must be a strong ordering relation; |
| 115 | + that is, for all non-null values <replaceable>A</replaceable>, |
| 116 | + <replaceable>B</replaceable>, <replaceable>C</replaceable>: |
| 117 | + |
| 118 | + <itemizedlist> |
| 119 | + <listitem> |
| 120 | + <para> |
| 121 | + <replaceable>A</replaceable> <literal><</literal> |
| 122 | + <replaceable>A</replaceable> is false |
| 123 | + (<firstterm>irreflexive law</firstterm>) |
| 124 | + </para> |
| 125 | + </listitem> |
| 126 | + <listitem> |
| 127 | + <para> |
| 128 | + if <replaceable>A</replaceable> <literal><</literal> |
| 129 | + <replaceable>B</replaceable> |
| 130 | + and <replaceable>B</replaceable> <literal><</literal> |
| 131 | + <replaceable>C</replaceable>, |
| 132 | + then <replaceable>A</replaceable> <literal><</literal> |
| 133 | + <replaceable>C</replaceable> |
| 134 | + (<firstterm>transitive law</firstterm>) |
| 135 | + </para> |
| 136 | + </listitem> |
| 137 | + </itemizedlist> |
| 138 | + </para> |
| 139 | + </listitem> |
| 140 | + |
| 141 | + <listitem> |
| 142 | + <para> |
| 143 | + Furthermore, the ordering is total; that is, for all non-null |
| 144 | + values <replaceable>A</replaceable>, <replaceable>B</replaceable>: |
| 145 | + |
| 146 | + <itemizedlist> |
| 147 | + <listitem> |
| 148 | + <para> |
| 149 | + exactly one of <replaceable>A</replaceable> <literal><</literal> |
| 150 | + <replaceable>B</replaceable>, <replaceable>A</replaceable> |
| 151 | + <literal>=</literal> <replaceable>B</replaceable>, and |
| 152 | + <replaceable>B</replaceable> <literal><</literal> |
| 153 | + <replaceable>A</replaceable> is true |
| 154 | + (<firstterm>trichotomy law</firstterm>) |
| 155 | + </para> |
| 156 | + </listitem> |
| 157 | + </itemizedlist> |
| 158 | + |
| 159 | + (The trichotomy law justifies the definition of the comparison support |
| 160 | + function, of course.) |
| 161 | + </para> |
| 162 | + </listitem> |
| 163 | + </itemizedlist> |
| 164 | + |
| 165 | + <para> |
| 166 | + The other three operators are defined in terms of <literal>=</literal> |
| 167 | + and <literal><</literal> in the obvious way, and must act consistently |
| 168 | + with them. |
| 169 | + </para> |
| 170 | + |
| 171 | + <para> |
| 172 | + For an operator family supporting multiple data types, the above laws must |
| 173 | + hold when <replaceable>A</replaceable>, <replaceable>B</replaceable>, |
| 174 | + <replaceable>C</replaceable> are taken from any data types in the family. |
| 175 | + The transitive laws are the trickiest to ensure, as in cross-type |
| 176 | + situations they represent statements that the behaviors of two or three |
| 177 | + different operators are consistent. |
| 178 | + As an example, it would not work to put <type>float8</type> |
| 179 | + and <type>numeric</type> into the same operator family, at least not with |
| 180 | + the current semantics that <type>numeric</type> values are converted |
| 181 | + to <type>float8</type> for comparison to a <type>float8</type>. Because |
| 182 | + of the limited accuracy of <type>float8</type>, this means there are |
| 183 | + distinct <type>numeric</type> values that will compare equal to the |
| 184 | + same <type>float8</type> value, and thus the transitive law would fail. |
| 185 | + </para> |
| 186 | + |
| 187 | + <para> |
| 188 | + Another requirement for a multiple-data-type family is that any implicit |
| 189 | + or binary-coercion casts that are defined between data types included in |
| 190 | + the operator family must not change the associated sort ordering. |
| 191 | + </para> |
| 192 | + |
| 193 | + <para> |
| 194 | + It should be fairly clear why a btree index requires these laws to hold |
| 195 | + within a single data type: without them there is no ordering to arrange |
| 196 | + the keys with. Also, index searches using a comparison key of a |
| 197 | + different data type require comparisons to behave sanely across two |
| 198 | + data types. The extensions to three or more data types within a family |
| 199 | + are not strictly required by the btree index mechanism itself, but the |
| 200 | + planner relies on them for optimization purposes. |
| 201 | + </para> |
| 202 | + |
| 203 | +</sect1> |
| 204 | + |
| 205 | +<sect1 id="btree-support-funcs"> |
| 206 | + <title>B-Tree Support Functions</title> |
| 207 | + |
| 208 | + <para> |
| 209 | + As shown in <xref linkend="xindex-btree-support-table"/>, btree defines |
| 210 | + one required and one optional support function. |
| 211 | + </para> |
| 212 | + |
| 213 | + <para> |
| 214 | + For each combination of data types that a btree operator family provides |
| 215 | + comparison operators for, it must provide a comparison support function, |
| 216 | + registered in <structname>pg_amproc</structname> with support function |
| 217 | + number 1 and |
| 218 | + <structfield>amproclefttype</structfield>/<structfield>amprocrighttype</structfield> |
| 219 | + equal to the left and right data types for the comparison (i.e., the |
| 220 | + same data types that the matching operators are registered with |
| 221 | + in <structname>pg_amop</structname>). |
| 222 | + The comparison function must take two non-null values |
| 223 | + <replaceable>A</replaceable> and <replaceable>B</replaceable> and |
| 224 | + return an <type>int32</type> value that |
| 225 | + is <literal><</literal> <literal>0</literal>, <literal>0</literal>, |
| 226 | + or <literal>></literal> <literal>0</literal> |
| 227 | + when <replaceable>A</replaceable> <literal><</literal> |
| 228 | + <replaceable>B</replaceable>, <replaceable>A</replaceable> |
| 229 | + <literal>=</literal> <replaceable>B</replaceable>, |
| 230 | + or <replaceable>A</replaceable> <literal>></literal> |
| 231 | + <replaceable>B</replaceable>, respectively. The function must not |
| 232 | + return <literal>INT_MIN</literal> for the <replaceable>A</replaceable> |
| 233 | + <literal><</literal> <replaceable>B</replaceable> case, |
| 234 | + since the value may be negated before being tested for sign. A null |
| 235 | + result is disallowed, too. |
| 236 | + See <filename>src/backend/access/nbtree/nbtcompare.c</filename> for |
| 237 | + examples. |
| 238 | + </para> |
| 239 | + |
| 240 | + <para> |
| 241 | + If the compared values are of a collatable data type, the appropriate |
| 242 | + collation OID will be passed to the comparison support function, using |
| 243 | + the standard <function>PG_GET_COLLATION()</function> mechanism. |
| 244 | + </para> |
| 245 | + |
| 246 | + <para> |
| 247 | + Optionally, a btree operator family may provide <firstterm>sort |
| 248 | + support</firstterm> function(s), registered under support function number |
| 249 | + 2. These functions allow implementing comparisons for sorting purposes |
| 250 | + in a more efficient way than naively calling the comparison support |
| 251 | + function. The APIs involved in this are defined in |
| 252 | + <filename>src/include/utils/sortsupport.h</filename>. |
| 253 | + </para> |
| 254 | + |
| 255 | +</sect1> |
| 256 | + |
| 257 | +<sect1 id="btree-implementation"> |
| 258 | + <title>Implementation</title> |
| 259 | + |
| 260 | + <para> |
| 261 | + An introduction to the btree index implementation can be found in |
| 262 | + <filename>src/backend/access/nbtree/README</filename>. |
| 263 | + </para> |
| 264 | + |
| 265 | +</sect1> |
| 266 | + |
| 267 | +</chapter> |
0 commit comments