当前位置:网站首页>Postgresql 15 source code analysis (5) - pg_control
Postgresql 15 source code analysis (5) - pg_control
2022-07-31 03:37:00 【Ink Sky Wheel】
摘要
postgresqlThe control file is savedinitdbinformation during initialization、WAL信息、Checkpoint information, etc.文件位于$PGDATA/global/pg_control.postgresqlThe duration of the cluster's existence(运行或停止),Some tools or processes can view or modify this file.本文整理了(几乎)All modified and viewedpg_controlThe place to control the file,Combining the source code has been combed,希望能对pgThe control file has a further understanding.
全局概览
先上图,共有5A server-side tool、4built-in functions and 1A backend process is OKpg_controlControl files for query or modification operations,It will be introduced later.

数据结构&方法
pg_controlA file is a size8192字节的二进制文件,The content of the file is the structureControlFileData以二进制的形式写入pg_control文件中.
- pg_control文件大小
#define PG_CONTROL_FILE_SIZE 8192
- ControlFileData数据结构
源码位于src/include/catalog/pg_control.h
/* * Contents of pg_control. */typedef struct ControlFileData{ uint64 system_identifier; uint32 pg_control_version; /* PG_CONTROL_VERSION */ uint32 catalog_version_no; /* see catversion.h */ DBState state; /* see enum above */ pg_time_t time; /* time stamp of last pg_control update */ XLogRecPtr checkPoint; /* last check point record ptr */ CheckPoint checkPointCopy; /* copy of last check point record */ XLogRecPtr unloggedLSN; /* current fake LSN value, for unlogged rels */ XLogRecPtr minRecoveryPoint; TimeLineID minRecoveryPointTLI; XLogRecPtr backupStartPoint; XLogRecPtr backupEndPoint; bool backupEndRequired; int wal_level; bool wal_log_hints; int MaxConnections; int max_worker_processes; int max_wal_senders; int max_prepared_xacts; int max_locks_per_xact; bool track_commit_timestamp; uint32 maxAlign; /* alignment requirement for tuples */ double floatFormat; /* constant 1234567.0 */#define FLOATFORMAT_VALUE 1234567.0 uint32 blcksz; /* data block size for this DB */ uint32 relseg_size; /* blocks per segment of large relation */ uint32 xlog_blcksz; /* block size within WAL files */ uint32 xlog_seg_size; /* size of each WAL segment */ uint32 nameDataLen; /* catalog name field width */ uint32 indexMaxKeys; /* max number of columns in an index */ uint32 toast_max_chunk_size; /* chunk size in TOAST tables */ uint32 loblksize; /* chunk size in pg_largeobject */ bool float8ByVal; /* float8, int8, etc pass-by-value? */ uint32 data_checksum_version; char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]; pg_crc32c crc;} ControlFileData;
- get_controlfile
get_controlfileThe main function is to convert binary filespg_control读取到ControlFileData中.
// 函数定义ControlFileData *get_controlfile(const char *DataDir, bool *crc_ok_p);
// 关键代码,Read the control file into the structurer = read(fd, ControlFile, sizeof(ControlFileData));
- update_controlfile
update_controlfileThe main function room will be the structureControlFileDataThe content in is written in binary formpg_control中.
// 函数定义void update_controlfile(const char *DataDir, ControlFileData *ControlFile, bool do_sync)
// 二进制形式打开文件if ((fd = open(ControlFilePath, O_WRONLY | PG_BINARY, pg_file_create_mode)) == -1)// 写入内容if (write(fd, buffer, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE)
- read_controlfile
在pg_resetwalimplemented a separate oneread_controlfile函数,Reads are handled herepg_control控制文件,The main function is to check the length of the control file、版本号、WAL文件的大小.个人觉得,其实这里用read_controlfileThe name may not be reasonable.
static boolread_controlfile(void){ …… if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0) { …… } …… len = read(fd, buffer, PG_CONTROL_FILE_SIZE); …… if (len >= sizeof(ControlFileData) && ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION) { …… if (!EQ_CRC32C(crc, ((ControlFileData *) buffer)->crc)) { …… } …… if (!IsValidWalSegSize(ControlFile.xlog_seg_size)) { …… } return true; } ……}
谁读了pg_control
服务端工具pg_controldata
This tool is very simple to implement,就是读取pg_control然后进行打印输出.

内部函数
postgresql 提供了4A built-in function categorizes and displays the information in the control file.
pg_control_init(Initialize the clusterinitdb的参数)

pg_control_system(系统参数)

pg_control_checkpoint(checkpoint参数)

pg_control_recovery(recovery参数)

服务端工具pg_checksums
pg_checksums在PostgreSQLIn-cluster check、Enable or disable data checksums.运行pg_checksums之前,The server must be shut down completely.When verifying the checksum,If there is no checksum error,则退出状态为零,If at least one checksum failure is detected,The exit status is non-zero.When checksumming is enabled or disabled,如果操作失败,The exit status is non-zero.
When verifying the checksum,Every file in the cluster is scanned.When checksumming is enabled,Every file in the cluster is overwritten.When checksumming is disabled,仅更新pg_control文件.

服务端工具pg_ctl
pg_cltat the standby pointpromote时,Need to judge the status of the standby point,That is, the status of the standby point needs to be DB_IN_ARCHIVE_RECOVERY.
static DBStateget_control_dbstate(void){ DBState ret; bool crc_ok; ControlFileData *control_file_data = get_controlfile(pg_data, &crc_ok); if (!crc_ok) { write_stderr(_("%s: control file appears to be corrupt\n"), progname); exit(1); } ret = control_file_data->state; pfree(control_file_data); return ret;}
DBStateSeveral states are as follows:
/* * System status indicator. Note this is stored in pg_control; if you change * it, you must bump PG_CONTROL_VERSION */typedef enum DBState{ DB_STARTUP = 0, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, DB_SHUTDOWNING, DB_IN_CRASH_RECOVERY, DB_IN_ARCHIVE_RECOVERY, DB_IN_PRODUCTION} DBState;
服务端工具pg_resetwal
通过函数read_controlfile,实现了读取pg_control,The purpose is for follow-uppg_control的修改.
服务端工具pg_rewind
通过函数read_controlfile,实现了读取pg_control,The purpose is for follow-uppg_control的修改.
谁写了pg_control
服务端工具pg_checksums
之前提到pg_checksums会读取pg_control控制文件,同时pg_checksums也会更新pg_control控制文件,主要是更新Data page checksum version的值.

当执行pg_checksums -e时,开启校验,will be in the control fileData page checksum version更新为1,如果是 -d 关闭校验,则Data page checksum version被更新为0.
/* * Finally make the data durable on disk if enabling or disabling * checksums. Flush first the data directory for safety, and then update * the control file to keep the switch consistent. */ if (mode == PG_MODE_ENABLE || mode == PG_MODE_DISABLE) { ControlFile->data_checksum_version = (mode == PG_MODE_ENABLE) ? PG_DATA_CHECKSUM_VERSION : 0; if (do_sync) { pg_log_info("syncing data directory"); fsync_pgdata(DataDir, PG_VERSION_NUM); } pg_log_info("updating control file"); update_controlfile(DataDir, ControlFile, do_sync); if (verbose) printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version); if (mode == PG_MODE_ENABLE) printf(_("Checksums enabled in cluster\n")); else printf(_("Checksums disabled in cluster\n")); }
服务端工具pg_resetwal
pg_resetwalDamaged ones can be resetwallog or reset based on transaction numberwal日志文件,Also update if necessarypg_control文件.
/* * Write out the new pg_control file. */static voidRewriteControlFile(void){ /* * Adjust fields as needed to force an empty XLOG starting at * newXlogSegNo. */ XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD, WalSegSz, ControlFile.checkPointCopy.redo); ControlFile.checkPointCopy.time = (pg_time_t) time(NULL); ControlFile.state = DB_SHUTDOWNED; ControlFile.checkPoint = ControlFile.checkPointCopy.redo; ControlFile.minRecoveryPoint = 0; ControlFile.minRecoveryPointTLI = 0; ControlFile.backupStartPoint = 0; ControlFile.backupEndPoint = 0; ControlFile.backupEndRequired = false; /* * Force the defaults for max_* settings. The values don't really matter * as long as wal_level='minimal'; the postmaster will reset these fields * anyway at startup. */ ControlFile.wal_level = WAL_LEVEL_MINIMAL; ControlFile.wal_log_hints = false; ControlFile.track_commit_timestamp = false; ControlFile.MaxConnections = 100; ControlFile.max_wal_senders = 10; ControlFile.max_worker_processes = 8; ControlFile.max_prepared_xacts = 0; ControlFile.max_locks_per_xact = 64; /* The control file gets flushed here. */ update_controlfile(".", &ControlFile, true);}
服务端工具pg_rewind
将PostgreSQLThe data directory is synced to the new timeline.
static ControlFileData ControlFile_target;static ControlFileData ControlFile_source;static ControlFileData ControlFile_source_after;
Read the necessary information from the source control file,and rewrite the target control file.
总结
pg_control保存了4类信息,分别是postgresCluster initialization information、系统信息、checkpoint信息、recovery信息.A variety of server-side tools will dopg_controlto view or modify.This article sorts out from the perspective of codepg_controlRead and write related code,希望能对大家了解postgresControl files help.
边栏推荐
- What is SQALE
- SocialFi 何以成就 Web3 去中心化社交未来
- A brief introduction to the showDatePicker method of the basic components of Flutter
- WebSocket Session为null
- els 方块向右移动边界判断、向下加速
- els block to the left to move the conditional judgment
- The use of beforeDestroy and destroyed
- LeetCode每日一练 —— 138. 复制带随机指针的链表
- Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
- With 7 years of experience, how can functional test engineers improve their abilities step by step?
猜你喜欢

type_traits metaprogramming library learning

LeetCode simple problem to find the subsequence of length K with the largest sum

Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)

SIP协议标准和实现机制

endian mode

web容器及IIS --- 中间件渗透方法1
![[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution](/img/60/fa75e06af4d143ee3fb493221fa3d9.jpg)
[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution

The application and practice of mid-to-platform brand advertising platform

【动态规划】连续子数组的最大和

TCP和UDP详解
随机推荐
Mysql 45 study notes (twenty-four) MYSQL master-slave consistency
组件传值 provide/inject
[C language] Preprocessing operation
IDEA 注释报红解决
【Cocos Creator 3.5】缓动系统停止所有动画
The Map Entry understanding and application
Web container and IIS --- Middleware penetration method 1
TCP详解(一)
No qualifying bean of type 问题
"A daily practice, happy water problem" 1331. Array serial number conversion
Redis counts new and retained users
安全20220722
type_traits metaprogramming library learning
安全20220715
$attrs/$listeners
C语言从入门到如土——数据的存储
分布式系统架构需要解决的问题
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
Mysql 45 study notes (23) How does MYSQL ensure that data is not lost
Several common errors when using MP