📄 xindex.sgml
字号:
<!--$PostgreSQL: pgsql/doc/src/sgml/xindex.sgml,v 1.41 2005/07/19 01:27:59 neilc Exp $--><sect1 id="xindex"> <title>Interfacing Extensions To Indexes</title> <indexterm zone="xindex"> <primary>index</primary> <secondary>for user-defined data type</secondary> </indexterm> <para> The procedures described thus far let you define new types, new functions, and new operators. However, we cannot yet define an index on a column of a new data type. To do this, we must define an <firstterm>operator class</> for the new data type. Later in this section, we will illustrate this concept in an example: a new operator class for the B-tree index method that stores and sorts complex numbers in ascending absolute value order. </para> <note> <para> Prior to <productname>PostgreSQL</productname> release 7.3, it was necessary to make manual additions to the system catalogs <classname>pg_amop</>, <classname>pg_amproc</>, and <classname>pg_opclass</> in order to create a user-defined operator class. That approach is now deprecated in favor of using <xref linkend="sql-createopclass" endterm="sql-createopclass-title">, which is a much simpler and less error-prone way of creating the necessary catalog entries. </para> </note> <sect2 id="xindex-im"> <title>Index Methods and Operator Classes</title> <para> The <classname>pg_am</classname> table contains one row for every index method (internally known as access method). Support for regular access to tables is built into <productname>PostgreSQL</productname>, but all index methods are described in <classname>pg_am</classname>. It is possible to add a new index method by defining the required interface routines and then creating a row in <classname>pg_am</classname> — but that is beyond the scope of this chapter (see <xref linkend="indexam">). </para> <para> The routines for an index method do not directly know anything about the data types that the index method will operate on. Instead, an <firstterm>operator class</><indexterm><primary>operator class</></indexterm> identifies the set of operations that the index method needs to use to work with a particular data type. Operator classes are so called because one thing they specify is the set of <literal>WHERE</>-clause operators that can be used with an index (i.e., can be converted into an index-scan qualification). An operator class may also specify some <firstterm>support procedures</> that are needed by the internal operations of the index method, but do not directly correspond to any <literal>WHERE</>-clause operator that can be used with the index. </para> <para> It is possible to define multiple operator classes for the same data type and index method. By doing this, multiple sets of indexing semantics can be defined for a single data type. For example, a B-tree index requires a sort ordering to be defined for each data type it works on. It might be useful for a complex-number data type to have one B-tree operator class that sorts the data by complex absolute value, another that sorts by real part, and so on. Typically, one of the operator classes will be deemed most commonly useful and will be marked as the default operator class for that data type and index method. </para> <para> The same operator class name can be used for several different index methods (for example, both B-tree and hash index methods have operator classes named <literal>int4_ops</literal>), but each such class is an independent entity and must be defined separately. </para> </sect2> <sect2 id="xindex-strategies"> <title>Index Method Strategies</title> <para> The operators associated with an operator class are identified by <quote>strategy numbers</>, which serve to identify the semantics of each operator within the context of its operator class. For example, B-trees impose a strict ordering on keys, lesser to greater, and so operators like <quote>less than</> and <quote>greater than or equal to</> are interesting with respect to a B-tree. Because <productname>PostgreSQL</productname> allows the user to define operators, <productname>PostgreSQL</productname> cannot look at the name of an operator (e.g., <literal><</> or <literal>>=</>) and tell what kind of comparison it is. Instead, the index method defines a set of <quote>strategies</>, which can be thought of as generalized operators. Each operator class specifies which actual operator corresponds to each strategy for a particular data type and interpretation of the index semantics. </para> <para> The B-tree index method defines five strategies, shown in <xref linkend="xindex-btree-strat-table">. </para> <table tocentry="1" id="xindex-btree-strat-table"> <title>B-tree Strategies</title> <tgroup cols="2"> <thead> <row> <entry>Operation</entry> <entry>Strategy Number</entry> </row> </thead> <tbody> <row> <entry>less than</entry> <entry>1</entry> </row> <row> <entry>less than or equal</entry> <entry>2</entry> </row> <row> <entry>equal</entry> <entry>3</entry> </row> <row> <entry>greater than or equal</entry> <entry>4</entry> </row> <row> <entry>greater than</entry> <entry>5</entry> </row> </tbody> </tgroup> </table> <para> Hash indexes express only bitwise equality, and so they use only one strategy, shown in <xref linkend="xindex-hash-strat-table">. </para> <table tocentry="1" id="xindex-hash-strat-table"> <title>Hash Strategies</title> <tgroup cols="2"> <thead> <row> <entry>Operation</entry> <entry>Strategy Number</entry> </row> </thead> <tbody> <row> <entry>equal</entry> <entry>1</entry> </row> </tbody> </tgroup> </table> <para> R-tree indexes express relationships in two-dimensional space. They use twelve strategies, shown in <xref linkend="xindex-rtree-strat-table">. Four of these are true two-dimensional tests (overlaps, same, contains, contained by); four of them consider only the X direction; and the other four provide the same tests in the Y direction. </para> <table tocentry="1" id="xindex-rtree-strat-table"> <title>R-tree Strategies</title> <tgroup cols="2"> <thead> <row> <entry>Operation</entry> <entry>Strategy Number</entry> </row> </thead> <tbody> <row> <entry>strictly left of</entry> <entry>1</entry> </row> <row> <entry>does not extend to right of</entry> <entry>2</entry> </row> <row> <entry>overlaps</entry> <entry>3</entry> </row> <row> <entry>does not extend to left of</entry> <entry>4</entry> </row> <row> <entry>strictly right of</entry> <entry>5</entry> </row> <row> <entry>same</entry> <entry>6</entry> </row> <row> <entry>contains</entry> <entry>7</entry> </row> <row> <entry>contained by</entry> <entry>8</entry> </row> <row> <entry>does not extend above</entry> <entry>9</entry> </row> <row> <entry>strictly below</entry> <entry>10</entry> </row> <row> <entry>strictly above</entry> <entry>11</entry> </row> <row> <entry>does not extend below</entry> <entry>12</entry> </row> </tbody> </tgroup> </table> <para> GiST indexes are even more flexible: they do not have a fixed set of strategies at all. Instead, the <quote>consistency</> support routine of each particular GiST operator class interprets the strategy numbers however it likes. </para> <para> Note that all strategy operators return Boolean values. In practice, all operators defined as index method strategies must return type <type>boolean</type>, since they must appear at the top level of a <literal>WHERE</> clause to be used with an index. </para> <para> By the way, the <structfield>amorderstrategy</structfield> column in <classname>pg_am</> tells whether the index method supports ordered scans. Zero means it doesn't; if it does, <structfield>amorderstrategy</structfield> is the strategy number that corresponds to the ordering operator. For example, B-tree has <structfield>amorderstrategy</structfield> = 1, which is its <quote>less than</quote> strategy number. </para> </sect2> <sect2 id="xindex-support"> <title>Index Method Support Routines</title> <para> Strategies aren't usually enough information for the system to figure out how to use an index. In practice, the index methods require additional support routines in order to work. For example, the B-tree index method must be able to compare two keys and determine whether one is greater than, equal to, or less than the other. Similarly, the R-tree index method must be able to compute intersections, unions, and sizes of rectangles. These operations do not correspond to operators used in qualifications in SQL commands; they are administrative routines used by the index methods, internally. </para> <para> Just as with strategies, the operator class identifies which specific functions should play each of these roles for a given data type and semantic interpretation. The index method defines the set of functions it needs, and the operator class identifies the correct functions to use by assigning them to the <quote>support function numbers</>. </para> <para> B-trees require a single support function, shown in <xref linkend="xindex-btree-support-table">. </para> <table tocentry="1" id="xindex-btree-support-table"> <title>B-tree Support Functions</title> <tgroup cols="2"> <thead> <row> <entry>Function</entry> <entry>Support Number</entry> </row> </thead> <tbody> <row> <entry> Compare two keys and return an integer less than zero, zero, or greater than zero, indicating whether the first key is less than, equal to, or greater than the second. </entry> <entry>1</entry> </row> </tbody> </tgroup> </table> <para> Hash indexes likewise require one support function, shown in <xref linkend="xindex-hash-support-table">. </para> <table tocentry="1" id="xindex-hash-support-table"> <title>Hash Support Functions</title> <tgroup cols="2"> <thead> <row> <entry>Function</entry> <entry>Support Number</entry> </row> </thead> <tbody> <row> <entry>Compute the hash value for a key</entry> <entry>1</entry> </row> </tbody> </tgroup> </table> <para> R-tree indexes require three support functions, shown in <xref linkend="xindex-rtree-support-table">. </para> <table tocentry="1" id="xindex-rtree-support-table"> <title>R-tree Support Functions</title> <tgroup cols="2"> <thead> <row> <entry>Function</entry> <entry>Support Number</entry> </row> </thead> <tbody> <row> <entry>union</entry> <entry>1</entry> </row> <row> <entry>intersection</entry> <entry>2</entry> </row> <row> <entry>size</entry> <entry>3</entry> </row> </tbody> </tgroup> </table> <para> GiST indexes require seven support functions, shown in <xref linkend="xindex-gist-support-table">. </para> <table tocentry="1" id="xindex-gist-support-table"> <title>GiST Support Functions</title> <tgroup cols="2"> <thead> <row> <entry>Function</entry> <entry>Support Number</entry> </row> </thead> <tbody> <row> <entry>consistent</entry> <entry>1</entry> </row> <row> <entry>union</entry> <entry>2</entry> </row> <row> <entry>compress</entry> <entry>3</entry> </row> <row> <entry>decompress</entry> <entry>4</entry>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -