PostgreSQL每日一贴-数据库对象依赖关系

来源:互联网 发布:七月十五插件数据下载 编辑:程序博客网 时间:2024/05/19 16:38

数据库的对象(用户,模式,表,视图,索引,类型等)之间只有依赖关系的,比如某些表示属于某个用户的,索引是在某个表上等。Postgresql的对象依赖关系记录在pg_depend表中

表定义

postgres=# \d pg_depend
   Table "pg_catalog.pg_depend"
   Column    |  Type   | Modifiers 
-------------+---------+-----------
 classid     | oid     | not null
 objid       | oid     | not null
 objsubid    | integer | not null
 refclassid  | oid     | not null
 refobjid    | oid     | not null
 refobjsubid | integer | not null
 deptype     | "char"  | not null
Indexes:
    "pg_depend_depender_index" btree (classid, objid, objsubid)
    "pg_depend_reference_index" btree (refclassid, refobjid, refobjsubid)

字段说明

classid 有倚赖对象所在系统表的 OID

objid    指定的有依赖对象的 OID

objsubid    对于表字段,这个是该属性的字段数(objidclassid引用表本身)。 对于所有其他对象类型,目前这个字段是零。

refclassid  被引用对象所在的系统表的 oid

refobjid    指定的被引用对象的 OID

refobjsubid 对于表字段,这个是该字段的字段号(objidclassid引用表本身)。 对于所有其他对象类型,目前这个字段是零。

deptype     一个定义这个依赖关系特定语义的代码


使用举例:

查询属于public模式的对象个数

postgres=# select relname, oid from pg_class where relname = 'pg_namespace';
   relname    | oid  
--------------+------
 pg_namespace | 2615
(1 row)


postgres=# select oid, nspname from pg_namespace where nspname = 'public';
 oid  | nspname 
------+---------
 2200 | public
(1 row)


postgres=# select count(1) from pg_depend where refclassid=2615 and refobjid=2200;
 count 
-------
     2
(1 row)


postgres=# select * from pg_depend where refclassid=2615 and refobjid=2200;
 classid | objid | objsubid | refclassid | refobjid | refobjsubid | deptype 
---------+-------+----------+------------+----------+-------------+---------
    1259 | 26338 |        0 |       2615 |     2200 |           0 | n
    1259 | 24129 |        0 |       2615 |     2200 |           0 | n
(2 rows)


postgres=# select * from pg_class where oid = 1259;
 relname  | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | reltoastrelid | re
ltoastidxid | relhasindex | relisshared | relpersistence | relkind | relnatts | relchecks | relhasoids | relhaspkey | relhasrules | relhast
riggers | relhassubclass | relfrozenxid |    relacl     | reloptions 
----------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---
------------+-------------+-------------+----------------+---------+----------+-----------+------------+------------+-------------+--------
--------+----------------+--------------+---------------+------------
 pg_class |           11 |      83 |         0 |       10 |     0 |           0 |             0 |       11 |       284 |             0 |   
          0 | t           | f           | p              | r       |       26 |         0 | t          | f          | f           | f      
        | f              |          710 | {=r/postgres} | 
(1 row)


postgres=# select * from pg_class where oid in (26338, 24129);
 relname  | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | reltoastrelid | re
ltoastidxid | relhasindex | relisshared | relpersistence | relkind | relnatts | relchecks | relhasoids | relhaspkey | relhasrules | relhast
riggers | relhassubclass | relfrozenxid | relacl | reloptions 
----------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---
------------+-------------+-------------+----------------+---------+----------+-----------+------------+------------+-------------+--------
--------+----------------+--------------+--------+------------
 test     |         2200 |   26340 |         0 |       10 |     0 |       26338 |             0 |        0 |         0 |             0 |   
          0 | f           | f           | p              | r       |        1 |         0 | f          | f          | f           | f      
        | f              |         9105 |        | 
 copytest |         2200 |   24131 |         0 |       10 |     0 |       24129 |             0 |        0 |         0 |         24132 |   
          0 | f           | f           | p              | r       |        3 |         0 | f          | f          | f           | f      
        | f              |         7463 |        | 
(2 rows)


postgres=# 



0 0