2 .\" Title: CREATE OPERATOR CLASS
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE OPERATOR CLASS" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_OPERATOR_CLASS \- define a new operator class
35 CREATE OPERATOR CLASS \fIname\fR [ DEFAULT ] FOR TYPE \fIdata_type\fR
36 USING \fIindex_method\fR [ FAMILY \fIfamily_name\fR ] AS
37 { OPERATOR \fIstrategy_number\fR \fIoperator_name\fR [ ( \fIop_type\fR, \fIop_type\fR ) ] [ FOR SEARCH | FOR ORDER BY \fIsort_family_name\fR ]
38 | FUNCTION \fIsupport_number\fR [ ( \fIop_type\fR [ , \fIop_type\fR ] ) ] \fIfunction_name\fR ( \fIargument_type\fR [, \&.\&.\&.] )
39 | STORAGE \fIstorage_type\fR
44 \fBCREATE OPERATOR CLASS\fR
45 creates a new operator class\&. An operator class defines how a particular data type can be used with an index\&. The operator class specifies that certain operators will fill particular roles or
47 for this data type and this index method\&. The operator class also specifies the support functions to be used by the index method when the operator class is selected for an index column\&. All the operators and functions used by an operator class must be defined before the operator class can be created\&.
49 If a schema name is given then the operator class is created in the specified schema\&. Otherwise it is created in the current schema\&. Two operator classes in the same schema can have the same name only if they are for different index methods\&.
51 The user who defines an operator class becomes its owner\&. Presently, the creating user must be a superuser\&. (This restriction is made because an erroneous operator class definition could confuse or even crash the server\&.)
53 \fBCREATE OPERATOR CLASS\fR
54 does not presently check whether the operator class definition includes all the operators and functions required by the index method, nor whether the operators and functions form a self\-consistent set\&. It is the user\*(Aqs responsibility to define a valid operator class\&.
56 Related operator classes can be grouped into
57 operator families\&. To add a new operator class to an existing family, specify the
60 \fBCREATE OPERATOR CLASS\fR\&. Without this option, the new class is placed into a family named the same as the new class (creating that family if it doesn\*(Aqt already exist)\&.
64 for further information\&.
69 The name of the operator class to be created\&. The name can be schema\-qualified\&.
74 If present, the operator class will become the default operator class for its data type\&. At most one operator class can be the default for a specific data type and index method\&.
79 The column data type that this operator class is for\&.
84 The name of the index method this operator class is for\&.
89 The name of the existing operator family to add this operator class to\&. If not specified, a family named the same as the operator class is used (creating it, if it doesn\*(Aqt already exist)\&.
94 The index method\*(Aqs strategy number for an operator associated with the operator class\&.
99 The name (optionally schema\-qualified) of an operator associated with the operator class\&.
106 clause, the operand data type(s) of the operator, or
108 to signify a prefix operator\&. The operand data types can be omitted in the normal case where they are the same as the operator class\*(Aqs data type\&.
112 clause, the operand data type(s) the function is intended to support, if different from the input data type(s) of the function (for B\-tree comparison functions and hash functions) or the class\*(Aqs data type (for B\-tree sort support functions, B\-tree equal image functions, and all functions in GiST, SP\-GiST, GIN and BRIN operator classes)\&. These defaults are correct, and so
114 need not be specified in
116 clauses, except for the case of a B\-tree sort support function that is meant to support cross\-data\-type comparisons\&.
119 \fIsort_family_name\fR
121 The name (optionally schema\-qualified) of an existing
123 operator family that describes the sort ordering associated with an ordering operator\&.
136 The index method\*(Aqs support function number for a function associated with the operator class\&.
141 The name (optionally schema\-qualified) of a function that is an index method support function for the operator class\&.
146 The parameter data type(s) of the function\&.
151 The data type actually stored in the index\&. Normally this is the same as the column data type, but some index methods (currently GiST, GIN, SP\-GiST and BRIN) allow it to be different\&. The
153 clause must be omitted unless the index method allows a different type to be used\&. If the column
160 to indicate that the index entries are members of the element type belonging to the actual array type that each particular index is created for\&.
167 clauses can appear in any order\&.
170 Because the index machinery does not check access permissions on functions before using them, including a function or operator in an operator class is tantamount to granting public execute permission on it\&. This is usually not an issue for the sorts of functions that are useful in an operator class\&.
172 The operators should not be defined by SQL functions\&. An SQL function is likely to be inlined into the calling query, which will prevent the optimizer from recognizing that the query matches an index\&.
175 The following example command defines a GiST index operator class for the data type
180 module for the complete example\&.
186 CREATE OPERATOR CLASS gist__int_ops
187 DEFAULT FOR TYPE _int4 USING gist AS
189 OPERATOR 6 = (anyarray, anyarray),
192 OPERATOR 20 @@ (_int4, query_int),
193 FUNCTION 1 g_int_consistent (internal, _int4, smallint, oid, internal),
194 FUNCTION 2 g_int_union (internal, internal),
195 FUNCTION 3 g_int_compress (internal),
196 FUNCTION 4 g_int_decompress (internal),
197 FUNCTION 5 g_int_penalty (internal, internal, internal),
198 FUNCTION 6 g_int_picksplit (internal, internal),
199 FUNCTION 7 g_int_same (_int4, _int4, internal);
206 \fBCREATE OPERATOR CLASS\fR
209 extension\&. There is no
210 \fBCREATE OPERATOR CLASS\fR
211 statement in the SQL standard\&.
213 ALTER OPERATOR CLASS (\fBALTER_OPERATOR_CLASS\fR(7)), DROP OPERATOR CLASS (\fBDROP_OPERATOR_CLASS\fR(7)), CREATE OPERATOR FAMILY (\fBCREATE_OPERATOR_FAMILY\fR(7)), ALTER OPERATOR FAMILY (\fBALTER_OPERATOR_FAMILY\fR(7))