ALTER_OPERATOR_FAMILY(7pg13)

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

✨ ALTER OPERATOR FAMILY ✨

okay so bestie, this is the ALTER OPERATOR FAMILY page from the PostgreSQL 13.23 docs and it's literally about changing the definition of an operator family. i'm gonna walk you through it, no cap, every single detail intact 🫢


🌸 NAME 🌸

ALTER OPERATOR_FAMILY β€” change the definition of an operator family. that's it, that's the vibe πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

ALTER OPERATOR FAMILY name USING index_method ADD
  {  OPERATOR strategy_number operator_name ( op_type, op_type )
              [ FOR SEARCH | FOR ORDER BY sort_family_name ]
   | FUNCTION support_number [ ( op_type [ , op_type ] ) ]
              function_name [ ( argument_type [, ...] ) ]
  } [, ... ]

ALTER OPERATOR FAMILY name USING index_method DROP
  {  OPERATOR strategy_number ( op_type [ , op_type ] )
   | FUNCTION support_number ( op_type [ , op_type ] )
  } [, ... ]

ALTER OPERATOR FAMILY name USING index_method
    RENAME TO new_name

ALTER OPERATOR FAMILY name USING index_method
    OWNER TO { new_owner | CURRENT_USER | SESSION_USER }

ALTER OPERATOR FAMILY name USING index_method
    SET SCHEMA new_schema

like, there are five whole moves you can make here: ADD, DROP, RENAME TO, OWNER TO, and SET SCHEMA. pick your fighter πŸŽ€


πŸ’– DESCRIPTION πŸ’–

so ALTER OPERATOR FAMILY changes the definition of an operator family. you can add operators and support functions to the family, remove them from the family, or change the family's name or owner. iconic, flexible, we love to see it ✨

when you add operators and support functions to a family with ALTER OPERATOR FAMILY, they are not part of any specific operator class within the family β€” they're just β€œloose” within the family. what does that mean bestie? it means these operators and functions are compatible with the family's semantics, but they're not required for correct functioning of any specific index. (operators and functions that are required like that should be declared as part of an operator class instead β€” see CREATE OPERATOR CLASS (CREATE_OPERATOR_CLASS(7)).) PostgreSQL will let loose members of a family be dropped from the family at any time, but members of an operator class cannot be dropped without dropping the whole class and any indexes that depend on it. typically, single-data-type operators and functions are part of operator classes because they're needed to support an index on that specific data type, while cross-data-type operators and functions are made loose members of the family. it's giving organizational hierarchy realness 🌈

⚠️ you must be a superuser to use ALTER OPERATOR FAMILY. (this restriction exists because an erroneous operator family definition could confuse or even crash the server. no pressure tho πŸ’€)

ALTER OPERATOR FAMILY does not presently check whether the operator family definition includes all the operators and functions required by the index method, nor whether the operators and functions form a self-consistent set. it is the user's responsibility to define a valid operator family. so like, you're the main character here, and main characters have responsibilities πŸŽ€

refer to Section 37.16 for further information.


🌟 PARAMETERS 🌟

okay here's where we meet the whole cast of characters:

  • name β€” the name (optionally schema-qualified) of an existing operator family. the one you're altering, duh πŸ’…
  • index_method β€” the name of the index method this operator family is for. like which flavor of index we're working with ✨
  • strategy_number β€” the index method's strategy number for an operator associated with the operator family. a number, a vibe, a whole identifier πŸ”’
  • operator_name β€” the name (optionally schema-qualified) of an operator associated with the operator family. the operator in question, bestie 🫢
  • op_type β€” okay this one's got layers, keep up:
    • in an OPERATOR clause, it's the operand data type(s) of the operator, or NONE to signify a left-unary or right-unary operator. unlike the comparable syntax in CREATE OPERATOR CLASS, the operand data types must always be specified. so like, no skipping, periodt πŸ’€
    • in an ADD FUNCTION clause, it's the operand data type(s) the function is intended to support, if different from the input data type(s) of the function. for B-tree comparison functions and hash functions it is not necessary to specify op_type since the function's input data type(s) are always the correct ones to use. for B-tree sort support functions, B-Tree equal image functions, and all functions in GiST, SP-GiST and GIN operator classes, it is necessary to specify the operand data type(s) the function is to be used with.
    • in a DROP FUNCTION clause, the operand data type(s) the function is intended to support must be specified. no exceptions πŸŽ€
  • sort_family_name β€” the name (optionally schema-qualified) of an existing btree operator family that describes the sort ordering associated with an ordering operator. think of it as the sorting lore πŸ¦‹
if neither FOR SEARCH nor FOR ORDER BY is specified, FOR SEARCH is the default. good to know, bestie ✨
  • support_number β€” the index method's support function number for a function associated with the operator family. the number that ties the function to its slot πŸ’…
  • function_name β€” the name (optionally schema-qualified) of a function that is an index method support function for the operator family. if no argument list is specified, the name must be unique in its schema. iykyk πŸ”₯
  • argument_type β€” the parameter data type(s) of the function. the function's whole type situation 🌸
  • new_name β€” the new name of the operator family. rebranding era πŸ‘‘
  • new_owner β€” the new owner of the operator family. passing the crown 🫢
  • new_schema β€” the new schema for the operator family. moving neighborhoods 🏠

also quick note: the OPERATOR and FUNCTION clauses can appear in any order. freedom of expression, we stan πŸ’œ


πŸŽ€ NOTES πŸŽ€

notice that the DROP syntax only specifies the β€œslot” in the operator family, by strategy or support number and input data type(s). the name of the operator or function occupying the slot is not mentioned. also, for DROP FUNCTION the type(s) to specify are the input data type(s) the function is intended to support β€” for GiST, SP-GiST and GIN indexes this might have nothing to do with the actual input argument types of the function. it's giving chaotic but intentional πŸ’€

because the index machinery does not check access permissions on functions before using them, including a function or operator in an operator family is tantamount to granting public execute permission on it. so like, security implications bestie. this is usually not an issue for the sorts of functions that are useful in an operator family, but still, be aware 🫢

the operators should not be defined by SQL functions. a SQL function is likely to be inlined into the calling query, which will prevent the optimizer from recognizing that the query matches an index. so yeah, that's a no-no, ngl πŸ’…

before PostgreSQL 8.4, the OPERATOR clause could include a RECHECK option. this is no longer supported because whether an index operator is β€œlossy” is now determined on-the-fly at run time. this allows efficient handling of cases where an operator might or might not be lossy. evolution, we love to see it ✨


πŸ”₯ EXAMPLES πŸ”₯

okay here's a real one, bestie. the following example command adds cross-data-type operators and support functions to an operator family that already contains B-tree operator classes for data types int4 and int2.

ALTER OPERATOR FAMILY integer_ops USING btree ADD

  -- int4 vs int2
  OPERATOR 1 < (int4, int2) ,
  OPERATOR 2 <= (int4, int2) ,
  OPERATOR 3 = (int4, int2) ,
  OPERATOR 4 >= (int4, int2) ,
  OPERATOR 5 > (int4, int2) ,
  FUNCTION 1 btint42cmp(int4, int2) ,

  -- int2 vs int4
  OPERATOR 1 < (int2, int4) ,
  OPERATOR 2 <= (int2, int4) ,
  OPERATOR 3 = (int2, int4) ,
  OPERATOR 4 >= (int2, int4) ,
  OPERATOR 5 > (int2, int4) ,
  FUNCTION 1 btint24cmp(int2, int4) ;

and to remove these entries again:

ALTER OPERATOR FAMILY integer_ops USING btree DROP

  -- int4 vs int2
  OPERATOR 1 (int4, int2) ,
  OPERATOR 2 (int4, int2) ,
  OPERATOR 3 (int4, int2) ,
  OPERATOR 4 (int4, int2) ,
  OPERATOR 5 (int4, int2) ,
  FUNCTION 1 (int4, int2) ,

  -- int2 vs int4
  OPERATOR 1 (int2, int4) ,
  OPERATOR 2 (int2, int4) ,
  OPERATOR 3 (int2, int4) ,
  OPERATOR 4 (int2, int4) ,
  OPERATOR 5 (int2, int4) ,
  FUNCTION 1 (int2, int4) ;

look at that symmetry, it's literally art 🎨✨


🌈 COMPATIBILITY 🌈

there is no ALTER OPERATOR FAMILY statement in the SQL standard. so like, this is a PostgreSQL thing, not a standard thing. periodt πŸ’…


⭐ SEE ALSO ⭐

CREATE OPERATOR FAMILY (CREATE_OPERATOR_FAMILY(7)), DROP OPERATOR FAMILY (DROP_OPERATOR_FAMILY(7)), CREATE OPERATOR CLASS (CREATE_OPERATOR_CLASS(7)), ALTER OPERATOR CLASS (ALTER_OPERATOR_CLASS(7)), DROP OPERATOR CLASS (DROP_OPERATOR_CLASS(7))

go read those too bestie, they're all connected, it's a whole universe πŸ¦‹πŸŒŒ


and that's the whole page, bestie! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά this has been PostgreSQL 13.23, dated 2025, of the ALTER OPERATOR FAMILY(7) manual. now go forth and alter those operator families responsibly, queen πŸ‘‘πŸŒˆβ­

πŸ“¦ where this page comes from