CREATE_OPERATOR(7pg15)

πŸ“¦ version 15.19, in opensuse-leap 16.0

πŸ’– CREATE OPERATOR πŸ’–

okay so bestie, we're doing CREATE OPERATOR today β€” it's page 7 of the PostgreSQL 15.19 docs and honestly? iconic territory. this is the page that tells you how to invent your very own operator. main character energy fr. let's get into it 🫢


✨ NAME ✨

CREATE_OPERATOR β€” define a new operator. that's it. that's the whole vibe. a brand new operator, minted by you πŸ’…


🌸 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 ]
)

like, look at her. so structured. so cute. ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

CREATE OPERATOR defines a new operator called name. whoever defines the operator becomes its owner β€” congrats, that's you, you're the parent now πŸ‘‘

if you give a schema name, the operator gets created in that schema. if you don't, it lands in the current schema. no cap.

so the operator name is a sequence of up to NAMEDATALEN-1 characters (which is 63 by default, iykyk) taken from this little squad right here:

+ - * / < > = ~ ! @ # % ^ & | ` ?

but there are a few restrictions on your name choices, and you do need to know them:

  • -- and /* cannot appear anywhere in an operator name, because the parser will take them as the start of a comment 😭 literally not allowed.
  • a multicharacter operator name cannot end in + or -, unless the name also contains at least one of these characters:
~ ! @ # % ^ & | ` ?

so for example, @- is an allowed operator name, but *- is not. why? because this restriction lets PostgreSQL parse SQL-compliant commands without needing spaces between tokens. she's just being efficient, leave her alone πŸ’€

  • the symbol => is reserved by the SQL grammar, so it cannot be used as an operator name. sorry bestie.

also != is mapped to <> on input, so these two names are always equivalent. same girl, different outfit πŸ’…

for binary operators, both LEFTARG and RIGHTARG must be defined. for prefix operators, only RIGHTARG should be defined. the function_name function must already exist (made with CREATE FUNCTION first, obviously) and must be defined to accept the correct number of arguments β€” either one or two β€” of the indicated types.

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 β€” so yeah, it's giving legacy, don't @ me πŸ™ˆ

the other clauses are all optional operator optimization clauses. their meaning is detailed in Section 38.15. go read it, it's a whole thing πŸ“–

and to be able to create an operator, you must have USAGE privilege on the argument types and the return type, plus EXECUTE privilege on the underlying function. if you specify a commutator or negator operator, you must own those operators. no borrowing bestie πŸŽ€


πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name of the operator you're defining. see above for which characters are allowed. the name can be schema-qualified, like CREATE OPERATOR myschema.+ (...). if it's not, the operator goes 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. the actual muscle behind the sparkle πŸ’ͺ
  • left_type β€” the data type of the operator's left operand, if there is one. 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. πŸ”₯

okay and if you wanna give a schema-qualified operator name in com_op or any of the other optional arguments, you use the OPERATOR() syntax, like this:

COMMUTATOR = OPERATOR(myschema.===) ,

note the trailing comma, bestie. keep it. 🧚


πŸ’œ NOTES πŸ’œ

refer to Section 38.14 for further information.

you cannot specify an operator's lexical precedence in CREATE OPERATOR, because the parser's precedence behavior is hard-wired. it's not a phase, mom. see Section 4.1.6 for precedence details.

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 still lowkey there, just not doing much πŸ’€

use DROP OPERATOR to delete user-defined operators from a database. use ALTER OPERATOR to modify operators in a database. clean up after yourself, queen 🌈


⭐ 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 her go. ate and left no crumbs πŸ‘‘


🌈 COMPATIBILITY 🌈

CREATE OPERATOR is a PostgreSQL extension. there are no provisions for user-defined operators in the SQL standard. we're simply built different, fr fr πŸ’…


πŸ“ SEE ALSO πŸ“


and that's the page, bestie!! straight from the PostgreSQL 15.19 docs, dated 2026 β€” go forth and make operators that slay πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from