我对PostgreSQL 的模式的一点理解

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com]

PostgreSQL 的模式,我感觉是后来添加的概念。因为在物理存储上,是:

base目录下, 一个子目录代表一个数据库。然后再进去,就是一个文件代表一个table了。

虽然逻辑上,是  数据库 ->模式->表  但是物理结构确实 /数据库/表 的形式。

那么模式的信息存储在什么地方呢?

作实验如下:

先建立模式:

postgres#create schema abababab;    
CREATE SCHEMA    
    
postgres=# \dn    
   List of schemas    
   Name   |  Owner       
----------+----------    
 abababab | postgres    
 public   | postgres    
(2 rows)    

如果 这时候我们去看 PostgreSQL 的数据目录,搜索一番看看:

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com]

[root@localhost pgsql]# find ./ | xargs grep "abababab"    
Binary file ./data/pg_xlog/000000010000000000000001 matches    

此时,由于内存和数据文件尚未同步,只有xlog中才有我们刚刚建立的模式的信息。

过一段时间或者关闭数据库,再次来搜索:

[root@localhost pgsql]# find ./ | xargs grep "abababab"    
Binary file ./data/base/12788/12649 matches    
Binary file ./data/base/12788/12647 matches    
Binary file ./data/pg_xlog/000000010000000000000001 matches    
[root@localhost pgsql]#     

我们来看看这个 12649 和  12647 都是什么东西:

postgres=# select relname from pg_class where relfilenode=12649;    
          relname               
----------------------------    
 pg_namespace_nspname_index    
(1 row)    
    
postgres=# select relname from pg_class where relfilenode=12647;    
   relname        
--------------    
 pg_namespace    
(1 row)    

就是说 很可能 模式相关的信息,都放入到 pg_namespace 中了。我推测模式充其量也就是个逻辑分割。应该和权限划分有关。

再在模式下建立表来看看:

postgres=# create table abababab.gaotab(id integer);     
postgres=# select relfilenode from pg_class where relname='gaotab';    
 relfilenode     
-------------    
24608    
(1 row)       
postgres=#    
[root@localhost
12788]# pwd /usr/local/pgsql/data/base/12788 [root@localhost 12788]# [root@localhost 12788]# ll 24608 -rw------- 1 postgres postgres 0 Oct 26 09:07 24608 [root@localhost 12788]#

可以看到刚刚建好的表,其所对应的文件是:24608, 而24608文件刚建好,为空。更无法容纳模式信息。

因此,模式信息和表的信息是分别存放的。

再来进一步验证一下:

postgres=# select * from pg_namespace;
      nspname       | nspowner |               nspacl                
--------------------+----------+-------------------------------------
 pg_toast           |       10 | 
 pg_temp_1          |       10 | 
 pg_toast_temp_1    |       10 | 
 pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
 public             |       10 | {postgres=UC/postgres,=UC/postgres}
 information_schema |       10 | {postgres=UC/postgres,=U/postgres}
 abababab           |       10 | 
(7 rows)

postgres=# 

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com]

原文地址:https://www.cnblogs.com/gaojian/p/2740597.html