PostgreSQL源码分析——pg_archivecleanup

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

shell 复制代码
postgres@slpc:~$ pg_archivecleanup --help
pg_archivecleanup removes older WAL files from PostgreSQL archives.

Usage:
  pg_archivecleanup [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE

Options:
  -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 exit

For 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中。

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

清理的代码实现如下:

c 复制代码
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);
	}
	else
		pg_fatal("could not open archive location \"%s\": %m",
				 archiveLocation);
}
相关推荐
G.O.G.O.G13 分钟前
《LeetCode SQL 从入门到进阶(MySQL)》06(下)
数据库·sql·oracle
zandy10112 小时前
多租户SaaS架构下的BI数据隔离与权限治理体系
数据库·架构·数据加密
多巴胺梦想家2 小时前
NoSQL 数据库:不只是关系型
数据库·nosql
数字孪生家族2 小时前
3DGS 数据编辑单体化技术:将高斯重建转化为结构化数字空间资产
数据库·3d·高斯泼溅·空间智能应用
广州灵眸科技有限公司2 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) 系统操作-线进程操作
数据库·单片机·嵌入式硬件·算法·php
石一峰6993 小时前
驱动:私有数据为什么要在三个地方各挂一遍?
数据库·python·算法
ClouGence4 小时前
跨云、跨地域数据同步,这样更省心
数据库·sql·saas
TDengine (老段)4 小时前
TDengine 函数完整参考 — 聚合、时序、字符串、时间、数学
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
TDengine (老段)5 小时前
昆仑数智选择 TDengine TSDB,提升页岩气生产运行可视化与精细化管理能力
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
陆水A5 小时前
【问数系统】SQL跑对了图表却空了?打通问数系统最后1公里的3个API坑
大数据·数据库·自然语言处理·big data·etl工程师