我對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]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM