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 type internal and return type internal. 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 returning internal without at least one argument of type 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. ๐Ÿงš
  • 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 type internal 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. ๐Ÿ“

๐Ÿ’œ 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 ๐Ÿ”ฅ


and that's the whole page, bestie ๐Ÿซถ this was PostgreSQL 18.6, dated 2026 โ€” go forth and transform, you iconic legend, i'm obsessed ๐Ÿ˜ญ๐Ÿ’–