1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CREATE CAST</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql-commands.html" title="SQL Commands">
<link rel="prev" href="sql-createaggregate.html" title="CREATE AGGREGATE">
<link rel="next" href="sql-createconstraint.html" title="CREATE CONSTRAINT TRIGGER">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
<a name="sql-createcast"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>CREATE CAST — define a new cast</p>
</div>
<a name="id752951"></a><div class="refsynopsisdiv">
<h2>Synopsis</h2>
<pre class="synopsis">CREATE CAST (<em class="replaceable"><code>sourcetype</code></em> AS <em class="replaceable"><code>targettype</code></em>)
WITH FUNCTION <em class="replaceable"><code>funcname</code></em> (<em class="replaceable"><code>argtypes</code></em>)
[ AS ASSIGNMENT | AS IMPLICIT ]
CREATE CAST (<em class="replaceable"><code>sourcetype</code></em> AS <em class="replaceable"><code>targettype</code></em>)
WITHOUT FUNCTION
[ AS ASSIGNMENT | AS IMPLICIT ]</pre>
</div>
<div class="refsect1" lang="en">
<a name="sql-createcast-description"></a><h2>Description</h2>
<p> <code class="command">CREATE CAST</code> defines a new cast. A cast
specifies how to perform a conversion between
two data types. For example,
</p>
<pre class="programlisting">SELECT CAST(42 AS text);</pre>
<p>
converts the integer constant 42 to type <code class="type">text</code> by
invoking a previously specified function, in this case
<code class="literal">text(int4)</code>. (If no suitable cast has been defined, the
conversion fails.)
</p>
<p> Two types may be <em class="firstterm">binary compatible</em>, which
means that they can be converted into one another “<span class="quote">for
free</span>” without invoking any function. This requires that
corresponding values use the same internal representation. For
instance, the types <code class="type">text</code> and <code class="type">varchar</code> are
binary compatible.
</p>
<p> By default, a cast can be invoked only by an explicit cast request,
that is an explicit <code class="literal">CAST(<em class="replaceable"><code>x</code></em> AS
<em class="replaceable"><code>typename</code></em>)</code> or
<em class="replaceable"><code>x</code></em><code class="literal">::</code><em class="replaceable"><code>typename</code></em>
construct.
</p>
<p> If the cast is marked <code class="literal">AS ASSIGNMENT</code> then it can be invoked
implicitly when assigning a value to a column of the target data type.
For example, supposing that <code class="literal">foo.f1</code> is a column of
type <code class="type">text</code>, then
</p>
<pre class="programlisting">INSERT INTO foo (f1) VALUES (42);</pre>
<p>
will be allowed if the cast from type <code class="type">integer</code> to type
<code class="type">text</code> is marked <code class="literal">AS ASSIGNMENT</code>, otherwise
not.
(We generally use the term <em class="firstterm">assignment
cast</em> to describe this kind of cast.)
</p>
<p> If the cast is marked <code class="literal">AS IMPLICIT</code> then it can be invoked
implicitly in any context, whether assignment or internally in an
expression. For example, since <code class="literal">||</code> takes <code class="type">text</code>
operands,
</p>
<pre class="programlisting">SELECT 'The time is ' || now();</pre>
<p>
will be allowed only if the cast from type <code class="type">timestamp</code> to
<code class="type">text</code> is marked <code class="literal">AS IMPLICIT</code>. Otherwise it
will be necessary to write the cast explicitly, for example
</p>
<pre class="programlisting">SELECT 'The time is ' || CAST(now() AS text);</pre>
<p>
(We generally use the term <em class="firstterm">implicit
cast</em> to describe this kind of cast.)
</p>
<p> It is wise to be conservative about marking casts as implicit. An
overabundance of implicit casting paths can cause
<span class="productname">PostgreSQL</span> to choose surprising
interpretations of commands, or to be unable to resolve commands at
all because there are multiple possible interpretations. A good
rule of thumb is to make a cast implicitly invokable only for
information-preserving transformations between types in the same
general type category. For example, the cast from <code class="type">int2</code> to
<code class="type">int4</code> can reasonably be implicit, but the cast from
<code class="type">float8</code> to <code class="type">int4</code> should probably be
assignment-only. Cross-type-category casts, such as <code class="type">text</code>
to <code class="type">int4</code>, are best made explicit-only.
</p>
<p> To be able to create a cast, you must own the source or the target
data type. To create a binary-compatible cast, you must be superuser.
(This restriction is made because an erroneous binary-compatible cast
conversion can easily crash the server.)
</p>
</div>
<div class="refsect1" lang="en">
<a name="id753252"></a><h2>Parameters</h2>
<div class="variablelist"><dl>
<dt><span class="term"><em class="replaceable"><code>sourcetype</code></em></span></dt>
<dd><p> The name of the source data type of the cast.
</p></dd>
<dt><span class="term"><em class="replaceable"><code>targettype</code></em></span></dt>
<dd><p> The name of the target data type of the cast.
</p></dd>
<dt><span class="term"><em class="replaceable"><code>funcname</code></em>(<em class="replaceable"><code>argtypes</code></em>)</span></dt>
<dd><p> The function used to perform the cast. The function name may
be schema-qualified. If it is not, the function will be looked
up in the schema search path. The function's result data type must
match the target type of the cast. Its arguments are discussed below.
</p></dd>
<dt><span class="term"><code class="literal">WITHOUT FUNCTION</code></span></dt>
<dd><p> Indicates that the source type and the target type are binary
compatible, so no function is required to perform the cast.
</p></dd>
<dt><span class="term"><code class="literal">AS ASSIGNMENT</code></span></dt>
<dd><p> Indicates that the cast may be invoked implicitly in assignment
contexts.
</p></dd>
<dt><span class="term"><code class="literal">AS IMPLICIT</code></span></dt>
<dd><p> Indicates that the cast may be invoked implicitly in any context.
</p></dd>
</dl></div>
<p> Cast implementation functions may have one to three arguments.
The first argument type must be identical to the cast's source type.
The second argument,
if present, must be type <code class="type">integer</code>; it receives the type
modifier associated with the destination type, or <code class="literal">-1</code>
if there is none. The third argument,
if present, must be type <code class="type">boolean</code>; it receives <code class="literal">true</code>
if the cast is an explicit cast, <code class="literal">false</code> otherwise.
(Bizarrely, the SQL spec demands different behaviors for explicit and
implicit casts in some cases. This argument is supplied for functions
that must implement such casts. It is not recommended that you design
your own data types so that this matters.)
</p>
<p> Ordinarily a cast must have different source and target data types.
However, it is allowed to declare a cast with identical source and
target types if it has a cast implementation function with more than one
argument. This is used to represent type-specific length coercion
functions in the system catalogs. The named function is used to
coerce a value of the type to the type modifier value given by its
second argument. (Since the grammar presently permits only certain
built-in data types to have type modifiers, this feature is of no
use for user-defined target types, but we mention it for completeness.)
</p>
<p> When a cast has different source and
target types and a function that takes more than one argument, it
represents converting from one type to another and applying a length
coercion in a single step. When no such entry is available, coercion
to a type that uses a type modifier involves two steps, one to
convert between data types and a second to apply the modifier.
</p>
</div>
<div class="refsect1" lang="en">
<a name="sql-createcast-notes"></a><h2>Notes</h2>
<p> Use <a href="sql-dropcast.html">DROP CAST</a> to remove user-defined casts.
</p>
<p> Remember that if you want to be able to convert types both ways you
need to declare casts both ways explicitly.
</p>
<p> Prior to <span class="productname">PostgreSQL</span> 7.3, every function that had
the same name as a data type, returned that data type, and took one
argument of a different type was automatically a cast function.
This convention has been abandoned in face of the introduction of
schemas and to be able to represent binary compatible casts in the
system catalogs. The built-in cast functions still follow this naming
scheme, but they have to be shown as casts in the system catalog
<code class="structname">pg_cast</code> as well.
</p>
<p> While not required, it is recommended that you continue to follow this old
convention of naming cast implementation functions after the target data
type. Many users are used to being able to cast data types using a
function-style notation, that is
<em class="replaceable"><code>typename</code></em>(<em class="replaceable"><code>x</code></em>). This notation is in fact
nothing more nor less than a call of the cast implementation function; it
is not specially treated as a cast. If your conversion functions are not
named to support this convention then you will have surprised users.
Since <span class="productname">PostgreSQL</span> allows overloading of the same function
name with different argument types, there is no difficulty in having
multiple conversion functions from different types that all use the
target type's name.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> There is one small lie in the preceding paragraph: there is still one
case in which <code class="structname">pg_cast</code> will be used to resolve the
meaning of an apparent function call. If a
function call <em class="replaceable"><code>name</code></em>(<em class="replaceable"><code>x</code></em>) matches no
actual function, but <em class="replaceable"><code>name</code></em> is the name of a data type
and <code class="structname">pg_cast</code> shows a binary-compatible cast to this
type from the type of <em class="replaceable"><code>x</code></em>, then the call will be construed
as an explicit cast. This exception is made so that binary-compatible
casts can be invoked using functional syntax, even though they lack
any function.
</p>
</div>
</div>
<div class="refsect1" lang="en">
<a name="sql-createcast-examples"></a><h2>Examples</h2>
<p> To create a cast from type <code class="type">text</code> to type
<code class="type">int4</code> using the function <code class="literal">int4(text)</code>:
</p>
<pre class="programlisting">CREATE CAST (text AS int4) WITH FUNCTION int4(text);</pre>
<p>
(This cast is already predefined in the system.)
</p>
</div>
<div class="refsect1" lang="en">
<a name="sql-createcast-compat"></a><h2>Compatibility</h2>
<p> The <code class="command">CREATE CAST</code> command conforms to the
<acronym class="acronym">SQL</acronym> standard,
except that SQL does not make provisions for binary-compatible
types or extra arguments to implementation functions.
<code class="literal">AS IMPLICIT</code> is a <span class="productname">PostgreSQL</span>
extension, too.
</p>
</div>
<div class="refsect1" lang="en">
<a name="sql-createcast-seealso"></a><h2>See Also</h2>
<p> <a href="sql-createfunction.html">CREATE FUNCTION</a>,
<a href="sql-createtype.html">CREATE TYPE</a>,
<a href="sql-dropcast.html">DROP CAST</a>
</p>
</div>
</div></body>
</html>
|