]> begriffs open source - ai-pg/blob - full-docs/txt/sql-createoperator.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-createoperator.txt
1
2 CREATE OPERATOR
3
4    CREATE OPERATOR — define a new operator
5
6 Synopsis
7
8 CREATE OPERATOR name (
9     {FUNCTION|PROCEDURE} = function_name
10     [, LEFTARG = left_type ] [, RIGHTARG = right_type ]
11     [, COMMUTATOR = com_op ] [, NEGATOR = neg_op ]
12     [, RESTRICT = res_proc ] [, JOIN = join_proc ]
13     [, HASHES ] [, MERGES ]
14 )
15
16 Description
17
18    CREATE OPERATOR defines a new operator, name. The user who defines an
19    operator becomes its owner. If a schema name is given then the operator
20    is created in the specified schema. Otherwise it is created in the
21    current schema.
22
23    The operator name is a sequence of up to NAMEDATALEN-1 (63 by default)
24    characters from the following list:
25
26    + - * / < > = ~ ! @ # % ^ & | ` ?
27
28    There are a few restrictions on your choice of name:
29      * -- and /* cannot appear anywhere in an operator name, since they
30        will be taken as the start of a comment.
31      * A multicharacter operator name cannot end in + or -, unless the
32        name also contains at least one of these characters:
33        ~ ! @ # % ^ & | ` ?
34        For example, @- is an allowed operator name, but *- is not. This
35        restriction allows PostgreSQL to parse SQL-compliant commands
36        without requiring spaces between tokens.
37      * The symbol => is reserved by the SQL grammar, so it cannot be used
38        as an operator name.
39
40    The operator != is mapped to <> on input, so these two names are always
41    equivalent.
42
43    For binary operators, both LEFTARG and RIGHTARG must be defined. For
44    prefix operators only RIGHTARG should be defined. The function_name
45    function must have been previously defined using CREATE FUNCTION and
46    must be defined to accept the correct number of arguments (either one
47    or two) of the indicated types.
48
49    In the syntax of CREATE OPERATOR, the keywords FUNCTION and PROCEDURE
50    are equivalent, but the referenced function must in any case be a
51    function, not a procedure. The use of the keyword PROCEDURE here is
52    historical and deprecated.
53
54    The other clauses specify optional operator optimization attributes.
55    Their meaning is detailed in Section 36.15.
56
57    To be able to create an operator, you must have USAGE privilege on the
58    argument types and the return type, as well as EXECUTE privilege on the
59    underlying function. If a commutator or negator operator is specified,
60    you must own those operators.
61
62 Parameters
63
64    name
65           The name of the operator to be defined. See above for allowable
66           characters. The name can be schema-qualified, for example CREATE
67           OPERATOR myschema.+ (...). If not, then the operator is created
68           in the current schema. Two operators in the same schema can have
69           the same name if they operate on different data types. This is
70           called overloading.
71
72    function_name
73           The function used to implement this operator.
74
75    left_type
76           The data type of the operator's left operand, if any. This
77           option would be omitted for a prefix operator.
78
79    right_type
80           The data type of the operator's right operand.
81
82    com_op
83           The commutator of this operator.
84
85    neg_op
86           The negator of this operator.
87
88    res_proc
89           The restriction selectivity estimator function for this
90           operator.
91
92    join_proc
93           The join selectivity estimator function for this operator.
94
95    HASHES
96           Indicates this operator can support a hash join.
97
98    MERGES
99           Indicates this operator can support a merge join.
100
101    To give a schema-qualified operator name in com_op or the other
102    optional arguments, use the OPERATOR() syntax, for example:
103 COMMUTATOR = OPERATOR(myschema.===) ,
104
105 Notes
106
107    Refer to Section 36.14 and Section 36.15 for further information.
108
109    When you are defining a self-commutative operator, you just do it. When
110    you are defining a pair of commutative operators, things are a little
111    trickier: how can the first one to be defined refer to the other one,
112    which you haven't defined yet? There are three solutions to this
113    problem:
114      * One way is to omit the COMMUTATOR clause in the first operator that
115        you define, and then provide one in the second operator's
116        definition. Since PostgreSQL knows that commutative operators come
117        in pairs, when it sees the second definition it will automatically
118        go back and fill in the missing COMMUTATOR clause in the first
119        definition.
120      * Another, more straightforward way is just to include COMMUTATOR
121        clauses in both definitions. When PostgreSQL processes the first
122        definition and realizes that COMMUTATOR refers to a nonexistent
123        operator, the system will make a dummy entry for that operator in
124        the system catalog. This dummy entry will have valid data only for
125        the operator name, left and right operand types, and owner, since
126        that's all that PostgreSQL can deduce at this point. The first
127        operator's catalog entry will link to this dummy entry. Later, when
128        you define the second operator, the system updates the dummy entry
129        with the additional information from the second definition. If you
130        try to use the dummy operator before it's been filled in, you'll
131        just get an error message.
132      * Alternatively, both operators can be defined without COMMUTATOR
133        clauses and then ALTER OPERATOR can be used to set their commutator
134        links. It's sufficient to ALTER either one of the pair.
135
136    In all three cases, you must own both operators in order to mark them
137    as commutators.
138
139    Pairs of negator operators can be defined using the same methods as for
140    commutator pairs.
141
142    It is not possible to specify an operator's lexical precedence in
143    CREATE OPERATOR, because the parser's precedence behavior is
144    hard-wired. See Section 4.1.6 for precedence details.
145
146    The obsolete options SORT1, SORT2, LTCMP, and GTCMP were formerly used
147    to specify the names of sort operators associated with a merge-joinable
148    operator. This is no longer necessary, since information about
149    associated operators is found by looking at B-tree operator families
150    instead. If one of these options is given, it is ignored except for
151    implicitly setting MERGES true.
152
153    Use DROP OPERATOR to delete user-defined operators from a database. Use
154    ALTER OPERATOR to modify operators in a database.
155
156 Examples
157
158    The following command defines a new operator, area-equality, for the
159    data type box:
160 CREATE OPERATOR === (
161     LEFTARG = box,
162     RIGHTARG = box,
163     FUNCTION = area_equal_function,
164     COMMUTATOR = ===,
165     NEGATOR = !==,
166     RESTRICT = area_restriction_function,
167     JOIN = area_join_function,
168     HASHES, MERGES
169 );
170
171 Compatibility
172
173    CREATE OPERATOR is a PostgreSQL extension. There are no provisions for
174    user-defined operators in the SQL standard.
175
176 See Also
177
178    ALTER OPERATOR, CREATE OPERATOR CLASS, DROP OPERATOR