I found an old patch in my local worktree closely related to this work; here we take the additional step of changing the pattern used for .snap file names in logical decoding to use %X/%08X as the other patterns we changed for this. This changes both the sscanf() part of it as well as sprinft().
Any objections against this? -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
>From 137bee91f616dcb098beb8993be5abafbf0aa1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= <[email protected]> Date: Thu, 16 Oct 2025 19:55:19 +0200 Subject: [PATCH] Use a #define for the .snap printf format string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to 2633dae2e487. Author: Álvaro Herrera <[email protected]> Discussion: https://postgr.es/m/[email protected] --- contrib/pg_logicalinspect/pg_logicalinspect.c | 4 ++-- src/backend/replication/logical/snapbuild.c | 10 +++++----- src/include/replication/reorderbuffer.h | 3 +++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/contrib/pg_logicalinspect/pg_logicalinspect.c b/contrib/pg_logicalinspect/pg_logicalinspect.c index 50e805d3195..4c2c9199c4e 100644 --- a/contrib/pg_logicalinspect/pg_logicalinspect.c +++ b/contrib/pg_logicalinspect/pg_logicalinspect.c @@ -69,7 +69,7 @@ parse_snapshot_filename(const char *filename) * check including the suffix. The subsequent check validates if the given * filename has the expected suffix. */ - if (sscanf(filename, "%X-%X.snap", &hi, &lo) != 2) + if (sscanf(filename, PG_LOGICAL_SNAPFILE_PATTERN, &hi, &lo) != 2) goto parse_error; /* @@ -77,7 +77,7 @@ parse_snapshot_filename(const char *filename) * to the given filename. This check strictly checks if the given filename * follows the format of the snapshot filename. */ - sprintf(tmpfname, "%X-%X.snap", hi, lo); + sprintf(tmpfname, PG_LOGICAL_SNAPFILE_PATTERN, hi, lo); if (strcmp(tmpfname, filename) != 0) goto parse_error; diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index 98ddee20929..704e7ee642b 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -1529,7 +1529,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn) * unless the user used pg_resetwal or similar. If a user did so, there's * no hope continuing to decode anyway. */ - sprintf(path, "%s/%X-%X.snap", + sprintf(path, "%s/" PG_LOGICAL_SNAPFILE_PATTERN, PG_LOGICAL_SNAPSHOTS_DIR, LSN_FORMAT_ARGS(lsn)); @@ -1573,7 +1573,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn) elog(DEBUG1, "serializing snapshot to %s", path); /* to make sure only we will write to this tempfile, include pid */ - sprintf(tmppath, "%s/%X-%X.snap.%d.tmp", + sprintf(tmppath, "%s/" PG_LOGICAL_SNAPFILE_PATTERN ".%d.tmp", PG_LOGICAL_SNAPSHOTS_DIR, LSN_FORMAT_ARGS(lsn), MyProcPid); @@ -1747,7 +1747,7 @@ SnapBuildRestoreSnapshot(SnapBuildOnDisk *ondisk, XLogRecPtr lsn, Size sz; char path[MAXPGPATH]; - sprintf(path, "%s/%X-%X.snap", + sprintf(path, "%s/" PG_LOGICAL_SNAPFILE_PATTERN, PG_LOGICAL_SNAPSHOTS_DIR, LSN_FORMAT_ARGS(lsn)); @@ -2019,7 +2019,7 @@ CheckPointSnapBuild(void) * We just log a message if a file doesn't fit the pattern, it's * probably some editors lock/state file or similar... */ - if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2) + if (sscanf(snap_de->d_name, PG_LOGICAL_SNAPFILE_PATTERN, &hi, &lo) != 2) { ereport(LOG, (errmsg("could not parse file name \"%s\"", path))); @@ -2061,7 +2061,7 @@ SnapBuildSnapshotExists(XLogRecPtr lsn) int ret; struct stat stat_buf; - sprintf(path, "%s/%X-%X.snap", + sprintf(path, "%s/" PG_LOGICAL_SNAPFILE_PATTERN, PG_LOGICAL_SNAPSHOTS_DIR, LSN_FORMAT_ARGS(lsn)); diff --git a/src/include/replication/reorderbuffer.h b/src/include/replication/reorderbuffer.h index 3cbe106a3c7..079de45cbe6 100644 --- a/src/include/replication/reorderbuffer.h +++ b/src/include/replication/reorderbuffer.h @@ -23,6 +23,9 @@ #define PG_LOGICAL_MAPPINGS_DIR PG_LOGICAL_DIR "/mappings" #define PG_LOGICAL_SNAPSHOTS_DIR PG_LOGICAL_DIR "/snapshots" +/* name pattern of snapshot spillfiles; always used with LSN_FORMAT_ARGS */ +#define PG_LOGICAL_SNAPFILE_PATTERN "%X-%08X.snap" + /* GUC variables */ extern PGDLLIMPORT int logical_decoding_work_mem; extern PGDLLIMPORT int debug_logical_replication_streaming; -- 2.47.3
