2020-05-15 14:52:24 +02:00
|
|
|
<!--
|
|
|
|
doc/src/sgml/ref/create_transform.sgml
|
|
|
|
PostgreSQL documentation
|
|
|
|
-->
|
2015-04-26 16:33:14 +02:00
|
|
|
|
2017-10-20 03:16:39 +02:00
|
|
|
<refentry id="sql-createtransform">
|
2015-04-26 16:33:14 +02:00
|
|
|
<indexterm zone="sql-createtransform">
|
|
|
|
<primary>CREATE TRANSFORM</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<refmeta>
|
|
|
|
<refentrytitle>CREATE TRANSFORM</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum>
|
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>CREATE TRANSFORM</refname>
|
|
|
|
<refpurpose>define a new transform</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<refsynopsisdiv>
|
|
|
|
<synopsis>
|
|
|
|
CREATE [ OR REPLACE ] TRANSFORM FOR <replaceable>type_name</replaceable> LANGUAGE <replaceable>lang_name</replaceable> (
|
2017-03-10 05:58:48 +01:00
|
|
|
FROM SQL WITH FUNCTION <replaceable>from_sql_function_name</replaceable> [ (<replaceable>argument_type</replaceable> [, ...]) ],
|
|
|
|
TO SQL WITH FUNCTION <replaceable>to_sql_function_name</replaceable> [ (<replaceable>argument_type</replaceable> [, ...]) ]
|
2015-04-26 16:33:14 +02:00
|
|
|
);
|
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1 id="sql-createtransform-description">
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE TRANSFORM</command> defines a new transform.
|
|
|
|
<command>CREATE OR REPLACE TRANSFORM</command> will either create a new
|
|
|
|
transform, or replace an existing definition.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A transform specifies how to adapt a data type to a procedural language.
|
|
|
|
For example, when writing a function in PL/Python using
|
|
|
|
the <type>hstore</type> type, PL/Python has no prior knowledge how to
|
|
|
|
present <type>hstore</type> values in the Python environment. Language
|
|
|
|
implementations usually default to using the text representation, but that
|
|
|
|
is inconvenient when, for example, an associative array or a list would be
|
|
|
|
more appropriate.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A transform specifies two functions:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A <quote>from SQL</quote> function that converts the type from the SQL
|
|
|
|
environment to the language. This function will be invoked on the
|
|
|
|
arguments of a function written in the language.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A <quote>to SQL</quote> function that converts the type from the
|
|
|
|
language to the SQL environment. This function will be invoked on the
|
|
|
|
return value of a function written in the language.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
It is not necessary to provide both of these functions. If one is not
|
|
|
|
specified, the language-specific default behavior will be used if
|
|
|
|
necessary. (To prevent a transformation in a certain direction from
|
|
|
|
happening at all, you could also write a transform function that always
|
|
|
|
errors out.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To be able to create a transform, you must own and
|
|
|
|
have <literal>USAGE</literal> privilege on the type, have
|
|
|
|
<literal>USAGE</literal> privilege on the language, and own and
|
|
|
|
have <literal>EXECUTE</literal> privilege on the from-SQL and to-SQL
|
|
|
|
functions, if specified.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>type_name</replaceable></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the data type of the transform.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>lang_name</replaceable></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the language of the transform.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2017-03-10 05:58:48 +01:00
|
|
|
<term><literal><replaceable>from_sql_function_name</replaceable>[(<replaceable>argument_type</replaceable> [, ...])]</literal></term>
|
2015-04-26 16:33:14 +02:00
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the function for converting the type from the SQL
|
|
|
|
environment to the language. It must take one argument of
|
|
|
|
type <type>internal</type> and return type <type>internal</type>. The
|
|
|
|
actual argument will be of the type for the transform, and the function
|
|
|
|
should be coded as if it were. (But it is not allowed to declare an
|
2016-01-06 00:01:40 +01:00
|
|
|
SQL-level function returning <type>internal</type> without at
|
2015-04-26 16:33:14 +02:00
|
|
|
least one argument of type <type>internal</type>.) The actual return
|
|
|
|
value will be something specific to the language implementation.
|
2017-03-10 05:58:48 +01:00
|
|
|
If no argument list is specified, the function name must be unique in
|
|
|
|
its schema.
|
2015-04-26 16:33:14 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2017-03-10 05:58:48 +01:00
|
|
|
<term><literal><replaceable>to_sql_function_name</replaceable>[(<replaceable>argument_type</replaceable> [, ...])]</literal></term>
|
2015-04-26 16:33:14 +02:00
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the function for converting the type from the language to
|
|
|
|
the SQL environment. It must take one argument of type
|
|
|
|
<type>internal</type> and return the type that is the type for the
|
|
|
|
transform. The actual argument value will be something specific to the
|
|
|
|
language implementation.
|
2017-03-10 05:58:48 +01:00
|
|
|
If no argument list is specified, the function name must be unique in
|
|
|
|
its schema.
|
2015-04-26 16:33:14 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 id="sql-createtransform-notes">
|
|
|
|
<title>Notes</title>
|
|
|
|
|
|
|
|
<para>
|
Improve <xref> vs. <command> formatting in the documentation
SQL commands are generally marked up as <command>, except when a link
to a reference page is used using <xref>. But the latter doesn't
create monospace markup, so this looks strange especially when a
paragraph contains a mix of links and non-links.
We considered putting <command> in the <refentrytitle> on the target
side, but that creates some formatting side effects elsewhere.
Generally, it seems safer to solve this on the link source side.
We can't put the <xref> inside the <command>; the DTD doesn't allow
this. DocBook 5 would allow the <command> to have the linkend
attribute itself, but we are not there yet.
So to solve this for now, convert the <xref>s to <link> plus
<command>. This gives the correct look and also gives some more
flexibility what we can put into the link text (e.g., subcommands or
other clauses). In the future, these could then be converted to
DocBook 5 style.
I haven't converted absolutely all xrefs to SQL command reference
pages, only those where we care about the appearance of the link text
or where it was otherwise appropriate to make the appearance match a
bit better. Also in some cases, the links where repetitive, so in
those cases the links where just removed and replaced by a plain
<command>. In cases where we just want the link and don't
specifically care about the generated link text (typically phrased
"for further information see <xref ...>") the xref is kept.
Reported-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Discussion: https://www.postgresql.org/message-id/flat/87o8pco34z.fsf@wibble.ilmari.org
2020-10-03 16:16:51 +02:00
|
|
|
Use <link linkend="sql-droptransform"><command>DROP TRANSFORM</command></link> to remove transforms.
|
2015-04-26 16:33:14 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 id="sql-createtransform-examples">
|
|
|
|
<title>Examples</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To create a transform for type <type>hstore</type> and language
|
2022-03-08 03:30:57 +01:00
|
|
|
<literal>plpython3u</literal>, first set up the type and the language:
|
2015-04-26 16:33:14 +02:00
|
|
|
<programlisting>
|
|
|
|
CREATE TYPE hstore ...;
|
|
|
|
|
2022-03-08 03:30:57 +01:00
|
|
|
CREATE EXTENSION plpython3u;
|
2015-04-26 16:33:14 +02:00
|
|
|
</programlisting>
|
|
|
|
Then create the necessary functions:
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION hstore_to_plpython(val internal) RETURNS internal
|
|
|
|
LANGUAGE C STRICT IMMUTABLE
|
|
|
|
AS ...;
|
|
|
|
|
|
|
|
CREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstore
|
|
|
|
LANGUAGE C STRICT IMMUTABLE
|
|
|
|
AS ...;
|
|
|
|
</programlisting>
|
|
|
|
And finally create the transform to connect them all together:
|
|
|
|
<programlisting>
|
2022-03-08 03:30:57 +01:00
|
|
|
CREATE TRANSFORM FOR hstore LANGUAGE plpython3u (
|
2015-04-26 16:33:14 +02:00
|
|
|
FROM SQL WITH FUNCTION hstore_to_plpython(internal),
|
|
|
|
TO SQL WITH FUNCTION plpython_to_hstore(internal)
|
|
|
|
);
|
|
|
|
</programlisting>
|
2018-04-27 19:42:03 +02:00
|
|
|
In practice, these commands would be wrapped up in an extension.
|
2015-04-26 16:33:14 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <filename>contrib</filename> section contains a number of extensions
|
|
|
|
that provide transforms, which can serve as real-world examples.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 id="sql-createtransform-compat">
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This form of <command>CREATE TRANSFORM</command> is a
|
|
|
|
<productname>PostgreSQL</productname> extension. There is a <command>CREATE
|
|
|
|
TRANSFORM</command> command in the <acronym>SQL</acronym> standard, but it
|
|
|
|
is for adapting data types to client languages. That usage is not supported
|
|
|
|
by <productname>PostgreSQL</productname>.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 id="sql-createtransform-seealso">
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<para>
|
2017-11-23 15:39:47 +01:00
|
|
|
<xref linkend="sql-createfunction"/>,
|
|
|
|
<xref linkend="sql-createlanguage"/>,
|
|
|
|
<xref linkend="sql-createtype"/>,
|
|
|
|
<xref linkend="sql-droptransform"/>
|
2015-04-26 16:33:14 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
</refentry>
|