postgresql/doc/src/sgml/ref/create_operator.sgml

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

300 lines
9.2 KiB
Plaintext
Raw Normal View History

<!--
2010-09-20 22:08:53 +02:00
doc/src/sgml/ref/create_operator.sgml
PostgreSQL documentation
-->
<refentry id="sql-createoperator">
<indexterm zone="sql-createoperator">
<primary>CREATE OPERATOR</primary>
</indexterm>
<refmeta>
<refentrytitle>CREATE OPERATOR</refentrytitle>
<manvolnum>7</manvolnum>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
2003-04-22 12:08:08 +02:00
<refnamediv>
2003-04-22 12:08:08 +02:00
<refname>CREATE OPERATOR</refname>
<refpurpose>define a new operator</refpurpose>
</refnamediv>
<refsynopsisdiv>
2003-04-22 12:08:08 +02:00
<synopsis>
CREATE OPERATOR <replaceable>name</replaceable> (
{FUNCTION|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</symbol>-1
2003-04-22 12:08:08 +02:00
(63 by default) characters from the following list:
<literallayout>
+ - * / &lt; &gt; = ~ ! @ # % ^ &amp; | ` ?
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,
2003-04-22 12:08:08 +02:00
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>
~ ! @ # % ^ &amp; | ` ?
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>
<listitem>
<para>
The use of <literal>=&gt;</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>&lt;&gt;</literal> on input, so these two names are always
equivalent.
</para>
<para>
For binary operators, both <literal>LEFTARG</literal> and
<literal>RIGHTARG</literal> must be defined. For prefix operators only
<literal>RIGHTARG</literal> should be defined.
The <replaceable class="parameter">function_name</replaceable>
function must have been previously defined using <command>CREATE
2003-04-22 12:08:08 +02:00
FUNCTION</command> and must be defined to accept the correct number
of arguments (either one or two) of the indicated types.
</para>
<para>
In the syntax of <literal>CREATE OPERATOR</literal>, the keywords
<literal>FUNCTION</literal> and <literal>PROCEDURE</literal> are
equivalent, but the referenced function must in any case be a function, not
a procedure. The use of the keyword <literal>PROCEDURE</literal> here is
historical and deprecated.
</para>
2003-04-22 12:08:08 +02:00
<para>
The other clauses specify optional operator optimization clauses.
Their meaning is detailed in <xref linkend="xoper-optimization"/>.
2003-04-22 12:08:08 +02:00
</para>
<para>
To be able to create an operator, you must have <literal>USAGE</literal>
privilege on the argument types and the return type, as well
as <literal>EXECUTE</literal> privilege on the underlying function. If a
commutator or negator operator is specified, you must own these operators.
</para>
2003-04-22 12:08:08 +02:00
</refsect1>
<refsect1>
<title>Parameters</title>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">name</replaceable></term>
<listitem>
<para>
The name of the operator to be defined. See above for allowable
characters. The name can be schema-qualified, for example
<literal>CREATE OPERATOR myschema.+ (...)</literal>. 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</firstterm>.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">function_name</replaceable></term>
<listitem>
<para>
The function used to implement this operator.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">left_type</replaceable></term>
<listitem>
<para>
The data type of the operator's left operand, if any.
This option would be omitted for a prefix operator.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">right_type</replaceable></term>
<listitem>
<para>
The data type of the operator's right operand.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">com_op</replaceable></term>
<listitem>
<para>
The commutator of this operator.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">neg_op</replaceable></term>
<listitem>
<para>
The negator of this operator.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">res_proc</replaceable></term>
<listitem>
<para>
The restriction selectivity estimator function for this operator.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
<term><replaceable class="parameter">join_proc</replaceable></term>
<listitem>
<para>
The join selectivity estimator function for this operator.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
2003-04-22 12:08:08 +02:00
<term><literal>HASHES</literal></term>
<listitem>
<para>
Indicates this operator can support a hash join.
</para>
</listitem>
</varlistentry>
2003-04-22 12:08:08 +02:00
<varlistentry>
2003-04-22 12:08:08 +02:00
<term><literal>MERGES</literal></term>
<listitem>
<para>
Indicates this operator can support a merge join.
</para>
</listitem>
</varlistentry>
</variablelist>
<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
arguments, use the <literal>OPERATOR()</literal> syntax, for example:
2003-04-22 12:08:08 +02:00
<programlisting>
COMMUTATOR = OPERATOR(myschema.===) ,
</programlisting></para>
2003-04-22 12:08:08 +02:00
</refsect1>
2003-04-22 12:08:08 +02:00
<refsect1>
<title>Notes</title>
<para>
Refer to <xref linkend="xoper"/> for further information.
</para>
2003-04-22 12:08:08 +02:00
<para>
It is not possible to specify an operator's lexical precedence in
<command>CREATE OPERATOR</command>, because the parser's precedence behavior
is hard-wired. See <xref linkend="sql-precedence"/> for precedence details.
</para>
<para>
The obsolete options <literal>SORT1</literal>, <literal>SORT2</literal>,
<literal>LTCMP</literal>, and <literal>GTCMP</literal> were formerly used to
2007-11-28 16:42:31 +01:00
specify the names of sort operators associated with a merge-joinable
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
instead. If one of these options is given, it is ignored except
for implicitly setting <literal>MERGES</literal> true.
</para>
<para>
Improve <xref> vs. <command> formatting in the documentation SQL commands are generally marked up as <command>, except when a link to a reference page is used using <xref>. But the latter doesn't create monospace markup, so this looks strange especially when a paragraph contains a mix of links and non-links. We considered putting <command> in the <refentrytitle> on the target side, but that creates some formatting side effects elsewhere. Generally, it seems safer to solve this on the link source side. We can't put the <xref> inside the <command>; the DTD doesn't allow this. DocBook 5 would allow the <command> to have the linkend attribute itself, but we are not there yet. So to solve this for now, convert the <xref>s to <link> plus <command>. This gives the correct look and also gives some more flexibility what we can put into the link text (e.g., subcommands or other clauses). In the future, these could then be converted to DocBook 5 style. I haven't converted absolutely all xrefs to SQL command reference pages, only those where we care about the appearance of the link text or where it was otherwise appropriate to make the appearance match a bit better. Also in some cases, the links where repetitive, so in those cases the links where just removed and replaced by a plain <command>. In cases where we just want the link and don't specifically care about the generated link text (typically phrased "for further information see <xref ...>") the xref is kept. Reported-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> Discussion: https://www.postgresql.org/message-id/flat/87o8pco34z.fsf@wibble.ilmari.org
2020-10-03 16:16:51 +02:00
Use <link linkend="sql-dropoperator"><command>DROP OPERATOR</command></link> to delete user-defined operators
from a database. Use <link linkend="sql-alteroperator"><command>ALTER OPERATOR</command></link> to modify operators in a
database.
</para>
</refsect1>
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>
CREATE OPERATOR === (
2003-04-22 12:08:08 +02:00
LEFTARG = box,
RIGHTARG = box,
FUNCTION = area_equal_function,
2003-04-22 12:08:08 +02:00
COMMUTATOR = ===,
NEGATOR = !==,
RESTRICT = area_restriction_function,
JOIN = area_join_function,
HASHES, MERGES
);
</programlisting></para>
</refsect1>
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>
</refsect1>
2005-01-04 01:39:53 +01:00
<refsect1>
<title>See Also</title>
<simplelist type="inline">
<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>
</refentry>