CREATE_TRANSFORM(7pg15)

πŸ“¦ version 15.19, in opensuse-leap 16.0

πŸ’… CREATE TRANSFORM β€” define a new transform (7pg15) πŸ¦‹

okay bestie, this is the CREATE TRANSFORM page from the PostgreSQL 15.19 docs, and honestly? it's giving "niche but iconic." stay with me πŸ’–


✨ NAME ✨

CREATE_TRANSFORM β€” literally just: define a new transform. that's the whole vibe.


🌸 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 [, ...]) ]
);

look at her. flawless. no notes πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so CREATE TRANSFORM defines a new transform. and CREATE OR REPLACE TRANSFORM? she's flexible β€” she'll either create a brand-new transform or replace an existing definition. main character energy, honestly.

a transform specifies how to adapt a data type to a procedural language. for example ✨ when you're writing a function in PL/Python using the hstore type, PL/Python has no prior knowledge how to present hstore 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. like... not the text rep when you could have a whole list. embarrassing for everyone involved 😭

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. (and to prevent a transformation in a certain direction from happening at all, you could also write a transform function that always errors out β€” chaotic, but valid πŸ’€)

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 privileges, no transform. rules are rules πŸ‘‘


🌈 PARAMETERS 🌈

  • type_name β€” the name of the data type of the transform. πŸ¦‹
  • lang_name β€” the 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 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 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 for converting 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. πŸŽ€
ngl the internal-argument rules are lowkey the trickiest part here β€” read them twice, bestie 🫢

⭐ NOTES ⭐

use DROP TRANSFORM to remove transforms. that's it. that's the note. short and slay πŸ’…


πŸ’œ EXAMPLES πŸ’œ

to 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. πŸ‘‘

the contrib section contains a number of extensions that provide transforms, which can serve as real-world examples β€” go look, it's giving reference material πŸ¦‹


🧚 COMPATIBILITY 🧚

this form of CREATE TRANSFORM is a PostgreSQL extension. there is a CREATE TRANSFORM command in the SQL standard, but it is for adapting data types to client languages. that usage is not supported by PostgreSQL. so like... same name, different girl πŸ’€


🌸 SEE ALSO 🌸


okay that's the whole page, bestie β€” you ate that πŸ’…βœ¨ this little masterpiece is from PostgreSQL 15.19, dated 2026, and it lives at CREATE TRANSFORM(7) in the manual. go forth and transform πŸ¦‹πŸŽ€

πŸ“¦ where this page comes from