2003-04-22 12:08:08 +02:00
|
|
|
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_cast.sgml,v 1.11 2003/04/22 10:08:08 petere Exp $ -->
|
2002-07-19 01:11:32 +02:00
|
|
|
|
|
|
|
<refentry id="SQL-CREATECAST">
|
|
|
|
<refmeta>
|
|
|
|
<refentrytitle id="SQL-CREATECAST-TITLE">CREATE CAST</refentrytitle>
|
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>CREATE CAST</refname>
|
|
|
|
<refpurpose>define a user-defined cast</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<refsynopsisdiv>
|
|
|
|
<synopsis>
|
|
|
|
CREATE CAST (<replaceable>sourcetype</replaceable> AS <replaceable>targettype</replaceable>)
|
|
|
|
WITH FUNCTION <replaceable>funcname</replaceable> (<replaceable>argtype</replaceable>)
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
[ AS ASSIGNMENT | AS IMPLICIT ]
|
2002-07-19 01:11:32 +02:00
|
|
|
|
|
|
|
CREATE CAST (<replaceable>sourcetype</replaceable> AS <replaceable>targettype</replaceable>)
|
|
|
|
WITHOUT FUNCTION
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
[ AS ASSIGNMENT | AS IMPLICIT ]
|
2002-07-19 01:11:32 +02:00
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1 id="sql-createcast-description">
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE CAST</command> defines a new cast. A cast
|
2002-09-01 04:37:02 +02:00
|
|
|
specifies how to perform a conversion between
|
|
|
|
two data types. For example,
|
2002-07-19 01:11:32 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT CAST(42 AS text);
|
|
|
|
</programlisting>
|
|
|
|
converts the integer constant 42 to type <type>text</type> by
|
|
|
|
invoking a previously specified function, in this case
|
|
|
|
<literal>text(int4)</>. (If no suitable cast has been defined, the
|
|
|
|
conversion fails.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Two types may be <firstterm>binary compatible</firstterm>, which
|
|
|
|
means that they can be converted into one another <quote>for
|
|
|
|
free</quote> without invoking any function. This requires that
|
|
|
|
corresponding values use the same internal representation. For
|
|
|
|
instance, the types <type>text</type> and <type>varchar</type> are
|
|
|
|
binary compatible.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
By default, a cast can be invoked only by an explicit cast request,
|
|
|
|
that is an explicit <literal>CAST(<replaceable>x</> AS
|
|
|
|
<replaceable>typename</>)</literal>,
|
2002-07-19 01:11:32 +02:00
|
|
|
<replaceable>x</><literal>::</><replaceable>typename</>, or
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
<replaceable>typename</>(<replaceable>x</>) construct.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If the cast is marked <literal>AS ASSIGNMENT</> then it can be invoked
|
2003-04-22 12:08:08 +02:00
|
|
|
implicitly when assigning a value to a column of the target data type.
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
For example, supposing that <literal>foo.f1</literal> is a column of
|
2002-07-19 01:11:32 +02:00
|
|
|
type <type>text</type>, then
|
|
|
|
<programlisting>
|
2003-04-22 12:08:08 +02:00
|
|
|
INSERT INTO foo (f1) VALUES (42);
|
2002-07-19 01:11:32 +02:00
|
|
|
</programlisting>
|
|
|
|
will be allowed if the cast from type <type>integer</type> to type
|
|
|
|
<type>text</type> is marked <literal>AS ASSIGNMENT</>, otherwise
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
not.
|
|
|
|
(We generally use the term <firstterm>assignment
|
|
|
|
cast</firstterm> to describe this kind of cast.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If the cast is marked <literal>AS IMPLICIT</> then it can be invoked
|
|
|
|
implicitly in any context, whether assignment or internally in an
|
|
|
|
expression. For example, since <literal>||</> takes <type>text</>
|
2003-04-22 12:08:08 +02:00
|
|
|
operands,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT 'The time is ' || now();
|
|
|
|
</programlisting>
|
|
|
|
will be allowed only if the cast from type <type>timestamp</> to
|
|
|
|
<type>text</type> is marked <literal>AS IMPLICIT</>. Otherwise it
|
2002-10-05 00:08:44 +02:00
|
|
|
will be necessary to write the cast explicitly, for example
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT 'The time is ' || CAST(now() AS text);
|
|
|
|
</programlisting>
|
|
|
|
(We generally use the term <firstterm>implicit
|
2002-07-19 01:11:32 +02:00
|
|
|
cast</firstterm> to describe this kind of cast.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
It is wise to be conservative about marking casts as implicit. An
|
|
|
|
overabundance of implicit casting paths can cause
|
|
|
|
<productname>PostgreSQL</productname> to choose surprising
|
|
|
|
interpretations of commands, or to be unable to resolve commands at
|
|
|
|
all because there are multiple possible interpretations. A good
|
2002-09-01 04:37:02 +02:00
|
|
|
rule of thumb is to make a cast implicitly invokable only for
|
2002-07-19 01:11:32 +02:00
|
|
|
information-preserving transformations between types in the same
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
general type category. For example, the cast from <type>int2</type> to
|
|
|
|
<type>int4</type> can reasonably be implicit, but the cast from
|
|
|
|
<type>float8</type> to <type>int4</type> should probably be
|
|
|
|
assignment-only. Cross-type-category casts, such as <type>text</>
|
|
|
|
to <type>int4</>, are best made explicit-only.
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-08-11 19:44:12 +02:00
|
|
|
To be able to create a cast, you must own the source or the target
|
2003-04-22 12:08:08 +02:00
|
|
|
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.)
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
2003-04-22 12:08:08 +02:00
|
|
|
</refsect1>
|
2002-07-19 01:11:32 +02:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
2002-07-19 01:11:32 +02:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<variablelist>
|
2002-07-19 01:11:32 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>sourcetype</replaceable></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the source data type of the cast.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>targettype</replaceable></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the target data type of the cast.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>funcname</replaceable>(<replaceable>argtype</replaceable>)</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
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 path. The argument type must be identical to the
|
|
|
|
source type, the result data type must match the target type of
|
2003-02-01 23:09:26 +01:00
|
|
|
the cast.
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>WITHOUT FUNCTION</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Indicates that the source type and the target type are binary
|
|
|
|
compatible, so no function is required to perform the cast.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>AS ASSIGNMENT</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
Indicates that the cast may be invoked implicitly in assignment
|
|
|
|
contexts.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>AS IMPLICIT</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Indicates that the cast may be invoked implicitly in any context.
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
|
|
|
|
</refsect1>
|
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Diagnostics</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>CREATE CAST</computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Message returned if the cast was successfully created.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
2002-07-19 01:11:32 +02:00
|
|
|
<refsect1 id="sql-createcast-notes">
|
|
|
|
<title>Notes</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Use <command>DROP CAST</command> to remove user-defined casts.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Remember that if you want to be able to convert types both ways you
|
|
|
|
need to declare casts both ways explicitly.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-11-15 04:11:18 +01:00
|
|
|
Prior to <productname>PostgreSQL</> 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
|
2003-04-22 12:08:08 +02:00
|
|
|
system catalogs. (The built-in cast functions still follow this naming
|
|
|
|
scheme, but they have to be shown as casts in the system catalog <literal>pg_cast</>
|
2002-11-15 04:11:18 +01:00
|
|
|
now.)
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
|
|
|
|
<refsect1 id="sql-createcast-examples">
|
|
|
|
<title>Examples</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To create a cast from type <type>text</type> to type
|
2002-09-01 04:37:02 +02:00
|
|
|
<type>int4</type> using the function <literal>int4(text)</literal>:
|
2002-07-19 01:11:32 +02:00
|
|
|
<programlisting>
|
|
|
|
CREATE CAST (text AS int4) WITH FUNCTION int4(text);
|
|
|
|
</programlisting>
|
|
|
|
(This cast is already predefined in the system.)
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
|
|
|
|
<refsect1 id="sql-createcast-compat">
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <command>CREATE CAST</command> command conforms to SQL99,
|
2003-04-22 12:08:08 +02:00
|
|
|
except that SQL99 does not make provisions for binary-compatible
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
types. <literal>AS IMPLICIT</> is a <productname>PostgreSQL</productname>
|
|
|
|
extension, too.
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
|
|
|
|
<refsect1 id="sql-createcast-seealso">
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<xref linkend="sql-createfunction" endterm="sql-createfunction-title">,
|
|
|
|
<xref linkend="sql-createtype" endterm="sql-createtype-title">,
|
2003-03-25 17:15:44 +01:00
|
|
|
<xref linkend="sql-dropcast" endterm="sql-dropcast-title">
|
2002-07-19 01:11:32 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode:sgml
|
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|