gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

256 行
19KB

  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 CAST</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-createaggregate.html" title="CREATE AGGREGATE" /><link rel="next" href="sql-createcollation.html" title="CREATE COLLATION" /></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 CAST</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createaggregate.html" title="CREATE AGGREGATE">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-createcollation.html" title="CREATE COLLATION">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATECAST"><div class="titlepage"></div><a id="id-1.9.3.58.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE CAST</span></h2><p>CREATE CAST — define a new cast</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE CAST (<em class="replaceable"><code>source_type</code></em> AS <em class="replaceable"><code>target_type</code></em>)
  4. WITH FUNCTION <em class="replaceable"><code>function_name</code></em> [ (<em class="replaceable"><code>argument_type</code></em> [, ...]) ]
  5. [ AS ASSIGNMENT | AS IMPLICIT ]
  6. CREATE CAST (<em class="replaceable"><code>source_type</code></em> AS <em class="replaceable"><code>target_type</code></em>)
  7. WITHOUT FUNCTION
  8. [ AS ASSIGNMENT | AS IMPLICIT ]
  9. CREATE CAST (<em class="replaceable"><code>source_type</code></em> AS <em class="replaceable"><code>target_type</code></em>)
  10. WITH INOUT
  11. [ AS ASSIGNMENT | AS IMPLICIT ]
  12. </pre></div><div class="refsect1" id="SQL-CREATECAST-DESCRIPTION"><h2>Description</h2><p>
  13. <code class="command">CREATE CAST</code> defines a new cast. A cast
  14. specifies how to perform a conversion between
  15. two data types. For example,
  16. </p><pre class="programlisting">
  17. SELECT CAST(42 AS float8);
  18. </pre><p>
  19. converts the integer constant 42 to type <code class="type">float8</code> by
  20. invoking a previously specified function, in this case
  21. <code class="literal">float8(int4)</code>. (If no suitable cast has been defined, the
  22. conversion fails.)
  23. </p><p>
  24. Two types can be <em class="firstterm">binary coercible</em>, which
  25. means that the conversion can be performed <span class="quote">“<span class="quote">for free</span>”</span>
  26. without invoking any function. This requires that corresponding
  27. values use the same internal representation. For instance, the
  28. types <code class="type">text</code> and <code class="type">varchar</code> are binary
  29. coercible both ways. Binary coercibility is not necessarily a
  30. symmetric relationship. For example, the cast
  31. from <code class="type">xml</code> to <code class="type">text</code> can be performed for
  32. free in the present implementation, but the reverse direction
  33. requires a function that performs at least a syntax check. (Two
  34. types that are binary coercible both ways are also referred to as
  35. binary compatible.)
  36. </p><p>
  37. You can define a cast as an <em class="firstterm">I/O conversion cast</em> by using
  38. the <code class="literal">WITH INOUT</code> syntax. An I/O conversion cast is
  39. performed by invoking the output function of the source data type, and
  40. passing the resulting string to the input function of the target data type.
  41. In many common cases, this feature avoids the need to write a separate
  42. cast function for conversion. An I/O conversion cast acts the same as
  43. a regular function-based cast; only the implementation is different.
  44. </p><p>
  45. By default, a cast can be invoked only by an explicit cast request,
  46. that is an explicit <code class="literal">CAST(<em class="replaceable"><code>x</code></em> AS
  47. <em class="replaceable"><code>typename</code></em>)</code> or
  48. <em class="replaceable"><code>x</code></em><code class="literal">::</code><em class="replaceable"><code>typename</code></em>
  49. construct.
  50. </p><p>
  51. If the cast is marked <code class="literal">AS ASSIGNMENT</code> then it can be invoked
  52. implicitly when assigning a value to a column of the target data type.
  53. For example, supposing that <code class="literal">foo.f1</code> is a column of
  54. type <code class="type">text</code>, then:
  55. </p><pre class="programlisting">
  56. INSERT INTO foo (f1) VALUES (42);
  57. </pre><p>
  58. will be allowed if the cast from type <code class="type">integer</code> to type
  59. <code class="type">text</code> is marked <code class="literal">AS ASSIGNMENT</code>, otherwise not.
  60. (We generally use the term <em class="firstterm">assignment
  61. cast</em> to describe this kind of cast.)
  62. </p><p>
  63. If the cast is marked <code class="literal">AS IMPLICIT</code> then it can be invoked
  64. implicitly in any context, whether assignment or internally in an
  65. expression. (We generally use the term <em class="firstterm">implicit
  66. cast</em> to describe this kind of cast.)
  67. For example, consider this query:
  68. </p><pre class="programlisting">
  69. SELECT 2 + 4.0;
  70. </pre><p>
  71. The parser initially marks the constants as being of type <code class="type">integer</code>
  72. and <code class="type">numeric</code> respectively. There is no <code class="type">integer</code>
  73. <code class="literal">+</code> <code class="type">numeric</code> operator in the system catalogs,
  74. but there is a <code class="type">numeric</code> <code class="literal">+</code> <code class="type">numeric</code> operator.
  75. The query will therefore succeed if a cast from <code class="type">integer</code> to
  76. <code class="type">numeric</code> is available and is marked <code class="literal">AS IMPLICIT</code> —
  77. which in fact it is. The parser will apply the implicit cast and resolve
  78. the query as if it had been written
  79. </p><pre class="programlisting">
  80. SELECT CAST ( 2 AS numeric ) + 4.0;
  81. </pre><p>
  82. </p><p>
  83. Now, the catalogs also provide a cast from <code class="type">numeric</code> to
  84. <code class="type">integer</code>. If that cast were marked <code class="literal">AS IMPLICIT</code> —
  85. which it is not — then the parser would be faced with choosing
  86. between the above interpretation and the alternative of casting the
  87. <code class="type">numeric</code> constant to <code class="type">integer</code> and applying the
  88. <code class="type">integer</code> <code class="literal">+</code> <code class="type">integer</code> operator. Lacking any
  89. knowledge of which choice to prefer, it would give up and declare the
  90. query ambiguous. The fact that only one of the two casts is
  91. implicit is the way in which we teach the parser to prefer resolution
  92. of a mixed <code class="type">numeric</code>-and-<code class="type">integer</code> expression as
  93. <code class="type">numeric</code>; there is no built-in knowledge about that.
  94. </p><p>
  95. It is wise to be conservative about marking casts as implicit. An
  96. overabundance of implicit casting paths can cause
  97. <span class="productname">PostgreSQL</span> to choose surprising
  98. interpretations of commands, or to be unable to resolve commands at
  99. all because there are multiple possible interpretations. A good
  100. rule of thumb is to make a cast implicitly invokable only for
  101. information-preserving transformations between types in the same
  102. general type category. For example, the cast from <code class="type">int2</code> to
  103. <code class="type">int4</code> can reasonably be implicit, but the cast from
  104. <code class="type">float8</code> to <code class="type">int4</code> should probably be
  105. assignment-only. Cross-type-category casts, such as <code class="type">text</code>
  106. to <code class="type">int4</code>, are best made explicit-only.
  107. </p><div class="note"><h3 class="title">Note</h3><p>
  108. Sometimes it is necessary for usability or standards-compliance reasons
  109. to provide multiple implicit casts among a set of types, resulting in
  110. ambiguity that cannot be avoided as above. The parser has a fallback
  111. heuristic based on <em class="firstterm">type categories</em> and <em class="firstterm">preferred
  112. types</em> that can help to provide desired behavior in such cases. See
  113. <a class="xref" href="sql-createtype.html" title="CREATE TYPE"><span class="refentrytitle">CREATE TYPE</span></a> for
  114. more information.
  115. </p></div><p>
  116. To be able to create a cast, you must own the source or the target data type
  117. and have <code class="literal">USAGE</code> privilege on the other type. To create a
  118. binary-coercible cast, you must be superuser. (This restriction is made
  119. because an erroneous binary-coercible cast conversion can easily crash the
  120. server.)
  121. </p></div><div class="refsect1" id="id-1.9.3.58.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>source_type</code></em></span></dt><dd><p>
  122. The name of the source data type of the cast.
  123. </p></dd><dt><span class="term"><em class="replaceable"><code>target_type</code></em></span></dt><dd><p>
  124. The name of the target data type of the cast.
  125. </p></dd><dt><span class="term"><code class="literal"><em class="replaceable"><code>function_name</code></em>[(<em class="replaceable"><code>argument_type</code></em> [, ...])]</code></span></dt><dd><p>
  126. The function used to perform the cast. The function name can
  127. be schema-qualified. If it is not, the function will be looked
  128. up in the schema search path. The function's result data type must
  129. match the target type of the cast. Its arguments are discussed below.
  130. If no argument list is specified, the function name must be unique in
  131. its schema.
  132. </p></dd><dt><span class="term"><code class="literal">WITHOUT FUNCTION</code></span></dt><dd><p>
  133. Indicates that the source type is binary-coercible to the target type,
  134. so no function is required to perform the cast.
  135. </p></dd><dt><span class="term"><code class="literal">WITH INOUT</code></span></dt><dd><p>
  136. Indicates that the cast is an I/O conversion cast, performed by
  137. invoking the output function of the source data type, and passing the
  138. resulting string to the input function of the target data type.
  139. </p></dd><dt><span class="term"><code class="literal">AS ASSIGNMENT</code></span></dt><dd><p>
  140. Indicates that the cast can be invoked implicitly in assignment
  141. contexts.
  142. </p></dd><dt><span class="term"><code class="literal">AS IMPLICIT</code></span></dt><dd><p>
  143. Indicates that the cast can be invoked implicitly in any context.
  144. </p></dd></dl></div><p>
  145. Cast implementation functions can have one to three arguments.
  146. The first argument type must be identical to or binary-coercible from
  147. the cast's source type. The second argument,
  148. if present, must be type <code class="type">integer</code>; it receives the type
  149. modifier associated with the destination type, or <code class="literal">-1</code>
  150. if there is none. The third argument,
  151. if present, must be type <code class="type">boolean</code>; it receives <code class="literal">true</code>
  152. if the cast is an explicit cast, <code class="literal">false</code> otherwise.
  153. (Bizarrely, the SQL standard demands different behaviors for explicit and
  154. implicit casts in some cases. This argument is supplied for functions
  155. that must implement such casts. It is not recommended that you design
  156. your own data types so that this matters.)
  157. </p><p>
  158. The return type of a cast function must be identical to or
  159. binary-coercible to the cast's target type.
  160. </p><p>
  161. Ordinarily a cast must have different source and target data types.
  162. However, it is allowed to declare a cast with identical source and
  163. target types if it has a cast implementation function with more than one
  164. argument. This is used to represent type-specific length coercion
  165. functions in the system catalogs. The named function is used to
  166. coerce a value of the type to the type modifier value given by its
  167. second argument.
  168. </p><p>
  169. When a cast has different source and
  170. target types and a function that takes more than one argument, it
  171. supports converting from one type to another and applying a length
  172. coercion in a single step. When no such entry is available, coercion
  173. to a type that uses a type modifier involves two cast steps, one to
  174. convert between data types and a second to apply the modifier.
  175. </p><p>
  176. A cast to or from a domain type currently has no effect. Casting
  177. to or from a domain uses the casts associated with its underlying type.
  178. </p></div><div class="refsect1" id="SQL-CREATECAST-NOTES"><h2>Notes</h2><p>
  179. Use <a class="xref" href="sql-dropcast.html" title="DROP CAST"><span class="refentrytitle">DROP CAST</span></a> to remove user-defined casts.
  180. </p><p>
  181. Remember that if you want to be able to convert types both ways you
  182. need to declare casts both ways explicitly.
  183. </p><a id="id-1.9.3.58.7.4" class="indexterm"></a><p>
  184. It is normally not necessary to create casts between user-defined types
  185. and the standard string types (<code class="type">text</code>, <code class="type">varchar</code>, and
  186. <code class="type">char(<em class="replaceable"><code>n</code></em>)</code>, as well as user-defined types that
  187. are defined to be in the string category). <span class="productname">PostgreSQL</span>
  188. provides automatic I/O conversion casts for that. The automatic casts to
  189. string types are treated as assignment casts, while the automatic casts
  190. from string types are
  191. explicit-only. You can override this behavior by declaring your own
  192. cast to replace an automatic cast, but usually the only reason to
  193. do so is if you want the conversion to be more easily invokable than the
  194. standard assignment-only or explicit-only setting. Another possible
  195. reason is that you want the conversion to behave differently from the
  196. type's I/O function; but that is sufficiently surprising that you
  197. should think twice about whether it's a good idea. (A small number of
  198. the built-in types do indeed have different behaviors for conversions,
  199. mostly because of requirements of the SQL standard.)
  200. </p><p>
  201. While not required, it is recommended that you continue to follow this old
  202. convention of naming cast implementation functions after the target data
  203. type. Many users are used to being able to cast data types using a
  204. function-style notation, that is
  205. <em class="replaceable"><code>typename</code></em>(<em class="replaceable"><code>x</code></em>). This notation is in fact
  206. nothing more nor less than a call of the cast implementation function; it
  207. is not specially treated as a cast. If your conversion functions are not
  208. named to support this convention then you will have surprised users.
  209. Since <span class="productname">PostgreSQL</span> allows overloading of the same function
  210. name with different argument types, there is no difficulty in having
  211. multiple conversion functions from different types that all use the
  212. target type's name.
  213. </p><div class="note"><h3 class="title">Note</h3><p>
  214. Actually the preceding paragraph is an oversimplification: there are
  215. two cases in which a function-call construct will be treated as a cast
  216. request without having matched it to an actual function.
  217. If a function call <em class="replaceable"><code>name</code></em>(<em class="replaceable"><code>x</code></em>) does not
  218. exactly match any existing function, but <em class="replaceable"><code>name</code></em> is the name
  219. of a data type and <code class="structname">pg_cast</code> provides a binary-coercible cast
  220. to this type from the type of <em class="replaceable"><code>x</code></em>, then the call will be
  221. construed as a binary-coercible cast. This exception is made so that
  222. binary-coercible casts can be invoked using functional syntax, even
  223. though they lack any function. Likewise, if there is no
  224. <code class="structname">pg_cast</code> entry but the cast would be to or from a string
  225. type, the call will be construed as an I/O conversion cast. This
  226. exception allows I/O conversion casts to be invoked using functional
  227. syntax.
  228. </p></div><div class="note"><h3 class="title">Note</h3><p>
  229. There is also an exception to the exception: I/O conversion casts from
  230. composite types to string types cannot be invoked using functional
  231. syntax, but must be written in explicit cast syntax (either
  232. <code class="literal">CAST</code> or <code class="literal">::</code> notation). This exception was added
  233. because after the introduction of automatically-provided I/O conversion
  234. casts, it was found too easy to accidentally invoke such a cast when
  235. a function or column reference was intended.
  236. </p></div></div><div class="refsect1" id="SQL-CREATECAST-EXAMPLES"><h2>Examples</h2><p>
  237. To create an assignment cast from type <code class="type">bigint</code> to type
  238. <code class="type">int4</code> using the function <code class="literal">int4(bigint)</code>:
  239. </p><pre class="programlisting">
  240. CREATE CAST (bigint AS int4) WITH FUNCTION int4(bigint) AS ASSIGNMENT;
  241. </pre><p>
  242. (This cast is already predefined in the system.)
  243. </p></div><div class="refsect1" id="SQL-CREATECAST-COMPAT"><h2>Compatibility</h2><p>
  244. The <code class="command">CREATE CAST</code> command conforms to the
  245. <acronym class="acronym">SQL</acronym> standard,
  246. except that SQL does not make provisions for binary-coercible
  247. types or extra arguments to implementation functions.
  248. <code class="literal">AS IMPLICIT</code> is a <span class="productname">PostgreSQL</span>
  249. extension, too.
  250. </p></div><div class="refsect1" id="SQL-CREATECAST-SEEALSO"><h2>See Also</h2><p>
  251. <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>,
  252. <a class="xref" href="sql-createtype.html" title="CREATE TYPE"><span class="refentrytitle">CREATE TYPE</span></a>,
  253. <a class="xref" href="sql-dropcast.html" title="DROP CAST"><span class="refentrytitle">DROP CAST</span></a>
  254. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createaggregate.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-createcollation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE AGGREGATE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE COLLATION</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1