gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
11KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE OPERATOR</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-creatematerializedview.html" title="CREATE MATERIALIZED VIEW" /><link rel="next" href="sql-createopclass.html" title="CREATE OPERATOR CLASS" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE OPERATOR</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-creatematerializedview.html" title="CREATE MATERIALIZED VIEW">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createopclass.html" title="CREATE OPERATOR CLASS">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEOPERATOR"><div class="titlepage"></div><a id="id-1.9.3.72.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE OPERATOR</span></h2><p>CREATE OPERATOR — define a new operator</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE OPERATOR <em class="replaceable"><code>name</code></em> (
  4. {FUNCTION|PROCEDURE} = <em class="replaceable"><code>function_name</code></em>
  5. [, LEFTARG = <em class="replaceable"><code>left_type</code></em> ] [, RIGHTARG = <em class="replaceable"><code>right_type</code></em> ]
  6. [, COMMUTATOR = <em class="replaceable"><code>com_op</code></em> ] [, NEGATOR = <em class="replaceable"><code>neg_op</code></em> ]
  7. [, RESTRICT = <em class="replaceable"><code>res_proc</code></em> ] [, JOIN = <em class="replaceable"><code>join_proc</code></em> ]
  8. [, HASHES ] [, MERGES ]
  9. )
  10. </pre></div><div class="refsect1" id="id-1.9.3.72.5"><h2>Description</h2><p>
  11. <code class="command">CREATE OPERATOR</code> defines a new operator,
  12. <em class="replaceable"><code>name</code></em>. The user who
  13. defines an operator becomes its owner. If a schema name is given
  14. then the operator is created in the specified schema. Otherwise it
  15. is created in the current schema.
  16. </p><p>
  17. The operator name is a sequence of up to <code class="symbol">NAMEDATALEN</code>-1
  18. (63 by default) characters from the following list:
  19. </p><div class="literallayout"><p><br />
  20. + - * / &lt; &gt; = ~ ! @ # % ^ &amp; | ` ?<br />
  21. </p></div><p>
  22. There are a few restrictions on your choice of name:
  23. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><code class="literal">--</code> and <code class="literal">/*</code> cannot appear anywhere in an operator name,
  24. since they will be taken as the start of a comment.
  25. </p></li><li class="listitem"><p>
  26. A multicharacter operator name cannot end in <code class="literal">+</code> or
  27. <code class="literal">-</code>,
  28. unless the name also contains at least one of these characters:
  29. </p><div class="literallayout"><p><br />
  30. ~ ! @ # % ^ &amp; | ` ?<br />
  31. </p></div><p>
  32. For example, <code class="literal">@-</code> is an allowed operator name,
  33. but <code class="literal">*-</code> is not.
  34. This restriction allows <span class="productname">PostgreSQL</span> to
  35. parse SQL-compliant commands without requiring spaces between tokens.
  36. </p></li><li class="listitem"><p>
  37. The use of <code class="literal">=&gt;</code> as an operator name is deprecated. It may
  38. be disallowed altogether in a future release.
  39. </p></li></ul></div><p>
  40. </p><p>
  41. The operator <code class="literal">!=</code> is mapped to
  42. <code class="literal">&lt;&gt;</code> on input, so these two names are always
  43. equivalent.
  44. </p><p>
  45. At least one of <code class="literal">LEFTARG</code> and <code class="literal">RIGHTARG</code> must be defined. For
  46. binary operators, both must be defined. For right unary
  47. operators, only <code class="literal">LEFTARG</code> should be defined, while for left
  48. unary operators only <code class="literal">RIGHTARG</code> should be defined.
  49. </p><p>
  50. The <em class="replaceable"><code>function_name</code></em>
  51. function must have been previously defined using <code class="command">CREATE
  52. FUNCTION</code> and must be defined to accept the correct number
  53. of arguments (either one or two) of the indicated types.
  54. </p><p>
  55. In the syntax of <code class="literal">CREATE OPERATOR</code>, the keywords
  56. <code class="literal">FUNCTION</code> and <code class="literal">PROCEDURE</code> are
  57. equivalent, but the referenced function must in any case be a function, not
  58. a procedure. The use of the keyword <code class="literal">PROCEDURE</code> here is
  59. historical and deprecated.
  60. </p><p>
  61. The other clauses specify optional operator optimization clauses.
  62. Their meaning is detailed in <a class="xref" href="xoper-optimization.html" title="37.15. Operator Optimization Information">Section 37.15</a>.
  63. </p><p>
  64. To be able to create an operator, you must have <code class="literal">USAGE</code>
  65. privilege on the argument types and the return type, as well
  66. as <code class="literal">EXECUTE</code> privilege on the underlying function. If a
  67. commutator or negator operator is specified, you must own these operators.
  68. </p></div><div class="refsect1" id="id-1.9.3.72.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  69. The name of the operator to be defined. See above for allowable
  70. characters. The name can be schema-qualified, for example
  71. <code class="literal">CREATE OPERATOR myschema.+ (...)</code>. If not, then
  72. the operator is created in the current schema. Two operators
  73. in the same schema can have the same name if they operate on
  74. different data types. This is called
  75. <em class="firstterm">overloading</em>.
  76. </p></dd><dt><span class="term"><em class="replaceable"><code>function_name</code></em></span></dt><dd><p>
  77. The function used to implement this operator.
  78. </p></dd><dt><span class="term"><em class="replaceable"><code>left_type</code></em></span></dt><dd><p>
  79. The data type of the operator's left operand, if any.
  80. This option would be omitted for a left-unary operator.
  81. </p></dd><dt><span class="term"><em class="replaceable"><code>right_type</code></em></span></dt><dd><p>
  82. The data type of the operator's right operand, if any.
  83. This option would be omitted for a right-unary operator.
  84. </p></dd><dt><span class="term"><em class="replaceable"><code>com_op</code></em></span></dt><dd><p>
  85. The commutator of this operator.
  86. </p></dd><dt><span class="term"><em class="replaceable"><code>neg_op</code></em></span></dt><dd><p>
  87. The negator of this operator.
  88. </p></dd><dt><span class="term"><em class="replaceable"><code>res_proc</code></em></span></dt><dd><p>
  89. The restriction selectivity estimator function for this operator.
  90. </p></dd><dt><span class="term"><em class="replaceable"><code>join_proc</code></em></span></dt><dd><p>
  91. The join selectivity estimator function for this operator.
  92. </p></dd><dt><span class="term"><code class="literal">HASHES</code></span></dt><dd><p>
  93. Indicates this operator can support a hash join.
  94. </p></dd><dt><span class="term"><code class="literal">MERGES</code></span></dt><dd><p>
  95. Indicates this operator can support a merge join.
  96. </p></dd></dl></div><p>
  97. To give a schema-qualified operator name in <em class="replaceable"><code>com_op</code></em> or the other optional
  98. arguments, use the <code class="literal">OPERATOR()</code> syntax, for example:
  99. </p><pre class="programlisting">
  100. COMMUTATOR = OPERATOR(myschema.===) ,
  101. </pre></div><div class="refsect1" id="id-1.9.3.72.7"><h2>Notes</h2><p>
  102. Refer to <a class="xref" href="xoper.html" title="37.14. User-Defined Operators">Section 37.14</a> for further information.
  103. </p><p>
  104. It is not possible to specify an operator's lexical precedence in
  105. <code class="command">CREATE OPERATOR</code>, because the parser's precedence behavior
  106. is hard-wired. See <a class="xref" href="sql-syntax-lexical.html#SQL-PRECEDENCE" title="4.1.6. Operator Precedence">Section 4.1.6</a> for precedence details.
  107. </p><p>
  108. The obsolete options <code class="literal">SORT1</code>, <code class="literal">SORT2</code>,
  109. <code class="literal">LTCMP</code>, and <code class="literal">GTCMP</code> were formerly used to
  110. specify the names of sort operators associated with a merge-joinable
  111. operator. This is no longer necessary, since information about
  112. associated operators is found by looking at B-tree operator families
  113. instead. If one of these options is given, it is ignored except
  114. for implicitly setting <code class="literal">MERGES</code> true.
  115. </p><p>
  116. Use <a class="xref" href="sql-dropoperator.html" title="DROP OPERATOR"><span class="refentrytitle">DROP OPERATOR</span></a> to delete user-defined operators
  117. from a database. Use <a class="xref" href="sql-alteroperator.html" title="ALTER OPERATOR"><span class="refentrytitle">ALTER OPERATOR</span></a> to modify operators in a
  118. database.
  119. </p></div><div class="refsect1" id="id-1.9.3.72.8"><h2>Examples</h2><p>
  120. The following command defines a new operator, area-equality, for
  121. the data type <code class="type">box</code>:
  122. </p><pre class="programlisting">
  123. CREATE OPERATOR === (
  124. LEFTARG = box,
  125. RIGHTARG = box,
  126. FUNCTION = area_equal_function,
  127. COMMUTATOR = ===,
  128. NEGATOR = !==,
  129. RESTRICT = area_restriction_function,
  130. JOIN = area_join_function,
  131. HASHES, MERGES
  132. );
  133. </pre></div><div class="refsect1" id="id-1.9.3.72.9"><h2>Compatibility</h2><p>
  134. <code class="command">CREATE OPERATOR</code> is a
  135. <span class="productname">PostgreSQL</span> extension. There are no
  136. provisions for user-defined operators in the SQL standard.
  137. </p></div><div class="refsect1" id="id-1.9.3.72.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alteroperator.html" title="ALTER OPERATOR"><span class="refentrytitle">ALTER OPERATOR</span></a>, <a class="xref" href="sql-createopclass.html" title="CREATE OPERATOR CLASS"><span class="refentrytitle">CREATE OPERATOR CLASS</span></a>, <a class="xref" href="sql-dropoperator.html" title="DROP OPERATOR"><span class="refentrytitle">DROP OPERATOR</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-creatematerializedview.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createopclass.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE MATERIALIZED VIEW </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE OPERATOR CLASS</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1