]> begriffs open source - ai-pg/blob - full-docs/src/sgml/man7/CREATE_CAST.7
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / man7 / CREATE_CAST.7
1 '\" t
2 .\"     Title: CREATE CAST
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE CAST" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_CAST \- define a new cast
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE CAST (\fIsource_type\fR AS \fItarget_type\fR)
36     WITH FUNCTION \fIfunction_name\fR [ (\fIargument_type\fR [, \&.\&.\&.]) ]
37     [ AS ASSIGNMENT | AS IMPLICIT ]
38
39 CREATE CAST (\fIsource_type\fR AS \fItarget_type\fR)
40     WITHOUT FUNCTION
41     [ AS ASSIGNMENT | AS IMPLICIT ]
42
43 CREATE CAST (\fIsource_type\fR AS \fItarget_type\fR)
44     WITH INOUT
45     [ AS ASSIGNMENT | AS IMPLICIT ]
46 .fi
47 .SH "DESCRIPTION"
48 .PP
49 \fBCREATE CAST\fR
50 defines a new cast\&. A cast specifies how to perform a conversion between two data types\&. For example,
51 .sp
52 .if n \{\
53 .RS 4
54 .\}
55 .nf
56 SELECT CAST(42 AS float8);
57 .fi
58 .if n \{\
59 .RE
60 .\}
61 .sp
62 converts the integer constant 42 to type
63 float8
64 by invoking a previously specified function, in this case
65 float8(int4)\&. (If no suitable cast has been defined, the conversion fails\&.)
66 .PP
67 Two types can be
68 binary coercible, which means that the conversion can be performed
69 \(lqfor free\(rq
70 without invoking any function\&. This requires that corresponding values use the same internal representation\&. For instance, the types
71 text
72 and
73 varchar
74 are binary coercible both ways\&. Binary coercibility is not necessarily a symmetric relationship\&. For example, the cast from
75 xml
76 to
77 text
78 can be performed for free in the present implementation, but the reverse direction requires a function that performs at least a syntax check\&. (Two types that are binary coercible both ways are also referred to as binary compatible\&.)
79 .PP
80 You can define a cast as an
81 I/O conversion cast
82 by using the
83 WITH INOUT
84 syntax\&. An I/O conversion cast is performed by invoking the output function of the source data type, and passing the resulting string to the input function of the target data type\&. In many common cases, this feature avoids the need to write a separate cast function for conversion\&. An I/O conversion cast acts the same as a regular function\-based cast; only the implementation is different\&.
85 .PP
86 By default, a cast can be invoked only by an explicit cast request, that is an explicit
87 CAST(\fIx\fR AS \fItypename\fR)
88 or
89 \fIx\fR::\fItypename\fR
90 construct\&.
91 .PP
92 If the cast is marked
93 AS ASSIGNMENT
94 then it can be invoked implicitly when assigning a value to a column of the target data type\&. For example, supposing that
95 foo\&.f1
96 is a column of type
97 text, then:
98 .sp
99 .if n \{\
100 .RS 4
101 .\}
102 .nf
103 INSERT INTO foo (f1) VALUES (42);
104 .fi
105 .if n \{\
106 .RE
107 .\}
108 .sp
109 will be allowed if the cast from type
110 integer
111 to type
112 text
113 is marked
114 AS ASSIGNMENT, otherwise not\&. (We generally use the term
115 assignment cast
116 to describe this kind of cast\&.)
117 .PP
118 If the cast is marked
119 AS IMPLICIT
120 then it can be invoked implicitly in any context, whether assignment or internally in an expression\&. (We generally use the term
121 implicit cast
122 to describe this kind of cast\&.) For example, consider this query:
123 .sp
124 .if n \{\
125 .RS 4
126 .\}
127 .nf
128 SELECT 2 + 4\&.0;
129 .fi
130 .if n \{\
131 .RE
132 .\}
133 .sp
134 The parser initially marks the constants as being of type
135 integer
136 and
137 numeric
138 respectively\&. There is no
139 integer
140 +
141 numeric
142 operator in the system catalogs, but there is a
143 numeric
144 +
145 numeric
146 operator\&. The query will therefore succeed if a cast from
147 integer
148 to
149 numeric
150 is available and is marked
151 AS IMPLICIT
152 \(em which in fact it is\&. The parser will apply the implicit cast and resolve the query as if it had been written
153 .sp
154 .if n \{\
155 .RS 4
156 .\}
157 .nf
158 SELECT CAST ( 2 AS numeric ) + 4\&.0;
159 .fi
160 .if n \{\
161 .RE
162 .\}
163 .PP
164 Now, the catalogs also provide a cast from
165 numeric
166 to
167 integer\&. If that cast were marked
168 AS IMPLICIT
169 \(em which it is not \(em then the parser would be faced with choosing between the above interpretation and the alternative of casting the
170 numeric
171 constant to
172 integer
173 and applying the
174 integer
175 +
176 integer
177 operator\&. Lacking any knowledge of which choice to prefer, it would give up and declare the query ambiguous\&. The fact that only one of the two casts is implicit is the way in which we teach the parser to prefer resolution of a mixed
178 numeric\-and\-integer
179 expression as
180 numeric; there is no built\-in knowledge about that\&.
181 .PP
182 It is wise to be conservative about marking casts as implicit\&. An overabundance of implicit casting paths can cause
183 PostgreSQL
184 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
185 int2
186 to
187 int4
188 can reasonably be implicit, but the cast from
189 float8
190 to
191 int4
192 should probably be assignment\-only\&. Cross\-type\-category casts, such as
193 text
194 to
195 int4, are best made explicit\-only\&.
196 .if n \{\
197 .sp
198 .\}
199 .RS 4
200 .it 1 an-trap
201 .nr an-no-space-flag 1
202 .nr an-break-flag 1
203 .br
204 .ps +1
205 \fBNote\fR
206 .ps -1
207 .br
208 .PP
209 Sometimes it is necessary for usability or standards\-compliance reasons to provide multiple implicit casts among a set of types, resulting in ambiguity that cannot be avoided as above\&. The parser has a fallback heuristic based on
210 type categories
211 and
212 preferred types
213 that can help to provide desired behavior in such cases\&. See
214 CREATE TYPE (\fBCREATE_TYPE\fR(7))
215 for more information\&.
216 .sp .5v
217 .RE
218 .PP
219 To be able to create a cast, you must own the source or the target data type and have
220 USAGE
221 privilege on the other type\&. To create a binary\-coercible cast, you must be superuser\&. (This restriction is made because an erroneous binary\-coercible cast conversion can easily crash the server\&.)
222 .SH "PARAMETERS"
223 .PP
224 \fIsource_type\fR
225 .RS 4
226 The name of the source data type of the cast\&.
227 .RE
228 .PP
229 \fItarget_type\fR
230 .RS 4
231 The name of the target data type of the cast\&.
232 .RE
233 .PP
234 \fIfunction_name\fR[(\fIargument_type\fR [, \&.\&.\&.])]
235 .RS 4
236 The function used to perform the cast\&. The function name can be schema\-qualified\&. If it is not, the function will be looked up in the schema search path\&. The function\*(Aqs result data type must match the target type of the cast\&. Its arguments are discussed below\&. If no argument list is specified, the function name must be unique in its schema\&.
237 .RE
238 .PP
239 WITHOUT FUNCTION
240 .RS 4
241 Indicates that the source type is binary\-coercible to the target type, so no function is required to perform the cast\&.
242 .RE
243 .PP
244 WITH INOUT
245 .RS 4
246 Indicates that the cast is an I/O conversion cast, performed by invoking the output function of the source data type, and passing the resulting string to the input function of the target data type\&.
247 .RE
248 .PP
249 AS ASSIGNMENT
250 .RS 4
251 Indicates that the cast can be invoked implicitly in assignment contexts\&.
252 .RE
253 .PP
254 AS IMPLICIT
255 .RS 4
256 Indicates that the cast can be invoked implicitly in any context\&.
257 .RE
258 .PP
259 Cast implementation functions can have one to three arguments\&. The first argument type must be identical to or binary\-coercible from the cast\*(Aqs source type\&. The second argument, if present, must be type
260 integer; it receives the type modifier associated with the destination type, or
261 \-1
262 if there is none\&. The third argument, if present, must be type
263 boolean; it receives
264 true
265 if the cast is an explicit cast,
266 false
267 otherwise\&. (Bizarrely, the SQL standard 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\&.)
268 .PP
269 The return type of a cast function must be identical to or binary\-coercible to the cast\*(Aqs target type\&.
270 .PP
271 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\&.
272 .PP
273 When a cast has different source and target types and a function that takes more than one argument, it supports 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 cast steps, one to convert between data types and a second to apply the modifier\&.
274 .PP
275 A cast to or from a domain type currently has no effect\&. Casting to or from a domain uses the casts associated with its underlying type\&.
276 .SH "NOTES"
277 .PP
278 Use
279 \fBDROP CAST\fR
280 to remove user\-defined casts\&.
281 .PP
282 Remember that if you want to be able to convert types both ways you need to declare casts both ways explicitly\&.
283 .PP
284 It is normally not necessary to create casts between user\-defined types and the standard string types (text,
285 varchar, and
286 char(\fIn\fR), as well as user\-defined types that are defined to be in the string category)\&.
287 PostgreSQL
288 provides automatic I/O conversion casts for that\&. The automatic casts to string types are treated as assignment casts, while the automatic casts from string types are explicit\-only\&. You can override this behavior by declaring your own cast to replace an automatic cast, but usually the only reason to do so is if you want the conversion to be more easily invokable than the standard assignment\-only or explicit\-only setting\&. Another possible reason is that you want the conversion to behave differently from the type\*(Aqs I/O function; but that is sufficiently surprising that you should think twice about whether it\*(Aqs a good idea\&. (A small number of the built\-in types do indeed have different behaviors for conversions, mostly because of requirements of the SQL standard\&.)
289 .PP
290 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
291 \fItypename\fR(\fIx\fR)\&. 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
292 PostgreSQL
293 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\*(Aqs name\&.
294 .if n \{\
295 .sp
296 .\}
297 .RS 4
298 .it 1 an-trap
299 .nr an-no-space-flag 1
300 .nr an-break-flag 1
301 .br
302 .ps +1
303 \fBNote\fR
304 .ps -1
305 .br
306 .PP
307 Actually the preceding paragraph is an oversimplification: there are two cases in which a function\-call construct will be treated as a cast request without having matched it to an actual function\&. If a function call
308 \fIname\fR(\fIx\fR) does not exactly match any existing function, but
309 \fIname\fR
310 is the name of a data type and
311 pg_cast
312 provides a binary\-coercible cast to this type from the type of
313 \fIx\fR, then the call will be construed as a binary\-coercible cast\&. This exception is made so that binary\-coercible casts can be invoked using functional syntax, even though they lack any function\&. Likewise, if there is no
314 pg_cast
315 entry but the cast would be to or from a string type, the call will be construed as an I/O conversion cast\&. This exception allows I/O conversion casts to be invoked using functional syntax\&.
316 .sp .5v
317 .RE
318 .if n \{\
319 .sp
320 .\}
321 .RS 4
322 .it 1 an-trap
323 .nr an-no-space-flag 1
324 .nr an-break-flag 1
325 .br
326 .ps +1
327 \fBNote\fR
328 .ps -1
329 .br
330 .PP
331 There is also an exception to the exception: I/O conversion casts from composite types to string types cannot be invoked using functional syntax, but must be written in explicit cast syntax (either
332 CAST
333 or
334 ::
335 notation)\&. This exception was added because after the introduction of automatically\-provided I/O conversion casts, it was found too easy to accidentally invoke such a cast when a function or column reference was intended\&.
336 .sp .5v
337 .RE
338 .SH "EXAMPLES"
339 .PP
340 To create an assignment cast from type
341 bigint
342 to type
343 int4
344 using the function
345 int4(bigint):
346 .sp
347 .if n \{\
348 .RS 4
349 .\}
350 .nf
351 CREATE CAST (bigint AS int4) WITH FUNCTION int4(bigint) AS ASSIGNMENT;
352 .fi
353 .if n \{\
354 .RE
355 .\}
356 .sp
357 (This cast is already predefined in the system\&.)
358 .SH "COMPATIBILITY"
359 .PP
360 The
361 \fBCREATE CAST\fR
362 command conforms to the
363 SQL
364 standard, except that SQL does not make provisions for binary\-coercible types or extra arguments to implementation functions\&.
365 AS IMPLICIT
366 is a
367 PostgreSQL
368 extension, too\&.
369 .SH "SEE ALSO"
370 .PP
371 CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7)),
372 CREATE TYPE (\fBCREATE_TYPE\fR(7)),
373 DROP CAST (\fBDROP_CAST\fR(7))