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

相关文章

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

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

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

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

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用