3 <chapter Id=
"typeconv">
4 <title>Type Conversion
</title>
6 <indexterm zone=
"typeconv">
7 <primary>data type
</primary>
8 <secondary>conversion
</secondary>
12 <acronym>SQL
</acronym> statements can, intentionally or not, require
13 mixing of different data types in the same expression.
14 <productname>PostgreSQL
</productname> has extensive facilities for
15 evaluating mixed-type expressions.
19 In many cases a user will not need
20 to understand the details of the type conversion mechanism.
21 However, the implicit conversions done by
<productname>PostgreSQL
</productname>
22 can affect the results of a query. When necessary, these results
23 can be tailored by using
<emphasis>explicit
</emphasis> type conversion.
27 This chapter introduces the
<productname>PostgreSQL
</productname>
28 type conversion mechanisms and conventions.
29 Refer to the relevant sections in
<xref linkend=
"datatype"> and
<xref linkend=
"functions">
30 for more information on specific data types and allowed functions and
34 <sect1 id=
"typeconv-overview">
35 <title>Overview
</title>
38 <acronym>SQL
</acronym> is a strongly typed language. That is, every data item
39 has an associated data type which determines its behavior and allowed usage.
40 <productname>PostgreSQL
</productname> has an extensible type system that is
41 much more general and flexible than other
<acronym>SQL
</acronym> implementations.
42 Hence, most type conversion behavior in
<productname>PostgreSQL
</productname>
43 is governed by general rules rather than by
<foreignphrase>ad hoc<
/>
44 heuristics. This allows
45 mixed-type expressions to be meaningful even with user-defined types.
49 The
<productname>PostgreSQL
</productname> scanner/parser divides lexical
50 elements into only five fundamental categories: integers, non-integer numbers,
51 strings, identifiers, and key words. Constants of most non-numeric types are
52 first classified as strings. The
<acronym>SQL
</acronym> language definition
53 allows specifying type names with strings, and this mechanism can be used in
54 <productname>PostgreSQL
</productname> to start the parser down the correct
55 path. For example, the query
58 SELECT text 'Origin' AS
"label", point '(
0,
0)' AS
"value";
66 has two literal constants, of type
<type>text
</type> and
<type>point
</type>.
67 If a type is not specified for a string literal, then the placeholder type
68 <type>unknown
</type> is assigned initially, to be resolved in later
69 stages as described below.
73 There are four fundamental
<acronym>SQL
</acronym> constructs requiring
74 distinct type conversion rules in the
<productname>PostgreSQL
</productname>
84 Much of the
<productname>PostgreSQL
</productname> type system is built around a
85 rich set of functions. Functions can have one or more arguments.
86 Since
<productname>PostgreSQL
</productname> permits function
87 overloading, the function name alone does not uniquely identify the function
88 to be called; the parser must select the right function based on the data
89 types of the supplied arguments.
99 <productname>PostgreSQL
</productname> allows expressions with
100 prefix and postfix unary (one-argument) operators,
101 as well as binary (two-argument) operators. Like functions, operators can
102 be overloaded, and so the same problem of selecting the right operator
113 <acronym>SQL
</acronym> <command>INSERT
</command> and
<command>UPDATE
</command> statements place the results of
114 expressions into a table. The expressions in the statement must be matched up
115 with, and perhaps converted to, the types of the target columns.
121 <literal>UNION
</literal>,
<literal>CASE
</literal>, and related constructs
125 Since all query results from a unionized
<command>SELECT
</command> statement
126 must appear in a single set of columns, the types of the results of each
127 <command>SELECT<
/> clause must be matched up and converted to a uniform set.
128 Similarly, the result expressions of a
<literal>CASE<
/> construct must be
129 converted to a common type so that the
<literal>CASE<
/> expression as a whole
130 has a known output type. The same holds for
<literal>ARRAY<
/> constructs,
131 and for the
<function>GREATEST<
/> and
<function>LEAST<
/> functions.
139 The system catalogs store information about which conversions, called
140 <firstterm>casts
</firstterm>, between data types are valid, and how to
141 perform those conversions. Additional casts can be added by the user
142 with the
<xref linkend=
"sql-createcast" endterm=
"sql-createcast-title">
143 command. (This is usually
144 done in conjunction with defining new data types. The set of casts
145 between the built-in types has been carefully crafted and is best not
150 <primary>data type
</primary>
151 <secondary>category
</secondary>
155 An additional heuristic is provided in the parser to allow better guesses
156 at proper casting behavior among groups of types that have implicit casts.
157 Data types are divided into several basic
<firstterm>type
158 categories
</firstterm>, including
<type>boolean
</type>,
<type>numeric
</type>,
159 <type>string
</type>,
<type>bitstring
</type>,
<type>datetime
</type>,
160 <type>timespan
</type>,
<type>geometric
</type>,
<type>network
</type>, and
161 user-defined. (For a list see
<xref linkend=
"catalog-typcategory-table">;
162 but note it is also possible to create custom type categories.) Within each
163 category there can be one or more
<firstterm>preferred types
</firstterm>, which
164 are preferentially selected when there is ambiguity. With careful selection
165 of preferred types and available implicit casts, it is possible to ensure that
166 ambiguous expressions (those with multiple candidate parsing solutions) can be
167 resolved in a useful way.
171 All type conversion rules are designed with several principles in mind:
176 Implicit conversions should never have surprising or unpredictable outcomes.
182 There should be no extra overhead from the parser or executor
183 if a query does not need implicit type conversion.
184 That is, if a query is well formulated and the types already match up, then the query should proceed
185 without spending extra time in the parser and without introducing unnecessary implicit conversion
186 calls into the query.
190 Additionally, if a query usually requires an implicit conversion for a function, and
191 if then the user defines a new function with the correct argument types, the parser
192 should use this new function and will no longer do the implicit conversion using the old function.
200 <sect1 id=
"typeconv-oper">
201 <title>Operators
</title>
203 <indexterm zone=
"typeconv-oper">
204 <primary>operator
</primary>
205 <secondary>type resolution in an invocation
</secondary>
209 The specific operator to be used in an operator invocation is determined
211 the procedure below. Note that this procedure is indirectly affected
212 by the precedence of the involved operators. See
<xref
213 linkend=
"sql-precedence"> for more information.
217 <title>Operator Type Resolution
</title>
219 <step performance=
"required">
221 Select the operators to be considered from the
222 <classname>pg_operator
</classname> system catalog. If an unqualified
223 operator name was used (the usual case), the operators
224 considered are those of the right name and argument count that are
225 visible in the current search path (see
<xref linkend=
"ddl-schemas-path">).
226 If a qualified operator name was given, only operators in the specified
227 schema are considered.
231 <step performance=
"optional">
233 If the search path finds multiple operators of identical argument types,
234 only the one appearing earliest in the path is considered. But operators of
235 different argument types are considered on an equal footing regardless of
236 search path position.
242 <step performance=
"required">
244 Check for an operator accepting exactly the input argument types.
245 If one exists (there can be only one exact match in the set of
246 operators considered), use it.
250 <step performance=
"optional">
252 If one argument of a binary operator invocation is of the
<type>unknown
</type> type,
253 then assume it is the same type as the other argument for this check.
254 Other cases involving
<type>unknown
</type> will never find a match at
261 <step performance=
"required">
263 Look for the best match.
266 <step performance=
"required">
268 Discard candidate operators for which the input types do not match
269 and cannot be converted (using an implicit conversion) to match.
270 <type>unknown
</type> literals are
271 assumed to be convertible to anything for this purpose. If only one
272 candidate remains, use it; else continue to the next step.
275 <step performance=
"required">
277 Run through all candidates and keep those with the most exact matches
278 on input types. (Domains are considered the same as their base type
279 for this purpose.) Keep all candidates if none have any exact matches.
280 If only one candidate remains, use it; else continue to the next step.
283 <step performance=
"required">
285 Run through all candidates and keep those that accept preferred types (of the
286 input data type's type category) at the most positions where type conversion
288 Keep all candidates if none accept preferred types.
289 If only one candidate remains, use it; else continue to the next step.
292 <step performance=
"required">
294 If any input arguments are
<type>unknown
</type>, check the type
295 categories accepted at those argument positions by the remaining
296 candidates. At each position, select the
<type>string
</type> category
298 candidate accepts that category. (This bias towards string is appropriate
299 since an unknown-type literal does look like a string.) Otherwise, if
300 all the remaining candidates accept the same type category, select that
301 category; otherwise fail because the correct choice cannot be deduced
302 without more clues. Now discard
303 candidates that do not accept the selected type category. Furthermore,
304 if any candidate accepts a preferred type in that category,
305 discard candidates that accept non-preferred types for that argument.
308 <step performance=
"required">
310 If only one candidate remains, use it. If no candidate or more than one
320 Some examples follow.
324 <title>Factorial Operator Type Resolution
</title>
327 There is only one factorial operator (postfix
<literal>!<
/>)
328 defined in the standard catalog, and it takes an argument of type
330 The scanner assigns an initial type of
<type>integer
</type> to the argument
331 in this query expression:
333 SELECT
40 ! AS
"40 factorial";
336 --------------------------------------------------
337 815915283247897734345611269596115894272000000000
341 So the parser does a type conversion on the operand and the query
345 SELECT CAST(
40 AS bigint) ! AS
"40 factorial";
351 <title>String Concatenation Operator Type Resolution
</title>
354 A string-like syntax is used for working with string types as well as for
355 working with complex extension types.
356 Strings with unspecified type are matched with likely operator candidates.
360 An example with one unspecified argument:
362 SELECT text 'abc' || 'def' AS
"text and unknown";
372 In this case the parser looks to see if there is an operator taking
<type>text
</type>
373 for both arguments. Since there is, it assumes that the second argument should
374 be interpreted as of type
<type>text
</type>.
378 Here is a concatenation on unspecified types:
380 SELECT 'abc' || 'def' AS
"unspecified";
390 In this case there is no initial hint for which type to use, since no types
391 are specified in the query. So, the parser looks for all candidate operators
392 and finds that there are candidates accepting both string-category and
393 bit-string-category inputs. Since string category is preferred when available,
394 that category is selected, and then the
395 preferred type for strings,
<type>text
</type>, is used as the specific
396 type to resolve the unknown literals to.
401 <title>Absolute-Value and Negation Operator Type Resolution
</title>
404 The
<productname>PostgreSQL
</productname> operator catalog has several
405 entries for the prefix operator
<literal>@<
/>, all of which implement
406 absolute-value operations for various numeric data types. One of these
407 entries is for type
<type>float8
</type>, which is the preferred type in
408 the numeric category. Therefore,
<productname>PostgreSQL
</productname>
409 will use that entry when faced with an
<type>unknown<
/> input:
411 SELECT @ '-
4.5' AS
"abs";
417 Here the system has implicitly resolved the unknown-type literal as type
418 <type>float8
</type> before applying the chosen operator. We can verify that
419 <type>float8
</type> and not some other type was used:
421 SELECT @ '-
4.5e500' AS
"abs";
423 ERROR:
"-4.5e500" is out of range for type double precision
428 On the other hand, the prefix operator
<literal>~<
/> (bitwise negation)
429 is defined only for integer data types, not for
<type>float8
</type>. So, if we
430 try a similar case with
<literal>~<
/>, we get:
432 SELECT ~ '
20' AS
"negation";
434 ERROR: operator is not unique: ~
"unknown"
435 HINT: Could not choose a best candidate operator. You might need to add
438 This happens because the system cannot decide which of the several
439 possible
<literal>~<
/> operators should be preferred. We can help
440 it out with an explicit cast:
442 SELECT ~ CAST('
20' AS int8) AS
"negation";
454 <sect1 id=
"typeconv-func">
455 <title>Functions
</title>
457 <indexterm zone=
"typeconv-func">
458 <primary>function
</primary>
459 <secondary>type resolution in an invocation
</secondary>
463 The specific function to be used in a function invocation is determined
464 according to the following steps.
468 <title>Function Type Resolution
</title>
470 <step performance=
"required">
472 Select the functions to be considered from the
473 <classname>pg_proc
</classname> system catalog. If an unqualified
474 function name was used, the functions
475 considered are those of the right name and argument count that are
476 visible in the current search path (see
<xref linkend=
"ddl-schemas-path">).
477 If a qualified function name was given, only functions in the specified
478 schema are considered.
482 <step performance=
"optional">
484 If the search path finds multiple functions of identical argument types,
485 only the one appearing earliest in the path is considered. But functions of
486 different argument types are considered on an equal footing regardless of
487 search path position.
490 <step performance=
"optional">
492 If a function is declared with a
<literal>VARIADIC<
/> array parameter, and
493 the call does not use the
<literal>VARIADIC<
/> keyword, then the function
494 is treated as if the array parameter were replaced by one or more occurrences
495 of its element type, as needed to match the call. After such expansion the
496 function might have effective argument types identical to some non-variadic
497 function. In that case the function appearing earlier in the search path is
498 used, or if the two functions are in the same schema, the non-variadic one is
502 <step performance=
"optional">
504 Functions that have default values for parameters are considered to match any
505 call that omits zero or more of the defaultable parameter positions. If more
506 than one such function matches a call, the one appearing earliest in the
507 search path is used. If there are two or more such functions in the same
508 schema with identical parameter types in the non-defaulted positions (which is
509 possible if they have different sets of defaultable parameters), the system
510 will not be able to determine which to prefer, and so an
<quote>ambiguous
511 function call<
/> error will result if no better match to the call can be
518 <step performance=
"required">
520 Check for a function accepting exactly the input argument types.
521 If one exists (there can be only one exact match in the set of
522 functions considered), use it.
523 (Cases involving
<type>unknown
</type> will never find a match at
528 <step performance=
"required">
530 If no exact match is found, see whether the function call appears
531 to be a special type conversion request. This happens if the function call
532 has just one argument and the function name is the same as the (internal)
533 name of some data type. Furthermore, the function argument must be either
534 an unknown-type literal, or a type that is binary-coercible to the named
535 data type, or a type that could be converted to the named data type by
536 applying that type's I/O functions (that is, the conversion is either to or
537 from one of the standard string types). When these conditions are met,
538 the function call is treated as a form of
<literal>CAST<
/> specification.
541 The reason for this step is to support function-style cast specifications
542 in cases where there is not an actual cast function. If there is a cast
543 function, it is conventionally named after its output type, and so there
544 is no need to have a special case. See
545 <xref linkend=
"sql-createcast" endterm=
"sql-createcast-title">
546 for additional commentary.
551 <step performance=
"required">
553 Look for the best match.
556 <step performance=
"required">
558 Discard candidate functions for which the input types do not match
559 and cannot be converted (using an implicit conversion) to match.
560 <type>unknown
</type> literals are
561 assumed to be convertible to anything for this purpose. If only one
562 candidate remains, use it; else continue to the next step.
565 <step performance=
"required">
567 Run through all candidates and keep those with the most exact matches
568 on input types. (Domains are considered the same as their base type
569 for this purpose.) Keep all candidates if none have any exact matches.
570 If only one candidate remains, use it; else continue to the next step.
573 <step performance=
"required">
575 Run through all candidates and keep those that accept preferred types (of the
576 input data type's type category) at the most positions where type conversion
578 Keep all candidates if none accept preferred types.
579 If only one candidate remains, use it; else continue to the next step.
582 <step performance=
"required">
584 If any input arguments are
<type>unknown
</type>, check the type categories
586 at those argument positions by the remaining candidates. At each position,
587 select the
<type>string
</type> category if any candidate accepts that category.
588 (This bias towards string
589 is appropriate since an unknown-type literal does look like a string.)
590 Otherwise, if all the remaining candidates accept the same type category,
591 select that category; otherwise fail because
592 the correct choice cannot be deduced without more clues.
593 Now discard candidates that do not accept the selected type category.
594 Furthermore, if any candidate accepts a preferred type in that category,
595 discard candidates that accept non-preferred types for that argument.
598 <step performance=
"required">
600 If only one candidate remains, use it. If no candidate or more than one
610 Note that the
<quote>best match<
/> rules are identical for operator and
611 function type resolution.
612 Some examples follow.
616 <title>Rounding Function Argument Type Resolution
</title>
619 There is only one
<function>round
</function> function with two
620 arguments. (The first is
<type>numeric
</type>, the second is
621 <type>integer
</type>.) So the following query automatically converts
622 the first argument of type
<type>integer
</type> to
623 <type>numeric
</type>:
634 That query is actually transformed by the parser to
636 SELECT round(CAST (
4 AS numeric),
4);
641 Since numeric constants with decimal points are initially assigned the
642 type
<type>numeric
</type>, the following query will require no type
643 conversion and might therefore be slightly more efficient:
645 SELECT round(
4.0,
4);
651 <title>Substring Function Type Resolution
</title>
654 There are several
<function>substr
</function> functions, one of which
655 takes types
<type>text
</type> and
<type>integer
</type>. If called
656 with a string constant of unspecified type, the system chooses the
657 candidate function that accepts an argument of the preferred category
658 <literal>string
</literal> (namely of type
<type>text
</type>).
661 SELECT substr('
1234',
3);
671 If the string is declared to be of type
<type>varchar
</type>, as might be the case
672 if it comes from a table, then the parser will try to convert it to become
<type>text
</type>:
674 SELECT substr(varchar '
1234',
3);
682 This is transformed by the parser to effectively become
684 SELECT substr(CAST (varchar '
1234' AS text),
3);
690 The parser learns from the
<structname>pg_cast<
/> catalog that
691 <type>text
</type> and
<type>varchar
</type>
692 are binary-compatible, meaning that one can be passed to a function that
693 accepts the other without doing any physical conversion. Therefore, no
694 type conversion call is really inserted in this case.
700 And, if the function is called with an argument of type
<type>integer
</type>,
701 the parser will try to convert that to
<type>text
</type>:
703 SELECT substr(
1234,
3);
704 ERROR: function substr(integer, integer) does not exist
705 HINT: No function matches the given name and argument types. You might need
706 to add explicit type casts.
709 This does not work because
<type>integer<
/> does not have an implicit cast
710 to
<type>text<
/>. An explicit cast will work, however:
712 SELECT substr(CAST (
1234 AS text),
3);
724 <sect1 id=
"typeconv-query">
725 <title>Value Storage
</title>
728 Values to be inserted into a table are converted to the destination
729 column's data type according to the
734 <title>Value Storage Type Conversion
</title>
736 <step performance=
"required">
738 Check for an exact match with the target.
742 <step performance=
"required">
744 Otherwise, try to convert the expression to the target type. This will succeed
745 if there is a registered cast between the two types.
746 If the expression is an unknown-type literal, the contents of
747 the literal string will be fed to the input conversion routine for the target
752 <step performance=
"required">
754 Check to see if there is a sizing cast for the target type. A sizing
755 cast is a cast from that type to itself. If one is found in the
756 <structname>pg_cast<
/> catalog, apply it to the expression before storing
757 into the destination column. The implementation function for such a cast
758 always takes an extra parameter of type
<type>integer
</type>, which receives
759 the destination column's declared length (actually, its
760 <structfield>atttypmod<
/> value; the interpretation of
761 <structfield>atttypmod<
/> varies for different data types). The cast function
762 is responsible for applying any length-dependent semantics such as size
763 checking or truncation.
770 <title><type>character
</type> Storage Type Conversion
</title>
773 For a target column declared as
<type>character(
20)
</type> the following statement
774 ensures that the stored value is sized correctly:
777 CREATE TABLE vv (v character(
20));
778 INSERT INTO vv SELECT 'abc' || 'def';
779 SELECT v, length(v) FROM vv;
782 ----------------------+--------
789 What has really happened here is that the two unknown literals are resolved
790 to
<type>text
</type> by default, allowing the
<literal>||
</literal> operator
791 to be resolved as
<type>text
</type> concatenation. Then the
<type>text
</type>
792 result of the operator is converted to
<type>bpchar
</type> (
<quote>blank-padded
793 char<
/>, the internal name of the
<type>character
</type> data type) to match the target
794 column type. (Since the conversion from
<type>text
</type> to
795 <type>bpchar
</type> is binary-coercible, this conversion does
796 not insert any real function call.) Finally, the sizing function
797 <literal>bpchar(bpchar, integer)
</literal> is found in the system catalog
798 and applied to the operator's result and the stored column length. This
799 type-specific function performs the required length check and addition of
805 <sect1 id=
"typeconv-union-case">
806 <title><literal>UNION
</literal>,
<literal>CASE
</literal>, and Related Constructs
</title>
808 <indexterm zone=
"typeconv-union-case">
809 <primary>UNION
</primary>
810 <secondary>determination of result type
</secondary>
813 <indexterm zone=
"typeconv-union-case">
814 <primary>CASE
</primary>
815 <secondary>determination of result type
</secondary>
818 <indexterm zone=
"typeconv-union-case">
819 <primary>ARRAY
</primary>
820 <secondary>determination of result type
</secondary>
823 <indexterm zone=
"typeconv-union-case">
824 <primary>VALUES
</primary>
825 <secondary>determination of result type
</secondary>
828 <indexterm zone=
"typeconv-union-case">
829 <primary>GREATEST
</primary>
830 <secondary>determination of result type
</secondary>
833 <indexterm zone=
"typeconv-union-case">
834 <primary>LEAST
</primary>
835 <secondary>determination of result type
</secondary>
839 SQL
<literal>UNION<
/> constructs must match up possibly dissimilar
840 types to become a single result set. The resolution algorithm is
841 applied separately to each output column of a union query. The
842 <literal>INTERSECT<
/> and
<literal>EXCEPT<
/> constructs resolve
843 dissimilar types in the same way as
<literal>UNION<
/>. The
844 <literal>CASE<
/>,
<literal>ARRAY<
/>,
<literal>VALUES<
/>,
845 <function>GREATEST<
/> and
<function>LEAST<
/> constructs use the identical
846 algorithm to match up their component expressions and select a result
851 <title>Type Resolution for
<literal>UNION
</literal>,
<literal>CASE
</literal>,
852 and Related Constructs
</title>
854 <step performance=
"required">
856 If all inputs are of the same type, and it is not
<type>unknown
</type>,
857 resolve as that type. Otherwise, replace any domain types in the list with
858 their underlying base types.
862 <step performance=
"required">
864 If all inputs are of type
<type>unknown
</type>, resolve as type
865 <type>text
</type> (the preferred type of the string category).
866 Otherwise, the
<type>unknown
</type> inputs will be ignored.
870 <step performance=
"required">
872 If the non-unknown inputs are not all of the same type category, fail.
876 <step performance=
"required">
878 Choose the first non-unknown input type which is a preferred type in
879 that category, if there is one.
883 <step performance=
"required">
885 Otherwise, choose the last non-unknown input type that allows all the
886 preceding non-unknown inputs to be implicitly converted to it. (There
887 always is such a type, since at least the first type in the list must
888 satisfy this condition.)
892 <step performance=
"required">
894 Convert all inputs to the selected type. Fail if there is not a
895 conversion from a given input to the selected type.
901 Some examples follow.
905 <title>Type Resolution with Underspecified Types in a Union
</title>
909 SELECT text 'a' AS
"text" UNION SELECT 'b';
917 Here, the unknown-type literal
<literal>'b'
</literal> will be resolved as type
<type>text
</type>.
922 <title>Type Resolution in a Simple Union
</title>
926 SELECT
1.2 AS
"numeric" UNION SELECT
1;
934 The literal
<literal>1.2<
/> is of type
<type>numeric<
/>,
935 and the
<type>integer
</type> value
<literal>1<
/> can be cast implicitly to
936 <type>numeric<
/>, so that type is used.
941 <title>Type Resolution in a Transposed Union
</title>
945 SELECT
1 AS
"real" UNION SELECT CAST('
2.2' AS REAL);
953 Here, since type
<type>real<
/> cannot be implicitly cast to
<type>integer<
/>,
954 but
<type>integer<
/> can be implicitly cast to
<type>real<
/>, the union
955 result type is resolved as
<type>real<
/>.