当前位置:网站首页>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.
边栏推荐
- (树) 最近公共祖先(LCA)
- 识Flutter 基本组件之showTimePicker 方法
- 想从手工测试转岗自动化测试,需要学习哪些技能?
- Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
- The Map Entry understanding and application
- A brief introduction to the showDatePicker method of the basic components of Flutter
- A brief introduction to the CheckboxListTile component of the basic components of Flutter
- LocalDate addition and subtraction operations and comparison size
- IDEA comment report red solution
- type_traits元编程库学习
猜你喜欢

LeetCode每日一练 —— 138. 复制带随机指针的链表

【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.

SIP Protocol Standard and Implementation Mechanism

IDEA comment report red solution

type_traits元编程库学习

C# remote debugging

Daily practice of LeetCode - palindrome structure of OR36 linked list

分布式系统架构需要解决的问题

《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记

"A daily practice, happy water problem" 1331. Array serial number conversion
随机推荐
Implementation of a sequence table
Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
Redis实现分布式锁
Good place to download jar packages
(线段树) 基础线段树常见问题总结
点云DBSCAN聚类(MATLAB,非内置函数)
下载jar包的好地方
errno error code and meaning (Chinese)
【HCIP】ISIS
【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
els block to the left to move the conditional judgment
els 方块向左移动条件判断
Pytest电商项目实战(上)
[Godot][GDScript] 2D cave map randomly generated
Several common errors when using MP
VS QT - ui does not display newly added members (controls) || code is silent
Golang中的addressable
SIP Protocol Standard and Implementation Mechanism
Annotation usage meaning
Daily practice of LeetCode - 138. Copy a linked list with random pointers