1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2010-09-20 22:08:53 +02:00
|
|
|
doc/src/sgml/ref/create_operator.sgml
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refentry id="SQL-CREATEOPERATOR">
|
|
|
|
<refmeta>
|
2010-04-03 09:23:02 +02:00
|
|
|
<refentrytitle>CREATE OPERATOR</refentrytitle>
|
2008-11-14 11:22:48 +01:00
|
|
|
<manvolnum>7</manvolnum>
|
1999-07-06 19:16:42 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refnamediv>
|
2003-04-22 12:08:08 +02:00
|
|
|
<refname>CREATE OPERATOR</refname>
|
|
|
|
<refpurpose>define a new operator</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
2003-08-31 19:32:24 +02:00
|
|
|
<indexterm zone="sql-createoperator">
|
|
|
|
<primary>CREATE OPERATOR</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refsynopsisdiv>
|
2003-04-22 12:08:08 +02:00
|
|
|
<synopsis>
|
|
|
|
CREATE OPERATOR <replaceable>name</replaceable> (
|
2009-09-19 12:23:27 +02:00
|
|
|
PROCEDURE = <replaceable class="parameter">function_name</replaceable>
|
|
|
|
[, LEFTARG = <replaceable class="parameter">left_type</replaceable> ] [, RIGHTARG = <replaceable class="parameter">right_type</replaceable> ]
|
2003-04-22 12:08:08 +02:00
|
|
|
[, COMMUTATOR = <replaceable class="parameter">com_op</replaceable> ] [, NEGATOR = <replaceable class="parameter">neg_op</replaceable> ]
|
|
|
|
[, RESTRICT = <replaceable class="parameter">res_proc</replaceable> ] [, JOIN = <replaceable class="parameter">join_proc</replaceable> ]
|
|
|
|
[, HASHES ] [, MERGES ]
|
|
|
|
)
|
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE OPERATOR</command> defines a new operator,
|
|
|
|
<replaceable class="parameter">name</replaceable>. The user who
|
|
|
|
defines an operator becomes its owner. If a schema name is given
|
|
|
|
then the operator is created in the specified schema. Otherwise it
|
|
|
|
is created in the current schema.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The operator name is a sequence of up to <symbol>NAMEDATALEN</>-1
|
|
|
|
(63 by default) characters from the following list:
|
|
|
|
<literallayout>
|
2003-08-18 00:09:00 +02:00
|
|
|
+ - * / < > = ~ ! @ # % ^ & | ` ?
|
2003-04-22 12:08:08 +02:00
|
|
|
</literallayout>
|
|
|
|
|
|
|
|
There are a few restrictions on your choice of name:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
|
|
|
|
since they will be taken as the start of a comment.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A multicharacter operator name cannot end in <literal>+</literal> or
|
|
|
|
<literal>-</literal>,
|
|
|
|
unless the name also contains at least one of these characters:
|
|
|
|
<literallayout>
|
2003-08-18 00:09:00 +02:00
|
|
|
~ ! @ # % ^ & | ` ?
|
2003-04-22 12:08:08 +02:00
|
|
|
</literallayout>
|
|
|
|
For example, <literal>@-</literal> is an allowed operator name,
|
|
|
|
but <literal>*-</literal> is not.
|
|
|
|
This restriction allows <productname>PostgreSQL</productname> to
|
|
|
|
parse SQL-compliant commands without requiring spaces between tokens.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
2010-06-22 13:36:16 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The use of <literal>=></> as an operator name is deprecated. It may
|
|
|
|
be disallowed altogether in a future release.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
2003-04-22 12:08:08 +02:00
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The operator <literal>!=</literal> is mapped to
|
|
|
|
<literal><></literal> on input, so these two names are always
|
|
|
|
equivalent.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
At least one of <literal>LEFTARG</> and <literal>RIGHTARG</> must be defined. For
|
|
|
|
binary operators, both must be defined. For right unary
|
|
|
|
operators, only <literal>LEFTARG</> should be defined, while for left
|
|
|
|
unary operators only <literal>RIGHTARG</> should be defined.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2009-09-19 12:23:27 +02:00
|
|
|
The <replaceable class="parameter">function_name</replaceable>
|
2003-04-22 12:08:08 +02:00
|
|
|
procedure must have been previously defined using <command>CREATE
|
|
|
|
FUNCTION</command> and must be defined to accept the correct number
|
|
|
|
of arguments (either one or two) of the indicated types.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The other clauses specify optional operator optimization clauses.
|
2005-01-04 01:39:53 +01:00
|
|
|
Their meaning is detailed in <xref linkend="xoper-optimization">.
|
2003-04-22 12:08:08 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The name of the operator to be defined. See above for allowable
|
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
|
|
|
characters. The name can be schema-qualified, for example
|
2004-06-25 23:55:59 +02:00
|
|
|
<literal>CREATE OPERATOR myschema.+ (...)</>. If not, then
|
|
|
|
the operator is created in the current schema. Two operators
|
|
|
|
in the same schema can have the same name if they operate on
|
|
|
|
different data types. This is called
|
|
|
|
<firstterm>overloading</>.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
2009-09-19 12:23:27 +02:00
|
|
|
<term><replaceable class="parameter">function_name</replaceable></term>
|
1999-07-06 19:16:42 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The function used to implement this operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
2009-09-19 12:23:27 +02:00
|
|
|
<term><replaceable class="parameter">left_type</replaceable></term>
|
1999-07-06 19:16:42 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The data type of the operator's left operand, if any.
|
|
|
|
This option would be omitted for a left-unary operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
2009-09-19 12:23:27 +02:00
|
|
|
<term><replaceable class="parameter">right_type</replaceable></term>
|
1999-07-06 19:16:42 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The data type of the operator's right operand, if any.
|
|
|
|
This option would be omitted for a right-unary operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">com_op</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The commutator of this operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">neg_op</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The negator of this operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">res_proc</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The restriction selectivity estimator function for this operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">join_proc</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-25 23:55:59 +02:00
|
|
|
The join selectivity estimator function for this operator.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
<term><literal>HASHES</literal></term>
|
1999-07-06 19:16:42 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2000-03-18 19:03:12 +01:00
|
|
|
Indicates this operator can support a hash join.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
2002-04-17 01:08:12 +02:00
|
|
|
<varlistentry>
|
2003-04-22 12:08:08 +02:00
|
|
|
<term><literal>MERGES</literal></term>
|
2002-04-17 01:08:12 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Indicates this operator can support a merge join.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
1999-07-06 19:16:42 +02:00
|
|
|
</variablelist>
|
|
|
|
|
2002-04-23 04:07:16 +02:00
|
|
|
<para>
|
2003-04-22 12:08:08 +02:00
|
|
|
To give a schema-qualified operator name in <replaceable
|
|
|
|
class="parameter">com_op</replaceable> or the other optional
|
2007-02-01 01:28:19 +01:00
|
|
|
arguments, use the <literal>OPERATOR()</> syntax, for example:
|
2003-04-22 12:08:08 +02:00
|
|
|
<programlisting>
|
|
|
|
COMMUTATOR = OPERATOR(myschema.===) ,
|
2010-11-23 21:27:50 +01:00
|
|
|
</programlisting>
|
2002-04-23 04:07:16 +02:00
|
|
|
</para>
|
2003-04-22 12:08:08 +02:00
|
|
|
</refsect1>
|
2010-11-23 21:27:50 +01:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
2000-03-18 19:03:12 +01:00
|
|
|
|
1998-07-14 05:47:34 +02:00
|
|
|
<para>
|
2003-04-22 12:08:08 +02:00
|
|
|
Refer to <xref linkend="xoper"> for further information.
|
1998-07-14 05:47:34 +02:00
|
|
|
</para>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
2010-07-08 18:30:13 +02:00
|
|
|
<para>
|
|
|
|
It is not possible to specify an operator's lexical precedence in
|
|
|
|
<command>CREATE OPERATOR</>, because the parser's precedence behavior
|
|
|
|
is hard-wired. See <xref linkend="sql-precedence"> for precedence details.
|
|
|
|
</para>
|
|
|
|
|
2006-12-23 01:43:13 +01:00
|
|
|
<para>
|
|
|
|
The obsolete options <literal>SORT1</>, <literal>SORT2</>,
|
|
|
|
<literal>LTCMP</>, and <literal>GTCMP</> were formerly used to
|
2007-11-28 16:42:31 +01:00
|
|
|
specify the names of sort operators associated with a merge-joinable
|
2006-12-23 01:43:13 +01:00
|
|
|
operator. This is no longer necessary, since information about
|
2007-11-28 16:42:31 +01:00
|
|
|
associated operators is found by looking at B-tree operator families
|
2006-12-23 01:43:13 +01:00
|
|
|
instead. If one of these options is given, it is ignored except
|
|
|
|
for implicitly setting <literal>MERGES</> true.
|
|
|
|
</para>
|
|
|
|
|
1998-07-14 05:47:34 +02:00
|
|
|
<para>
|
2010-04-03 09:23:02 +02:00
|
|
|
Use <xref linkend="sql-dropoperator"> to delete user-defined operators
|
|
|
|
from a database. Use <xref linkend="sql-alteroperator"> to modify operators in a
|
2004-09-20 02:04:19 +02:00
|
|
|
database.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refsect1>
|
2010-11-23 21:27:50 +01:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The following command defines a new operator, area-equality, for
|
|
|
|
the data type <type>box</type>:
|
|
|
|
<programlisting>
|
1999-07-06 19:16:42 +02:00
|
|
|
CREATE OPERATOR === (
|
2003-04-22 12:08:08 +02:00
|
|
|
LEFTARG = box,
|
|
|
|
RIGHTARG = box,
|
|
|
|
PROCEDURE = area_equal_procedure,
|
|
|
|
COMMUTATOR = ===,
|
|
|
|
NEGATOR = !==,
|
|
|
|
RESTRICT = area_restriction_procedure,
|
|
|
|
JOIN = area_join_procedure,
|
2006-12-23 01:43:13 +01:00
|
|
|
HASHES, MERGES
|
1999-07-06 19:16:42 +02:00
|
|
|
);
|
2010-11-23 21:27:50 +01:00
|
|
|
</programlisting>
|
2003-04-22 12:08:08 +02:00
|
|
|
</para>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refsect1>
|
2010-11-23 21:27:50 +01:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE OPERATOR</command> is a
|
|
|
|
<productname>PostgreSQL</productname> extension. There are no
|
|
|
|
provisions for user-defined operators in the SQL standard.
|
|
|
|
</para>
|
1998-07-14 05:47:34 +02:00
|
|
|
</refsect1>
|
2005-01-04 01:39:53 +01:00
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<simplelist type="inline">
|
2010-04-03 09:23:02 +02:00
|
|
|
<member><xref linkend="sql-alteroperator"></member>
|
|
|
|
<member><xref linkend="sql-createopclass"></member>
|
|
|
|
<member><xref linkend="sql-dropoperator"></member>
|
2005-01-04 01:39:53 +01:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refentry>
|