pg_get_object_address() 是一个系统函数,用于返回给定类型代码、对象名称和(可选的)参数数组的对象标识符。
pg_get_object_address() 在 PostgreSQL 9.5 中添加。
用法
pg_get_object_address (typetext,object_namestext[],object_argstext[] )
→ record (classidoid,objidoid,objsubidinteger)
返回的值对应于 pg_depend 中使用的标识列;要执行反向操作,即返回对象类型等的名称,请参见 pg_identify_object_as_address()。
对于 type 的有效值没有明确记录,但可以通过以下查询推断:
postgres=# SELECT DISTINCT (t.type) FROM (
SELECT DISTINCT (o.type)
FROM pg_depend d,
pg_identify_object_as_address(d.refclassid, d.refobjid, d.refobjsubid) o
UNION ALL
SELECT DISTINCT (o.type)
FROM pg_depend d,
pg_identify_object_as_address(d.classid, d.objid, d.objsubid) o
WHERE d.classid != 0
) t;
type
---------------------------
access method
aggregate
cast
collation
conversion
domain constraint
extension
function
function of access method
index
language
materialized view
operator
operator class
operator family
operator of access method
rule
schema
table
table column
text search configuration
text search dictionary
text search parser
text search template
toast table
toast table column
type
view
view column
(29 rows)
变更历史
- PostgreSQL 9.5
- 添加(提交 d7ee82e5)
示例
pg_get_object_address() 的基本用法
postgres=# SELECT * FROM pg_get_object_address('extension', '{plpgsql}', '{}');
classid | objid | objsubid
---------+-------+----------
3079 | 14000 | 0
(1 row)
使用 pg_identify_object_as_address() 的上述反向操作
postgres=# SELECT * FROM pg_identify_object_as_address(3079, 14000, 0);
type | object_names | object_args
-----------+--------------+-------------
extension | {plpgsql} | {}
(1 row)
标识表
postgres=# SELECT * FROM pg_get_object_address('table', '{foo}', '{}');
classid | objid | objsubid
---------+-------+----------
1259 | 16525 | 0
(1 row)
标识带有显式模式的表
postgres=# SELECT * FROM pg_get_object_address('table', '{public,foo}', '{}');
classid | objid | objsubid
---------+-------+----------
1259 | 16525 | 0
(1 row)
标识表列
postgres=# SELECT * FROM pg_get_object_address('table column', '{foo,id}','{}');
classid | objid | objsubid
---------+-------+----------
1259 | 16525 | 1
(1 row)
标识函数
postgres=# CREATE FUNCTION bar (int) RETURNS int LANGUAGE SQL AS 'SELECT 1';
CREATE FUNCTION
postgres=# SELECT * FROM pg_get_object_address('function', '{bar}', '{int}');
classid | objid | objsubid
---------+-------+----------
1255 | 16528 | 0
(1 row)
标识访问方法运算符
postgres=# SELECT * FROM pg_get_object_address('operator of access method', '{btree,integer_ops,1}', '{integer,integer}');
classid | objid | objsubid
---------+-------+----------
2602 | 10148 | 0
(1 row)
尝试标识不存在的对象类型
postgres=# SELECT * FROM pg_get_object_address('foo', '{bar}', '{}');
ERROR: unrecognized object type "foo"
参考资料
- PostgreSQL 文档: 对象信息和寻址函数
