PostgreSQL 17新增了增量块级备份功能, 大幅减轻了备份带来的空间消耗和备份耗时, 通过pg_combinebackup则支持将增量备份的数据与之前的数据文件进行合并, 生成新的全量备份片段.
但是之前生成新的全量备份片段需要copy增量数据, 实际上是把空间消耗和备份耗时挪到了合并阶段. 还是有槽点.
这个补丁给 PostgreSQL 的 pg_combinebackup 工具引入了一个新的优化策略,目的是在合并增量备份时,尽可能使用硬链接来代替复制文件。
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=99aeb84703177308c1541e2d11c09fdc59acb724
pg_combinebackup: Add -k, --link option.
author Robert Haas <rhaas@postgresql.org>
Mon, 17 Mar 2025 18:03:14 +0000 (14:03 -0400)
committer Robert Haas <rhaas@postgresql.org>
Mon, 17 Mar 2025 18:03:14 +0000 (14:03 -0400)
This is similar to pg_upgrade's --link option, except that here we won't
typically be able to use it for every input file: sometimes we will need
to reconstruct a complete backup from blocks stored in different files.
However, when a whole file does need to be copied, we can use an
optimized copying strategy: see the existing --clone and
--copy-file-range options and the code to use CopyFile() on Windows.
This commit adds a new strategy: add a hard link to an existing file.
Making a hard link doesn't actually copy anything, but it makes sense
for the code to treat it as doing so.
This is useful when the input directories are merely staging directories
that will be removed once the restore is complete. In such cases, there
is no need to actually copy the data, and making a bunch of new hard
links can be very quick. However, it would be quite dangerous to use it
if the input directories might later be reused for any other purpose,
since starting postgres on the output directory would destructively
modify the input directories. For that reason, using this new option
causes pg_combinebackup to emit a warning about the danger involved.
Author: Israel Barth Rubio <barthisrael@gmail.com>
Co-authored-by: Robert Haas <robertmhaas@gmail.com> (cosmetic changes)
Reviewed-by: Vignesh C <vignesh21@gmail.com>
Discussion: http://postgr.es/m/CA+TgmoaEFsYHsMefNaNkU=2SnMRufKE3eVJxvAaX=OWgcnPmPg@mail.gmail.com
这个补丁给 PostgreSQL 的 pg_combinebackup 工具引入了一个新的优化策略,目的是在合并增量备份时,尽可能使用硬链接来代替复制文件。
背景:
pg_combinebackup: 这个工具用于将增量备份合并成一个完整的备份。增量备份只存储自上次备份以来的更改,因此需要合并它们才能进行完整恢复。pg_upgrade --link: 这是 pg_upgrade 中一个类似的功能,在 PostgreSQL 升级期间创建硬链接而不是复制数据文件。它速度更快,但存在风险。pg_combinebackup 已经有复制文件的策略,包括优化策略,如 --clone(使用写时复制)和 --copy-file-range(使用系统调用进行高效复制)。在 Windows 上,它使用 CopyFile()。新的策略:硬链接
危险和警告
pg_combinebackup 在使用硬链接选项时发出警告消息。此警告提醒用户,如果输入目录不被视为临时目录,则存在数据损坏的风险。总结:
该补丁引入了一种新的、非常快速的备份合并方式,使用硬链接。当输入目录是临时目录时,这是一个显著的性能改进。但是,如果输入目录被重用,则存在严重的数据损坏风险。该补丁包含一个警告,以提醒用户注意这种风险。
关键要点:
代码审查要点(从提交消息推断):
pg_combinebackup 添加了一个新的命令行选项来启用硬链接策略。link())创建硬链接的逻辑。pg_combinebackup 为每个文件选择最佳策略。