Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/typeconv.sgml`
fb4356b1748b5b7e0ed835bb565c3f3387bf55a83d3690530000000100000fa2

overloading, the function name alone does not uniquely identify the function
to be called; the parser must select the right function based on the data
types of the supplied arguments.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
Operators
</term>
<listitem>
<para>
<productname>PostgreSQL</productname> allows expressions with
prefix (one-argument) operators,
as well as infix (two-argument) operators.  Like functions, operators can
be overloaded, so the same problem of selecting the right operator
exists.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
Value Storage
</term>
<listitem>
<para>
<acronym>SQL</acronym> <command>INSERT</command> and <command>UPDATE</command> statements place the results of
expressions into a table. The expressions in the statement must be matched up
with, and perhaps converted to, the types of the target columns.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>UNION</literal>, <literal>CASE</literal>, and related constructs
</term>
<listitem>
<para>
Since all query results from a unionized <command>SELECT</command> statement
must appear in a single set of columns, the types of the results of each
<command>SELECT</command> clause must be matched up and converted to a uniform set.
Similarly, the result expressions of a <literal>CASE</literal> construct must be
converted to a common type so that the <literal>CASE</literal> expression as a whole
has a known output type.  Some other constructs, such
as <literal>ARRAY[]</literal> and the <function>GREATEST</function>
and <function>LEAST</function> functions, likewise require determination of a
common type for several subexpressions.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>

<para>
The system catalogs store information about which conversions, or
<firstterm>casts</firstterm>, exist between which data types, and how to
perform those conversions.  Additional casts can be added by the user
with the <xref linkend="sql-createcast"/>
command.  (This is usually
done in conjunction with defining new data types.  The set of casts
between built-in types has been carefully crafted and is best not
altered.)
</para>

<indexterm>
 <primary>data type</primary>
 <secondary>category</secondary>
</indexterm>

<para>
An additional heuristic provided by the parser allows improved determination
of the proper casting behavior among groups of types that have implicit casts.
Data types are divided into several basic <firstterm>type
categories</firstterm>, including <type>boolean</type>, <type>numeric</type>,
<type>string</type>, <type>bitstring</type>, <type>datetime</type>,
<type>timespan</type>, <type>geometric</type>, <type>network</type>, and
user-defined.  (For a list see <xref linkend="catalog-typcategory-table"/>;
but note it is also possible to create custom type categories.)  Within each
category there can be one or more <firstterm>preferred types</firstterm>, which
are preferred when there is a choice of possible types.  With careful selection
of preferred types and available implicit casts, it is possible to ensure that
ambiguous expressions (those with multiple candidate parsing solutions) can be
resolved in a useful way.
</para>

<para>
All type conversion rules are designed with several principles in mind:

<itemizedlist>
<listitem>
<para>
Implicit conversions should never have surprising or unpredictable outcomes.
</para>
</listitem>

<listitem>
<para>
There should be no extra overhead in the parser or executor
if a query does not need implicit type conversion.
That is, if a query is well-formed and the types already match, then the query should execute
without spending extra time in the parser and without introducing unnecessary implicit conversion
calls in the query.
</para>
</listitem>

<listitem>
<para>
Additionally, if a query usually requires an implicit conversion for a function, and
if then the user defines a new function with the correct argument types, the parser
should use this new function and no

Title: Type Conversion Rules in PostgreSQL
Summary
This section discusses the rules and principles governing type conversion in PostgreSQL, including the use of casts, type categories, and preferred types to resolve ambiguous expressions, with a focus on implicit conversions, parser efficiency, and predictability of outcomes.