CREATE TRANSFORM
CREATE TRANSFORM — define a new transform
Synopsis
CREATE [ OR REPLACE ] TRANSFORM FORtype_nameLANGUAGElang_name( FROM SQL WITH FUNCTIONfrom_sql_function_name[ (argument_type[, ...]) ], TO SQL WITH FUNCTIONto_sql_function_name[ (argument_type[, ...]) ]);
Description
CREATE TRANSFORM defines a new transform.CREATE OR REPLACE TRANSFORM will either create a new transform, or replace an existing definition.
A transform specifies how to adapt a data type to a procedural language. For example, when writing a function in PL/Python using thehstore type, PL/Python has no prior knowledge how to presenthstore 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.
A transform specifies two functions:
A“from SQL” 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.
A“to SQL” 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.
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.)
To be able to create a transform, you must own and haveUSAGE privilege on the type, haveUSAGE privilege on the language, and own and haveEXECUTE privilege on the from-SQL and to-SQL functions, if specified.
Parameters
type_nameThe name of the data type of the transform.
lang_nameThe name of the language of the transform.
from_sql_function_name[(argument_type[, ...])]The name of the function for converting the type from the SQL environment to the language. It must take one argument of type
internaland return typeinternal. 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 returninginternalwithout at least one argument of typeinternal.) 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.to_sql_function_name[(argument_type[, ...])]The name of the function for converting the type from the language to the SQL environment. It must take one argument of type
internaland 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.
Notes
UseDROP TRANSFORM to remove transforms.
Examples
To create a transform for typehstore and languageplpythonu, first set up the type and the language:
CREATE TYPE hstore ...;CREATE LANGUAGE plpythonu ...;
Then create the necessary functions:
CREATE FUNCTION hstore_to_plpython(val internal) RETURNS internalLANGUAGE C STRICT IMMUTABLEAS ...;CREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstoreLANGUAGE C STRICT IMMUTABLEAS ...;
And finally create the transform to connect them all together:
CREATE TRANSFORM FOR hstore LANGUAGE plpythonu ( FROM SQL WITH FUNCTION hstore_to_plpython(internal), TO SQL WITH FUNCTION plpython_to_hstore(internal));
In practice, these commands would be wrapped up in extensions.
Thecontrib section contains a number of extensions that provide transforms, which can serve as real-world examples.
Compatibility
This form ofCREATE TRANSFORM is aPostgreSQL extension. There is aCREATE TRANSFORM command in theSQL standard, but it is for adapting data types to client languages. That usage is not supported byPostgreSQL.