当前位置:网站首页>Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
2022-07-31 07:08:00 【Hehuyi_In】
一、 整体流程
Earlier we looked at the specific functions of each step separately,Here is another simple passinsertstatement tracking,Let's take a look at the overall process.目前只看WAL相关的部分,因为insertThere are a lot of things involved,Some have not yet been learned.


最简单的insertThe statement corresponding to the function is heap_insert,其中跟WAL相关的代码:
XLogBeginInsert();
XLogRegisterData((char *) &xlrec, SizeOfHeapInsert);
xlhdr.t_infomask2 = heaptup->t_data->t_infomask2;
xlhdr.t_infomask = heaptup->t_data->t_infomask;
xlhdr.t_hoff = heaptup->t_data->t_hoff;
/*
* note we mark xlhdr as belonging to buffer; if XLogInsert decides to
* write the whole page to the xlog, we don't need to store
* xl_heap_header in the xlog.
*/
XLogRegisterBuffer(0, buffer, REGBUF_STANDARD | bufflags);
XLogRegisterBufData(0, (char *) &xlhdr, SizeOfHeapHeader);
/* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
XLogRegisterBufData(0,
(char *) heaptup->t_data + SizeofHeapTupleHeader,
heaptup->t_len - SizeofHeapTupleHeader);
/* filtering by origin on a row level is much more efficient */
XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
recptr = XLogInsert(RM_HEAP_ID, info);
PageSetLSN(page, recptr);二、 代码调试
1. 调试方法
会话1
postgres=# create table t_insert(a int);
CREATE TABLE
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
8632会话2
vscode跟踪8632进程
会话1
postgres=# insert into t_insert values(1);
Add a breakpoint to WAL相关部分,点击继续

2. 调试过程
- 首先是XLogBeginInsert,做代码安全检查工作,另外是设置begininsert_called标志,防止递归调用日志生成函数.

- XLogRegisterData,将WAL日志数据注册到rdatas数组.
参数的xlrec为xl_heap_insert结构体(日志记录中的main data部分),So this step is building and registeringmain data数据到WAL记录中.


- 构建xl_heap_header(block data)

- XLogRegisterBuffer:Registration data page,将被修改的buffer页面信息注册到registerd_buffers数组.由于XLOG是page-oriented log,So you need to register the data page before registering the tuple.

- XLogRegisterBufData:注册xl_heap_header(block data),这里的xlhdrThe parameters are constructed earlierxl_heap_header


- Register the actual tuple data
heaptup->t_data + SizeofHeapTupleHeaderis the actual tuple

- 组装WALRecord and writewal buffer

XLogInsertThe log assembly function is called in the functionXLogRecordAssemble 以及 XLogInsertRecord完成上述工作,The last one returned is what was writtenEndPos.

- 设置PageLSN
Set the page headerPageLSN为recptr(即EndPos).Every page will have itPageLSN,页面中所有LSN<=Page LSN的WALIndicates that the corresponding operations have been placed.So on crash recovery,None of these operations need to be performedredo.

参考
https://blog.csdn.net/obvious__/article/details/119242908?spm=1001.2014.3001.5502
postgresql源码学习(23)—— 事务日志④-日志组装_Hehuyi_In的博客-CSDN博客_postgresql事务日志
https://blog.csdn.net/Hehuyi_In/article/details/125447500 postgresql源码学习(22)—— 事务日志③-Log registration_Hehuyi_In的博客-CSDN博客
边栏推荐
猜你喜欢
随机推荐
防抖和节流
js原型详解
【Star项目】小帽飞机大战(八)
批量免费文字翻译
MySQL笔记下
QFileInfo常规方法
tidyverse笔记——管道函数
磁盘管理与文件系统
线程中断方法
文本三剑客之e`grep,seq文本编辑工具
nohup原理
LeetCode刷题——摆动序列#376#Medium
Oracle入门 12 - Linux 磁盘分区及LVM实战
Third-party library-store
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
编辑时过滤当前节点及根据限制的层数过滤数据
JS函数柯里化
简单谈谈Feign
深度解析 z-index
【编程题】【Scratch三级】2022.03 冬天下雪了









