From 322fd5b96e9739937c587460b2780308705f5a83 Mon Sep 17 00:00:00 2001 From: Amul Sul Date: Sat, 21 Mar 2026 20:34:15 +0530 Subject: [PATCH v2 1/3] Fix-pg_waldump-archive-reader-file-handle-leak-and-r --- src/bin/pg_waldump/archive_waldump.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_waldump/archive_waldump.c b/src/bin/pg_waldump/archive_waldump.c index b078c2d6960..1e9ae637940 100644 --- a/src/bin/pg_waldump/archive_waldump.c +++ b/src/bin/pg_waldump/archive_waldump.c @@ -474,7 +474,16 @@ get_archive_wal_entry(const char *fname, XLogDumpPrivate *privateInfo) entry = ArchivedWAL_lookup(privateInfo->archive_wal_htab, fname); if (entry != NULL) + { + /* + * Found the target segment. Close any open spill file handle to + * avoid a leak; any remaining data for that segment will be + * written when the file is reopened in a subsequent call. + */ + if (write_fp != NULL) + fclose(write_fp); return entry; + } /* * Capture the current entry before calling read_archive_file(), @@ -508,8 +517,8 @@ get_archive_wal_entry(const char *fname, XLogDumpPrivate *privateInfo) */ Assert(strcmp(fname, entry->fname) != 0); - /* Create a temporary file if one does not already exist */ - if (!entry->spilled) + /* Open a spill file for this segment if we haven't already */ + if (!write_fp) { write_fp = prepare_tmp_write(entry->fname, privateInfo); entry->spilled = true; @@ -631,7 +640,7 @@ prepare_tmp_write(const char *fname, XLogDumpPrivate *privateInfo) snprintf(fpath, MAXPGPATH, "%s/%s", TmpWalSegDir, fname); /* Open the spill file for writing */ - file = fopen(fpath, PG_BINARY_W); + file = fopen(fpath, PG_BINARY_A); if (file == NULL) pg_fatal("could not create file \"%s\": %m", fpath); -- 2.47.1