PostgreSQL源码分析——pg_archivecleanup

2024-06-20 02:28

本文主要是介绍PostgreSQL源码分析——pg_archivecleanup,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pg_archivecleanup用于清理PostgreSQL WAL归档文件。指定归档目录,指定一个最老的日志段文件(在此之前的WAL日志都删掉), 用法如下:

postgres@slpc:~$ pg_archivecleanup --help
pg_archivecleanup removes older WAL files from PostgreSQL archives.Usage:pg_archivecleanup [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILEOptions:-d             generate debug output (verbose mode)-n             dry run, show the names of the files that would be removed-V, --version  output version information, then exit-x EXT         clean up files if they have this extension-?, --help     show this help, then exitFor use as archive_cleanup_command in postgresql.conf:archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %r'
e.g.archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %r'Or for use as a standalone archive cleaner:
e.g.pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup

可参考文档:http://www.postgres.cn/docs/14/pgarchivecleanup.html

源码分析

源码不多,在pg_archivecleanup.c中。

main(int argc, char **argv)
--> Initialize();	// 检查归档目录是否有效
--> SetWALFileNameForCleanup();		// 检查the oldest file we want to remain in archive是否有效
--> CleanupPriorWALFiles();  // 具体的清理归档日志

清理的代码实现如下:

static void CleanupPriorWALFiles(void)
{int			rc;DIR		   *xldir;struct dirent *xlde;char		walfile[MAXPGPATH];if ((xldir = opendir(archiveLocation)) != NULL){while (errno = 0, (xlde = readdir(xldir)) != NULL){strlcpy(walfile, xlde->d_name, MAXPGPATH);TrimExtension(walfile, additional_ext);// 比较的时候,忽略时间线,/** 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 exclusiveCleanupFileName* file. Note that this means files are not removed in the order* they were originally written, in case this worries you.*/if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0){char		WALFilePath[MAXPGPATH * 2]; /* the file path* including archive *//** Use the original file name again now, including any* extension that might have been chopped off before testing* the sequence.*/snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s",archiveLocation, xlde->d_name);if (dryrun){/** Prints the name of the file to be removed and skips the* actual removal.  The regular printout is so that the* user can pipe the output into some other program.*/printf("%s\n", WALFilePath);pg_log_debug("file \"%s\" would be removed", WALFilePath);continue;}pg_log_debug("removing file \"%s\"", WALFilePath);rc = unlink(WALFilePath);if (rc != 0)pg_fatal("could not remove file \"%s\": %m",WALFilePath);}}if (errno)pg_fatal("could not read archive location \"%s\": %m",archiveLocation);if (closedir(xldir))pg_fatal("could not close archive location \"%s\": %m",archiveLocation);}elsepg_fatal("could not open archive location \"%s\": %m",archiveLocation);
}

这篇关于PostgreSQL源码分析——pg_archivecleanup的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1076840

相关文章

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结