怎么在Postgresql中实现一个xlog生成功能

  介绍

本篇文章为大家展示了怎么在Postgresql中实现一个xlog生成功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1的WAL归档

#,在自动的细胞膜检查点之间的日志文件段的最大数量   时间=checkpoint_segments    #,在自动细胞膜检查点之间的最长时间   时间=checkpoint_timeout    #,缓解io压力   时间=checkpoint_completion_target    #,日志文件段的保存最小数量,为了备库保留更多段   时间=wal_keep_segments    #,已完成的细胞膜段通过archive_command发送到归档存储   时间=archive_mode    #,强制超时切换到新的细胞膜段文件   时间=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生成一个无限长的细胞膜日志序列。每段16 m,这些段文件的名字是数值命名的,反映在细胞膜序列中的位置。在不用细胞膜归档的时候,系统通常只是创建几个段文件然后循环使用,方法是把不再使用的段文件重命名为更高的段编号。

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

2 PG源码分析

<强> 2.1删除逻辑

触发删除动作

RemoveOldXlogFiles   祝辞CreateCheckPoint   祝辞,CreateRestartPoint

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

static 空白   KeepLogSeg (XLogRecPtr  recptr, XLogSegNo  * logSegNo)   {   ,XLogSegNo  segno;   ,XLogRecPtr 保持;   ,XLByteToSeg (recptr, segno);=,,keep  XLogGetReplicationSlotMinimumLSN ();   ,/* compute  limit  for  wal_keep_segments  first  */,if  (wal_keep_segments 祝辞,0)   ,{   ,/* avoid 下溢,不要# 39;t  go  below  1, */,if  (segno  & lt;=, wal_keep_segments)   时间=segno 才能;1;   其他的,   segno 才能=,segno 作用;wal_keep_segments;   ,}   ,/* then  check  whether  slots  limit  removal  further  */,if  (max_replication_slots 祝辞,0,,,,keep  !=, InvalidXLogRecPtr)   ,{   ,XLogSegNo  slotSegNo;   ,XLByteToSeg(保持,slotSegNo);   ,if  (slotSegNo  & lt;=, 0)   时间=segno 才能;1;   ,else  if  (slotSegNo  & lt;, segno)   时间=segno 才能;slotSegNo;   ,}   ,/*不要# 39;t  delete  WAL  segments  newer  than 从而calculated  segment  */,if  (segno  & lt;, * logSegNo)   ,* logSegNo =, segno;   }

删除逻辑

static 空白   RemoveOldXlogFiles (XLogSegNo  segno, XLogRecPtr  endptr)   {   ,,…   ,,…   ,while  ((xlde =, ReadDir (xldir, XLOGDIR)), !=, NULL)   ,{   ,/* Ignore  files  that 断开连接;not  XLOG  segments  */,if  (strlen (xlde→d_name), !=, 24, | |   strspn才能(xlde→d_name,,“0123456789 abcdef"), !=, 24)   继续才能;   ,/*   *,才能ignore 我方表示歉意,timeline  part  of 从而XLOG  segment  identifiers 在   *,才能deciding  whether  a  segment  is  still 只需要却;能够ensures  that 我们   *,才能赢得# 39;t  prematurely  remove  a  segment 得到a  parent 时间只可能我方表示歉意   *,才能probably  be  a  little  more  proactive  about  removing  segments    *,才能non-parent 时间,,but  that  would  be  a  whole  lot 更多   *才能,复杂。   ,*   *,才能use 我方表示歉意,alphanumeric  sorting  property  of 从而filenames 用决定   *,才能which  ones 断开连接;earlier  than 从而lastoff 段。   ,*/,if  (strcmp (xlde→d_name  +, 8日,lastoff  +, 8), & lt;=, 0)   ,{   if 才能;(XLogArchiveCheckDone (xlde→d_name))   ,,,,,,,#,归档关闭返回真   ,,,,,,,#,存在做文件返回真   ,,,,,,,#,存在时返回假   ,,,,,,,#,复核存在做文件返回真   ,,,,,,,#,重建时文件返回假   {才能/*,才能Update 从而last  removed 位置拷贝shared  memory  first  */UpdateLastRemovedPtr才能(xlde→d_name);   ,,,,,,,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Postgresql中实现一个xlog生成功能