深入PostgreSQL中的pg_global表空间

news/2024/10/18 0:32:44/

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 | ipg_toast_2964_index                     | ipg_toast_1213_index                     | ipg_toast_1260_index                     | ipg_toast_2396_index                     | ipg_toast_6000_index                     | ipg_toast_3592_index                     | ipg_toast_6100_index                     | ipg_database_datname_index               | ipg_database_oid_index                   | ipg_toast_1262_index                     | ipg_tablespace_oid_index                 | ipg_tablespace_spcname_index             | ipg_authid_rolname_index                 | ipg_authid_oid_index                     | ipg_auth_members_role_member_index       | ipg_auth_members_member_role_index       | ipg_shdepend_depender_index              | ipg_shdepend_reference_index             | ipg_shdescription_o_c_index              | ipg_replication_origin_roiident_index    | ipg_replication_origin_roname_index      | ipg_shseclabel_object_index              | ipg_subscription_oid_index               | ipg_subscription_subname_index           | ipg_subscription                         | rpg_db_role_setting                      | rpg_tablespace                           | rpg_auth_members                         | rpg_shdepend                             | rpg_shdescription                        | rpg_replication_origin                   | rpg_shseclabel                           | rpg_authid                               | rpg_database                             | rpg_toast_1262                           | tpg_toast_2964                           | tpg_toast_1213                           | tpg_toast_1260                           | tpg_toast_2396                           | tpg_toast_6000                           | tpg_toast_3592                           | tpg_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 cLEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespaceLEFT 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

关键时候很有用。


http://www.ppmy.cn/news/1401966.html

相关文章

【OceanBase实战之路】第3篇:多租户架构实现资源隔离

码到三十五 : 个人主页 心中有诗画,指尖舞代码,目光览世界,步履越千山,人间尽值得 ! 目录 一、什么是OceanBase的多租户二、兼容模式2.1 MySQL 模式2.2 Oracle 模式三、租户介绍3.1 系统租户3.2 用户租户3.3 Meta 租…

【Pandas】(5)eval和query

使用 eval() 进行高效计算 eval() 函数在 Pandas 中是一种高效率的字符串表达式求值器,允许对 DataFrame 进行快速计算。这个功能基于 Numexpr 库,能够加速某些特定类型的操作,尤其是在处理大型 DataFrame 时。 基本使用 eval() 允许你使用字符串表达式来执行算术运算、比…

【JSON2WEB】11 基于 Amis 角色功能权限设置页面

【JSON2WEB】01 WEB管理信息系统架构设计 【JSON2WEB】02 JSON2WEB初步UI设计 【JSON2WEB】03 go的模板包html/template的使用 【JSON2WEB】04 amis低代码前端框架介绍 【JSON2WEB】05 前端开发三件套 HTML CSS JavaScript 速成 【JSON2WEB】06 JSON2WEB前端框架搭建 【J…

浏览器页面缓存机制

HTTP缓存机制的核心思想是,对于已经请求过的资源,如果其在服务器上没有发生变化,那么浏览器就可以直接从本地缓存中获取这些资源,而无需再次向服务器发送请求。 强缓存 就是确定可用的缓存 浏览器和和服务器对每个缓存资源先商量一…

Ubuntu下udp通信

一、知识准备阶段 socket是什么?套接字是什么? https://blog.csdn.net/m0_37925202/article/details/80286946 Socket程序从Windows移植到Linux下的一些注意事项 sockaddr和sockaddr_in详解 bzero和memset函数 函数原型:void bzero&…

Android开发 OCR:通过Tesseract实现图片文字识别

下面是整个详解步骤过程 效果图一、OCR的含义二、什么是Tesseract三、前提准备1、添加依赖2、数据文件下载路径 四、实际代码案例Demo如下:Main.xmlMain.java 效果图 流程:获取assets中的图片显示到页面,提取照片内的文字 一、OCR的含义 o…

二、分布式事务

目录 二、分布式事务2.1 什么是分布式事务2.2 分布式事务产生的背景2.3 分布式事务产生的场景2.4 分布式事务理论4.1 CAP理论4.2 Base理论 5、分布式事务的解决方案 二、分布式事务 2.1 什么是分布式事务 一组操作会产⽣多个数据库session会话 此时就会出现分布式事务 2.2 分…

开源AI引擎:文本自动分类在公安及消防执法办案自动化中的应用

一、实际案例介绍 通过文本分类算法自动化处理文本数据,快速识别案件性质和关键特征,极大地提高了案件管理和分派的效率。本文将探讨这两种技术如何帮助执法机构优化资源分配,确保案件得到及时而恰当的处理,并增强公共安全管理的…