postgresql 之 数据目录内部结构 简介
转:https://blog.csdn.net/happytree001/article/details/125610460
一、一切皆为Oid
在Linux中一切皆为文件,在postgresql中一切皆为Oid。
1.1 什么是Oid
Object identifier(Oid), 对象标识符。 在postgresql内部,所有的数据库对象都是通过相应的Oid进行管理。
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义