对PostgreSQL中tablespace 与 database, table的理解

开始:

当前的tablesapce信息

pgsql=# select * from pg_tablespace;
  spcname   | spcowner |      spclocation      | spcacl | spcoptions 
------------+----------+-----------------------+--------+------------
 pg_default |       10 |                       |        | 
 pg_global  |       10 |                       |        | 
 tbs01      |       10 | /home/pgsql/tbs/tbs01 |        | 
(3 rows)

pgsql=# 

查看目录结构:

[pgsql@localhost tbs]$ tree
.
|-- dbtbs01
|-- dbtbs02
|-- tbs01
|   `-- PG_9.1_201105231
`-- tbs02

5 directories, 0 files
[pgsql@localhost tbs]$ 

增设tablespace:

pgsql=# create tablespace dbtbs01 location '/home/pgsql/tbs/dbtbs01';
CREATE TABLESPACE
pgsql=# 

创建数据,使用此tablespace:

使用上述tablespace建立数据库,在该数据库下建表:

[pgsql@localhost bin]$ ./psql
psql (9.1.2)
Type "help" for help.

pgsql=# create database db1 owner pgsql tablespace dbtbs01;
CREATE DATABASE
pgsql=# connect db1;
You are now connected to database "db1" as user "pgsql".
db1=# create table tab100(id integer);
CREATE TABLE
db1=# insert into tab100 values(generate_series(1,1000000));
INSERT 0 1000000
db1=# select pg_relation_filepath('tab100');
              pg_relation_filepath               
-------------------------------------------------
 pg_tblspc/139315/PG_9.1_201105231/139316/139317
(1 row)

db1=# 
[pgsql@localhost tbs]$ pwd
/home/pgsql/tbs
[pgsql@localhost tbs]$ tree
.
|-- dbtbs01
|   `-- PG_9.1_201105231
|       `-- 139316
|           |-- 12436
|           |-- 12436_fsm
|           |-- 12436_vm
|           |-- 12438
|           |-- 12438_fsm
|           |-- 12438_vm
|           |-- 12440
|           |-- 12441
|           |-- 12442
|           |-- 12442_fsm
|           |-- 12442_vm
|           |-- 12444
|           |-- 12445
|           |-- 12446
|           |-- 12446_fsm
|           |-- 12446_vm
|           |-- 12448
|           |-- 12449
|           |-- 12454
|           |-- 12456
|           |-- 12457
|           |-- 12458
|           |-- 12458_fsm
|           |-- 12458_vm
|           |-- 12460
|           |-- 12462
|           |-- 12463
|           |-- 12464
|           |-- 12465
|           |-- 12465_fsm
|           |-- 12467
|           |-- 12468
|           |-- 12469
|           |-- 12471
|           |-- 12473
|           |-- 12474
|           |-- 12475
|           |-- 12476
|           |-- 12476_fsm
|           |-- 12476_vm
|           |-- 12478
|           |-- 12480
|           |-- 12481
|           |-- 12482
|           |-- 12483
|           |-- 12484
|           |-- 12485
|           |-- 12487
|           |-- 12488
|           |-- 12489
|           |-- 12489_fsm
|           |-- 12489_vm
|           |-- 12491
|           |-- 12492
|           |-- 12493
|           |-- 12493_fsm
|           |-- 12493_vm
|           |-- 12495
|           |-- 12496
|           |-- 12497
|           |-- 12497_fsm
|           |-- 12497_vm
|           |-- 12499
|           |-- 12500
|           |-- 12501
|           |-- 12501_fsm
|           |-- 12501_vm
|           |-- 12503
|           |-- 12504
|           |-- 12505
|           |-- 12505_fsm
|           |-- 12505_vm
|           |-- 12507
|           |-- 12508
|           |-- 12509
|           |-- 12509_fsm
|           |-- 12509_vm
|           |-- 12511
|           |-- 12512
|           |-- 12513
|           |-- 12514
|           |-- 12514_fsm
|           |-- 12514_vm
|           |-- 12516
|           |-- 12517
|           |-- 12518
|           |-- 12518_fsm
|           |-- 12518_vm
|           |-- 12520
|           |-- 12521
|           |-- 12522
|           |-- 12524
|           |-- 12525
|           |-- 12525_fsm
|           |-- 12525_vm
|           |-- 12527
|           |-- 12528
|           |-- 12528_fsm
|           |-- 12528_vm
|           |-- 12530
|           |-- 12530_fsm
|           |-- 12530_vm
|           |-- 12532
|           |-- 12533
|           |-- 12534
|           |-- 12535
|           |-- 12537
|           |-- 12539
|           |-- 12540
|           |-- 12541
|           |-- 12542
|           |-- 12543
|           |-- 12543_fsm
|           |-- 12543_vm
|           |-- 12545
|           |-- 12547
|           |-- 12548
|           |-- 12549
|           |-- 12549_fsm
|           |-- 12549_vm
|           |-- 12551
|           |-- 12552
|           |-- 12553
|           |-- 12555
|           |-- 12556
|           |-- 12557
|           |-- 12558
|           |-- 12558_fsm
|           |-- 12558_vm
|           |-- 12560
|           |-- 12561
|           |-- 12562
|           |-- 12562_fsm
|           |-- 12562_vm
|           |-- 12564
|           |-- 12565
|           |-- 12566
|           |-- 12567
|           |-- 12567_fsm
|           |-- 12567_vm
|           |-- 12569
|           |-- 12570
|           |-- 12571
|           |-- 12573
|           |-- 12608
|           |-- 12608_fsm
|           |-- 12608_vm
|           |-- 12610
|           |-- 12611
|           |-- 12612
|           |-- 12612_fsm
|           |-- 12612_vm
|           |-- 12614
|           |-- 12615
|           |-- 12615_fsm
|           |-- 12615_vm
|           |-- 12617
|           |-- 12618
|           |-- 12619
|           |-- 12619_fsm
|           |-- 12619_vm
|           |-- 12621
|           |-- 12622
|           |-- 12623
|           |-- 12623_fsm
|           |-- 12623_vm
|           |-- 12625
|           |-- 12626
|           |-- 12627
|           |-- 12627_fsm
|           |-- 12627_vm
|           |-- 12629
|           |-- 12630
|           |-- 12631
|           |-- 12633
|           |-- 12634
|           |-- 12635
|           |-- 12637
|           |-- 12638
|           |-- 12639
|           |-- 12641
|           |-- 12642
|           |-- 12644
|           |-- 12645
|           |-- 12646
|           |-- 12648
|           |-- 12650
|           |-- 12651
|           |-- 12652
|           |-- 12652_fsm
|           |-- 12652_vm
|           |-- 12654
|           |-- 12655
|           |-- 12656
|           |-- 12656_fsm
|           |-- 12656_vm
|           |-- 12658
|           |-- 12660
|           |-- 12661
|           |-- 12661_fsm
|           |-- 12661_vm
|           |-- 12663
|           |-- 12665
|           |-- 12666
|           |-- 12666_fsm
|           |-- 12666_vm
|           |-- 12668
|           |-- 12670
|           |-- 12671
|           |-- 12671_fsm
|           |-- 12671_vm
|           |-- 12673
|           |-- 12675
|           |-- 12676
|           |-- 12676_fsm
|           |-- 12676_vm
|           |-- 12678
|           |-- 12680
|           |-- 12681
|           |-- 12681_fsm
|           |-- 12681_vm
|           |-- 12683
|           |-- 12685
|           |-- 12686
|           |-- 12688
|           |-- 12690
|           |-- 139317
|           |-- 139317_fsm
|           |-- PG_VERSION
|           |-- pg_filenode.map
|           `-- pg_internal.init
|-- dbtbs02
|-- tbs01
|   `-- PG_9.1_201105231
`-- tbs02

7 directories, 231 files
[pgsql@localhost tbs]$ 

先看数据库信息:

db1=# select oid,datname from pg_database;
  oid   |  datname  
--------+-----------
      1 | template1
  12691 | template0
  12699 | postgres
  16384 | pgsql
  16386 | tester
 139316 | db1
(6 rows)

db1=# 

再仔细看看刚刚建立的表,位于何处:

db1=# select pg_relation_filepath('tab100');
              pg_relation_filepath               
-------------------------------------------------
 pg_tblspc/139315/PG_9.1_201105231/139316/139317
(1 row)

db1=# 

我的数据库cluster根目录是 /home/pgsql/DemoDir

可以看到:

[pgsql@localhost DemoDir]$ pwd
/home/pgsql/DemoDir
[pgsql@localhost DemoDir]$ find ./ -name "139315"
./pg_tblspc/139315
[pgsql@localhost DemoDir]$ 

我的表的真实路径位于:

[pgsql@localhost tbs]$ pwd
/home/pgsql/tbs
[pgsql@localhost tbs]$ find ./ -name "139317"
./dbtbs01/PG_9.1_201105231/139316/139317
[pgsql@localhost tbs]$ 

可以看到,pg_tblspc/139315/PG_9.1_201105231/139316/139317 是拼接出来的。

看实际大小:

[pgsql@localhost 139316]$ pwd
/home/pgsql/tbs/dbtbs01/PG_9.1_201105231/139316
[pgsql@localhost 139316]$ ls -l --block-size=MB 139317-rw------- 1 pgsql pgsql 37MB Jul  4 10:17 139317
[pgsql@localhost 139316]$ 
原文地址:https://www.cnblogs.com/gaojian/p/3171095.html