PostgreSQL WAL解析:构建WAL记录准备
时间:2022-07-28
本文章向大家介绍PostgreSQL WAL解析:构建WAL记录准备,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
以heap_insert为例,简述WAL的插入过程。
在构建WAL日志记录的过程中主要涉及2个数据变量:static XLogRecData *rdatas数组和static registered_buffer *registered_buffers数组。这两个数组分别用来保存WAL数据和管理rdatas链表。
主要涉及3个重要的函数:XLogRegisterData、XLogRegisterBuffer和XLogRegisterBufData。这三个函数的作用分别是将WAL记录的特殊结构体数据注册到WAL,比如heap_insert中的xl_heap_insert结构体;将涉及到的buf注册到wal记录,比如heap_insert中page页赋予regbuf->page;将元组内容注册到WAL记录,比如insert语句的元组数据等。
下面首先介绍相关数据结构。
1、数据结构
HeapTupleData
typedef struct HeapTupleData
{
uint32 t_len; /* length of *t_data */
ItemPointerData t_self; /* SelfItemPointer */
Oid t_tableOid; /* table the tuple came from */
HeapTupleHeader t_data; /* -> tuple header and data */
} HeapTupleData;
xl_heap_header
/*
* We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
* or updated tuple in WAL; we can save a few bytes by reconstructing the
* fields that are available elsewhere in the WAL record, or perhaps just
* plain needn't be reconstructed. These are the fields we must store.
* NOTE: t_hoff could be recomputed, but we may as well store it because
* it will come for free due to alignment considerations.
*/
typedef struct xl_heap_header
{
uint16 t_infomask2;
uint16 t_infomask;
uint8 t_hoff;
} xl_heap_header;
xl_heap_insert
/* This is what we need to know about insert */
typedef struct xl_heap_insert
{
OffsetNumber offnum; /* inserted tuple's offset */
uint8 flags;
/* xl_heap_header & TUPLE DATA in backup block 0 */
} xl_heap_insert;
XLogRecData
/*
* The functions in xloginsert.c construct a chain of XLogRecData structs
* to represent the final WAL record.
*/
typedef struct XLogRecData
{
struct XLogRecData *next; /* next struct in chain, or NULL */
char *data; /* start of rmgr data to include */
uint32 len; /* length of rmgr data to include */
} XLogRecData;
registered_buffer
/*
* For each block reference registered with XLogRegisterBuffer, we fill in
* a registered_buffer struct.
*/
typedef struct
{
bool in_use; /* is this slot in use? */
uint8 flags; /* REGBUF_* flags */
RelFileNode rnode; /* identifies the relation and block */
ForkNumber forkno;
BlockNumber block;
Page page; /* page content */
uint32 rdata_len; /* total length of data in rdata chain */
XLogRecData *rdata_head; /* head of the chain of data registered with this block */
XLogRecData *rdata_tail; /* last entry in the chain, or &rdata_head if empty */
XLogRecData bkp_rdatas[2]; /* temporary rdatas used to hold references to
* backup block data in XLogRecordAssemble() */
/* buffer to store a compressed version of backup block image */
char compressed_page[PGLZ_MAX_BLCKSZ];
} registered_buffer;
2、heap_insert涉及WAL的流程
第一步中,得到如下结果,mainrdata_last保存rdata[0],存储的是xl_heap_insert结构:
第二步,得到如下结果,取registered_buffer[0],其rdata_head->next指向rdata[1],存储tuple记录的头信息:
接着进入第三步,取rdata[2],将其放到rdata[1]->next中,即加入registered_buffers[0]的rdata_head链表中,存储TUPLE值:
以上是构建WAL记录的准备阶段,下一节介绍WAL的构建及其通用结构。
- 微信小程序中用户唯一ID的获取
- Mysql备份系列(2)--mysqldump备份(全量+增量)方案操作记录
- Enterprise Library 4.1学习笔记1----配置应用程序块(c/s和b/s均适用)
- 简单账本-用完即走的微信小程序
- 新技术革命和新产业变革正进行 “互联网+大数据+人工智能+”时代正到来
- 微信小程序开发及相关设置小结
- gitlab两种连接方式:ssh和http配置介绍
- C#实现微信AES-128-CBC加密数据的解密
- UrlReferrer为空的问题?
- DeepMind发文回顾2017:AlphaGo团队已迎战下一个重大挑战
- [你必须知道的.Net]读书笔记--浅clone与深clone
- Python基础语法学习整理
- 通过部分感知深度卷积网络进行人脸特征点定位
- [你必须知道的.Net]读书笔记--override与new在继承中的区别
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- Python+PyQt5+MySQL实现天气管理系统
- 浅谈Python协程
- Python logging模块异步线程写日志实现过程解析
- php-fpm重启导致的程序执行中断问题详解
- Python Socket TCP双端聊天功能实现过程详解
- django 将自带的数据库sqlite3改成mysql实例
- 利用python对mysql表做全局模糊搜索并分页实例
- PHP chop()函数讲解
- Linux下安装Memcached服务器和客户端与PHP使用示例
- PHP xpath()函数讲解
- CentOS7编译安装php7.1的教程详解
- PHP addcslashes()函数讲解
- 浅谈优化Django ORM中的性能问题
- PHP chr()函数讲解
- iOS音视频接入 - TRTC常见问题