2 .\" Title: CREATE TRANSFORM
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE TRANSFORM" "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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_TRANSFORM \- define a new transform
35 CREATE [ OR REPLACE ] TRANSFORM FOR \fItype_name\fR LANGUAGE \fIlang_name\fR (
36 FROM SQL WITH FUNCTION \fIfrom_sql_function_name\fR [ (\fIargument_type\fR [, \&.\&.\&.]) ],
37 TO SQL WITH FUNCTION \fIto_sql_function_name\fR [ (\fIargument_type\fR [, \&.\&.\&.]) ]
42 \fBCREATE TRANSFORM\fR
43 defines a new transform\&.
44 \fBCREATE OR REPLACE TRANSFORM\fR
45 will either create a new transform, or replace an existing definition\&.
47 A transform specifies how to adapt a data type to a procedural language\&. For example, when writing a function in PL/Python using the
49 type, PL/Python has no prior knowledge how to present
51 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\&.
53 A transform specifies two functions:
65 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\&.
78 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\&.
81 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\&.)
83 To be able to create a transform, you must own and have
85 privilege on the type, have
87 privilege on the language, and own and have
89 privilege on the from\-SQL and to\-SQL functions, if specified\&.
94 The name of the data type of the transform\&.
99 The name of the language of the transform\&.
102 \fIfrom_sql_function_name\fR[(\fIargument_type\fR [, \&.\&.\&.])]
104 The name of the function for converting the type from the SQL environment to the language\&. It must take one argument of type
107 internal\&. 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 SQL\-level function returning
109 without at least one argument of type
110 internal\&.) The actual return value will be something specific to the language implementation\&. If no argument list is specified, the function name must be unique in its schema\&.
113 \fIto_sql_function_name\fR[(\fIargument_type\fR [, \&.\&.\&.])]
115 The name of the function for converting the type from the language to the SQL environment\&. It must take one argument of type
117 and return the type that is the type for the transform\&. The actual argument value will be something specific to the language implementation\&. If no argument list is specified, the function name must be unique in its schema\&.
123 to remove transforms\&.
126 To create a transform for type
129 plpython3u, first set up the type and the language:
135 CREATE TYPE hstore \&.\&.\&.;
137 CREATE EXTENSION plpython3u;
143 Then create the necessary functions:
149 CREATE FUNCTION hstore_to_plpython(val internal) RETURNS internal
150 LANGUAGE C STRICT IMMUTABLE
153 CREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstore
154 LANGUAGE C STRICT IMMUTABLE
161 And finally create the transform to connect them all together:
167 CREATE TRANSFORM FOR hstore LANGUAGE plpython3u (
168 FROM SQL WITH FUNCTION hstore_to_plpython(internal),
169 TO SQL WITH FUNCTION plpython_to_hstore(internal)
176 In practice, these commands would be wrapped up in an extension\&.
180 section contains a number of extensions that provide transforms, which can serve as real\-world examples\&.
184 \fBCREATE TRANSFORM\fR
187 extension\&. There is a
188 \fBCREATE TRANSFORM\fR
191 standard, but it is for adapting data types to client languages\&. That usage is not supported by
195 CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7)),
196 CREATE LANGUAGE (\fBCREATE_LANGUAGE\fR(7)),
197 CREATE TYPE (\fBCREATE_TYPE\fR(7)),
198 DROP TRANSFORM (\fBDROP_TRANSFORM\fR(7))