CREATE_OPERATOR(7pg14)

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

✨ 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 have USAGE privilege on the argument types and the return type, as well as EXECUTE privilege 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 example CREATE 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 🫢


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