剑道第一仙

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

postgresql 之 数据目录内部结构 简介

转:https://blog.csdn.net/happytree001/article/details/125610460

一、一切皆为Oid

在Linux中一切皆为文件,在postgresql中一切皆为Oid。

1.1 什么是Oid

Object identifier(Oid), 对象标识符。 在postgresql内部,所有的数据库对象都是通过相应的Oid进行管理。

Oid在代码中是一个4字节的无符号的整数。

1.2 数据库Oid

复制代码
postgres@2f9d6ce41c2b:~$ /usr/local/pgsql/bin/psql 
psql (14.2)
Type "help" for help.

postgres=# select datname,oid from pg_database where datname='test';
 datname |  oid  
---------+-------
 test    | 16384
(1 row)

postgres@2f9d6ce41c2b:~$ ls /usr/local/pgsql/data/base/ -l
total 24
drwx------ 2 postgres postgres 4096 Jun 6 14:38 1
drwx------ 2 postgres postgres 4096 Jun 6 14:38 12971
drwx------ 2 postgres postgres 4096 Jul 4 12:50 12972
drwx------ 2 postgres postgres 4096 Jun 7 12:19 16384
drwx------ 2 postgres postgres 4096 Jun 11 02:01 24577
drwx------ 2 postgres postgres 4096 Jul 4 12:50 40960


复制代码

1.3 表Oid

复制代码
postgres@2f9d6ce41c2b:~$ /usr/local/pgsql/bin/psql test
psql (14.2)
Type "help" for help.

test=# create table stu (name varchar(32), age int, sex char(2));
CREATE TABLE
test=# \dS+ stu
                                                  Table "public.stu"
 Column |         Type          | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
--------+-----------------------+-----------+----------+---------+----------+-------------+--------------+-------------
 name   | character varying(32) |           |          |         | extended |             |              | 
 age    | integer               |           |          |         | plain    |             |              | 
 sex    | character(2)          |           |          |         | extended |             |              | 
Access method: heap

test=# select oid,relname from pg_class where relname  = 'stu';
  oid  | relname 
-------+---------
 49152 | stu
(1 row)

test=# 
复制代码
postgres@2f9d6ce41c2b:~$ ls /usr/local/pgsql/data/base/16384/49152 -hal
-rw------- 1 postgres postgres 0 Jul  4 12:57 /usr/local/pgsql/data/base/16384/49152

二、和MySQL对比
数据库          数据库存储                                表存储                                      编码影响
postgresql   目录名为对应的Oid(数字)      文件名为对应的Oid (数字)    只是一个数值,不受影响
mysql          目录名为数据库名(字符串)     文件名为表名 (字符串)          受影响
postgresql和mysql都是将数据库作为目录,对应的表则在对应的目录下,方便查找以及管理。

三、 数据目录结构

复制代码
root@2f9d6ce41c2b:/# tree /usr/local/pgsql/data/ -L 1
/usr/local/pgsql/data/
|-- PG_VERSION 
|-- base
|-- global
|-- pg_commit_ts
|-- pg_dynshmem
|-- pg_hba.conf
|-- pg_ident.conf
|-- pg_logical
|-- pg_multixact
|-- pg_notify
|-- pg_replslot
|-- pg_serial
|-- pg_snapshots
|-- pg_stat
|-- pg_stat_tmp
|-- pg_subtrans
|-- pg_tblspc
|-- pg_twophase
|-- pg_wal
|-- pg_xact
|-- postgresql.auto.conf 
|-- postgresql.conf 
|-- postmaster.opts
`-- postmaster.pid

17 directories, 7 files
复制代码

 

 

posted on   剑道第一仙  阅读(287)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示