1CREATE OPERATOR CLASS(7) PostgreSQL 15.4 DocumentationCREATE OPERATOR CLASS(7)
2
3
4
6 CREATE_OPERATOR_CLASS - define a new operator class
7
9 CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type
10 USING index_method [ FAMILY family_name ] AS
11 { OPERATOR strategy_number operator_name [ ( op_type, op_type ) ] [ FOR SEARCH | FOR ORDER BY sort_family_name ]
12 | FUNCTION support_number [ ( op_type [ , op_type ] ) ] function_name ( argument_type [, ...] )
13 | STORAGE storage_type
14 } [, ... ]
15
17 CREATE OPERATOR CLASS creates a new operator class. An operator class
18 defines how a particular data type can be used with an index. The
19 operator class specifies that certain operators will fill particular
20 roles or “strategies” for this data type and this index method. The
21 operator class also specifies the support functions to be used by the
22 index method when the operator class is selected for an index column.
23 All the operators and functions used by an operator class must be
24 defined before the operator class can be created.
25
26 If a schema name is given then the operator class is created in the
27 specified schema. Otherwise it is created in the current schema. Two
28 operator classes in the same schema can have the same name only if they
29 are for different index methods.
30
31 The user who defines an operator class becomes its owner. Presently,
32 the creating user must be a superuser. (This restriction is made
33 because an erroneous operator class definition could confuse or even
34 crash the server.)
35
36 CREATE OPERATOR CLASS does not presently check whether the operator
37 class definition includes all the operators and functions required by
38 the index method, nor whether the operators and functions form a
39 self-consistent set. It is the user's responsibility to define a valid
40 operator class.
41
42 Related operator classes can be grouped into operator families. To add
43 a new operator class to an existing family, specify the FAMILY option
44 in CREATE OPERATOR CLASS. Without this option, the new class is placed
45 into a family named the same as the new class (creating that family if
46 it doesn't already exist).
47
48 Refer to Section 38.16 for further information.
49
51 name
52 The name of the operator class to be created. The name can be
53 schema-qualified.
54
55 DEFAULT
56 If present, the operator class will become the default operator
57 class for its data type. At most one operator class can be the
58 default for a specific data type and index method.
59
60 data_type
61 The column data type that this operator class is for.
62
63 index_method
64 The name of the index method this operator class is for.
65
66 family_name
67 The name of the existing operator family to add this operator class
68 to. If not specified, a family named the same as the operator class
69 is used (creating it, if it doesn't already exist).
70
71 strategy_number
72 The index method's strategy number for an operator associated with
73 the operator class.
74
75 operator_name
76 The name (optionally schema-qualified) of an operator associated
77 with the operator class.
78
79 op_type
80 In an OPERATOR clause, the operand data type(s) of the operator, or
81 NONE to signify a prefix operator. The operand data types can be
82 omitted in the normal case where they are the same as the operator
83 class's data type.
84
85 In a FUNCTION clause, the operand data type(s) the function is
86 intended to support, if different from the input data type(s) of
87 the function (for B-tree comparison functions and hash functions)
88 or the class's data type (for B-tree sort support functions, B-tree
89 equal image functions, and all functions in GiST, SP-GiST, GIN and
90 BRIN operator classes). These defaults are correct, and so op_type
91 need not be specified in FUNCTION clauses, except for the case of a
92 B-tree sort support function that is meant to support
93 cross-data-type comparisons.
94
95 sort_family_name
96 The name (optionally schema-qualified) of an existing btree
97 operator family that describes the sort ordering associated with an
98 ordering operator.
99
100 If neither FOR SEARCH nor FOR ORDER BY is specified, FOR SEARCH is
101 the default.
102
103 support_number
104 The index method's support function number for a function
105 associated with the operator class.
106
107 function_name
108 The name (optionally schema-qualified) of a function that is an
109 index method support function for the operator class.
110
111 argument_type
112 The parameter data type(s) of the function.
113
114 storage_type
115 The data type actually stored in the index. Normally this is the
116 same as the column data type, but some index methods (currently
117 GiST, GIN, SP-GiST and BRIN) allow it to be different. The STORAGE
118 clause must be omitted unless the index method allows a different
119 type to be used. If the column data_type is specified as anyarray,
120 the storage_type can be declared as anyelement to indicate that the
121 index entries are members of the element type belonging to the
122 actual array type that each particular index is created for.
123
124 The OPERATOR, FUNCTION, and STORAGE clauses can appear in any order.
125
127 Because the index machinery does not check access permissions on
128 functions before using them, including a function or operator in an
129 operator class is tantamount to granting public execute permission on
130 it. This is usually not an issue for the sorts of functions that are
131 useful in an operator class.
132
133 The operators should not be defined by SQL functions. An SQL function
134 is likely to be inlined into the calling query, which will prevent the
135 optimizer from recognizing that the query matches an index.
136
137 Before PostgreSQL 8.4, the OPERATOR clause could include a RECHECK
138 option. This is no longer supported because whether an index operator
139 is “lossy” is now determined on-the-fly at run time. This allows
140 efficient handling of cases where an operator might or might not be
141 lossy.
142
144 The following example command defines a GiST index operator class for
145 the data type _int4 (array of int4). See the intarray module for the
146 complete example.
147
148 CREATE OPERATOR CLASS gist__int_ops
149 DEFAULT FOR TYPE _int4 USING gist AS
150 OPERATOR 3 &&,
151 OPERATOR 6 = (anyarray, anyarray),
152 OPERATOR 7 @>,
153 OPERATOR 8 <@,
154 OPERATOR 20 @@ (_int4, query_int),
155 FUNCTION 1 g_int_consistent (internal, _int4, smallint, oid, internal),
156 FUNCTION 2 g_int_union (internal, internal),
157 FUNCTION 3 g_int_compress (internal),
158 FUNCTION 4 g_int_decompress (internal),
159 FUNCTION 5 g_int_penalty (internal, internal, internal),
160 FUNCTION 6 g_int_picksplit (internal, internal),
161 FUNCTION 7 g_int_same (_int4, _int4, internal);
162
164 CREATE OPERATOR CLASS is a PostgreSQL extension. There is no CREATE
165 OPERATOR CLASS statement in the SQL standard.
166
168 ALTER OPERATOR CLASS (ALTER_OPERATOR_CLASS(7)), DROP OPERATOR CLASS
169 (DROP_OPERATOR_CLASS(7)), CREATE OPERATOR FAMILY
170 (CREATE_OPERATOR_FAMILY(7)), ALTER OPERATOR FAMILY
171 (ALTER_OPERATOR_FAMILY(7))
172
173
174
175PostgreSQL 15.4 2023 CREATE OPERATOR CLASS(7)