ALTER_OPERATOR_FAMILY(7)

โœจ ALTER OPERATOR FAMILY โœจ

okay bestie, welcome to the ALTER OPERATOR FAMILY page from the PostgreSQL 18.6 Documentation โ€” yes it's ALTER_OPERATOR_FAMILY in the index, same girl, different outfit ๐Ÿ’…. This is the one where you go in and redecorate an operator family: add stuff, remove stuff, rename it, hand it to someone else, move it to a new schema. Very glow-up, very hands-on. ๐ŸŽ€


๐ŸŒธ 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_ROLE | CURRENT_USER | SESSION_USER }

ALTER OPERATOR FAMILY name USING index_method
    SET SCHEMA new_schema

four whole forms, bestie, pick your fighter ๐ŸŒˆ


๐Ÿ’– DESCRIPTION ๐Ÿ’–

So ALTER OPERATOR FAMILY changes the definition of an operator family. That's the whole vibe. You can add operators and support functions to the family, remove them from the family, or change the family's name or owner. pick your little makeover ๐Ÿ’…

Now here's the part that actually matters, so listen up ๐Ÿซถ. When operators and support functions get added 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. Loose! Living their free little life โœจ. What that means: these operators and functions are compatible with the family's semantics, but they are not required for correct functioning of any specific index. If something is required like that, it should be declared as part of an operator class instead โ€” go check CREATE OPERATOR CLASS (CREATE_OPERATOR_CLASS(7)) for that whole situation. ๐Ÿ“š

And the rule that follows from all that: PostgreSQL will happily 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. no half-measures, periodt. 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 get to be the loose members of the family. iconic behaviour, honestly ๐Ÿ‘‘

๐Ÿšจ You must be a superuser to use ALTER OPERATOR FAMILY. And no, this isn't gatekeeping for fun โ€” the restriction is made because an erroneous operator family definition could confuse or even crash the server. ๐Ÿ’€ so like. be careful.

Also, real talk: ALTER OPERATOR FAMILY does not presently check whether the operator family definition includes all the operators and functions required by the index method, and it doesn't check whether the operators and functions form a self-consistent set either. It is the user's responsibility to define a valid operator family. that's you. you're the main character here. ๐ŸŒŸ

Refer to Section 36.16 for further information. ๐Ÿ“–


๐Ÿฆ‹ PARAMETERS ๐Ÿฆ‹

  • name โ€” the name (optionally schema-qualified) of an existing operator family. the family you're about to mess with ๐Ÿ’–
  • index_method โ€” the name of the index method this operator family is for.
  • strategy_number โ€” the index method's strategy number for an operator associated with the operator family.
  • operator_name โ€” the name (optionally schema-qualified) of an operator associated with the operator family.
  • op_type โ€” okay this one has layers, bestie, let's go one clause at a time ๐ŸŽ€
    • In an OPERATOR clause, this is the operand data type(s) of the operator, or NONE to signify a prefix operator. And unlike the comparable syntax in CREATE OPERATOR CLASS, the operand data types must always be specified. no shortcuts here, sorry.
    • 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. But 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. non-negotiable ๐Ÿ’…
  • sort_family_name โ€” the name (optionally schema-qualified) of an existing btree operator family that describes the sort ordering associated with an ordering operator.
    • And psst: if neither FOR SEARCH nor FOR ORDER BY is specified, FOR SEARCH is the default. โœจ
  • support_number โ€” the index method's support function number for a function associated with the operator family.
  • 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. ๐Ÿ‘€
  • argument_type โ€” the parameter data type(s) of the function.
  • new_name โ€” the new name of the operator family. rebranding era ๐Ÿ’…
  • new_owner โ€” the new owner of the operator family.
  • new_schema โ€” the new schema for the operator family.

And a lil note that lives down here with the parameters: the OPERATOR and FUNCTION clauses can appear in any order. go wild ๐ŸŒˆ


๐ŸŽ€ NOTES ๐ŸŽ€

Notice that the DROP syntax only specifies the "slot" in the operator family โ€” by strategy or support number and input data type(s). It does not mention the name of the operator or function occupying the slot. so you're dropping a position, not a name, bestie, don't get it twisted ๐Ÿ™ˆ. 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. yes, really. ๐Ÿ˜ญ

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 that's a whole thing. That said, it's usually not an issue for the sorts of functions that are useful in an operator family โ€” so like, breathe. ๐Ÿซถ

And one more: the operators should not be defined by SQL functions. An SQL function is likely to be inlined into the calling query, which will prevent the optimizer from recognizing that the query matches an index. bye bye index usage ๐Ÿ’€ so don't do that.


๐ŸŒˆ EXAMPLES ๐ŸŒˆ

Okay so here's the moment. 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. watch her work โœจ

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 โ€” the un-glow-up, the "we're so back to before" arc ๐Ÿฆ‹:

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) ;

see how the names vanish on the way out? slot only. told you. ๐Ÿ’…


โญ COMPATIBILITY โญ

There is no ALTER OPERATOR FAMILY statement in the SQL standard. she's built different, she's not in the standard, and we love her anyway ๐Ÿ’–


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

go read the whole family of pages, they're all in their little group chat ๐Ÿซถ


and that's the page!! this one is PostgreSQL 18.6, dated 2026, filed under ALTER OPERATOR FAMILY(7) โ€” tysm for reading, bestie, go define a valid operator family, you've got this ๐Ÿ’œโœจ๐ŸŒธ