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

相关文章

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

最详细安装 PostgreSQL方法及常见问题解决

《最详细安装PostgreSQL方法及常见问题解决》:本文主要介绍最详细安装PostgreSQL方法及常见问题解决,介绍了在Windows系统上安装PostgreSQL及Linux系统上安装Po... 目录一、在 Windows 系统上安装 PostgreSQL1. 下载 PostgreSQL 安装包2.

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序