From 355bda5d86975fa8024b5858df2e938fb9cc5f9b Mon Sep 17 00:00:00 2001 From: Goutham Reddy Aenugu Date: Sat, 21 Nov 2026 03:37:48 -0800 Subject: [PATCH v1 3/3] Log the duration of slow end-of-recovery steps Standby promotion and archive recovery can spend substantial time between WAL replay and the server accepting writes. Report completed steps that take at least log_startup_progress_interval, including recovery_end_command and the end-of-recovery logical decoding update. Measure durations independently of the periodic startup progress alarm so blocking operations report when they return. Keep the existing progress timer scoped to resetting unlogged relations. --- doc/src/sgml/config.sgml | 15 +++- src/backend/access/transam/xlog.c | 89 +++++++++++++++++++ src/test/recovery/meson.build | 1 + .../t/063_end_of_recovery_progress.pl | 60 +++++++++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/test/recovery/t/063_end_of_recovery_progress.pl diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 2844093..ca3cf52 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -7796,10 +7796,23 @@ local0.* /var/log/postgresql file or on the server command line. + + At the end of recovery, the startup process logs the duration of + individual steps that take at least this long. Operations that + cannot report their own progress while running, such as an + fsync, the + , or a wait for a + process signal barrier, are reported when the corresponding step + completes. If hot standby connections are available, the + wait_event column of + can show what the startup + process is currently waiting for. + + For example, if syncing the data directory takes 25 seconds and thereafter resetting unlogged relations takes 8 seconds, and if this - setting has the default value of 10 seconds, then a messages will be + setting has the default value of 10 seconds, then messages will be logged for syncing the data directory after it has been in progress for 10 seconds and again after it has been in progress for 20 seconds, but nothing will be logged for resetting unlogged relations. diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 5ba6bee..7038306 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5936,6 +5936,43 @@ XLogInitNewTimeline(TimeLineID endTLI, XLogRecPtr endOfLog, TimeLineID newTLI) XLogArchiveCleanup(xlogfname); } +/* + * Measure elapsed time separately from the periodic startup progress timer. + * Some operations, such as recovery_end_command, cannot report progress + * until they return. + */ +static instr_time +StartEndOfRecoveryStep(bool in_recovery) +{ + instr_time start; + + if (in_recovery && log_startup_progress_interval > 0) + INSTR_TIME_SET_CURRENT(start); + else + INSTR_TIME_SET_ZERO(start); + + return start; +} + +static void +LogEndOfRecoveryStep(instr_time start, const char *msg) +{ + instr_time elapsed; + int64 elapsed_us; + + if (INSTR_TIME_IS_ZERO(start) || log_startup_progress_interval <= 0) + return; + + INSTR_TIME_SET_CURRENT(elapsed); + INSTR_TIME_SUBTRACT(elapsed, start); + elapsed_us = INSTR_TIME_GET_MICROSEC(elapsed); + + if (elapsed_us / 1000 >= log_startup_progress_interval) + ereport(LOG, + (errmsg(msg, (long) (elapsed_us / USECS_PER_SEC), + (int) (elapsed_us % USECS_PER_SEC / 10000)))); +} + /* * Perform cleanup actions at the conclusion of archive recovery. */ @@ -5943,14 +5980,23 @@ static void CleanupAfterArchiveRecovery(TimeLineID EndOfLogTLI, XLogRecPtr EndOfLog, TimeLineID newTLI) { + instr_time step_start; + /* * Execute the recovery_end_command, if any. */ if (recoveryEndCommand && strcmp(recoveryEndCommand, "") != 0) + { + step_start = StartEndOfRecoveryStep(true); ExecuteRecoveryCommand(recoveryEndCommand, "recovery_end_command", true, WAIT_EVENT_RECOVERY_END_COMMAND); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: \"recovery_end_command\" took %ld.%02d s")); + } + + step_start = StartEndOfRecoveryStep(true); /* * We switched to a new timeline. Clean up segments on the old timeline. @@ -6025,6 +6071,9 @@ CleanupAfterArchiveRecovery(TimeLineID EndOfLogTLI, XLogRecPtr EndOfLog, XLogArchiveNotify(partialfname); } } + + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: cleaning up WAL from the old timeline took %ld.%02d s")); } /* @@ -6097,6 +6146,8 @@ StartupXLOG(void) XLogRecPtr missingContrecPtr; TransactionId oldestActiveXID; bool promoted = false; + bool report_recovery_progress; + instr_time step_start; char timebuf[128]; /* @@ -6567,7 +6618,11 @@ StartupXLOG(void) /* * Finish WAL recovery. */ + report_recovery_progress = InRecovery; + step_start = StartEndOfRecoveryStep(report_recovery_progress); endOfRecoveryInfo = FinishWalRecovery(); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: finishing WAL recovery took %ld.%02d s")); EndOfLog = endOfRecoveryInfo->endOfLog; EndOfLogTLI = endOfRecoveryInfo->endOfLogTLI; abortedRecPtr = endOfRecoveryInfo->abortedRecPtr; @@ -6629,14 +6684,25 @@ StartupXLOG(void) * end-of-recovery steps fail. */ if (InRecovery) + { + step_start = StartEndOfRecoveryStep(true); + + /* This restarts the progress timer disabled during standby replay. */ ResetUnloggedRelations(UNLOGGED_RELATION_INIT); + disable_startup_progress_timeout(); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: resetting unlogged relations took %ld.%02d s")); + } /* * Pre-scan prepared transactions to find out the range of XIDs present. * This information is not quite needed yet, but it is positioned here so * as potential problems are detected before any on-disk change is done. */ + step_start = StartEndOfRecoveryStep(report_recovery_progress); oldestActiveXID = PrescanPreparedTransactions(NULL, NULL); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: scanning prepared transactions took %ld.%02d s")); /* * Allow ordinary WAL segment creation before possibly switching to a new @@ -6661,6 +6727,7 @@ StartupXLOG(void) newTLI = endOfRecoveryInfo->lastRecTLI; if (ArchiveRecoveryRequested) { + step_start = StartEndOfRecoveryStep(true); newTLI = findNewestTimeLine(recoveryTargetTLI) + 1; INJECTION_POINT("promotion-before-timeline-switch", NULL); ereport(LOG, @@ -6702,6 +6769,9 @@ StartupXLOG(void) ereport(LOG, (errmsg("archive recovery complete"))); + + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: switching to the new timeline took %ld.%02d s")); } /* Save the selected TimeLineID in shared memory, too */ @@ -6786,7 +6856,10 @@ StartupXLOG(void) /* * Preallocate additional log files, if wanted. */ + step_start = StartEndOfRecoveryStep(report_recovery_progress); PreallocXlogFiles(EndOfLog, newTLI); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: preallocating WAL files took %ld.%02d s")); /* * Okay, we're officially UP. @@ -6821,7 +6894,10 @@ StartupXLOG(void) * happen before renaming the last partial segment of the old timeline as * it may be possible that we have to recover some transactions from it. */ + step_start = StartEndOfRecoveryStep(report_recovery_progress); RecoverPreparedTransactions(); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: recovering prepared transactions took %ld.%02d s")); /* Shut down xlogreader */ ShutdownWalRecovery(); @@ -6848,7 +6924,12 @@ StartupXLOG(void) * Emit checkpoint or end-of-recovery record in XLOG, if required. */ if (performedWalRecovery) + { + step_start = StartEndOfRecoveryStep(report_recovery_progress); promoted = PerformRecoveryXLogAction(); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: recording the end of recovery took %ld.%02d s")); + } /* * If any of the critical GUCs have changed, log them before we allow @@ -6872,7 +6953,12 @@ StartupXLOG(void) * Update logical decoding status in shared memory and write an * XLOG_LOGICAL_DECODING_STATUS_CHANGE, if necessary. */ + step_start = StartEndOfRecoveryStep(report_recovery_progress); UpdateLogicalDecodingStatusEndOfRecovery(); + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: updating logical decoding status took %ld.%02d s")); + + step_start = StartEndOfRecoveryStep(report_recovery_progress); /* Clean up EndOfWalRecoveryInfo data to appease Valgrind leak checking */ if (endOfRecoveryInfo->lastPage) @@ -6984,6 +7070,9 @@ StartupXLOG(void) */ if (promoted) RequestCheckpoint(CHECKPOINT_FORCE); + + LogEndOfRecoveryStep(step_start, + gettext_noop("end of recovery: finalizing recovery took %ld.%02d s")); } /* diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index 7b9d3e8..bdc5d9a 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -71,6 +71,7 @@ tests += { 't/060_multixact_crashed_updater.pl', 't/061_recovery_target_prefetch.pl', 't/062_promotion_state.pl', + 't/063_end_of_recovery_progress.pl', ], }, } diff --git a/src/test/recovery/t/063_end_of_recovery_progress.pl b/src/test/recovery/t/063_end_of_recovery_progress.pl new file mode 100644 index 0000000..0f10e60 --- /dev/null +++ b/src/test/recovery/t/063_end_of_recovery_progress.pl @@ -0,0 +1,60 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test completion logging for a slow end-of-recovery step. + +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $primary = PostgreSQL::Test::Cluster->new('primary'); +$primary->init(allows_streaming => 1); +$primary->start; +$primary->backup('backup'); + +my $perlbin = $^X; +$perlbin =~ s!\\!/!g if $windows_os; + +for my $interval ('100ms', '0') +{ + my $standby = PostgreSQL::Test::Cluster->new("standby_$interval"); + $standby->init_from_backup($primary, 'backup', has_streaming => 1); + $standby->append_conf( + 'postgresql.conf', qq( +log_startup_progress_interval = '$interval' +recovery_end_command = '"$perlbin" -e "sleep 1; print qq(done)" > recovery_end_command.done' +)); + $standby->start; + $primary->wait_for_replay_catchup($standby); + + my $log_offset = -s $standby->logfile; + $standby->promote; + $standby->wait_for_log(qr/database system is ready to accept connections/, + $log_offset); + + is(slurp_file($standby->data_dir . '/recovery_end_command.done'), + 'done', "recovery_end_command completed with interval $interval"); + + my $progress = + qr/end of recovery: "recovery_end_command" took \d+\.\d{2} s/; + if ($interval eq '0') + { + unlike( + slurp_file($standby->logfile, $log_offset), + qr/end of recovery:/, + 'zero interval suppresses end-of-recovery progress'); + } + else + { + ok( $standby->wait_for_log( + qr/archive recovery complete.*$progress.*database system is ready to accept connections/s, + $log_offset), + 'slow recovery_end_command is logged during promotion'); + } + + $standby->stop; +} + +done_testing(); -- 2.56.1