{"Entry":{"collection":"sql","key":"create-transform","name":"CREATE TRANSFORM","aliases":["createtransform"],"metadata":{"aliases":["createtransform"],"changed_in":["10"],"changes":[{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"9.5"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters"],"removed":[]},"status":"changed","synopsis":{"added":["FROM SQL WITH FUNCTION from_sql_function_name [ (argument_type [, ...]) ],","TO SQL WITH FUNCTION to_sql_function_name [ (argument_type [, ...]) ]"],"removed":[]},"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["examples"],"removed":[]},"status":"changed","synopsis":null,"to":"11"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["examples"],"removed":[]},"status":"changed","synopsis":null,"to":"15"}],"content_hash":"e288278e6cec7f7ccb7987f128c2b64ee39d0ccbb0f1ab6e4e4ffa67e2fb3fbb","editorial":{},"first_version":"9.5","group":"routine","imported_at":"2026-09-30T17:43:37.324694+08:00","last_version":"20","name":"CREATE TRANSFORM","object":"TRANSFORM","position":2016,"present_in":["9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"define a new transform","purpose_zh":"","related":["create-function","create-language","create-type","drop-transform"],"slug":"create-transform","source_rev":"a709ab85","synopsis":"CREATE [ OR REPLACE ] TRANSFORM FOR type_name LANGUAGE lang_name (\nFROM SQL WITH FUNCTION from_sql_function_name [ (argument_type [, ...]) ],\nTO SQL WITH FUNCTION to_sql_function_name [ (argument_type [, ...]) ]\n);","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-transform","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"create-transform","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-CREATETRANSFORM","file":"sql-createtransform.html","lang":"en","name":"CREATE TRANSFORM","purpose":"define a new transform","purpose_zh":"","related":["create-function","create-language","create-type","drop-transform"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TRANSFORM\u003c/code\u003e defines a new transform. \u003ccode class=\"command\"\u003eCREATE OR REPLACE TRANSFORM\u003c/code\u003e will either create a new transform, or replace an existing definition.\u003c/p\u003e\u003cp\u003eA transform specifies how to adapt a data type to a procedural language. For example, when writing a function in PL/Python using the \u003ccode class=\"type\"\u003ehstore\u003c/code\u003e type, PL/Python has no prior knowledge how to present \u003ccode class=\"type\"\u003ehstore\u003c/code\u003e 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.\u003c/p\u003e\u003cp\u003eA transform specifies two functions:\u003c/p\u003e\u003cdiv class=\"itemizedlist\"\u003e\u003cul class=\"itemizedlist\"\u003e\u003cli class=\"listitem\"\u003e\u003cp\u003eA \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003efrom SQL\u003c/span\u003e”\u003c/span\u003e 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.\u003c/p\u003e\u003c/li\u003e\u003cli class=\"listitem\"\u003e\u003cp\u003eA \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eto SQL\u003c/span\u003e”\u003c/span\u003e 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.\u003c/p\u003e\u003c/li\u003e\u003c/ul\u003e\u003c/div\u003e\u003cp\u003eIt 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.)\u003c/p\u003e\u003cp\u003eTo be able to create a transform, you must own and have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege on the type, have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege on the language, and own and have \u003ccode class=\"literal\"\u003eEXECUTE\u003c/code\u003e privilege on the from-SQL and to-SQL functions, if specified.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the data type of the transform.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the language of the transform.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003efrom_sql_function_name\u003c/code\u003e\u003c/em\u003e[(\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...])]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the function for converting the type from the SQL environment to the language. It must take one argument of type \u003ccode class=\"type\"\u003einternal\u003c/code\u003e and return type \u003ccode class=\"type\"\u003einternal\u003c/code\u003e. 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 \u003ccode class=\"type\"\u003einternal\u003c/code\u003e without at least one argument of type \u003ccode class=\"type\"\u003einternal\u003c/code\u003e.) 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.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eto_sql_function_name\u003c/code\u003e\u003c/em\u003e[(\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...])]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the function for converting the type from the language to the SQL environment. It must take one argument of type \u003ccode class=\"type\"\u003einternal\u003c/code\u003e 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.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eUse \u003ca href=\"/docs/18/sql-droptransform.html\" title=\"DROP TRANSFORM\"\u003e\u003ccode class=\"command\"\u003eDROP TRANSFORM\u003c/code\u003e\u003c/a\u003e to remove transforms.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eTo create a transform for type \u003ccode class=\"type\"\u003ehstore\u003c/code\u003e and language \u003ccode class=\"literal\"\u003eplpython3u\u003c/code\u003e, first set up the type and the language:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TYPE hstore ...;\n\nCREATE EXTENSION plpython3u;\n\u003c/pre\u003e\u003cp\u003eThen create the necessary functions:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE FUNCTION hstore_to_plpython(val internal) RETURNS internal\nLANGUAGE C STRICT IMMUTABLE\nAS ...;\n\nCREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstore\nLANGUAGE C STRICT IMMUTABLE\nAS ...;\n\u003c/pre\u003e\u003cp\u003eAnd finally create the transform to connect them all together:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TRANSFORM FOR hstore LANGUAGE plpython3u (\n    FROM SQL WITH FUNCTION hstore_to_plpython(internal),\n    TO SQL WITH FUNCTION plpython_to_hstore(internal)\n);\n\u003c/pre\u003e\u003cp\u003eIn practice, these commands would be wrapped up in an extension.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"filename\"\u003econtrib\u003c/code\u003e section contains a number of extensions that provide transforms, which can serve as real-world examples.\u003c/p\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThis form of \u003ccode class=\"command\"\u003eCREATE TRANSFORM\u003c/code\u003e is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension. There is a \u003ccode class=\"command\"\u003eCREATE TRANSFORM\u003c/code\u003e command in the \u003cacronym\u003eSQL\u003c/acronym\u003e standard, but it is for adapting data types to client languages. That usage is not supported by \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cp\u003e\u003ca href=\"/wiki/sql/create-function/?v=18\" title=\"CREATE FUNCTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE FUNCTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-language/?v=18\" title=\"CREATE LANGUAGE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-type/?v=18\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-transform/?v=18\" title=\"DROP TRANSFORM\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP TRANSFORM\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"CREATE [ OR REPLACE ] TRANSFORM FOR \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e LANGUAGE \u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e (\n    FROM SQL WITH FUNCTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003efrom_sql_function_name\u003c/code\u003e\u003c/em\u003e [ (\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...]) ],\n    TO SQL WITH FUNCTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003eto_sql_function_name\u003c/code\u003e\u003c/em\u003e [ (\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...]) ]\n);","synopsis_text":"CREATE [ OR REPLACE ] TRANSFORM FOR type_name LANGUAGE lang_name (\nFROM SQL WITH FUNCTION from_sql_function_name [ (argument_type [, ...]) ],\nTO SQL WITH FUNCTION to_sql_function_name [ (argument_type [, ...]) ]\n);"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"create-transform","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"CREATE TRANSFORM","Summary":"定义一个新的转换","BodyHTML":"\u003cpre\u003eCREATE [ OR REPLACE ] TRANSFORM FOR type_name LANGUAGE lang_name (\nFROM SQL WITH FUNCTION from_sql_function_name [ (argument_type [, ...]) ],\nTO SQL WITH FUNCTION to_sql_function_name [ (argument_type [, ...]) ]\n);\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE TRANSFORM\u003c/code\u003e定义一种新的转换。\u003ccode\u003eCREATE OR REPLACE TRANSFORM\u003c/code\u003e会创建一种新的转换，或替换现有定义。\u003c/p\u003e\u003cp\u003e转换指定如何将一种数据类型适配到某种过程语言。例如，在用 PL/Python 编写一个使用\u003ccode\u003ehstore\u003c/code\u003e类型的函数时，PL/Python 事先并不知道该如何在 Python 环境中表示\u003ccode\u003ehstore\u003c/code\u003e值。语言实现通常默认使用文本表示，但这并不方便，例如在更适合使用关联数组或列表时就是如此。\u003c/p\u003e\u003cp\u003e转换指定两个函数：\u003c/p\u003e\u003cdiv\u003e\u003cul\u003e\u003cli\u003e\u003cp\u003e一个\u003cspan\u003e“\u003cspan\u003efrom SQL\u003c/span\u003e”\u003c/span\u003e函数，用于将该类型从 SQL 环境转换到该语言。该函数会作用于用这种语言编写的函数的参数。\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003e一个\u003cspan\u003e“\u003cspan\u003eto SQL\u003c/span\u003e”\u003c/span\u003e函数，用于将该类型从该语言转换到 SQL 环境。该函数会作用于用这种语言编写的函数的返回值。\u003c/p\u003e\u003c/li\u003e\u003c/ul\u003e\u003c/div\u003e\u003cp\u003e不必同时提供这两个函数。如果其中一个未指定，则会在需要时使用该语言相关的默认行为。（若要彻底阻止某一方向上的转换，也可以编写一个总是报错的转换函数。）\u003c/p\u003e\u003cp\u003e要创建一种转换，你必须拥有该类型并具有其上的 \u003ccode\u003eUSAGE\u003c/code\u003e权限，具有该语言上的 \u003ccode\u003eUSAGE\u003c/code\u003e权限，并且在指定了 from-SQL 和 to-SQL 函数时，还必须拥有这些函数并具有其上的\u003ccode\u003eEXECUTE\u003c/code\u003e权限。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该转换的数据类型名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该转换所用语言的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003e\u003cem\u003e\u003ccode\u003efrom_sql_function_name\u003c/code\u003e\u003c/em\u003e[(\u003cem\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...])]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于将该类型从 SQL 环境转换到该语言的函数名称。它必须接受一个 \u003ccode\u003einternal\u003c/code\u003e类型参数，并返回\u003ccode\u003einternal\u003c/code\u003e类型。实际传入的参数将是该转换对应的类型，因此编写该函数时应按这一点处理。（但不允许声明一个返回\u003ccode\u003einternal\u003c/code\u003e、却没有至少一个 \u003ccode\u003einternal\u003c/code\u003e类型参数的 SQL 层函数。）实际返回值将是语言实现特有的某个对象。如果未指定参数列表，则函数名在其所在模式中必须唯一。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003e\u003cem\u003e\u003ccode\u003eto_sql_function_name\u003c/code\u003e\u003c/em\u003e[(\u003cem\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...])]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于将该类型从该语言转换到 SQL 环境的函数名称。它必须接受一个 \u003ccode\u003einternal\u003c/code\u003e类型参数，并返回该转换对应的类型。实际参数值将是语言实现特有的某个对象。如果未指定参数列表，则函数名在其所在模式中必须唯一。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e使用\u003ca href=\"/docs/18/sql-droptransform.html\" title=\"DROP TRANSFORM\" rel=\"nofollow\"\u003e\u003ccode\u003eDROP TRANSFORM\u003c/code\u003e\u003c/a\u003e删除转换。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e要为类型\u003ccode\u003ehstore\u003c/code\u003e和语言\u003ccode\u003eplpython3u\u003c/code\u003e创建转换，首先准备好该类型和该语言：\u003c/p\u003e\u003cpre\u003eCREATE TYPE hstore ...;\n\nCREATE EXTENSION plpython3u;\n\u003c/pre\u003e\u003cp\u003e然后创建必要的函数：\u003c/p\u003e\u003cpre\u003eCREATE FUNCTION hstore_to_plpython(val internal) RETURNS internal\nLANGUAGE C STRICT IMMUTABLE\nAS ...;\n\nCREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstore\nLANGUAGE C STRICT IMMUTABLE\nAS ...;\n\u003c/pre\u003e\u003cp\u003e最后创建把它们连接在一起的转换：\u003c/p\u003e\u003cpre\u003eCREATE TRANSFORM FOR hstore LANGUAGE plpython3u (\n    FROM SQL WITH FUNCTION hstore_to_plpython(internal),\n    TO SQL WITH FUNCTION plpython_to_hstore(internal)\n);\n\u003c/pre\u003e\u003cp\u003e在实际使用中，这些命令通常会封装在一个扩展中。\u003c/p\u003e\u003cp\u003e\u003ccode\u003econtrib\u003c/code\u003e部分包含若干提供转换的扩展，可以作为实际示例。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e这种形式的\u003ccode\u003eCREATE TRANSFORM\u003c/code\u003e是 \u003cspan\u003ePostgreSQL\u003c/span\u003e的一种扩展。\u003cacronym\u003eSQL\u003c/acronym\u003e标准中也有\u003ccode\u003eCREATE TRANSFORM\u003c/code\u003e命令，但它用于将数据类型适配到客户端语言。\u003cspan\u003ePostgreSQL\u003c/span\u003e不支持那种用法。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cp\u003e\u003ca href=\"/wiki/sql/create-function/?v=18\" title=\"CREATE FUNCTION\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE FUNCTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-language/?v=18\" title=\"CREATE LANGUAGE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-type/?v=18\" title=\"CREATE TYPE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-transform/?v=18\" title=\"DROP TRANSFORM\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP TRANSFORM\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"b41856b6785986aeadc49a02a4b393f8225905a29ed798ea126c9a45b3dc9571","Payload":{"purpose_zh":"定义一个新的转换","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TRANSFORM\u003c/code\u003e定义一种新的转换。\u003ccode class=\"command\"\u003eCREATE OR REPLACE TRANSFORM\u003c/code\u003e会创建一种新的转换，或替换现有定义。\u003c/p\u003e\u003cp\u003e转换指定如何将一种数据类型适配到某种过程语言。例如，在用 PL/Python 编写一个使用\u003ccode class=\"type\"\u003ehstore\u003c/code\u003e类型的函数时，PL/Python 事先并不知道该如何在 Python 环境中表示\u003ccode class=\"type\"\u003ehstore\u003c/code\u003e值。语言实现通常默认使用文本表示，但这并不方便，例如在更适合使用关联数组或列表时就是如此。\u003c/p\u003e\u003cp\u003e转换指定两个函数：\u003c/p\u003e\u003cdiv class=\"itemizedlist\"\u003e\u003cul class=\"itemizedlist\"\u003e\u003cli class=\"listitem\"\u003e\u003cp\u003e一个\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003efrom SQL\u003c/span\u003e”\u003c/span\u003e函数，用于将该类型从 SQL 环境转换到该语言。该函数会作用于用这种语言编写的函数的参数。\u003c/p\u003e\u003c/li\u003e\u003cli class=\"listitem\"\u003e\u003cp\u003e一个\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eto SQL\u003c/span\u003e”\u003c/span\u003e函数，用于将该类型从该语言转换到 SQL 环境。该函数会作用于用这种语言编写的函数的返回值。\u003c/p\u003e\u003c/li\u003e\u003c/ul\u003e\u003c/div\u003e\u003cp\u003e不必同时提供这两个函数。如果其中一个未指定，则会在需要时使用该语言相关的默认行为。（若要彻底阻止某一方向上的转换，也可以编写一个总是报错的转换函数。）\u003c/p\u003e\u003cp\u003e要创建一种转换，你必须拥有该类型并具有其上的 \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e权限，具有该语言上的 \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e权限，并且在指定了 from-SQL 和 to-SQL 函数时，还必须拥有这些函数并具有其上的\u003ccode class=\"literal\"\u003eEXECUTE\u003c/code\u003e权限。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该转换的数据类型名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该转换所用语言的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003efrom_sql_function_name\u003c/code\u003e\u003c/em\u003e[(\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...])]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于将该类型从 SQL 环境转换到该语言的函数名称。它必须接受一个 \u003ccode class=\"type\"\u003einternal\u003c/code\u003e类型参数，并返回\u003ccode class=\"type\"\u003einternal\u003c/code\u003e类型。实际传入的参数将是该转换对应的类型，因此编写该函数时应按这一点处理。（但不允许声明一个返回\u003ccode class=\"type\"\u003einternal\u003c/code\u003e、却没有至少一个 \u003ccode class=\"type\"\u003einternal\u003c/code\u003e类型参数的 SQL 层函数。）实际返回值将是语言实现特有的某个对象。如果未指定参数列表，则函数名在其所在模式中必须唯一。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eto_sql_function_name\u003c/code\u003e\u003c/em\u003e[(\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...])]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于将该类型从该语言转换到 SQL 环境的函数名称。它必须接受一个 \u003ccode class=\"type\"\u003einternal\u003c/code\u003e类型参数，并返回该转换对应的类型。实际参数值将是语言实现特有的某个对象。如果未指定参数列表，则函数名在其所在模式中必须唯一。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e使用\u003ca href=\"/docs/18/sql-droptransform.html\" title=\"DROP TRANSFORM\"\u003e\u003ccode class=\"command\"\u003eDROP TRANSFORM\u003c/code\u003e\u003c/a\u003e删除转换。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e要为类型\u003ccode class=\"type\"\u003ehstore\u003c/code\u003e和语言\u003ccode class=\"literal\"\u003eplpython3u\u003c/code\u003e创建转换，首先准备好该类型和该语言：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TYPE hstore ...;\n\nCREATE EXTENSION plpython3u;\n\u003c/pre\u003e\u003cp\u003e然后创建必要的函数：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE FUNCTION hstore_to_plpython(val internal) RETURNS internal\nLANGUAGE C STRICT IMMUTABLE\nAS ...;\n\nCREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstore\nLANGUAGE C STRICT IMMUTABLE\nAS ...;\n\u003c/pre\u003e\u003cp\u003e最后创建把它们连接在一起的转换：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TRANSFORM FOR hstore LANGUAGE plpython3u (\n    FROM SQL WITH FUNCTION hstore_to_plpython(internal),\n    TO SQL WITH FUNCTION plpython_to_hstore(internal)\n);\n\u003c/pre\u003e\u003cp\u003e在实际使用中，这些命令通常会封装在一个扩展中。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"filename\"\u003econtrib\u003c/code\u003e部分包含若干提供转换的扩展，可以作为实际示例。\u003c/p\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e这种形式的\u003ccode class=\"command\"\u003eCREATE TRANSFORM\u003c/code\u003e是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e的一种扩展。\u003cacronym\u003eSQL\u003c/acronym\u003e标准中也有\u003ccode class=\"command\"\u003eCREATE TRANSFORM\u003c/code\u003e命令，但它用于将数据类型适配到客户端语言。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e不支持那种用法。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cp\u003e\u003ca href=\"/wiki/sql/create-function/?v=18\" title=\"CREATE FUNCTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE FUNCTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-language/?v=18\" title=\"CREATE LANGUAGE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-type/?v=18\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-transform/?v=18\" title=\"DROP TRANSFORM\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP TRANSFORM\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"CREATE [ OR REPLACE ] TRANSFORM FOR \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e LANGUAGE \u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e (\n    FROM SQL WITH FUNCTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003efrom_sql_function_name\u003c/code\u003e\u003c/em\u003e [ (\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...]) ],\n    TO SQL WITH FUNCTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003eto_sql_function_name\u003c/code\u003e\u003c/em\u003e [ (\u003cem class=\"replaceable\"\u003e\u003ccode\u003eargument_type\u003c/code\u003e\u003c/em\u003e [, ...]) ]\n);","synopsis_text":"CREATE [ OR REPLACE ] TRANSFORM FOR type_name LANGUAGE lang_name (\nFROM SQL WITH FUNCTION from_sql_function_name [ (argument_type [, ...]) ],\nTO SQL WITH FUNCTION to_sql_function_name [ (argument_type [, ...]) ]\n);"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
