pg_global表空间的位置
在PG当中,一个实例(cluster)初始化完以后,你会看到有下边两个与表空间相关的目录生成: $PGDATA/base $PGDATA/global
我们再用元命令\db+以及相关视图看看相应的表空间信息:
postgres=# \db+ List of tablespaces Name | Owner | Location | Access privileges | Options | Size | Description------------+----------+----------+-------------------+---------+---------+------------- pg_default | postgres | | | | 2683 MB | pg_global | postgres | | | | 576 kB |(2 rows)postgres=# select * from pg_tablespace; oid | spcname | spcowner | spcacl | spcoptions------+------------+----------+--------+------------ 1663 | pg_default | 10 | | 1664 | pg_global | 10 | |(2 rows)
postgres=# \! oid2nameAll databases: Oid Database Name Tablespace---------------------------------- 16391 demo pg_default 16521 internals pg_default 16401 mydb pg_default 16402 postgres pg_default 14485 template0 pg_default 16400 template1 pg_default
这里头,pg_default对应的就是默认数据库的表空间。默认情况下,所有新建的数据库对象都建立在这个表空间下,对应的根目录是$PGDATA/base。而pg_global对应的是全局表空间,对应的根目录是$PGDATA/global。
pg_global表空间里放了啥?
那么它里边到底放了些什么? 首先我们继续使用oid2name -s命令,它可以列出所有表空间的oid的值:
postgres=# \! oid2name -sAll tablespaces: Oid Tablespace Name----------------------- 1663 pg_default 1664 pg_global
官方文档中的定义是,pg_global中存放的都是同一个实例当中全局共享的对象。
我们可以想象,你database, tablespace之类的,甚至还有role之类的定义,肯定要放到全局共享的地方,以保证每个数据库在检索的时候,可以检索得到。
除此以外,应该还有很多其它全局对象。可以通过相关的查询一步步得到。
postgres=# create tablespace myts location '/pgccc/myts';CREATE TABLESPACEpostgres=# select * from pg_tablespace; oid | spcname | spcowner | spcacl | spcoptions-------+------------+----------+--------+------------ 1663 | pg_default | 10 | | 1664 | pg_global | 10 | | 24917 | myts | 10 | |(3 rows)
再分析一下tablespace的oid值:
db1=# select oid, reltablespace from pg_class where relname='pg_database'; oid | reltablespace------+--------------- 1262 | 1664(1 row)db1=# select distinct(reltablespace) from pg_class; reltablespace--------------- 1664 0(2 rows)
居然会有reltablespace的值为0的。它到底指的是哪个表空间,显而易见,它就是pg_default表空间。另外一个1664那个才是pg_global表空间的reltablespace的值。
有了这个值,我们想找出所有的全局共享对应就比较容易了。
postgres=# select relname from pg_class where reltablespace != 0; relname----------------------------------------- pg_toast_1262_index pg_toast_2964_index pg_toast_1213_index pg_toast_1260_index pg_toast_2396_index pg_toast_6000_index pg_toast_3592_index pg_toast_6100_index pg_database_datname_index pg_database_oid_index pg_db_role_setting_databaseid_rol_index pg_tablespace_oid_index pg_tablespace_spcname_index pg_authid_rolname_index pg_authid_oid_index pg_auth_members_role_member_index pg_auth_members_member_role_index pg_shdepend_depender_index pg_shdepend_reference_index pg_shdescription_o_c_index pg_replication_origin_roiident_index pg_replication_origin_roname_index pg_shseclabel_object_index pg_subscription_oid_index pg_subscription_subname_index pg_authid pg_toast_1262 pg_toast_2964 pg_toast_1213 pg_toast_1260 pg_toast_2396 pg_toast_6000 pg_toast_3592 pg_toast_6100 pg_database pg_db_role_setting pg_tablespace pg_auth_members pg_shdepend pg_shdescription pg_replication_origin pg_shseclabel pg_subscription(43 rows)
上边43行值是所有的对象名。
还可以将它们进行分类:
1、 所有的全局表:
postgres=# select relname from pg_class where reltablespace = 1664 and relkind='r'; relname----------------------- pg_authid pg_database pg_db_role_setting pg_tablespace pg_auth_members pg_shdepend pg_shdescription pg_replication_origin pg_shseclabel pg_subscription(10 rows)
2、所有的全局表、包括toast表
postgres=# select relname from pg_class where reltablespace = 1664 and relkind in ('r', 't'); relname----------------------- pg_authid pg_toast_1262 pg_toast_2964 pg_toast_1213 pg_toast_1260 pg_toast_2396 pg_toast_6000 pg_toast_3592 pg_toast_6100 pg_database pg_db_role_setting pg_tablespace pg_auth_members pg_shdepend pg_shdescription pg_replication_origin pg_shseclabel pg_subscription(18 rows)
3、汇成一条SQL语句:
postgres=# with global as (select oid from pg_tablespace where spcname='pg_global') select relname, relkind from pg_class, global where reltablespace=global.oid order by relkind; relname | relkind -----------------------------------------+--------- pg_db_role_setting_databaseid_rol_index | i pg_toast_2964_index | i pg_toast_1213_index | i pg_toast_1260_index | i pg_toast_2396_index | i pg_toast_6000_index | i pg_toast_3592_index | i pg_toast_6100_index | i pg_database_datname_index | i pg_database_oid_index | i pg_toast_1262_index | i pg_tablespace_oid_index | i pg_tablespace_spcname_index | i pg_authid_rolname_index | i pg_authid_oid_index | i pg_auth_members_role_member_index | i pg_auth_members_member_role_index | i pg_shdepend_depender_index | i pg_shdepend_reference_index | i pg_shdescription_o_c_index | i pg_replication_origin_roiident_index | i pg_replication_origin_roname_index | i pg_shseclabel_object_index | i pg_subscription_oid_index | i pg_subscription_subname_index | i pg_subscription | r pg_db_role_setting | r pg_tablespace | r pg_auth_members | r pg_shdepend | r pg_shdescription | r pg_replication_origin | r pg_shseclabel | r pg_authid | r pg_database | r pg_toast_1262 | t pg_toast_2964 | t pg_toast_1213 | t pg_toast_1260 | t pg_toast_2396 | t pg_toast_6000 | t pg_toast_3592 | t pg_toast_6100 | t (43 rows)
大家看到里边有表、toast表、索引。
pg_global表空间里放了啥?
关于relkind有哪些值?我们一样也可以得到。
postgres=# \dtS+ ********* QUERY ********** SELECT n.nspname as "Schema", c.relname as "Name", CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type", pg_catalog.pg_get_userbyid(c.relowner) as "Owner", CASE c.relpersistence WHEN 'p' THEN 'permanent' WHEN 't' THEN 'temporary' WHEN 'u' THEN 'unlogged' END as "Persistence", am.amname as "Access method", pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "Size", pg_catalog.obj_description(c.oid, 'pg_class') as "Description" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam WHERE c.relkind IN ('r','p','t','s','') AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2; **************************
其有效值就是以下这些:
c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' r: table v: view m: materialized view i: index S: sequence s: special t: TOAST table f: foreign table p: partitioned table I: partitioned index
关键时候很有用。