pgsql: Use WAIT FOR LSN in PostgreSQL::Test::Cluster::wait_for_catchup(

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Use WAIT FOR LSN in PostgreSQL::Test::Cluster::wait_for_catchup(
Date: 2026-04-06 19:47:42
Message-ID: E1w9pvB-003HW2-1E@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Use WAIT FOR LSN in PostgreSQL::Test::Cluster::wait_for_catchup()

When the standby is passed as a PostgreSQL::Test::Cluster instance,
use the WAIT FOR LSN command on the standby server to implement
wait_for_catchup() for replay, write, and flush modes. This is more
efficient than polling pg_stat_replication on the upstream, as the
WAIT FOR LSN command uses a latch-based wakeup mechanism.

The optimization applies when:
- The standby is passed as a Cluster object (not just a name string)
- The mode is 'replay', 'write', or 'flush' (not 'sent')

Rather than pre-checking pg_is_in_recovery() on the standby (which
would add an extra round-trip on every call), we issue WAIT FOR LSN
directly and handle the 'not in recovery' result as a signal to fall
back to polling.

For 'sent' mode, when the standby is passed as a string (e.g., a
subscription name for logical replication), when the standby has been
promoted, or when WAIT FOR LSN is interrupted by a recovery conflict,
the function falls back to the original polling-based approach using
pg_stat_replication on the upstream. The recovery conflict fallback
is necessary because some conflicts are unavoidable - for example,
ResolveRecoveryConflictWithTablespace() kills all backends
unconditionally, regardless of what they are doing.

The recovery conflict detection matches the English error message
"conflict with recovery", which is reliable because the test suite
runs with LC_MESSAGES=C.

Discussion: https://postgr.es/m/CABPTF7UiArgW-sXj9CNwRzUhYOQrevLzkYcgBydmX5oDes1sjg%40mail.gmail.com
Author: Xuneng Zhou <xunengzhou(at)gmail(dot)com>
Reviewed-by: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Reviewed-by: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Reviewed-by: Alvaro Herrera <alvherre(at)kurilemu(dot)de>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7e8aeb9e483db48500987da8981a70b491b7844c

Modified Files
--------------
src/test/perl/PostgreSQL/Test/Cluster.pm | 108 ++++++++++++++++++++++++++++---
1 file changed, 100 insertions(+), 8 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2026-04-06 19:56:52 pgsql: Add CONCURRENTLY option to REPACK
Previous Message Nathan Bossart 2026-04-06 19:31:32 pgsql: Remove recheck_relation_needs_vacanalyze().