1. 程式人生 > >postgresql 查詢表的儲存檔案

postgresql 查詢表的儲存檔案

postgresql 是使用檔案系統儲存資料的,有時需要找表及索引對應的磁碟檔案,就必須瞭解以下知識點。

zabbix=# create table tmp_t0(c0 varchar(100),c1 varchar(100), c2 varchar(100));
CREATE TABLE
zabbix=# 
zabbix=# create index idx_tmp_t0 on tmp_t0(c0);
CREATE INDEX
zabbix=# 
zabbix=# insert into tmp_t0 select id::varchar,md5(id::varchar),md5(md5(id::varchar)) from generate_series(1,100000) as id ;
INSERT 0 100000
zabbix=# 
zabbix=# delete from tmp_t0 where c0 > '1';
DELETE 99999

查看錶對應的作業系統檔案.

zabbix=# select pg_relation_filenode('tmp_t0'),pg_relation_filepath('tmp_t0');
 pg_relation_filenode | pg_relation_filepath 
----------------------+----------------------
                24583 | base/24579/24583
(1 row)

檢視索引對應的作業系統檔案.

zabbix=# select pg_relation_filenode('idx_tmp_t0'),pg_relation_filepath('idx_tmp_t0');
 pg_relation_filenode | pg_relation_filepath 
----------------------+----------------------
                24588 | base/24579/24588
(1 row)

使用 pg_class 檢視

zabbix=# select pc.oid,pc.relname,pc.relfilenode from pg_class pc where 1=1 and pc.relname in ('tmp_t0','idx_tmp_t0');
  oid  |  relname   | relfilenode 
-------+------------+-------------
 24583 | tmp_t0     |       24583
 24588 | idx_tmp_t0 |       24588
(2 rows)

作業系統檢視

$ ls -l |grep -i 24583; ls -l |grep -i 24588;
-rw------- 1 postgres postgres 10117120 Sep 19 11:18 24583
-rw------- 1 postgres postgres    24576 Sep 19 11:18 24583_fsm
-rw------- 1 postgres postgres     8192 Sep 19 11:20 24583_vm
-rw------- 1 postgres postgres  2260992 Sep 19 11:25 24588

這個時候做個truncate操作

zabbix=# truncate table tmp_t0;
TRUNCATE table
zabbix=#
zabbix=# insert into tmp_t0 select id::varchar,md5(id::varchar),md5(md5(id::varchar)) from generate_series(1,100000) as id ;
INSERT 0 100000

依次檢視

zabbix=# select pg_relation_filenode('tmp_t0'),pg_relation_filepath('tmp_t0');
 pg_relation_filenode | pg_relation_filepath 
----------------------+----------------------
                24589 | base/24579/24589
(1 row)

zabbix=# select pg_relation_filenode('idx_tmp_t0'),pg_relation_filepath('idx_tmp_t0');
 pg_relation_filenode | pg_relation_filepath 
----------------------+----------------------
                24590 | base/24579/24590
(1 row)
zabbix=# select pc.oid,pc.relname,pc.relfilenode from pg_class pc where 1=1 and pc.relname in ('tmp_t0','idx_tmp_t0');
  oid  |  relname   | relfilenode 
-------+------------+-------------
 24583 | tmp_t0     |       24589
 24588 | idx_tmp_t0 |       24590
(2 rows)

$ ls -l |grep -i 24583; ls -l |grep -i 24588;
-rw------- 1 postgres postgres        0 Sep 19 11:33 24583
-rw------- 1 postgres postgres        0 Sep 19 11:33 24588
$ ls -l |grep -i 24589; ls -l |grep -i 24590;
-rw------- 1 postgres postgres 10117120 Sep 19 11:35 24589
-rw------- 1 postgres postgres    24576 Sep 19 11:35 24589_fsm
-rw------- 1 postgres postgres  3932160 Sep 19 11:35 24590

總結如下: 1、create table、create index 時,pg_class 的 oid 與 relfilenode 相同。 1、truncate table 後,table與index的oid均沒有發生變化,但是 relfilenode 發生了變化。

下面是查看錶及索引對應的儲存檔案

select pt.schemaname||'.'||pt.tablename,pg_relation_filepath(pt.schemaname||'.'||pt.tablename),
       pi.schemaname||'.'||pi.tablename,pg_relation_filepath(pi.schemaname||'.'||pi.tablename)
 from pg_tables pt
      left outer join pg_indexes pi 
                   on pt.schemaname||'.'||pt.tablename = pi.schemaname||'.'||pi.tablename
where 1=1
  and pt.schemaname='public'
  and pt.tablename='tmp_t0'
;