CREATE_OPERATOR(7pg14)
β¨ CREATE OPERATOR π
okay so bestie, welcome to CREATE OPERATOR β the PostgreSQL 14.24 glow-up where you literally invent a brand-new operator. yes, your own symbol. main character energy fr fr π
πΈ NAME πΈ
CREATE_OPERATOR β define a new operator β¨ (it does exactly what it says on the tin, no cap)
π SYNOPSIS π
CREATE OPERATOR name (
{FUNCTION|PROCEDURE} = function_name
[, LEFTARG = left_type ] [, RIGHTARG = right_type ]
[, COMMUTATOR = com_op ] [, NEGATOR = neg_op ]
[, RESTRICT = res_proc ] [, JOIN = join_proc ]
[, HASHES ] [, MERGES ]
)
π¦ DESCRIPTION π¦
So CREATE OPERATOR defines a brand-new operator called name. The person who defines the operator becomes its owner β congratulations, it's yours now, you're literally the parent π. If you give a schema name, the operator gets created in that schema; otherwise it lands in the current schema. simple as that.
The operator name is a sequence of up to NAMEDATALEN-1 (63 by default) characters, pulled from this iconic little lineup:
+ - * / < > = ~ ! @ # % ^ & | ` ?
But bestie, there are some restrictions on your name choice, and you need to know them:
--and/*cannot appear anywhere in an operator name, because they'd get read as the start of a comment π (rude, but fair).- A multicharacter operator name cannot end in
+or-, unless the name also contains at least one of these characters:
~ ! @ # % ^ & | ` ?
Like, for example, @- is an allowed operator name, but *- is not. The whole point of this restriction is so PostgreSQL can parse SQL-compliant commands without needing spaces between tokens. iykyk.
- The symbol
=>is reserved by the SQL grammar, so it simply cannot be used as an operator name. nope. not happening. π
Also, the operator != is mapped to <> on input, so those two names are always equivalent. forever and always, they're the same thing.
For binary operators, both LEFTARG and RIGHTARG must be defined. For prefix operators, only RIGHTARG should be defined. And the function_name function? It must have been previously defined using CREATE FUNCTION, and it must be defined to accept the correct number of arguments (either one or two) of the indicated types. no improvising, okay? π«Ά
In the syntax of CREATE OPERATOR, the keywords FUNCTION and PROCEDURE are equivalent β but the referenced thing must in any case be a function, not a procedure. The use of the keyword PROCEDURE here is historical and deprecated π. it's giving legacy baggage, honestly.
The other clauses specify optional operator optimization clauses. Their meaning is detailed in Section 38.15 β go read it if you want the full tea π.
To be able to create an operator, you must haveUSAGEprivilege on the argument types and the return type, as well asEXECUTEprivilege on the underlying function. And if a commutator or negator operator is specified, you must own these operators. no borrowing, no borrowing without permission π
π PARAMETERS π
nameβ the name of the operator you're defining. see above for the allowable characters β¨. The name can be schema-qualified, for exampleCREATE OPERATOR myschema.+ (...). If it isn't, then the operator gets created in the current schema. And here's the cute part: two operators in the same schema can have the same name if they operate on different data types β that's called overloading π.function_nameβ the function used to implement this operator.left_typeβ the data type of the operator's left operand, if any. You'd omit this option entirely for a prefix operator π.right_typeβ the data type of the operator's right operand.com_opβ the commutator of this operator.neg_opβ the negator of this operator.res_procβ the restriction selectivity estimator function for this operator.join_procβ the join selectivity estimator function for this operator.HASHESβ indicates this operator can support a hash join π₯.MERGESβ indicates this operator can support a merge join π₯.
To give a schema-qualified operator name in com_op or the other optional arguments, use the OPERATOR() syntax, for example:
COMMUTATOR = OPERATOR(myschema.===) ,
β NOTES β
Refer to Section 38.14 for further information π.
It is not possible to specify an operator's lexical precedence in CREATE OPERATOR, because the parser's precedence behavior is hard-wired. See Section 4.1.6 for precedence details. (sorry bestie, that dial is not yours to turn π)
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. so like... they're basically relics. vintage. πΊ
Use DROP OPERATOR to delete user-defined operators from a database. Use ALTER OPERATOR to modify operators in a database. clean, simple, slay.
π EXAMPLES π
The following command defines a new operator, area-equality, for the data type box:
CREATE OPERATOR === (
LEFTARG = box,
RIGHTARG = box,
FUNCTION = area_equal_function,
COMMUTATOR = ===,
NEGATOR = !==,
RESTRICT = area_restriction_function,
JOIN = area_join_function,
HASHES, MERGES
);
look at that. gorgeous. absolutely ate. β¨
π§ COMPATIBILITY π§
CREATE OPERATOR is a PostgreSQL extension. There are no provisions for user-defined operators in the SQL standard. so yeah, this is a PostgreSQL-only flex π
π«Ά SEE ALSO π«Ά
ALTER OPERATOR(ALTER_OPERATOR(7))CREATE OPERATOR CLASS(CREATE_OPERATOR_CLASS(7))DROP OPERATOR(DROP_OPERATOR(7))
and that's the whole page, bestie π this gorgeous little doc is from PostgreSQL 14.24, dated 2026 β go forth and make operators, you absolute icon ππ¦β
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms