to_regclass() 是一个系统函数,用于将关系(relation)的名称转换为其 OID。
to_regclass() 函数是在 PostgreSQL 9.4 中添加的。
用法
to_regclass (text) →regclass
to_regclass() 函数等同于使用 regclass 对象标识符类型 进行转换,但如果找不到匹配的关系,它会返回 NULL 而不是引发 ERROR。
在评估提供的关系名称时,会考虑当前的搜索路径。
变更历史
- PostgreSQL 16
- PostgreSQL 9.4
- 已添加(提交 0886fc6a)
示例
to_regclass() 的基本用法示例
postgres=# SELECT to_regclass('foo'), to_regclass('foo')::oid;
to_regclass | to_regclass
-------------+-------------
foo | 16392
(1 row)
如果指定的关系不存在或未找到,则返回 NULL。
postgres=# SELECT to_regclass('bar') IS NULL;
?column?
----------
t
(1 row)
如果指定的关系在当前搜索路径中不可见,则必须对其进行模式限定。
postgres=# SET search_path TO '';
SET
postgres=# SELECT to_regclass('foo'), to_regclass('public.foo');
to_regclass | to_regclass
-------------+-------------
| public.foo
(1 row)
参考资料
- PostgreSQL 文档: 系统目录信息函数
分类
另请参阅
regclass, to_regnamespace()
