当前位置:网站首页>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博客
边栏推荐
猜你喜欢
随机推荐
Oracle入门 04 - Vmware虚拟机安装配置
TypeScript编译(tsconfig.json)
磁盘管理与文件系统
银河麒麟v10 sp1 安装 PostgreSQL 11.16
新瓶陈酒 --- 矩阵快速幂
2022.7.29 数组
(border-box) The difference between box model w3c and IE
引导过程和服务控制
In-depth analysis of z-index
进程和计划任务管理
DHCP原理与配置
mysql索引失效的常见9种原因详解
快速傅里叶变换(FFT)
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
数据库概论 - MySQL的简单介绍
Oracle 日期函数相关
DNS域名解析服务
shell的脚本的基本用法
安装gstreamer开发依赖库到项目sysroot目录
在级联选择器,根据不会重复的字段,来获取当前的对象