summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/CREATE_OPERATOR.7
blob: eb75451605c810117ef785069ed4b06f7ab1845b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'\" t
.\"     Title: CREATE OPERATOR
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2024
.\"    Manual: PostgreSQL 15.6 Documentation
.\"    Source: PostgreSQL 15.6
.\"  Language: English
.\"
.TH "CREATE OPERATOR" "7" "2024" "PostgreSQL 15.6" "PostgreSQL 15.6 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
CREATE_OPERATOR \- define a new operator
.SH "SYNOPSIS"
.sp
.nf
CREATE OPERATOR \fIname\fR (
    {FUNCTION|PROCEDURE} = \fIfunction_name\fR
    [, LEFTARG = \fIleft_type\fR ] [, RIGHTARG = \fIright_type\fR ]
    [, COMMUTATOR = \fIcom_op\fR ] [, NEGATOR = \fIneg_op\fR ]
    [, RESTRICT = \fIres_proc\fR ] [, JOIN = \fIjoin_proc\fR ]
    [, HASHES ] [, MERGES ]
)
.fi
.SH "DESCRIPTION"
.PP
\fBCREATE OPERATOR\fR
defines a new operator,
\fIname\fR\&. The user who defines an operator becomes its owner\&. If a schema name is given then the operator is created in the specified schema\&. Otherwise it is created in the current schema\&.
.PP
The operator name is a sequence of up to
NAMEDATALEN\-1 (63 by default) characters from the following list:
.sp
.if n \{\
.RS 4
.\}
.nf
+ \- * / < > = ~ ! @ # % ^ & | ` ?
.fi
.if n \{\
.RE
.\}
.sp
There are a few restrictions on your choice of name:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
\-\-
and
/*
cannot appear anywhere in an operator name, since they will be taken as the start of a comment\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
A multicharacter operator name cannot end in
+
or
\-, unless the name also contains at least one of these characters:
.sp
.if n \{\
.RS 4
.\}
.nf
~ ! @ # % ^ & | ` ?
.fi
.if n \{\
.RE
.\}
.sp
For example,
@\-
is an allowed operator name, but
*\-
is not\&. This restriction allows
PostgreSQL
to parse SQL\-compliant commands without requiring spaces between tokens\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The symbol
=>
is reserved by the SQL grammar, so it cannot be used as an operator name\&.
.RE
.PP
The operator
!=
is mapped to
<>
on input, so these two names are always equivalent\&.
.PP
For binary operators, both
LEFTARG
and
RIGHTARG
must be defined\&. For prefix operators only
RIGHTARG
should be defined\&. The
\fIfunction_name\fR
function must have been previously defined using
\fBCREATE FUNCTION\fR
and must be defined to accept the correct number of arguments (either one or two) of the indicated types\&.
.PP
In the syntax of
CREATE OPERATOR, the keywords
FUNCTION
and
PROCEDURE
are equivalent, but the referenced function must in any case be a function, not a procedure\&. The use of the keyword
PROCEDURE
here is historical and deprecated\&.
.PP
The other clauses specify optional operator optimization clauses\&. Their meaning is detailed in
Section\ \&38.15\&.
.PP
To be able to create an operator, you must have
USAGE
privilege on the argument types and the return type, as well as
EXECUTE
privilege on the underlying function\&. If a commutator or negator operator is specified, you must own these operators\&.
.SH "PARAMETERS"
.PP
\fIname\fR
.RS 4
The name of the operator to be defined\&. See above for allowable characters\&. The name can be schema\-qualified, for example
CREATE OPERATOR myschema\&.+ (\&.\&.\&.)\&. If not, then the operator is created in the current schema\&. Two operators in the same schema can have the same name if they operate on different data types\&. This is called
overloading\&.
.RE
.PP
\fIfunction_name\fR
.RS 4
The function used to implement this operator\&.
.RE
.PP
\fIleft_type\fR
.RS 4
The data type of the operator\*(Aqs left operand, if any\&. This option would be omitted for a prefix operator\&.
.RE
.PP
\fIright_type\fR
.RS 4
The data type of the operator\*(Aqs right operand\&.
.RE
.PP
\fIcom_op\fR
.RS 4
The commutator of this operator\&.
.RE
.PP
\fIneg_op\fR
.RS 4
The negator of this operator\&.
.RE
.PP
\fIres_proc\fR
.RS 4
The restriction selectivity estimator function for this operator\&.
.RE
.PP
\fIjoin_proc\fR
.RS 4
The join selectivity estimator function for this operator\&.
.RE
.PP
HASHES
.RS 4
Indicates this operator can support a hash join\&.
.RE
.PP
MERGES
.RS 4
Indicates this operator can support a merge join\&.
.RE
.PP
To give a schema\-qualified operator name in
\fIcom_op\fR
or the other optional arguments, use the
OPERATOR()
syntax, for example:
.sp
.if n \{\
.RS 4
.\}
.nf
COMMUTATOR = OPERATOR(myschema\&.===) ,
.fi
.if n \{\
.RE
.\}
.SH "NOTES"
.PP
Refer to
Section\ \&38.14
for further information\&.
.PP
It is not possible to specify an operator\*(Aqs lexical precedence in
\fBCREATE OPERATOR\fR, because the parser\*(Aqs precedence behavior is hard\-wired\&. See
Section\ \&4.1.6
for precedence details\&.
.PP
The obsolete options
SORT1,
SORT2,
LTCMP, and
GTCMP
were formerly used to specify the names of sort operators associated with a merge\-joinable operator\&. This is no longer necessary, since information about associated operators is found by looking at B\-tree operator families instead\&. If one of these options is given, it is ignored except for implicitly setting
MERGES
true\&.
.PP
Use
\fBDROP OPERATOR\fR
to delete user\-defined operators from a database\&. Use
\fBALTER OPERATOR\fR
to modify operators in a database\&.
.SH "EXAMPLES"
.PP
The following command defines a new operator, area\-equality, for the data type
box:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE OPERATOR === (
    LEFTARG = box,
    RIGHTARG = box,
    FUNCTION = area_equal_function,
    COMMUTATOR = ===,
    NEGATOR = !==,
    RESTRICT = area_restriction_function,
    JOIN = area_join_function,
    HASHES, MERGES
);
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
\fBCREATE OPERATOR\fR
is a
PostgreSQL
extension\&. There are no provisions for user\-defined operators in the SQL standard\&.
.SH "SEE ALSO"
ALTER OPERATOR (\fBALTER_OPERATOR\fR(7)), CREATE OPERATOR CLASS (\fBCREATE_OPERATOR_CLASS\fR(7)), DROP OPERATOR (\fBDROP_OPERATOR\fR(7))