postgresql/src/backend
Fujii Masao eef1ba704d Fix premature NULL lag reporting in pg_stat_replication
pg_stat_replication is documented to keep the last measured lag values for
a short time after the standby catches up, and then set them to NULL when
there is no WAL activity. However, previously lag values could become NULL
prematurely even while WAL activity was ongoing, especially in logical
replication.

This happened because the code cleared lag when two consecutive reply messages
indicated that the apply location had caught up with the send location.
It did not verify that the reported positions were unchanged, so lag could be
cleared even when positions had advanced between messages. In logical
replication, where the apply location often quickly catches up, this issue was
more likely to occur.

This commit fixes the issue by clearing lag only when the standby reports that
it has fully replayed WAL (i.e., both flush and apply locations have caught up
with the send location) and the write/flush/apply positions remain unchanged
across two consecutive reply messages.

The second message with unchanged positions typically results from
wal_receiver_status_interval, so lag values are cleared after that interval
when there is no activity. This avoids showing stale lag data while preventing
premature NULL values.

Even with this fix, lag may rarely become NULL during activity if identical
position reports are sent repeatedly. Eliminating such duplicate messages
would address this fully, but that change is considered too invasive for stable
branches and will be handled in master only later.

Backpatch to all supported branches.

Author: Shinya Kato <shinya11.kato@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAOzEurTzcUrEzrH97DD7+Yz=HGPU81kzWQonKZvqBwYhx2G9_A@mail.gmail.com
Backpatch-through: 14
2026-03-26 20:49:31 +09:00
..
access Improve timeout handling of pg_promote() 2026-03-26 10:39:40 +09:00
archive Don't include wait_event.h in pgstat.h 2026-03-06 16:24:58 +01:00
backup Add missing deflateEnd() for server-side gzip base backups 2026-03-23 09:04:44 +09:00
bootstrap Reduce header inclusions via execnodes.h 2026-03-16 14:34:57 +01:00
catalog Refactor to remove ForeignServerName(). 2026-03-24 15:20:28 -07:00
commands Refactor to remove ForeignServerName(). 2026-03-24 15:20:28 -07:00
executor Make many cast functions error safe 2026-03-24 12:08:22 +01:00
foreign Refactor to remove ForeignServerName(). 2026-03-24 15:20:28 -07:00
jit Reduce header inclusions via execnodes.h 2026-03-16 14:34:57 +01:00
lib dshash: Make it possible to suppress out of memory errors 2026-03-19 11:51:17 -04:00
libpq ssl: Serverside SNI support for libpq 2026-03-18 12:37:11 +01:00
main Update copyright for 2026 2026-01-01 13:24:10 -05:00
nodes Allow IS JSON predicate to work with domain types 2026-03-17 15:20:22 -04:00
optimizer get_memoize_path: Don't exit quickly when PGS_NESTLOOP_PLAIN is unset. 2026-03-24 16:17:26 -04:00
parser Don't include storage/lock.h in so many headers 2026-03-24 17:11:12 +01:00
partitioning Add some const qualifiers enabled by typeof_unqual change on copyObject 2026-03-19 06:35:54 +01:00
po Update copyright for 2026 2026-01-01 13:24:10 -05:00
port Disable warnings in system headers in MSVC 2026-03-25 15:03:52 +01:00
postmaster Remove unused autovac_table.at_sharedrel 2026-03-25 18:24:34 +01:00
regex Use fallthrough attribute instead of comment 2026-02-19 08:51:12 +01:00
replication Fix premature NULL lag reporting in pg_stat_replication 2026-03-26 20:49:31 +09:00
rewrite Fix some typos and make small stylistic improvements 2026-03-25 09:17:40 +01:00
snowball Fix meson build of snowball code. 2026-01-05 16:51:36 -05:00
statistics Fix inconsistency with HeapTuple freeing in extended_stats_funcs.c 2026-03-06 14:49:00 +09:00
storage Refactor ShmemIndex initialization 2026-03-26 11:35:55 +02:00
tcop Reduce header inclusions via execnodes.h 2026-03-16 14:34:57 +01:00
tsearch Require superuser to install a non-built-in selectivity estimator. 2026-02-09 10:07:31 -05:00
utils Add base32hex support to encode() and decode() functions. 2026-03-25 11:35:19 -07:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile ssl: Serverside SNI support for libpq 2026-03-18 12:37:11 +01:00
meson.build Add backtrace support for Windows using DbgHelp API 2026-02-24 17:34:56 +01:00
nls.mk Create a separate file listing backend types 2025-09-26 15:21:49 +02:00