Postgresql 中文操作指南
53.10. pg_cast #
目录 pg_cast 存储数据类型转换路径,包括内置和用户定义的路径。
The catalog pg_cast stores data type conversion paths, both built-in and user-defined.
应该注意的是,pg_cast 并不表示系统知道如何执行的每种类型转换,仅表示不能从某些通用规则推断出的类型转换。例如,在 pg_cast 中没有明确表示在域及其基本类型之间进行转换。另一个重要的例外是,“自动 I/O 转换转换”(使用数据类型自己的 I/O 函数执行转换以从 text 或其他字符串类型转换到 text 或其他字符串类型)在 pg_cast 中没有明确表示。
It should be noted that pg_cast does not represent every type conversion that the system knows how to perform; only those that cannot be deduced from some generic rule. For example, casting between a domain and its base type is not explicitly represented in pg_cast. Another important exception is that “automatic I/O conversion casts”, those performed using a data type’s own I/O functions to convert to or from text or other string types, are not explicitly represented in pg_cast.
Table 53.10. pg_cast Columns
Table 53.10. pg_cast Columns
Column Type Description |
oid oid Row identifier |
castsource oid (references pg_type.oid) OID of the source data type |
casttarget oid (references pg_type.oid) OID of the target data type |
castfunc oid (references pg_proc.oid) The OID of the function to use to perform this cast. Zero is stored if the cast method doesn’t require a function. |
castcontext char Indicates what contexts the cast can be invoked in. e means only as an explicit cast (using CAST or :: syntax). a means implicitly in assignment to a target column, as well as explicitly. i means implicitly in expressions, as well as the other cases. |
castmethod char Indicates how the cast is performed. f means that the function specified in the castfunc field is used. i means that the input/output functions are used. b means that the types are binary-coercible, thus no conversion is required. |
pg_cast 中列出的强制转换函数必须始终采用强制转换源类型作为其第一个参数类型,并返回强制转换目标类型作为其结果类型。强制转换函数可以有最多三个参数。如果存在第二个参数,它必须是 integer 类型;它接收与目标类型相关的类型限定符,如果没有,则接收 -1。如果存在第三个参数,它必须是 boolean 类型;它接收 true,如果强制转换是显式强制转换,否则接收 false。
The cast functions listed in pg_cast must always take the cast source type as their first argument type, and return the cast destination type as their result type. A cast function can have up to three arguments. The second argument, if present, must be type integer; it receives the type modifier associated with the destination type, or -1 if there is none. The third argument, if present, must be type boolean; it receives true if the cast is an explicit cast, false otherwise.
如果关联函数采用多个参数,则创建包含源类型和目标类型相同的 pg_cast 条目是合法的。这样的条目表示“长度强制转换函数”,该函数可将类型的值强制为特定类型限定符值的合法值。
It is legitimate to create a pg_cast entry in which the source and target types are the same, if the associated function takes more than one argument. Such entries represent “length coercion functions” that coerce values of the type to be legal for a particular type modifier value.
当 pg_cast 条目具有不同的源类型和目标类型并且函数采用多个参数时,它表示单步从一种类型转换为另一种类型并应用长度强制转换。当没有这样的条目时,强制转换为使用类型限定符的类型涉及两个步骤,一个步骤在数据类型间进行转换,另一个步骤应用限定符。
When a pg_cast entry has different source and target types and a function that takes more than one argument, it represents converting from one type to another and applying a length coercion in a single step. When no such entry is available, coercion to a type that uses a type modifier involves two steps, one to convert between data types and a second to apply the modifier.