]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_OPERATOR_CLASS.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_OPERATOR_CLASS.7
1 '\" t
2 .\"     Title: CREATE OPERATOR CLASS
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_OPERATOR_CLASS \- define a new operator class
32 .SH "SYNOPSIS"
33 .sp
34 .nf
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
40   } [, \&.\&.\&. ]
41 .fi
42 .SH "DESCRIPTION"
43 .PP
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
46 \(lqstrategies\(rq
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\&.
48 .PP
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\&.
50 .PP
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\&.)
52 .PP
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\&.
55 .PP
56 Related operator classes can be grouped into
57 operator families\&. To add a new operator class to an existing family, specify the
58 FAMILY
59 option in
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)\&.
61 .PP
62 Refer to
63 Section\ \&36.16
64 for further information\&.
65 .SH "PARAMETERS"
66 .PP
67 \fIname\fR
68 .RS 4
69 The name of the operator class to be created\&. The name can be schema\-qualified\&.
70 .RE
71 .PP
72 DEFAULT
73 .RS 4
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\&.
75 .RE
76 .PP
77 \fIdata_type\fR
78 .RS 4
79 The column data type that this operator class is for\&.
80 .RE
81 .PP
82 \fIindex_method\fR
83 .RS 4
84 The name of the index method this operator class is for\&.
85 .RE
86 .PP
87 \fIfamily_name\fR
88 .RS 4
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)\&.
90 .RE
91 .PP
92 \fIstrategy_number\fR
93 .RS 4
94 The index method\*(Aqs strategy number for an operator associated with the operator class\&.
95 .RE
96 .PP
97 \fIoperator_name\fR
98 .RS 4
99 The name (optionally schema\-qualified) of an operator associated with the operator class\&.
100 .RE
101 .PP
102 \fIop_type\fR
103 .RS 4
104 In an
105 OPERATOR
106 clause, the operand data type(s) of the operator, or
107 NONE
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\&.
109 .sp
110 In a
111 FUNCTION
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
113 \fIop_type\fR
114 need not be specified in
115 FUNCTION
116 clauses, except for the case of a B\-tree sort support function that is meant to support cross\-data\-type comparisons\&.
117 .RE
118 .PP
119 \fIsort_family_name\fR
120 .RS 4
121 The name (optionally schema\-qualified) of an existing
122 btree
123 operator family that describes the sort ordering associated with an ordering operator\&.
124 .sp
125 If neither
126 FOR SEARCH
127 nor
128 FOR ORDER BY
129 is specified,
130 FOR SEARCH
131 is the default\&.
132 .RE
133 .PP
134 \fIsupport_number\fR
135 .RS 4
136 The index method\*(Aqs support function number for a function associated with the operator class\&.
137 .RE
138 .PP
139 \fIfunction_name\fR
140 .RS 4
141 The name (optionally schema\-qualified) of a function that is an index method support function for the operator class\&.
142 .RE
143 .PP
144 \fIargument_type\fR
145 .RS 4
146 The parameter data type(s) of the function\&.
147 .RE
148 .PP
149 \fIstorage_type\fR
150 .RS 4
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
152 STORAGE
153 clause must be omitted unless the index method allows a different type to be used\&. If the column
154 \fIdata_type\fR
155 is specified as
156 anyarray, the
157 \fIstorage_type\fR
158 can be declared as
159 anyelement
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\&.
161 .RE
162 .PP
163 The
164 OPERATOR,
165 FUNCTION, and
166 STORAGE
167 clauses can appear in any order\&.
168 .SH "NOTES"
169 .PP
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\&.
171 .PP
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\&.
173 .SH "EXAMPLES"
174 .PP
175 The following example command defines a GiST index operator class for the data type
176 _int4
177 (array of
178 int4)\&. See the
179 intarray
180 module for the complete example\&.
181 .sp
182 .if n \{\
183 .RS 4
184 .\}
185 .nf
186 CREATE OPERATOR CLASS gist__int_ops
187     DEFAULT FOR TYPE _int4 USING gist AS
188         OPERATOR        3       &&,
189         OPERATOR        6       = (anyarray, anyarray),
190         OPERATOR        7       @>,
191         OPERATOR        8       <@,
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);
200 .fi
201 .if n \{\
202 .RE
203 .\}
204 .SH "COMPATIBILITY"
205 .PP
206 \fBCREATE OPERATOR CLASS\fR
207 is a
208 PostgreSQL
209 extension\&. There is no
210 \fBCREATE OPERATOR CLASS\fR
211 statement in the SQL standard\&.
212 .SH "SEE ALSO"
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))