pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
6+ messages / 4 participants
[nested] [flat]

* 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
@ 2026-08-22 13:00  Alexander Lakhin <exclusion@gmail.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Alexander Lakhin @ 2026-08-22 13:00 UTC (permalink / raw)
  To: pgsql-hackers

Hello hackers,

I took a look at an interesting failure of 028_pitr_timelines olingo
produced a month ago [1]:
regress_log_028_pitr_timelines
### Starting node "node_pitr"
# Running: pg_ctl --wait --pgdata 
/home/bf/bf-build/olingo/HEAD/pgsql.build/testrun/recovery/028_pitr_timelines/data/t_028_pitr_timelines_node_pitr_data/pgdata 
--log /home/bf/bf-build/olingo/HEAD/pgsql.build/testrun/recovery/028_pitr_timelines/log/028_pitr_timelines_node_pitr.log 
--options --cluster-name=node_pitr start
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
# pg_ctl start failed; see logfile for details: 
/home/bf/bf-build/olingo/HEAD/pgsql.build/testrun/recovery/028_pitr_timelines/log/028_pitr_timelines_node_pitr.log

---
028_pitr_timelines_node_pitr.log
2026-07-16 08:17:47.326 CEST [1690808][startup][:0] LOG:  restored log file "00000002.history" from archive
cp: cannot stat 
'/home/bf/bf-build/olingo/HEAD/pgsql.build/testrun/recovery/028_pitr_timelines/data/t_028_pitr_timelines_primary_data/archives/00000003.history': 
No such file or directory
2026-07-16 08:17:47.357 CEST [1690808][startup][:0] LOG:  starting backup recovery with redo LSN 0/02000028, checkpoint 
LSN 0/02000088, on timeline ID 1
2026-07-16 08:17:47.364 CEST [1690808][startup][:0] LOG:  restored log file "00000002.history" from archive
2026-07-16 08:17:47.411 CEST [1690808][startup][:0] FATAL:  archive file "000000010000000000000002" has wrong size: 
15269888 instead of 16777216
2026-07-16 08:17:47.419 CEST [1690729][postmaster][:0] LOG:  startup process (PID 1690808) exited with exit code 1
2026-07-16 08:17:47.425 CEST [1690729][postmaster][:0] LOG: terminating any other active server processes

---
028_pitr_timelines_primary.log
2026-07-16 08:17:45.671 CEST [1690459][client backend][:0] LOG: disconnection: session time: 0:00:00.009 user=bf 
database=postgres host=[local]
2026-07-16 08:17:45.717 CEST [1689729][postmaster][:0] LOG: received immediate shutdown request
2026-07-16 08:17:45.721 CEST [1689796][archiver][:0] FATAL:  archive command was terminated by signal 3: Quit
2026-07-16 08:17:45.721 CEST [1689796][archiver][:0] DETAIL:  The failed archive command was: cp 
"pg_wal/000000010000000000000002" 
"/home/bf/bf-build/olingo/HEAD/pgsql.build/testrun/recovery/028_pitr_timelines/data/t_028_pitr_timelines_primary_data/archives/000000010000000000000002"
2026-07-16 08:17:45.725 CEST [1689729][postmaster][:0] LOG: database system is shut down

That is, cp was interrupted mid-flight, probably due to slow IO at that
moment. However, I could not reproduce this failure with the slow io
emulator (attached):
SLOWIO_TARGET=archives/000000010000000000000002 LD_PRELOAD=`pwd`/slowio.so PROVE_TESTS="t/028*" make -s check -s -C 
src/test/recovery

despite:
src/test/recovery/tmp_check/log/028_pitr_timelines_primary.log
2026-08-22 14:55:44.673 EEST [74335][archiver][:0] FATAL:  archive command was terminated by signal 3: Quit
2026-08-22 14:55:44.673 EEST [74335][archiver][:0] DETAIL:  The failed archive command was: cp 
"pg_wal/000000010000000000000002" 
".../src/test/recovery/tmp_check/t_028_pitr_timelines_primary_data/archives/000000010000000000000002"

As it turned out, under normal conditions, the standby instance overwrites
segment 0002 in the archive on $node_standby->stop:
2026-08-22 14:55:44.901 EEST [74358][archiver][:0] DEBUG:  executing archive command "cp 
"pg_wal/000000010000000000000002" 
".../src/test/recovery/tmp_check/t_028_pitr_timelines_primary_data/archives/000000010000000000000002""

Thus, node_pitr can see the incomplete segment from primary only if
standby doesn't copy over the full 0002 segment it has, and this is
possible if standby recycles the segment instead, that is, standby should
find .done for it/get it with basebackup.

I've managed to re-create the condition needed with:
inline patch 1: src/bin/pg_basebackup/pg_basebackup.c (+1/-0 lines) download inline-1.patch--- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2219,2 +2219,3 @@ BaseBackup(char *compression_algorithm, char *compression_detail,  #ifndef WIN32 +pg_usleep(100000);          if (write(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend))
This makes the test fail exactly as on olingo: SLOWIO_TARGET=archives/000000010000000000000002 LD_PRELOAD=`pwd`/slowio.so PROVE_TESTS="t/028*" make -s check -s -C src/test/recovery # +++ tap check in src/test/recovery +++ t/028_pitr_timelines.pl .. 1/? Bailout called.  Further testing stopped:  pg_ctl start failed 2026-08-22 15:25:18.141 EEST startup[252021] LOG:  restored log file "00000002.history" from archive cp: cannot stat '.../src/test/recovery/tmp_check/t_028_pitr_timelines_primary_data/archives/00000003.history': No such file or directory 2026-08-22 15:25:18.143 EEST startup[252021] LOG:  starting backup recovery with redo LSN 0/02000028, checkpoint LSN 0/02000088, on timeline ID 1 2026-08-22 15:25:18.145 EEST startup[252021] LOG:  restored log file "00000002.history" from archive 2026-08-22 15:25:18.151 EEST startup[252021] FATAL:  archive file "000000010000000000000002" has wrong size: 5046272 instead of 16777216 2026-08-22 15:25:18.152 EEST postmaster[252016] LOG:  startup process (PID 252021) exited with exit code 1 [1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=olingo&dt=2026-07-16%2006%3A14%3A41 Best regards, Alexander Attachments:
slowio.c (text/x-csrc, 5.2K) download /*------------------------------------------------------------------------- * * slowio.c * LD_PRELOAD library to slow down destination writes during cp. * * Environment variables: * SLOWIO_TARGET - if set, only slow writes to fds whose path contains * this substring (e.g. "archives") * * Delays are applied only when the process name is "cp", so the library can * be inherited broadly without slowing unrelated children. * * Example: * LD_PRELOAD=./slowio.so SLOWIO_TARGET=archives/000000010000000000000002 \ * cp src dst * *------------------------------------------------------------------------- */ #define _GNU_SOURCE #include <dlfcn.h> #include <errno.h> #include <fcntl.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> #ifdef __linux__ #include <sys/sendfile.h> #endif static ssize_t (*next_write)(int fd, const void *buf, size_t count); static ssize_t (*next_pwrite)(int fd, const void *buf, size_t count, off_t offset); #ifdef __linux__ static ssize_t (*next_copy_file_range)(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags); static ssize_t (*next_sendfile)(int out_fd, int in_fd, off_t *offset, size_t count); #endif static bool symbols_initialized; static int is_cp_process = -1; /* tri-state cache for process_is_cp() */ static void init_symbols(void) { if (symbols_initialized) return; next_write = (ssize_t (*)(int, const void *, size_t)) dlsym(RTLD_NEXT, "write"); next_pwrite = (ssize_t (*)(int, const void *, size_t, off_t)) dlsym(RTLD_NEXT, "pwrite"); #ifdef __linux__ next_copy_file_range = (ssize_t (*)(int, loff_t *, int, loff_t *, size_t, unsigned int)) dlsym(RTLD_NEXT, "copy_file_range"); next_sendfile = (ssize_t (*)(int, int, off_t *, size_t)) dlsym(RTLD_NEXT, "sendfile"); #endif symbols_initialized = true; srand(time(NULL)); } static bool name_is_cp(const char *name) { const char *base; if (name == NULL || name[0] == '\0') return false; base = strrchr(name, '/'); base = base ? base + 1 : name; return strcmp(base, "cp") == 0; } static bool process_is_cp(void) { if (is_cp_process >= 0) return is_cp_process; #if defined(__APPLE__) is_cp_process = name_is_cp(getprogname()); #elif defined(__linux__) { char comm[32]; FILE *file; is_cp_process = 0; file = fopen("/proc/self/comm", "r"); if (file != NULL) { if (fgets(comm, sizeof(comm), file) != NULL) { char *nl = strchr(comm, '\n'); if (nl != NULL) *nl = '\0'; is_cp_process = name_is_cp(comm); } fclose(file); } if (!is_cp_process) { char cmdline[256]; ssize_t n; int fd; fd = open("/proc/self/cmdline", O_RDONLY); if (fd >= 0) { n = read(fd, cmdline, sizeof(cmdline) - 1); close(fd); if (n > 0) { cmdline[n] = '\0'; is_cp_process = name_is_cp(cmdline); } } } } #else is_cp_process = 0; #endif return is_cp_process; } static bool fd_is_target(int fd) { const char *needle = getenv("SLOWIO_TARGET"); char path[4096]; if (needle == NULL || needle[0] == '\0') return true; #if defined(__APPLE__) if (fcntl(fd, F_GETPATH, path) == -1) return false; #elif defined(__linux__) { char linkpath[64]; ssize_t n; snprintf(linkpath, sizeof(linkpath), "/proc/self/fd/%d", fd); n = readlink(linkpath, path, sizeof(path) - 1); if (n < 0) return false; path[n] = '\0'; } #else return true; #endif return strstr(path, needle) != NULL; } static void maybe_delay(int fd) { if (process_is_cp() && fd_is_target(fd)) { if (rand() % 10 == 0) usleep(50000); } } ssize_t write(int fd, const void *buf, size_t count) { ssize_t rc; init_symbols(); if (next_write == NULL) { errno = ENOSYS; return -1; } rc = next_write(fd, buf, count); if (rc > 0) maybe_delay(fd); return rc; } ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) { ssize_t rc; init_symbols(); if (next_pwrite == NULL) { errno = ENOSYS; return -1; } rc = next_pwrite(fd, buf, count, offset); if (rc > 0) maybe_delay(fd); return rc; } #ifdef __linux__ ssize_t copy_file_range(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags) { size_t chunk = 65536; ssize_t total = 0; init_symbols(); if (next_copy_file_range == NULL) { errno = ENOSYS; return -1; } while (len > 0) { size_t n = len < chunk ? len : chunk; ssize_t rc; rc = next_copy_file_range(fd_in, off_in, fd_out, off_out, n, flags); if (rc < 0) return total > 0 ? total : -1; if (rc == 0) break; total += rc; len -= (size_t) rc; maybe_delay(fd_out); } return total; } ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count) { size_t chunk = 65536; ssize_t total = 0; init_symbols(); if (next_sendfile == NULL) { errno = ENOSYS; return -1; } while (count > 0) { size_t n = count < chunk ? count : chunk; ssize_t rc; rc = next_sendfile(out_fd, in_fd, offset, n); if (rc < 0) return total > 0 ? total : -1; if (rc == 0) break; total += rc; count -= (size_t) rc; maybe_delay(out_fd); } return total; } #endif
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
@ 2026-08-23 10:27  Andrey Borodin <x4mmm@yandex-team.ru>
  parent: Alexander Lakhin <exclusion@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Andrey Borodin @ 2026-08-23 10:27 UTC (permalink / raw)
  To: Alexander Lakhin <exclusion@gmail.com>; +Cc: pgsql-hackers



Hi Alexander,

On Sat, Aug 22, 2026, Alexander Lakhin wrote:
> That is, cp was interrupted mid-flight, probably due to slow IO at
> that moment.
>
> As it turned out, under normal conditions, the standby instance
> overwrites segment 0002 in the archive on $node_standby->stop:

Thanks for tracking this down. I think this points to a general issue in
Test::Cluster rather than another race specific to 028_pitr_timelines.
The archive command publishes the final file name while cp is still
writing it. The test then happens to rely on the standby overwriting a
partial file left by the immediate shutdown.

I propose making archive publication more atomic. The attached patch makes
the archive command follow this sequence:

  copy source temporary
  mv temporary archive-file

Test::Cluster generates a random component for the temporary name, and
the shell PID further distinguishes concurrent Unix commands. The move
publishes the completed file atomically and retains the existing overwrite
behavior. On both platforms, a failed copy remains invisible to
restore_command.

On Unix we could instead use ln followed by cmp to avoid replacing an
existing archive file with different contents. I did not do that here because
it would change the semantics of Test::Cluster's archive command. WAL-G also
overwrites WAL by default. With WALG_PREVENT_WAL_OVERWRITE enabled, it first
downloads an existing object and compares it with the local WAL, although
there is still a race between that check and the upload. As a maintainer of a
backup tool, I would prefer more PostgreSQL tests to exercise the default
overwrite semantics used by such tools.  For end user it is twice cheaper in
calls to S3 (PUT vs GET+PUT).

WDYT?

Thank you!

Best regards, Andrey Borodin.

Attachments:

v1-0001-Make-test-WAL-archiving-publish-files-atomically.patch (application/octet-stream, 1.7K) download From f6f4c94c3c291f8b473e7f9048b42c0115331b8a Mon Sep 17 00:00:00 2001 From: Andrey Borodin <amborodin@acm.org> Date: Sun, 23 Aug 2026 12:15:15 +0300 Subject: [PATCH v1] Make test WAL archiving publish files atomically --- src/test/perl/PostgreSQL/Test/Cluster.pm | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm index 920d831be9e..da0499ad14b 100644 --- a/src/test/perl/PostgreSQL/Test/Cluster.pm +++ b/src/test/perl/PostgreSQL/Test/Cluster.pm @@ -1541,10 +1541,25 @@ sub enable_archiving # first. Paths also need to be double-quoted to prevent failures where # the path contains spaces. $path =~ s{\\}{\\\\}g if ($PostgreSQL::Test::Utils::windows_os); - my $copy_command = - $PostgreSQL::Test::Utils::windows_os - ? qq{copy "%p" "$path\\\\%f"} - : qq{cp "%p" "$path/%f"}; + # Copy to a temporary file in the archive directory, then publish it under + # the name expected by restore_command. In particular, don't leave a + # partially-copied file visible if the archive command is interrupted. + # Generate the suffix here so that all commands use exactly the same name. + my $temp_suffix = sprintf("%08x", int(rand(0x7fffffff))); + my $copy_command; + + if ($PostgreSQL::Test::Utils::windows_os) + { + my $temp_path = "$path\\\\.%f.tmp.$temp_suffix"; + $copy_command = qq{copy /Y "%p" "$temp_path"} + . qq{ && move /Y "$temp_path" "$path\\\\%f"}; + } + else + { + my $temp_path = "$path/.%f.tmp.$temp_suffix.\$\$"; + $copy_command = qq{cp "%p" "$temp_path"} + . qq{ && mv -f "$temp_path" "$path/%f"}; + } # Enable archive_mode and archive_command on node $self->append_conf( -- That's all, folks. May the source be with you. =
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
@ 2026-08-23 10:32  Andrey Borodin <x4mmm@yandex-team.ru>
  parent: Alexander Lakhin <exclusion@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Andrey Borodin @ 2026-08-23 10:32 UTC (permalink / raw)
  To: Alexander Lakhin <exclusion@gmail.com>; +Cc: pgsql-hackers



Hi Alexander,

On Sat, Aug 22, 2026, Alexander Lakhin wrote:
> That is, cp was interrupted mid-flight, probably due to slow IO at
> that moment.
> 
> As it turned out, under normal conditions, the standby instance
> overwrites segment 0002 in the archive on $node_standby->stop:

Thanks for tracking this down. I think this points to a general issue in
Test::Cluster rather than another race specific to 028_pitr_timelines.
The archive command publishes the final file name while cp is still
writing it. The test then happens to rely on the standby overwriting a
partial file left by the immediate shutdown.

I propose making archive publication more atomic. The attached patch makes
the archive command follow this sequence:

 copy source temporary
 mv temporary archive-file

Test::Cluster generates a random component for the temporary name, and
the shell PID further distinguishes concurrent Unix commands. The move
publishes the completed file atomically and retains the existing overwrite
behavior. On both platforms, a failed copy remains invisible to
restore_command.

On Unix we could instead use ln followed by cmp to avoid replacing an
existing archive file with different contents. I did not do that here because
it would change the semantics of Test::Cluster's archive command. WAL-G also
overwrites WAL by default. With WALG_PREVENT_WAL_OVERWRITE enabled, it first
downloads an existing object and compares it with the local WAL, although
there is still a race between that check and the upload. As a maintainer of a
backup tool, I would prefer more PostgreSQL tests to exercise the default
overwrite semantics used by such tools.  For end user it is twice cheaper in
calls to S3 (PUT vs GET+PUT).

WDYT?

Thank you!

Best regards, Andrey Borodin.

Attachments:

v1-0001-Make-test-WAL-archiving-publish-files-atomically.patch (application/octet-stream, 1.7K) download From f6f4c94c3c291f8b473e7f9048b42c0115331b8a Mon Sep 17 00:00:00 2001 From: Andrey Borodin <amborodin@acm.org> Date: Sun, 23 Aug 2026 12:15:15 +0300 Subject: [PATCH v1] Make test WAL archiving publish files atomically --- src/test/perl/PostgreSQL/Test/Cluster.pm | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm index 920d831be9e..da0499ad14b 100644 --- a/src/test/perl/PostgreSQL/Test/Cluster.pm +++ b/src/test/perl/PostgreSQL/Test/Cluster.pm @@ -1541,10 +1541,25 @@ sub enable_archiving # first. Paths also need to be double-quoted to prevent failures where # the path contains spaces. $path =~ s{\\}{\\\\}g if ($PostgreSQL::Test::Utils::windows_os); - my $copy_command = - $PostgreSQL::Test::Utils::windows_os - ? qq{copy "%p" "$path\\\\%f"} - : qq{cp "%p" "$path/%f"}; + # Copy to a temporary file in the archive directory, then publish it under + # the name expected by restore_command. In particular, don't leave a + # partially-copied file visible if the archive command is interrupted. + # Generate the suffix here so that all commands use exactly the same name. + my $temp_suffix = sprintf("%08x", int(rand(0x7fffffff))); + my $copy_command; + + if ($PostgreSQL::Test::Utils::windows_os) + { + my $temp_path = "$path\\\\.%f.tmp.$temp_suffix"; + $copy_command = qq{copy /Y "%p" "$temp_path"} + . qq{ && move /Y "$temp_path" "$path\\\\%f"}; + } + else + { + my $temp_path = "$path/.%f.tmp.$temp_suffix.\$\$"; + $copy_command = qq{cp "%p" "$temp_path"} + . qq{ && mv -f "$temp_path" "$path/%f"}; + } # Enable archive_mode and archive_command on node $self->append_conf( -- That's all, folks. May the source be with you. =
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
@ 2026-08-31 10:21  Stepan Filippov <stepan.filippov@yandex.ru>
  parent: Andrey Borodin <x4mmm@yandex-team.ru>
  0 siblings, 1 reply; 6+ messages in thread

From: Stepan Filippov @ 2026-08-31 10:21 UTC (permalink / raw)
  To: pgsql-hackers@lists.postgresql.org; +Cc: Andrey Borodin <x4mmm@yandex-team.ru>

The following review has been posted through the commitfest application:
make installcheck-world:  tested, passed
Implements feature:       tested, passed
Spec compliant:           tested, passed
Documentation:            tested, failed

Hi! 
This looks like a reasonable improvement.
However, i have some questions about realization.

1. In general case we use $$ to add pid to temp_path, whereas on windows we do not use this.
I believe, this can cause name collision, which is not great.
2. In documentation [1] we do not specify that archive command needs to be atomic. Furthermore, we use actual cp as an example.
This can lead to similar problems not only in tests.

Thank you!

[1] https://www.postgresql.org/docs/current/continuous-archiving.html#BACKUP-ARCHIVING-WAL
Best regards, Stepan Filippov.

The new status of this patch is: Waiting on Author


^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
@ 2026-08-31 11:30  Andrey Borodin <x4mmm@yandex-team.ru>
  parent: Stepan Filippov <stepan.filippov@yandex.ru>
  0 siblings, 1 reply; 6+ messages in thread

From: Andrey Borodin @ 2026-08-31 11:30 UTC (permalink / raw)
  To: Stepan Filippov <stepan.filippov@yandex.ru>; +Cc: pgsql-hackers mailing list <pgsql-hackers@lists.postgresql.org>

Hi Stepan,

On Mon, Aug 31, 2026 at 10:21 AM Stepan Filippov wrote:
> In general case we use $$ to add pid to temp_path, whereas on windows
> we do not use this. I believe, this can cause name collision, which is
> not great.
>
> In documentation we do not specify that archive command needs to be
> atomic. Furthermore, we use actual cp as an example. This can lead to
> similar problems not only in tests.

Thank you for the review. Both points make sense.

In v2, both platforms use the test process PID and the node port. The
PID distinguishes parallel tests, and the port distinguishes nodes in
one test. There is only one archiver per node, so retries can safely
reuse the same temporary path.

I also documented that archive commands with concurrent readers should
publish complete files atomically, and warned that the simple cp example
does not. I did not present the helper's mv -f as a production recipe,
because that would contradict the existing requirement not to overwrite
a different pre-existing segment.

There is a trade-off: every archive command now performs an additional
rename. I expect the same-directory rename to be a less frequent source
of failures than partial files from an interrupted copy, but only
buildfarm experience can confirm that we have not replaced a rare failure
mode with a more frequent one.

PFA v2.

Thank you!


Best regards, Andrey Borodin.

Attachments:

v2-0001-Make-test-WAL-archiving-publish-files-atomically.patch (application/octet-stream, 4.4K) download From 477a834f801b6b7571ad0d7343cfb249c6a368a2 Mon Sep 17 00:00:00 2001 From: Andrey Borodin <amborodin@acm.org> Date: Sun, 23 Aug 2026 12:15:15 +0300 Subject: [PATCH v2] Make test WAL archiving publish files atomically An interrupted archive command can leave a partial WAL segment visible under its final name. A restore command running against the same archive can then copy the segment before a later archive attempt replaces it, making recovery tests fail intermittently. Have PostgreSQL::Test::Cluster copy WAL files to per-node temporary paths and rename them into place only after copying completes. Document that archive commands with concurrent readers should likewise publish complete files atomically without unconditionally overwriting a pre-existing file. Discussion: https://postgr.es/m/8fd856ff-b37c-4731-a10b-202ef53a679d@gmail.com --- doc/src/sgml/backup.sgml | 16 ++++++++++++--- src/test/perl/PostgreSQL/Test/Cluster.pm | 25 ++++++++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/backup.sgml b/doc/src/sgml/backup.sgml index 168444eccc5..e34ae12fa5d 100644 --- a/doc/src/sgml/backup.sgml +++ b/doc/src/sgml/backup.sgml @@ -632,9 +632,10 @@ archive_command = 'copy "%p" "C:\\server\\archivedir\\%f"' # Windows </programlisting> which will copy archivable WAL segments to the directory <filename>/mnt/server/archivedir</filename>. (This is an example, not a - recommendation, and might not work on all platforms.) After the - <literal>%p</literal> and <literal>%f</literal> parameters have been replaced, - the actual command executed might look like this: + recommendation, and might not work on all platforms. In particular, + concurrent readers can see a partially copied file.) After the + <literal>%p</literal> and <literal>%f</literal> parameters have been + replaced, the actual command executed might look like this: <programlisting> test ! -f /mnt/server/archivedir/00000001000000A900000065 &amp;&amp; cp pg_wal/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900000065 </programlisting> @@ -679,6 +680,15 @@ test ! -f /mnt/server/archivedir/00000001000000A900000065 &amp;&amp; cp pg_wal/0 not reported in <xref linkend="pg-stat-archiver-view"/>. </para> + <para> + If archive files can be read while archiving is in progress, an archive + command or library should make each file visible under its final name only + after it has been completely written. For example, it can write a + temporary file in the same file system and then publish the file + atomically. This must not unconditionally overwrite an existing file; + pre-existing files should be handled as described below. + </para> + <para> Archive commands and libraries should generally be designed to refuse to overwrite any pre-existing archive file. This is an important safety feature to diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm index 920d831be9e..87289f743f9 100644 --- a/src/test/perl/PostgreSQL/Test/Cluster.pm +++ b/src/test/perl/PostgreSQL/Test/Cluster.pm @@ -1541,10 +1541,27 @@ sub enable_archiving # first. Paths also need to be double-quoted to prevent failures where # the path contains spaces. $path =~ s{\\}{\\\\}g if ($PostgreSQL::Test::Utils::windows_os); - my $copy_command = - $PostgreSQL::Test::Utils::windows_os - ? qq{copy "%p" "$path\\\\%f"} - : qq{cp "%p" "$path/%f"}; + # Copy to a temporary file in the archive directory, then publish it under + # the name expected by restore_command. In particular, don't leave a + # partially-copied file visible if the archive command is interrupted. + # A test process's PID distinguishes concurrent tests, while the port + # distinguishes nodes belonging to the same test. Since each node runs + # archive commands serially, retries can safely reuse the temporary path. + my $temp_suffix = "$$." . $self->port; + my $copy_command; + + if ($PostgreSQL::Test::Utils::windows_os) + { + my $temp_path = "$path\\\\.%f.tmp.$temp_suffix"; + $copy_command = qq{copy /Y "%p" "$temp_path"} + . qq{ && move /Y "$temp_path" "$path\\\\%f"}; + } + else + { + my $temp_path = "$path/.%f.tmp.$temp_suffix"; + $copy_command = qq{cp "%p" "$temp_path"} + . qq{ && mv -f "$temp_path" "$path/%f"}; + } # Enable archive_mode and archive_command on node $self->append_conf( -- That's all, folks. May the source be with you. =
^ permalink raw reply [nested|flat] 6+ messages in thread

* 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment
@ 2026-09-03 04:54  Филиппов Степан <stepan.filippov@yandex.ru>
  parent: Andrey Borodin <x4mmm@yandex-team.ru>
  0 siblings, 0 replies; 6+ messages in thread

From: Филиппов Степан @ 2026-09-03 04:54 UTC (permalink / raw)
  To: x4mmm@yandex-team.ru <x4mmm@yandex-team.ru>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

<div>+ pgsql-hackers@</div><div> </div><div> </div><div> </div><div>----------------</div><div>31.08.2026, 16:30, "Andrey Borodin" &lt;x4mmm@yandex-team.ru&gt;:</div><blockquote><p>In v2, both platforms use the test process PID and the node port. The<br />PID distinguishes parallel tests, and the port distinguishes nodes in<br />one test. There is only one archiver per node, so retries can safely<br />reuse the same temporary path.<br /><br />I also documented that archive commands with concurrent readers should<br />publish complete files atomically, and warned that the simple cp example<br />does not. I did not present the helper's mv -f as a production recipe,<br />because that would contradict the existing requirement not to overwrite<br />a different pre-existing segment.</p></blockquote><div> </div><div><div style="background-color:rgb( 255 , 255 , 255 );color:rgb( 26 , 26 , 26 );font-family:'ys text' , 'arial' , sans-serif;font-size:16px;font-style:normal;font-weight:400;text-align:start;text-decoration-color:initial;text-decoration-style:initial;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Thank you for your work. Checked code and documentation.<br />Now this patch looks great to me.<br /><br />As for frequency of failures, I believe that renaming will be more reliable.<br />As long as we expect it to be atomic on all systems.<br /> </div><div style="background-color:rgb( 255 , 255 , 255 );color:rgb( 26 , 26 , 26 );font-family:'ys text' , 'arial' , sans-serif;font-size:16px;font-style:normal;font-weight:400;text-align:start;text-decoration-color:initial;text-decoration-style:initial;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div>Best regards, Stepan Filippov.</div></div></div>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-09-03 04:54 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-22 13:00 028_pitr_timelines.pl might fail when cp interrupted and backup includes required segment Alexander Lakhin <exclusion@gmail.com>
2026-08-23 10:27 ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-08-23 10:32 ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-08-31 10:21   ` Stepan Filippov <stepan.filippov@yandex.ru>
2026-08-31 11:30     ` Andrey Borodin <x4mmm@yandex-team.ru>
2026-09-03 04:54       ` Филиппов Степан <stepan.filippov@yandex.ru>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox