当前位置:网站首页>Analysis of PostgreSQL storage structure
Analysis of PostgreSQL storage structure
2022-07-01 16:17:00 【Inspur Yunxi database】
Introduction
PostgreSQL Database is a powerful open source database , More and more companies are using PostgreSQL. The storage system is PostgreSQL The lowest level module of , It accesses physical data down through the operating system interface , Provide the interface and function of storage operation for the upper module . This article through to PostgreSQL The storage structure of , Help you understand how this powerful relational database stores data .
- Data directory -
PostgreSQL After installation, you must use initdb The program initializes the data store on the disk , Generate template database and corresponding directory 、 file information .
initdb -D /usr/local/pgsql/data
The initialization directory contains data files 、 Parameter file 、 Control documents 、 Database operation log and WAL Log files, etc , The purpose of the directories and sub files in the following figure will not be described here .

By default ,PostgreSQL All data in is stored in its data directory , This data directory usually uses environment variables PGDATA To quote , For a specific database , stay PGDATA/base There is a subdirectory in the , The name of the subdirectory is the database in the system table pg_database Inside OID, The data of each table is stored in an independent file under its database directory , The file is in the form of filenode Name No , To avoid some file systems that do not support large files ,PostgreSQL Limit the table file size to no more than 1GB( Default 1GB, Compile time via ./configure --with-segsize= x modify ) therefore , When the table file exceeds 1GB when , Will create another file with a suffix relfilenode.1,relfilenode.2…… And so on .
The physical location of the table file is :$PGDATA/BASE/DATABASE_OID/PG_CLASS.RELFILENODE
-- view the database test Of OID
#select oid,datname from pg_database where datname='test';


-- See the table t1 Of filenode
#select pg_relation_filepath('t1');


- Data file structure -
stay PG in , The smallest management unit in both disk storage and memory is the block , Data blocks stored on disk are called Page, Data blocks in memory are called Buffer, Tables and indexes are called Relation, The lines are called Tuple. The reading and writing of data is based on Page Is the smallest unit , Every Page The default size is 8kB, When compiling the source code, you can use ./configure --with-blocksize=BLOCKSIZE Set other sizes , Cannot be changed thereafter . Each table file consists of multiple BLCKSZ Byte size Page form , Every Page Contains a number of Tuple.
The shared buffer pool in memory caches block block ( Default 1000 individual ), If... In buffer pool block Block is dirty , Need to brush back to the disk , The details of buffer pool will not be repeated here , If necessary, it can be explained in another article .
Page structure

Page The structure consists of five parts :
PageHeaderData -- stay page Head ,24 Byte length , Record page Metadata information .
pg_lsn, Storage page When the latest changes are made ,WAL The log lsn Information .
pg_checksum, Storage page Check value .
pd_flags, Sign a
pg_lower, The offset to the beginning of the free space .
pg_upper, Offset to the end of free space .
pd_pagesize_version, Page size and layout version number information
pd_prune_xid, The page was not deleted at the earliest XMAX, If not, it is 0.
ItemIdData -- stay page header after , A record ( Offset , length ) Array of pairs , Point to the actual tuple term , Every 4 byte .


Free space -- Unallocated space ( Free space ). The new item pointer is allocated from the beginning of this area , The new item is assigned from its end .
Items -- Used to store row data Tuple.
Special space-- Index access mode related data . Different index access methods store different data . Empty in normal table .
Tuple
Tuples in a page can be subdivided into “ Ordinary data tuples and TOAST Tuples ”.
TOAST (The Oversized-Attribute Storage Technique, Super large attribute storage technology ) It is mainly used to store variable length data , When the data size of the tuple to be inserted is greater than about 2KB ( That is, the of the page 1/4) When , It will start automatically TOAST Technology to store the tuple .TOAST Slightly more complex than ordinary tuples , This is mainly for the description of ordinary tuple files .

Tuples can be divided into three parts , Namely : Heap tuple header (23 byte )、NULL Value bitmap and Data stored by users .

Pageinspect Expand
PostgreSQL In the source directory contrib Many extended functions are provided under ,pageinspect The function provided by the extension module allows you to observe the contents of the database page from a low level , This is useful for debugging purposes .
install
#cd $PGSRC/contrib/pageinspect
#make
#sudo make install
Easy to use
#psql -d test
test=#create extension pageinspect; -- For the first time, you need to create Extension
-- Create test table
drop table if exists t1;
create table t1 (id int, name varchar(20));
insert into t1 values(1,'aaa');
-- see page header&item
SELECT * FROM page_header(get_raw_page('t1', 0));

select * from heap_page_items(get_raw_page('t1',0));

-- Update a row of data
update t1 set name='bbb' where id=1;

see heap_page_items(), Found one more item, as a result of PG Updating data does not modify the original tuple, Instead, insert a new tuple, And mark the original tuple.t_xmax= new tuple The business of id.
边栏推荐
- Learn selenium to simulate mouse operation, and you can be lazy a little bit
- In the era of super video, what kind of technology will become the base?
- Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
- 分享在大疆DJI(深圳总部)工作的日常和福利
- 接口测试框架中的鉴权处理
- 芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
- laravel的模型删除后动作
- Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
- 学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
- The picgo shortcut is amazing. This person thinks exactly the same as me
猜你喜欢

Im instant messaging develops a message delivery scheme for 10000 people

IM即时通讯开发实现心跳保活遇到的问题

Vscode find and replace the data of all files in a folder

Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme

基于PHP的轻量企业销售管理系统

Nuxt. JS data prefetching

怎麼用MySQL語言進行行列裝置?

普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...

动作捕捉系统用于苹果采摘机器人

StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
随机推荐
接口测试框架中的鉴权处理
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
【LeetCode】43. 字符串相乘
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
Win11如何设置用户权限?Win11设置用户权限的方法
Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
【Hot100】19. 删除链表的倒数第 N 个结点
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
Use Tencent cloud to build a map bed service
IM即时通讯开发实现心跳保活遇到的问题
Which MySQL functions are currently supported by tablestore in table storage?
What is the forkjoin framework in the concurrent programming series?
Zero copy technology of MySQL
[IDM] IDM downloader installation
【Hot100】17. Letter combination of telephone number
【LeetCode】43. String multiplication
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
laravel的模型删除后动作