ALTER_OPERATOR_FAMILY(7pg17)
okay so bestie, welcome to the iconic PostgreSQL 17.11 documentation for ALTER OPERATOR FAMILY(7) π
β¨ like this is the glow-up guide your operator families didn't know they needed. no cap, this page is lowkey essential if you're out here trying to add or remove operators and support functions from a family, or honestly just change its name or owner. it's giving database admin main character energy and we're here for it π«Ά
β¨ NAME β¨
ALTER OPERATOR FAMILY β change the definition of an operator family π
π¦ 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
πΈ DESCRIPTION πΈ
okay so ALTER OPERATOR FAMILY changes the definition of an operator family. think of it like a little wardrobe update for your database: you can add operators and support functions to the family, remove them from the family, or change the family's name or owner. it's honestly that simple π
when operators and support functions are added to a family with ALTER OPERATOR FAMILY, they are not part of any specific operator class within the family, but are just βlooseβ within the family. this indicates that these operators and functions are compatible with the family's semantics, but are not required for correct functioning of any specific index. π₯
no cap: operators and functions that are so required should be declared as part of an operator class, instead; seeCREATE OPERATOR CLASS(CREATE_OPERATOR_CLASS(7)). periodt.
PostgreSQL will allow loose members of a family to 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 are needed to support an index on that specific data type, while cross-data-type operators and functions are made loose members of the family π§
you must be a superuser to use ALTER OPERATOR FAMILY. (this restriction is made because an erroneous operator family definition could confuse or even crash the server π scary but true bestie.)
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. π
refer to Section 36.16 for further information β¨
π PARAMETERS π
nameβ the name (optionally schema-qualified) of an existing operator family. like, you need to know which family you're editing, bestie πindex_methodβ the name of the index method this operator family is for. the vibe check of index methods, if you will β¨strategy_numberβ the index method's strategy number for an operator associated with the operator family. it's giving numbered strategy realness.operator_nameβ the name (optionally schema-qualified) of an operator associated with the operator family. like, which operator are we adding or dropping? πop_typeβ this one's got layers, ngl. here's the breakdown:- in an
OPERATORclause, the operand data type(s) of the operator, orNONEto signify a prefix operator. unlike the comparable syntax inCREATE OPERATOR CLASS, the operand data types must always be specified. no skipping, bestie π - in an
ADD FUNCTIONclause, 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 specifyop_typesince 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. iykyk π¦ - in a
DROP FUNCTIONclause, the operand data type(s) the function is intended to support must be specified. no shortcuts here either π€
- in an
sort_family_nameβ the name (optionally schema-qualified) of an existing btree operator family that describes the sort ordering associated with an ordering operator. if neitherFOR SEARCHnorFOR ORDER BYis specified,FOR SEARCHis the default. periodt πsupport_numberβ the index method's support function number for a function associated with the operator family. like a little ID number for support functions π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. no duplicates allowed, bestie β¨argument_typeβ the parameter data type(s) of the function. the little type hints that keep everything slaying πΈnew_nameβ the new name of the operator family. glow-up name change πnew_ownerβ the new owner of the operator family. handing over the keys to the kingdom πnew_schemaβ the new schema for the operator family. moving it to a new neighborhood π‘
the OPERATOR and FUNCTION clauses can appear in any order. no pressure, do you boo π«Ά
π 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. like, it's giving "slot-based removal" energy and honestly it's a vibe π
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. this is usually not an issue for the sorts of functions that are useful in an operator family. but like, lowkey keep it in mind π
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. so don't do that bestie, it's not the move π€
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. we love an on-the-fly glow-up β¨
π₯ EXAMPLES π₯
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. slay.
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) ;
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) ;
π COMPATIBILITY π
there is no ALTER OPERATOR FAMILY statement in the SQL standard. so like, it's a PostgreSQL exclusive, and honestly that's iconic π
π 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))
and that's the whole vibe, bestie! this page is from PostgreSQL 17.11, dated 2026, and it's the ALTER OPERATOR FAMILY(7) manual page β now go forth and slay those operator families πβ¨ππ¦πΈπ
π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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