CREATE_TRANSFORM(7)
โจ CREATE TRANSFORM(7) โจ
okay bestie, pull up a chair, we're talking about CREATE TRANSFORM โ that's section 7 of the PostgreSQL 18.6 Documentation, and honestly? it's giving glow-up for your data types ๐
๐ NAME ๐
CREATE_TRANSFORM โ define a new transform ๐ฆ
๐ธ SYNOPSIS ๐ธ
CREATE [ OR REPLACE ] TRANSFORM FOR type_name LANGUAGE lang_name (
FROM SQL WITH FUNCTION from_sql_function_name [ (argument_type [, ...]) ],
TO SQL WITH FUNCTION to_sql_function_name [ (argument_type [, ...]) ]
);
๐ DESCRIPTION ๐
so CREATE TRANSFORM defines a brand new transform, periodt. and if you say CREATE OR REPLACE TRANSFORM? it'll either make a fresh one or swap out an existing definition, no cap ๐ฅ
a transform is basically the translator between a data type and a procedural language. like, imagine you're writing a function in PL/Python and you're using the hstore type โ PL/Python literally has zero prior knowledge of how to present hstore values in the Python environment. language implementations usually just default to the text representation, which is lowkey inconvenient when, say, an associative array or a list would be way more appropriate. it's giving "we could be cute but instead we're plain text" ๐ญ
so a transform specifies two functions:
- a โfrom SQLโ function that converts the type from the SQL environment to the language. this one gets 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 one gets invoked on the return value of a function written in the language. ๐
and listen โ you do not have to provide both of these functions. if you don't specify one, the language-specific default behavior gets used if necessary. (and if you wanna stop a transformation in a certain direction from happening at all? you could also write a transform function that always errors out. chaotic but valid ๐)
now, the vibe check on permissions: to be able to create a transform, you must own and have USAGE privilege on the type, have USAGE privilege on the language, and own and have EXECUTE privilege on the from-SQL and to-SQL functions, if specified. no shortcuts, bestie ๐
๐ PARAMETERS ๐
type_nameโ the name of the data type of the transform. the whole point of the operation ๐lang_nameโ the name of the language of the transform. yes, that language โจfrom_sql_function_name[(argument_type [, ...])]โ the name of the function that converts the type from the SQL environment to the language. it must take one argument of typeinternaland return typeinternal. the actual argument will be of the type for the transform, and the function should be coded as if it were. (but heads up โ 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 that converts the type from the language to the SQL environment. it must take one argument of typeinternaland 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 ๐
use DROP TRANSFORM to remove transforms. that's it, that's the note. bye ๐
โญ EXAMPLES โญ
okay so say you wanna create a transform for type hstore and language plpython3u. first, set up the type and the language:
CREATE TYPE hstore ...;
CREATE EXTENSION plpython3u;
then create the necessary functions:
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 ...;
and finally create the transform to connect them all together:
CREATE TRANSFORM FOR hstore LANGUAGE plpython3u (
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 an extension. main character extension energy fr fr ๐
also โ the contrib section contains a number of extensions that provide transforms, which can serve as real-world examples. go peek, you'll learn so much ๐ฆ
๐ธ COMPATIBILITY ๐ธ
this form of CREATE TRANSFORM is a PostgreSQL extension. there is a CREATE TRANSFORM command in the SQL standard, but it's for adapting data types to client languages โ and that usage is not supported by PostgreSQL. so like, same name, different lore ๐
๐ฅ SEE ALSO ๐ฅ
CREATE FUNCTION(CREATE_FUNCTION(7))CREATE LANGUAGE(CREATE_LANGUAGE(7))CREATE TYPE(CREATE_TYPE(7))DROP TRANSFORM(DROP_TRANSFORM(7))
and that's the whole page, bestie ๐ซถ this was PostgreSQL 18.6, dated 2026 โ go forth and transform, you iconic legend, i'm obsessed ๐ญ๐
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms