↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3
历史版本。 PostgreSQL 9.1 已结束支持。 2016-10-27. 请参阅 当前版本手册.

45.10. pg_cast #

目录 pg_cast 存储数据类型转换路径,包括内建的路径和用CREATE CAST定义的路径。

需要注意的是,pg_cast并不表示系统知道如何执行的所有类型转换,它只包括那些不能从某些通用规则推导出的转换。例如,一个域及其基础类型之间的转换并未显式地在pg_cast中展示。另一个重要的例外是“自动的基于 I/O 的类型转换”,它们通过数据类型自己的 I/O 函数来转换成(或者转换自)text或其他字符串类型,这些转换也没有显式地在pg_cast中表示。

表 45.10. pg_cast 列

NameTypeReferencesDescription
castsourceoidpg_type.oidOID of the source data type
casttargetoidpg_type.oidOID of the target data type
castfuncoidpg_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.
castcontextchar  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.
castmethodchar  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。

在pg_cast里创建一条源类型和目标类型相同的记录是合理的,只要相关联的函数接受多过一个参数。这样的记录代表“长度转换函数”,它们把该类型的值转换为对特定的类型合法的值。

如果一个pg_cast的项有着不同的原类型和目标类型,并且有一个接收多于一个参数的函数,那么它会在一个步骤中完成从一种类型到另外一种类型的转换并应用一个长度转换。如果没有这样的项,使用一个类型修改器的转换涉及两个步骤,一个是在数据类型之间转换,另外一个是应用修改器。

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.