0 前言

1、2部分是对XLOG生成和清理逻辑的分析,XLOG暴涨的处理直接看第3部分。

1 WAL归档

# 在自动的WAL检查点之间的日志文件段的最大数量
checkpoint_segments =
# 在自动WAL检查点之间的最长时间
checkpoint_timeout =
# 缓解io压力
checkpoint_completion_target =
# 日志文件段的保存最小数量,为了备库保留更多段
wal_keep_segments =
# 已完成的WAL段通过archive_command发送到归档存储
archive_mode =
# 强制timeout切换到新的wal段文件
archive_timeout = max_wal_size =
min_wal_size =

1.1 不开启归档时

文件数量受下面几个参数控制,通常不超过

(2 + checkpoint_completion_target) * checkpoint_segments + 1

checkpoint_segments + wal_keep_segments + 1个文件。

如果一个旧段文件不再需要了会重命名然后继续覆盖使用,如果由于短期的日志输出高峰导致了超过

3 * checkpoint_segments + 1个文件,直接删除文件。

1.2 开启归档时

文件数量:删除归档成功的段文件

抽象来看一个运行的PG生成一个无限长的WAL日志序列。每段16M,这些段文件的名字是数值命名的,反映在WAL序列中的位置。在不用WAL归档的时候,系统通常只是创建几个段文件然后循环使用,方法是把不再使用的段文件重命名为更高的段编号。

当且仅当归档命令成功时,归档命令返回零。 在得到一个零值结果之后,PostgreSQL将假设该WAL段文件已经成功归档,稍后将删除段文件。一个非零值告诉PostgreSQL该文件没有被归档,会周期性的重试直到成功。

2 PG源码分析

2.1 删除逻辑

触发删除动作

RemoveOldXlogFiles
> CreateCheckPoint
> CreateRestartPoint

wal_keep_segments判断(调用这个函数修改_logSegNo,然后再传入RemoveOldXlogFiles)

static void
KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
{XLogSegNo  segno;XLogRecPtr    keep;XLByteToSeg(recptr, segno);keep = XLogGetReplicationSlotMinimumLSN();/* compute limit for wal_keep_segments first */if (wal_keep_segments > 0){/* avoid underflow, don't go below 1 */if (segno <= wal_keep_segments)segno = 1;elsesegno = segno - wal_keep_segments;}/* then check whether slots limit removal further */if (max_replication_slots > 0 && keep != InvalidXLogRecPtr){XLogSegNo slotSegNo;XLByteToSeg(keep, slotSegNo);if (slotSegNo <= 0)segno = 1;else if (slotSegNo < segno)segno = slotSegNo;}/* don't delete WAL segments newer than the calculated segment */if (segno < *logSegNo)*logSegNo = segno;
}

删除逻辑

static void
RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr endptr)
{......while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL){/* Ignore files that are not XLOG segments */if (strlen(xlde->d_name) != 24 ||strspn(xlde->d_name, "0123456789ABCDEF") != 24)continue;/** We ignore the timeline part of the XLOG segment identifiers in* deciding whether a segment is still needed.  This ensures that we* won't prematurely remove a segment from a parent timeline. We could* probably be a little more proactive about removing segments of* non-parent timelines, but that would be a whole lot more* complicated.** We use the alphanumeric sorting property of the filenames to decide* which ones are earlier than the lastoff segment.*/if (strcmp(xlde->d_name + 8, lastoff + 8) <= 0){if (XLogArchiveCheckDone(xlde->d_name))# 归档关闭返回真# 存在done文件返回真# 存在.ready返回假# recheck存在done文件返回真# 重建.ready文件返回假{/* Update the last removed location in shared memory first */UpdateLastRemovedPtr(xlde->d_name);# 回收 或者 直接删除,清理.done和.ready文件RemoveXlogFile(xlde->d_name, endptr);}}}......
}

2.2 归档逻辑

static void
pgarch_ArchiverCopyLoop(void)
{char       xlog[MAX_XFN_CHARS + 1];# 拿到最老那个没有被归档的xlog文件名while (pgarch_readyXlog(xlog)){int            failures = 0;for (;;){/** Do not initiate any more archive commands after receiving* SIGTERM, nor after the postmaster has died unexpectedly. The* first condition is to try to keep from having init SIGKILL the* command, and the second is to avoid conflicts with another* archiver spawned by a newer postmaster.*/if (got_SIGTERM || !PostmasterIsAlive())return;/** Check for config update.  This is so that we'll adopt a new* setting for archive_command as soon as possible, even if there* is a backlog of files to be archived.*/if (got_SIGHUP){got_SIGHUP = false;ProcessConfigFile(PGC_SIGHUP);}# archive_command没设的话不再执行# 我们的command没有设置,走的是这个分支if (!XLogArchiveCommandSet()){/** Change WARNING to DEBUG1, since we will left archive_command empty to * let external tools to manage archive*/ereport(DEBUG1,(errmsg("archive_mode enabled, yet archive_command is not set")));return;}# 执行归档命令!if (pgarch_archiveXlog(xlog)){# 成功了,把.ready改名为.donepgarch_archiveDone(xlog);/** Tell the collector about the WAL file that we successfully* archived*/pgstat_send_archiver(xlog, false);break;            /* out of inner retry loop */}else{/** Tell the collector about the WAL file that we failed to* archive*/pgstat_send_archiver(xlog, true);if (++failures >= NUM_ARCHIVE_RETRIES){ereport(WARNING,(errmsg("archiving transaction log file \"%s\" failed too many times, will try again later",xlog)));return;      /* give up archiving for now */}pg_usleep(1000000L);    /* wait a bit before retrying */}}}
}

2.3 ready生成逻辑

static void
XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
{
...if (finishing_seg){issue_xlog_fsync(openLogFile, openLogSegNo);/* signal that we need to wakeup walsenders later */WalSndWakeupRequest();LogwrtResult.Flush = LogwrtResult.Write;       /* end of page */# 归档打开 && wal_level >= archiveif (XLogArchivingActive())# 生成ready文件XLogArchiveNotifySeg(openLogSegNo);XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
...
}

2.4 总结

  • ready文件只要满足archive_mode=on和wal_lever>=archive,就总会生成(XLogWrite函数调用生成)

    • 因为archive_command设置空,所以ready文件的消费完全由外部程序控制
  • done文件的处理由PG完成,两个地方会触发done文件处理,检查点和重启点
    • 处理多少done文件受wal_keep_segments和replication_slot控制(KeepLogSeg函数)

3 WAL段累积的原因(长求总?)

  • 注意:无论如何注意不要手动删除xlog文件

  • 注意:checkpoint产生的日志回不立即生成ready文件,是在下一个xlog后一块生成的

3.1 ReplicationSlot

打开流了复制槽

-- 流复制插槽
-- 如果restart_lsn和当前XLOG相差非常大的字节数, 需要排查slot的订阅者是否能正常接收XLOG,
-- 或者订阅者是否正常. 长时间不将slot的数据取走, pg_xlog目录可能会撑爆
select pg_xlog_location_diff(pg_current_xlog_location(),restart_lsn), *
from pg_replication_slots;

删除

select pg_drop_replication_slot('xxx');

删除后PG会在下一个checkpoint清理xlog

3.2 较大的wal_keep_segments

检查参数配置,注意打开这个参数会使xlog和ready有一定延迟

3.3 回收出现问题

如果不使用PG自动回收机制,数据库依赖外部程序修改.ready文件,需要检测回收进程

(archive_mode=on archive_command=’’)

3.4 检查点间隔过长

检查参数配置

Postgresql中xlog生成和清理逻辑相关推荐

  1. SQL 中的生成列/计算列以及主流数据库实现

    文章目录 什么是生成列? Oracle 中的虚拟列 MySQL 中的生成列 SQL Server 中的计算列 PostgreSQL 中的存储生成列 SQLite 中的生成列 什么是生成列? 在 SQL ...

  2. PostgreSQL中生成的列

    目录 介绍 背景 PostgreSQL 12 与SQL Server计算列比较 那么,生成的列与带有DEFAULT子句的常规列有何不同? 局限性 其他注意事项 PostgreSQL 11.x及更高版本 ...

  3. PG中XLOG日志结构

    概括起来,XLOG日志分为多个XLOG逻辑日志文件,每个逻辑日志文件包含多个XLOG段文件,每个XLOG段文件包含多个XLOG日志页: 每个XLOG逻辑日志文件都有一个ID 即LSN中的逻辑日志文件号 ...

  4. 一条 Select 语句 在 Postgresql 中的执行链路

    本文只是逻辑上的概览,并没有太过深入的设计细节的描述,对应的postgresql 的代码版本是 REL_12_2 1. DML 语句入口 如何搭建 gdb 调试pg 源代码环境,可以参考:利用GDB ...

  5. PostgreSQL 中如何delete重复数据

    问题提出 时常有这样的case: DB实例运行一段时间后,发现需要给1个table中的某(些)字段加unique 约束, 但创建unique constraints(或 index)时, 报出 DET ...

  6. JPA实体中数据库生成ID的最终指南1

    只需用@ID注释JPA实体的ID字段,并允许DB关心其余的!在某些情况下,缺省值应该改变.在本文中,我们将看到更改ID生成策略会如何影响应用程序的性能. 根据JPA规范,Entity是满足以下要求的J ...

  7. 数仓知识12:PostgreSQL预写日志(WAL)和逻辑解码方案

    目录 PostgreSQL预写日志(WAL) PostgreSQL逻辑解码(Logical Decoding) 逻辑解码方案研究分析 PostgreSQL预写日志(WAL) 从PostgreSQL 9 ...

  8. PostgreSQL中的MVCC机制

    MVCC,Multi-Version Concurrency Control,多版本并发控制. 一句话讲,MVCC就是用同一份数据临时保留多版本的方式,实现并发控制.它可以避免读写事务之间的互相阻塞, ...

  9. PostgreSQL中的分区表

    PostgreSQL中的分区表 参考:https://www.xmmup.com/pgzhongdefenqubiao.html#PG_11xin_te_xing PostgreSQL分区的意思是把逻 ...

最新文章

  1. Ubuntu 14.04 64bit上安装Scrapy
  2. 2018年中国65家机器人产业园布局与规划汇总盘点
  3. 关于python的打包打包完生成exe文件之后,不能正常执行程序的问题解决。
  4. c语言给定变量a的初始值,2018年9月计算机二级C语言考试冲刺提分试题(一)
  5. STM32 容易烧成电源短路解决方法
  6. linux malloc 线程,Linux上的侧线程的malloc/calloc崩溃
  7. 多点在线构建Noxmobi全球化精准营销系统
  8. MySQL server has gone away 问题的解决方法
  9. 我的Python之路:浏览器模拟
  10. cad老是弹出命令中发生异常_打开CAD是时出现错误报告怎么解决?
  11. Photoshop脚本 图层的栅格化
  12. China Mobile announces acquisition of China Railway Communication, unveiling industry reshuffle
  13. unity3D期末作业-太空飞机射击游戏
  14. Power Platform实战技术分享
  15. Hadoop 图像小文件查重方法
  16. 【软件工程实践】Hive研究-Blog7
  17. maven更换阿里云仓库
  18. 魔兽世界服务器卡顿原理,暴雪解释《魔兽世界》怀旧服卡顿原因 这款插件你还在用吗?...
  19. 有这3种情况,你就该离职了!
  20. 如何实现flex栅格布局及合并单元格

热门文章

  1. android平板外接显示器,iPad平板外接显示器教程 | iPad平板怎么外接显示器_什么值得买...
  2. CLUSTER 05: 块存储应用案例 分布式文件系统 对象存储
  3. Outlook邮箱开源许可-android
  4. Python小工具:批量给视频加水印!
  5. SNMPWALK 命令
  6. java日常基础知识
  7. 计算机设备管理器不显示网卡,驱动技巧:解决设备管理器中找不到网卡的问题...
  8. 不要一个人吃饭---人脉就是钱脉
  9. 花呗上征信,一文看懂征信所有问题
  10. 正则表达式里“-“中划线的使用注意