format_type() 是一个系统函数,它根据数据类型的 OID 和(如果适用)类型修饰符来返回该数据类型的可读名称。
format_type() 在 PostgreSQL 7.1 中添加。
用法
format_type (typeoid,typemodinteger) →text
如果没有类型修饰符,则应提供 NULL。
如果不存在与指定的 OID / 类型修饰符组合匹配的数据类型,则返回字符串 "???"。
format_type() 主要用于查询 系统目录 关系。
变更历史
- PostgreSQL 7.1
- 已添加(提交 de85dd1d)
示例
format_type() 的基本用法示例,此处返回关系中列的名称和数据类型
postgres=# SELECT attname, format_type(a.atttypid, a.atttypmod)
FROM pg_attribute a
WHERE a.attrelid = 'foo'::regclass
AND attnum > 0;
attname | format_type
---------+-------------
id | integer
val | text
(2 rows)
尝试检索不存在的数据类型的名称
postgres=# SELECT format_type(999, NULL); format_type ------------- ??? (1 row)
参考资料
- PostgreSQL 文档: 系统目录信息函数
