<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CREATE OPERATOR CLASS</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.0.11 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CREATE OPERATOR" HREF="sql-createoperator.html"><LINK REL="NEXT" TITLE="CREATE RULE" HREF="sql-createrule.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2007-02-02T03:57:22"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.0.11 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-createoperator.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-createoperator.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-createrule.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-createrule.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-CREATEOPCLASS" ></A >CREATE OPERATOR CLASS</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN39873" ></A ><H2 >Name</H2 >CREATE OPERATOR CLASS -- define a new operator class</DIV ><A NAME="AEN39876" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN39878" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CREATE OPERATOR CLASS <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ DEFAULT ] FOR TYPE <TT CLASS="REPLACEABLE" ><I >data_type</I ></TT > USING <TT CLASS="REPLACEABLE" ><I >index_method</I ></TT > AS { OPERATOR <TT CLASS="REPLACEABLE" ><I >strategy_number</I ></TT > <TT CLASS="REPLACEABLE" ><I >operator_name</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >op_type</I ></TT >, <TT CLASS="REPLACEABLE" ><I >op_type</I ></TT > ) ] [ RECHECK ] | FUNCTION <TT CLASS="REPLACEABLE" ><I >support_number</I ></TT > <TT CLASS="REPLACEABLE" ><I >funcname</I ></TT > ( <TT CLASS="REPLACEABLE" ><I >argument_type</I ></TT > [, ...] ) | STORAGE <TT CLASS="REPLACEABLE" ><I >storage_type</I ></TT > } [, ... ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN39891" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >CREATE OPERATOR CLASS</TT > creates a new operator class. An operator class defines how a particular data type can be used with an index. The operator class specifies that certain operators will fill particular roles or <SPAN CLASS="QUOTE" >"strategies"</SPAN > for this data type and this index method. The operator class also specifies the support procedures to be used by the index method when the operator class is selected for an index column. All the operators and functions used by an operator class must be defined before the operator class is created. </P ><P > If a schema name is given then the operator class is created in the specified schema. Otherwise it is created in the current schema. Two operator classes in the same schema can have the same name only if they are for different index methods. </P ><P > The user who defines an operator class becomes its owner. Presently, the creating user must be a superuser. (This restriction is made because an erroneous operator class definition could confuse or even crash the server.) </P ><P > <TT CLASS="COMMAND" >CREATE OPERATOR CLASS</TT > does not presently check whether the operator class definition includes all the operators and functions required by the index method. It is the user's responsibility to define a valid operator class. </P ><P > Refer to <A HREF="xindex.html" >Section 31.14</A > for further information. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN39902" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >name</I ></TT ></DT ><DD ><P > The name of the operator class to be created. The name may be schema-qualified. </P ></DD ><DT ><TT CLASS="LITERAL" >DEFAULT</TT ></DT ><DD ><P > If present, the operator class will become the default operator class for its data type. At most one operator class can be the default for a specific data type and index method. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >data_type</I ></TT ></DT ><DD ><P > The column data type that this operator class is for. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >index_method</I ></TT ></DT ><DD ><P > The name of the index method this operator class is for. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >strategy_number</I ></TT ></DT ><DD ><P > The index method's strategy number for an operator associated with the operator class. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >operator_name</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of an operator associated with the operator class. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >op_type</I ></TT ></DT ><DD ><P > The operand data type(s) of an operator, or <TT CLASS="LITERAL" >NONE</TT > to signify a left-unary or right-unary operator. The operand data types may be omitted in the normal case where they are the same as the operator class's data type. </P ></DD ><DT ><TT CLASS="LITERAL" >RECHECK</TT ></DT ><DD ><P > If present, the index is <SPAN CLASS="QUOTE" >"lossy"</SPAN > for this operator, and so the rows retrieved using the index must be rechecked to verify that they actually satisfy the qualification clause involving this operator. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >support_number</I ></TT ></DT ><DD ><P > The index method's support procedure number for a function associated with the operator class. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >funcname</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of a function that is an index method support procedure for the operator class. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >argument_types</I ></TT ></DT ><DD ><P > The parameter data type(s) of the function. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >storage_type</I ></TT ></DT ><DD ><P > The data type actually stored in the index. Normally this is the same as the column data type, but some index methods (only GiST at this writing) allow it to be different. The <TT CLASS="LITERAL" >STORAGE</TT > clause must be omitted unless the index method allows a different type to be used. </P ></DD ></DL ></DIV ><P > The <TT CLASS="LITERAL" >OPERATOR</TT >, <TT CLASS="LITERAL" >FUNCTION</TT >, and <TT CLASS="LITERAL" >STORAGE</TT > clauses may appear in any order. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN39972" ></A ><H2 >Notes</H2 ><P > 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. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN39975" ></A ><H2 >Examples</H2 ><P > The following example command defines a GiST index operator class for the data type <TT CLASS="LITERAL" >_int4</TT > (array of <TT CLASS="TYPE" >int4</TT >). See <TT CLASS="FILENAME" >contrib/intarray/</TT > for the complete example. </P ><PRE CLASS="PROGRAMLISTING" >CREATE OPERATOR CLASS gist__int_ops DEFAULT FOR TYPE _int4 USING gist AS OPERATOR 3 &&, OPERATOR 6 = RECHECK, OPERATOR 7 @, OPERATOR 8 ~, OPERATOR 20 @@ (_int4, query_int), FUNCTION 1 g_int_consistent (internal, _int4, int4), FUNCTION 2 g_int_union (bytea, internal), FUNCTION 3 g_int_compress (internal), FUNCTION 4 g_int_decompress (internal), FUNCTION 5 g_int_penalty (internal, internal, internal), FUNCTION 6 g_int_picksplit (internal, internal), FUNCTION 7 g_int_same (_int4, _int4, internal);</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN39982" ></A ><H2 >Compatibility</H2 ><P > <TT CLASS="COMMAND" >CREATE OPERATOR CLASS</TT > is a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extension. There is no <TT CLASS="COMMAND" >CREATE OPERATOR CLASS</TT > statement in the SQL standard. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN39988" ></A ><H2 >See Also</H2 ><A HREF="sql-alteropclass.html" ><I >ALTER OPERATOR CLASS</I ></A >, <A HREF="sql-dropopclass.html" ><I >DROP OPERATOR CLASS</I ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-createoperator.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-createrule.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >CREATE OPERATOR</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >CREATE RULE</TD ></TR ></TABLE ></DIV ></BODY ></HTML >