]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_opclass.sgml
Remove use of '<' and '>' in SGML, use '&' escapes.
[postgresql] / doc / src / sgml / ref / create_opclass.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_opclass.sgml,v 1.18 2006/10/16 17:28:03 momjian Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-CREATEOPCLASS">
7  <refmeta>
8   <refentrytitle id="sql-createopclass-title">CREATE OPERATOR CLASS</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11
12  <refnamediv>
13   <refname>CREATE OPERATOR CLASS</refname>
14   <refpurpose>define a new operator class</refpurpose>
15  </refnamediv>
16
17  <indexterm zone="sql-createopclass">
18   <primary>CREATE OPERATOR CLASS</primary>
19  </indexterm>
20
21  <refsynopsisdiv>
22 <synopsis>
23 CREATE OPERATOR CLASS <replaceable class="parameter">name</replaceable> [ DEFAULT ] FOR TYPE <replaceable class="parameter">data_type</replaceable> USING <replaceable class="parameter">index_method</replaceable> AS
24   {  OPERATOR <replaceable class="parameter">strategy_number</replaceable> <replaceable class="parameter">operator_name</replaceable> [ ( <replaceable class="parameter">op_type</replaceable>, <replaceable class="parameter">op_type</replaceable> ) ] [ RECHECK ]
25    | FUNCTION <replaceable class="parameter">support_number</replaceable> <replaceable class="parameter">funcname</replaceable> ( <replaceable class="parameter">argument_type</replaceable> [, ...] )
26    | STORAGE <replaceable class="parameter">storage_type</replaceable>
27   } [, ... ]
28 </synopsis>
29  </refsynopsisdiv>
30
31  <refsect1>
32   <title>Description</title>
33
34   <para>
35    <command>CREATE OPERATOR CLASS</command> creates a new operator class.
36    An operator class defines how a particular data type can be used with
37    an index.  The operator class specifies that certain operators will fill
38    particular roles or <quote>strategies</> for this data type and this
39    index method.  The operator class also specifies the support procedures to
40    be used by 
41    the index method when the operator class is selected for an
42    index column.  All the operators and functions used by an operator
43    class must be defined before the operator class is created.
44   </para>
45
46   <para>
47    If a schema name is given then the operator class is created in the
48    specified schema.  Otherwise it is created in the current schema.
49    Two operator classes in the same schema can have the same name only if they
50    are for different index methods.
51   </para>
52
53   <para>
54    The user who defines an operator class becomes its owner.  Presently,
55    the creating user must be a superuser.  (This restriction is made because
56    an erroneous operator class definition could confuse or even crash the
57    server.)
58   </para>
59
60   <para>
61    <command>CREATE OPERATOR CLASS</command> does not presently check
62    whether the operator class definition includes all the operators and
63    functions required by the index method, nor whether the operators and
64    functions form a self-consistent set.  It is the user's
65    responsibility to define a valid operator class.
66   </para>
67
68   <para>
69    Refer to <xref linkend="xindex"> for further information.
70   </para>
71  </refsect1>
72   
73  <refsect1>
74   <title>Parameters</title>
75
76   <variablelist>
77    <varlistentry>
78     <term><replaceable class="parameter">name</replaceable></term>
79     <listitem>
80      <para>
81       The name of the operator class to be created.  The name may be
82       schema-qualified.
83      </para>
84     </listitem>
85    </varlistentry>
86
87    <varlistentry>
88     <term><literal>DEFAULT</></term>
89     <listitem>
90      <para>
91       If present, the operator class will become the default
92       operator class for its data type.  At most one operator class
93       can be the default for a specific data type and index method.
94      </para>
95     </listitem>
96    </varlistentry>
97
98    <varlistentry>
99     <term><replaceable class="parameter">data_type</replaceable></term>
100     <listitem>
101      <para>
102       The column data type that this operator class is for.
103      </para>
104     </listitem>
105    </varlistentry>
106
107    <varlistentry>
108     <term><replaceable class="parameter">index_method</replaceable></term>
109     <listitem>
110      <para>
111       The name of the index method this operator class is for.
112      </para>
113     </listitem>
114    </varlistentry>
115
116    <varlistentry>
117     <term><replaceable class="parameter">strategy_number</replaceable></term>
118     <listitem>
119      <para>
120       The index method's strategy number for an operator
121       associated with the operator class.
122      </para>
123     </listitem>
124    </varlistentry>
125
126    <varlistentry>
127     <term><replaceable class="parameter">operator_name</replaceable></term>
128     <listitem>
129      <para>
130       The name (optionally schema-qualified) of an operator associated
131       with the operator class.
132      </para>
133     </listitem>
134    </varlistentry>
135
136    <varlistentry>
137     <term><replaceable class="parameter">op_type</replaceable></term>
138     <listitem>
139      <para>
140       The operand data type(s) of an operator, or <literal>NONE</> to
141       signify a left-unary or right-unary operator.  The operand data
142       types may be omitted in the normal case where they are the same
143       as the operator class's data type.
144      </para>
145     </listitem>
146    </varlistentry>
147
148    <varlistentry>
149     <term><literal>RECHECK</></term>
150     <listitem>
151      <para>
152       If present, the index is <quote>lossy</> for this operator, and
153       so the rows retrieved using the index must be rechecked to
154       verify that they actually satisfy the qualification clause
155       involving this operator.
156      </para>
157     </listitem>
158    </varlistentry>
159
160    <varlistentry>
161     <term><replaceable class="parameter">support_number</replaceable></term>
162     <listitem>
163      <para>
164       The index method's support procedure number for a
165       function associated with the operator class.
166      </para>
167     </listitem>
168    </varlistentry>
169
170    <varlistentry>
171     <term><replaceable class="parameter">funcname</replaceable></term>
172     <listitem>
173      <para>
174       The name (optionally schema-qualified) of a function that is an
175       index method support procedure for the operator class.
176      </para>
177     </listitem>
178    </varlistentry>
179
180    <varlistentry>
181     <term><replaceable class="parameter">argument_types</replaceable></term>
182     <listitem>
183      <para>
184       The parameter data type(s) of the function.
185      </para>
186     </listitem>
187    </varlistentry>
188
189    <varlistentry>
190     <term><replaceable class="parameter">storage_type</replaceable></term>
191     <listitem>
192      <para>
193       The data type actually stored in the index.  Normally this is
194       the same as the column data type, but some index methods
195       (GIN and GiST for now) allow it to be different.  The
196       <literal>STORAGE</> clause must be omitted unless the index
197       method allows a different type to be used.
198      </para>
199     </listitem>
200    </varlistentry>
201   </variablelist>
202
203   <para>
204    The <literal>OPERATOR</>, <literal>FUNCTION</>, and <literal>STORAGE</>
205    clauses may appear in any order.
206   </para>
207  </refsect1>
208   
209  <refsect1>
210   <title>Notes</title>
211
212   <para>
213    Because the index machinery does not check access permissions on functions
214    before using them, including a function or operator in an operator class
215    is tantamount to granting public execute permission on it.  This is usually
216    not an issue for the sorts of functions that are useful in an operator
217    class.
218   </para>
219
220   <para>
221    The operators should not be defined by SQL functions.  A SQL function
222    is likely to be inlined into the calling query, which will prevent
223    the optimizer from recognizing that the query matches an index.
224   </para>
225  </refsect1>
226   
227  <refsect1>
228   <title>Examples</title>
229
230   <para>
231    The following example command defines a GiST index operator class
232    for the data type <literal>_int4</> (array of <type>int4</type>).  See
233    <filename>contrib/intarray/</> for the complete example.
234   </para>
235
236 <programlisting>
237 CREATE OPERATOR CLASS gist__int_ops
238     DEFAULT FOR TYPE _int4 USING gist AS
239         OPERATOR        3       &&,
240         OPERATOR        6       =       RECHECK,
241         OPERATOR        7       @&gt;,
242         OPERATOR        8       &lt;@,
243         OPERATOR        20      @@ (_int4, query_int),
244         FUNCTION        1       g_int_consistent (internal, _int4, int4),
245         FUNCTION        2       g_int_union (bytea, internal),
246         FUNCTION        3       g_int_compress (internal),
247         FUNCTION        4       g_int_decompress (internal),
248         FUNCTION        5       g_int_penalty (internal, internal, internal),
249         FUNCTION        6       g_int_picksplit (internal, internal),
250         FUNCTION        7       g_int_same (_int4, _int4, internal);
251 </programlisting>  
252  </refsect1>
253  
254  <refsect1>
255   <title>Compatibility</title>
256
257   <para>
258    <command>CREATE OPERATOR CLASS</command> is a
259    <productname>PostgreSQL</productname> extension.  There is no
260    <command>CREATE OPERATOR CLASS</command> statement in the SQL
261    standard.
262   </para>
263  </refsect1>
264
265  <refsect1>
266   <title>See Also</title>
267
268   <simplelist type="inline">
269    <member><xref linkend="sql-alteropclass" endterm="sql-alteropclass-title"></member>
270    <member><xref linkend="sql-dropopclass" endterm="sql-dropopclass-title"></member>
271   </simplelist>
272  </refsect1>
273 </refentry>