v3-0002-Add-pg_last_wal_write_lsn-SQL-function.patch
application/octet-stream
Filename: v3-0002-Add-pg_last_wal_write_lsn-SQL-function.patch
Type: application/octet-stream
Part: 1
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v3-0002
Subject: Add pg_last_wal_write_lsn() SQL function
| File | + | − |
|---|---|---|
| doc/src/sgml/func/func-admin.sgml | 22 | 0 |
| src/backend/access/transam/xlogfuncs.c | 20 | 0 |
| src/include/catalog/pg_proc.dat | 4 | 0 |
From 9d22e09d378e8f6c52aa95bc4a0e1650f4621a39 Mon Sep 17 00:00:00 2001
From: alterego655 <824662526@qq.com>
Date: Tue, 25 Nov 2025 19:07:52 +0800
Subject: [PATCH v3 2/5] Add pg_last_wal_write_lsn() SQL function
Returns the current WAL write position on a standby server using
GetWalRcvWriteRecPtr(). This enables verification of WAIT FOR LSN MODE WRITE
and operational monitoring of standby WAL write progress.
---
doc/src/sgml/func/func-admin.sgml | 22 ++++++++++++++++++++++
src/backend/access/transam/xlogfuncs.c | 20 ++++++++++++++++++++
src/include/catalog/pg_proc.dat | 4 ++++
3 files changed, 46 insertions(+)
diff --git a/doc/src/sgml/func/func-admin.sgml b/doc/src/sgml/func/func-admin.sgml
index 1b465bc8ba7..9ff196c4be4 100644
--- a/doc/src/sgml/func/func-admin.sgml
+++ b/doc/src/sgml/func/func-admin.sgml
@@ -688,6 +688,28 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
</para></entry>
</row>
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
+ <primary>pg_last_wal_write_lsn</primary>
+ </indexterm>
+ <function>pg_last_wal_write_lsn</function> ()
+ <returnvalue>pg_lsn</returnvalue>
+ </para>
+ <para>
+ Returns the last write-ahead log location that has been received and
+ passed to the operating system by streaming replication, but not
+ necessarily synced to durable storage. This is faster than
+ <function>pg_last_wal_receive_lsn</function> but provides weaker
+ durability guarantees since the data may still be in OS buffers.
+ While streaming replication is in progress this will increase
+ monotonically. If recovery has completed then this will remain static
+ at the location of the last WAL record written during recovery. If
+ streaming replication is disabled, or if it has not yet started, the
+ function returns <literal>NULL</literal>.
+ </para></entry>
+ </row>
+
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index 3e45fce43ed..2797b2bf158 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -347,6 +347,26 @@ pg_last_wal_receive_lsn(PG_FUNCTION_ARGS)
PG_RETURN_LSN(recptr);
}
+/*
+ * Report the last WAL write location (same format as pg_backup_start etc)
+ *
+ * This is useful for determining how much of WAL has been received and
+ * passed to the operating system by walreceiver. Unlike pg_last_wal_receive_lsn,
+ * this data may still be in OS buffers and not yet synced to durable storage.
+ */
+Datum
+pg_last_wal_write_lsn(PG_FUNCTION_ARGS)
+{
+ XLogRecPtr recptr;
+
+ recptr = GetWalRcvWriteRecPtr();
+
+ if (!XLogRecPtrIsValid(recptr))
+ PG_RETURN_NULL();
+
+ PG_RETURN_LSN(recptr);
+}
+
/*
* Report the last WAL replay location (same format as pg_backup_start etc)
*
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 66431940700..478e0a8139f 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6782,6 +6782,10 @@
proname => 'pg_last_wal_receive_lsn', provolatile => 'v',
prorettype => 'pg_lsn', proargtypes => '',
prosrc => 'pg_last_wal_receive_lsn' },
+{ oid => '6434', descr => 'last wal write location on standby',
+ proname => 'pg_last_wal_write_lsn', provolatile => 'v',
+ prorettype => 'pg_lsn', proargtypes => '',
+ prosrc => 'pg_last_wal_write_lsn' },
{ oid => '3821', descr => 'last wal replay location',
proname => 'pg_last_wal_replay_lsn', provolatile => 'v',
prorettype => 'pg_lsn', proargtypes => '',
--
2.51.0