当前位置:网站首页>PostgreSQL数据库WAL——资源管理器RMGR
PostgreSQL数据库WAL——资源管理器RMGR
2022-06-27 16:16:00 【墨天轮】
XLog日志被划分为多个类型的资源管理器,每个资源管理器只需要负责与自己相关的日志处理(抽象出操作函数,不同的日志实现不同的操作函数)。资源管理器的定义如下所示:
typedef struct RmgrData {const char *rm_name; 资源名称void (*rm_redo) (XLogReaderState *record); 重做函数void (*rm_desc) (StringInfo buf, XLogReaderState *record); 负责解析对应资源事务日志const char *(*rm_identify) (uint8 info); // 解析xlog记录中的Info字段void (*rm_startup) (void); // 启动时的初始化工作void (*rm_cleanup) (void); // 结束时的清理工作void (*rm_mask) (char *pagedata, BlockNumber blkno); // 在对页面做一致性检查前,是否需要对页面的某些部分做掩码} RmgrData;extern const RmgrData RmgrTable[];
多个类型的资源管理器定义在src/include/access/rmgrlist.h中,它们的作用和操作函数如下所示。

操作函数调用流程
rm_identify接口和rm_desc接口用于pg_waldump工具对WAL日志进行反向解析(将二进制日志记录解析成字符串)。rm_startup、rm_redo、rm_mask和rm_cleanup用于startup进程执行StartupXLOG函数进行XLOG日志回放中的初始化资源管理器RMGR、回放xlog、对页面做一致性检查和清理工作,其执行流程如下所示:
void StartupXLOG(void){.../* Initialize resource managers */ // 初始化资源管理器RMGRfor (rmid = 0; rmid <= RM_MAX_ID; rmid++) {if (RmgrTable[rmid].rm_startup != NULL)RmgrTable[rmid].rm_startup();}.../* main redo apply loop */ // 主回放逻辑do {.../* Now apply the WAL record itself */ // 调用rm_redo回放xlogRmgrTable[record->xl_rmid].rm_redo(xlogreader);/* After redo, check whether the backup pages associated with the WAL record are consistent with the existing pages. This check is done only if consistency check is enabled for this record. */ // redo 后,检查与 WAL 记录关联的备份页是否与现有页一致。仅当为此记录启用一致性检查时才进行此检查。if ((record->xl_info & XLR_CHECK_CONSISTENCY) != 0)checkXLogConsistency(xlogreader);...record = ReadRecord(xlogreader, LOG, false); /* Else, try to fetch the next WAL record */} while (record != NULL);/* Allow resource managers to do any required cleanup. */ // 进行清理工作for (rmid = 0; rmid <= RM_MAX_ID; rmid++) {if (RmgrTable[rmid].rm_cleanup != NULL)RmgrTable[rmid].rm_cleanup();}}
1.rm_startup用于针对不同的类型xlog资源调用不同的初始化函数从而初始化资源管理器RMGR。
2.根据xlog中的xl_rmid调用资源管理器中不同资源的rm_redo回放函数进行回放。
3.我们知道startup进程recovery过程中,由于wal replay BUG或者备库的物理数据块异常会导致的wal replay回放出来的块不正确。PostgreSQL 10.0新增的wal_consistency_checking参数,可以用于发现这种问题。wal_consistency_checking 参数可以设置为如下值:all, heap, heap2, btree, hash, gin, gist, sequence, spgist, brin, and generic。

wal_consistency_checking (string)
This parameter is intended to be used to check for bugs in the WAL redo routines. When enabled, full-page images of any buffers modified in conjunction with the WAL record are added to the record. If the record is subsequently replayed, the system will first apply each record and then test whether the buffers modified by the record match the stored images. In certain cases (such as hint bits), minor variations are acceptable, and will be ignored. Any unexpected differences will result in a fatal error, terminating recovery. The default value of this setting is the empty string, which disables the feature. It can be set to all to check all records, or to a comma-separated list of resource managers to check only records originating from those resource managers. Currently, the supported resource managers are heap, heap2, btree, hash, gin, gist, sequence, spgist, brin, and generic. Only superusers can change this setting.
StartupXLOG --> checkXLogConsistency 针对不同的资源管理ID,执行相应的函数,其主要逻辑如下:
RmgrId rmid = XLogRecGetRmid(record); // record的类型为XLogReaderState *if (RmgrTable[rmid].rm_mask != NULL) {RmgrTable[rmid].rm_mask(replay_image_masked, blkno);RmgrTable[rmid].rm_mask(primary_image_masked, blkno);}
4.针对不同的资源管理ID,调用其相应的函数rm_cleanup进行资源清理
从上面流程中看出回放过程中进行wal_consistency_check,取决于xlog info中是否设置XLR_CHECK_CONSISTENCY,而设置过程是处于XLogRecordAssemble函数中,如果wal_consistency_checking数组对应资源ID设置为true(wal_consistency_checking数组在GUC 模块中的assign_wal_consistency_checking函数中根据wal_consistency_checking参数设置),则对应的xlog info将设置XLR_CHECK_CONSISTENCY标志。
static XLogRecData *XLogRecordAssemble(RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi){/* Enforce consistency checks for this record if user is looking for it. Do this before at the beginning of this routine to give the possibility for callers of XLogInsert() to pass XLR_CHECK_CONSISTENCY directly for a record.*/ // 如果用户正在查找此记录,则强制执行一致性检查。在此例程开始之前执行此操作,以使 XLogInsert() 的调用者可以直接传递 XLR_CHECK_CONSISTENCY 以获取记录if (wal_consistency_checking[rmid])info |= XLR_CHECK_CONSISTENCY;}
参考文献:
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a507b86900f695aacc8d52b7d2cfcb65f58862a2
https://www.postgresql.org/docs/devel/static/runtime-config-developer.html
边栏推荐
- The power of code refactoring: how to measure the success of refactoring
- Teach you how to realize pynq-z2 bar code recognition
- About binary
- (5) SPI application design and simulation verification 2 - design code implementation
- [qt learning notes]qt uses MFC compilation to generate DLL libraries. The problem that DLL cannot be loaded occurs on computers without programming environment
- Current market situation and development prospect forecast of global concrete shrinkage reducing agent industry in 2022
- 【ELT.ZIP】OpenHarmony啃论文俱乐部—见证文件压缩系统EROFS
- Handwritten promise series - all
- [JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes
- 简历如何去写?
猜你喜欢

电子智慧套装教程

TDengine在数控机床监控中的应用

建立自己的网站(10)

428 binary tree (501. mode in binary search tree, 701. insert operation in binary search tree, 450. delete node in binary search tree, 669. prune binary search tree)

MySQL中的行转列和列转行

Shardingsphere & Atlas & MYCAT comparison

Mise à jour SQL mise à jour par lots

Teach you to use elastic search: run the first hello world search command
![[Tang Laoshi] C -- encapsulation: member method](/img/47/9a4ffd787624f6208b6aee66c38b48.jpg)
[Tang Laoshi] C -- encapsulation: member method

Wechat applet association search
随机推荐
All you want to know about large screen visualization is here
Market status and development prospect forecast of global triisopropyl chlorosilane industry in 2022
Offline disk group
Market status and development prospect of 4-butyl resorcinol used in skin care industry in the world in 2022
1. introduction to MariaDB
(5) SPI application design and simulation verification 1 - logic sorting
TP5 restrict access frequency
Software testing - the concept of testing, the detailed introduction of unit testing, and how to design test cases
Software testing Basics - software testing history, process, classification, benefits, limitations
Current market situation and development prospect forecast of global concrete shrinkage reducing agent industry in 2022
Can the selectforupdateexecution in Seata obtain the global lock before executing SQL?
New products, new personnel and new services, Infiniti will continue to plough into China's future!
leetcode 19. Delete the penultimate node of the linked list
[qt learning notes]qt uses MFC compilation to generate DLL libraries. The problem that DLL cannot be loaded occurs on computers without programming environment
数据同步工具 DataX 已经正式支持读写 TDengine
软件测试基础-软件测试历史流程,分类,好处,限制
Control file related views
【协会通知】关于举办人工智能与物联网领域暑假专题师资培训的通知
Shardingsphere & Atlas & MYCAT comparison
Application of scaleflux CSD 2000 in Ctrip