From b9bd131f12e4bec77ab35525579e14cbc418286a Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Sun, 20 Mar 2022 08:51:50 +0000
Subject: [PATCH v12] pg_walinspect docs

---
 doc/src/sgml/contrib.sgml      |   1 +
 doc/src/sgml/filelist.sgml     |   1 +
 doc/src/sgml/pgwalinspect.sgml | 202 +++++++++++++++++++++++++++++++++
 3 files changed, 204 insertions(+)
 create mode 100644 doc/src/sgml/pgwalinspect.sgml

diff --git a/doc/src/sgml/contrib.sgml b/doc/src/sgml/contrib.sgml
index 1e42ce1a7f..4e7b87a42f 100644
--- a/doc/src/sgml/contrib.sgml
+++ b/doc/src/sgml/contrib.sgml
@@ -131,6 +131,7 @@ CREATE EXTENSION <replaceable>module_name</replaceable>;
  &pgsurgery;
  &pgtrgm;
  &pgvisibility;
+ &pgwalinspect;
  &postgres-fdw;
  &seg;
  &sepgsql;
diff --git a/doc/src/sgml/filelist.sgml b/doc/src/sgml/filelist.sgml
index fd853af01f..34c19c80f1 100644
--- a/doc/src/sgml/filelist.sgml
+++ b/doc/src/sgml/filelist.sgml
@@ -147,6 +147,7 @@
 <!ENTITY pgsurgery       SYSTEM "pgsurgery.sgml">
 <!ENTITY pgtrgm          SYSTEM "pgtrgm.sgml">
 <!ENTITY pgvisibility    SYSTEM "pgvisibility.sgml">
+<!ENTITY pgwalinspect 	 SYSTEM "pgwalinspect.sgml">
 <!ENTITY postgres-fdw    SYSTEM "postgres-fdw.sgml">
 <!ENTITY seg             SYSTEM "seg.sgml">
 <!ENTITY contrib-spi     SYSTEM "contrib-spi.sgml">
diff --git a/doc/src/sgml/pgwalinspect.sgml b/doc/src/sgml/pgwalinspect.sgml
new file mode 100644
index 0000000000..d47d98b91e
--- /dev/null
+++ b/doc/src/sgml/pgwalinspect.sgml
@@ -0,0 +1,202 @@
+<!-- doc/src/sgml/pgwalinspect.sgml -->
+
+<sect1 id="pgwalinspect" xreflabel="pg_walinspect">
+ <title>pg_walinspect</title>
+
+ <indexterm zone="pgwalinspect">
+  <primary>pg_walinspect</primary>
+ </indexterm>
+
+ <para>
+  The <filename>pg_walinspect</filename> module provides functions that allow
+  you to inspect the contents of write-ahead log of <productname>PostgreSQL</productname>
+  database cluster at a low level, which is useful for debugging or analytical
+  or reporting or educational purposes.
+ </para>
+
+ <para>
+  All the functions of this module will provide the WAL information using the
+  current server's timeline ID.
+ </para>
+
+ <sect2>
+  <title>General Functions</title>
+
+  <variablelist>
+   <varlistentry>
+    <term>
+     <function>
+      pg_get_raw_wal_record(in_lsn pg_lsn,
+                            start_lsn OUT pg_lsn,
+                            end_lsn OUT pg_lsn,
+                            record_length OUT int4,
+                            record OUT bytea)
+     </function>
+    </term>
+
+    <listitem>
+     <para>
+      Gets raw WAL record data of a given LSN. This function emits an error if
+      a future (the LSN database system doesn't know about) <replaceable>in_lsn</replaceable>
+      is specified.
+     </para>
+
+      <para>
+       By default, use of this function is restricted to superusers and members
+       of the <literal>pg_read_server_files</literal> role. Access may be
+       granted by superusers to others using <command>GRANT</command>.
+      </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term>
+     <function>
+      pg_get_wal_record_info(in_lsn pg_lsn,
+                             start_lsn OUT pg_lsn,
+                             end_lsn OUT pg_lsn,
+                             prev_lsn OUT pg_lsn,
+                             xid OUT xid,
+                             resource_manager OUT text,
+                             record_length OUT int4,
+                             fpi_length OUT int4,
+                             description OUT text,
+                             block_ref OUT text,
+                             data_length OUT int4,
+                             data OUT bytea)
+     </function>
+    </term>
+
+    <listitem>
+     <para>
+      Gets WAL record information of the given LSN. This function emits an
+      error if a future (the LSN database system doesn't know about) <replaceable>in_lsn</replaceable>
+      is specified.
+     </para>
+
+      <para>
+       By default, use of this function is restricted to superusers and members
+       of the <literal>pg_monitor</literal> role. Access may be granted by
+       superusers to others using <command>GRANT</command>.
+      </para>
+    </listitem>
+   </varlistentry>
+
+    <varlistentry>
+    <term>
+     <function>
+      pg_get_wal_records_info(start_lsn pg_lsn,
+                              end_lsn pg_lsn,
+                              start_lsn OUT pg_lsn,
+                              end_lsn OUT pg_lsn,
+                              prev_lsn OUT pg_lsn,
+                              xid OUT xid,
+                              resource_manager OUT text,
+                              record_length OUT int4,
+                              fpi_length OUT int4,
+                              description OUT text,
+                              block_ref OUT text,
+                              data_length OUT int4,
+                              data OUT bytea)
+      returns setof record
+     </function>
+    </term>
+
+    <listitem>
+     <para>
+      Gets information of all the valid WAL records between <replaceable>start_lsn</replaceable>
+      and <replaceable>end_lsn</replaceable>. Returns one row per each valid
+      WAL record. This function figures out the <replaceable>end_lsn</replaceable>
+      if it's not specified, that means, it returns information up to the end
+      of WAL. Default value of <replaceable>end_lsn</replaceable> is
+      <literal>NULL</literal>. This function emits an error if a future (the
+      LSN database system doesn't know about) <replaceable>start_lsn</replaceable>
+      or <replaceable>end_lsn</replaceable> is specified. For example:
+<screen>
+postgres=# select start_lsn, end_lsn, prev_lsn, xid, resource_manager, record_length, fpi_length, block_ref, data_length from pg_get_wal_records_info('0/1602790', '0/16038D0');
+ start_lsn |  end_lsn  | prev_lsn  | xid | resource_manager | record_length | fpi_length |                                block_ref                                 | data_length 
+-----------+-----------+-----------+-----+------------------+---------------+------------+--------------------------------------------------------------------------+-------------
+ 0/1602790 | 0/16027C7 | 0/1602758 |   0 | Standby          |            50 |          0 |                                                                          |          24
+ 0/16027C8 | 0/160283F | 0/1602790 |   0 | XLOG             |           114 |          0 |                                                                          |          88
+ 0/1602840 | 0/1602877 | 0/16027C8 |   0 | Standby          |            50 |          0 |                                                                          |          24
+ 0/1602878 | 0/16028AF | 0/1602840 |   0 | Standby          |            50 |          0 |                                                                          |          24
+ 0/16028B0 | 0/1602927 | 0/1602878 |   0 | XLOG             |           114 |          0 |                                                                          |          88
+ 0/1602928 | 0/16037AF | 0/16028B0 | 751 | Heap             |          3714 |       3660 | blkref #0: rel 1663/5/24628 blk 0 (FPW); hole: offset: 428, length: 4532 |           3
+ 0/16037B0 | 0/16037EF | 0/1602928 | 751 | Heap             |            59 |          0 | blkref #0: rel 1663/5/24628 blk 0                                        |           3
+ 0/16037F0 | 0/160382F | 0/16037B0 | 751 | Heap             |            59 |          0 | blkref #0: rel 1663/5/24628 blk 0                                        |           3
+ 0/1603830 | 0/160386F | 0/16037F0 | 751 | Heap             |            59 |          0 | blkref #0: rel 1663/5/24628 blk 0                                        |           3
+ 0/1603870 | 0/16038A7 | 0/1603830 |   0 | Standby          |            54 |          0 |                                                                          |          28
+ 0/16038A8 | 0/16038CF | 0/1603870 | 751 | Transaction      |            34 |          0 |                                                                          |           8
+(11 rows)
+</screen>
+     </para>
+
+      <para>
+       By default, use of this function is restricted to superusers and members
+       of the <literal>pg_monitor</literal> role. Access may be granted by
+       superusers to others using <command>GRANT</command>.
+      </para>
+    </listitem>
+   </varlistentry>
+
+    <varlistentry>
+    <term>
+     <function>
+      pg_get_wal_stats(start_lsn pg_lsn,
+                       end_lsn pg_lsn,
+                       resource_manager OUT text,
+                       count OUT int8,
+                       count_percentage OUT float4,
+                       record_length OUT int8,
+                       record_length_percentage OUT float4,
+                       fpi_length OUT int8,
+                       fpi_length_percentage OUT float4)
+      returns setof record
+     </function>
+    </term>
+
+    <listitem>
+     <para>
+      Gets statistics of all the valid WAL records between <replaceable>start_lsn</replaceable>
+      and <replaceable>end_lsn</replaceable>. Returns one row per each <replaceable>resource_manager</replaceable>
+      type. This function figures out the <replaceable>end_lsn</replaceable> if
+      it's not specified, that means, it returns information up to the end of
+      WAL. Default value of <replaceable>end_lsn</replaceable> is
+      <literal>NULL</literal>. This function emits an error if a future (the
+      LSN database system doesn't know about) <replaceable>start_lsn</replaceable>
+      or <replaceable>end_lsn</replaceable> is specified. For example:
+<screen>
+postgres=# select * from pg_get_wal_stats('0/15E80C0', '0/1600000');
+ resource_manager | count | count_percentage | record_length | record_length_percentage | fpi_length | fpi_length_percentage 
+------------------+-------+------------------+---------------+--------------------------+------------+-----------------------
+ Storage          |     1 |              0.7 |            42 |                     0.04 |          0 |                     0
+ Btree            |    27 |            18.88 |         65116 |                    66.98 |      63456 |                 72.45
+ Heap             |   101 |            70.63 |          7697 |                     7.92 |       1596 |                  1.82
+ Transaction      |     1 |              0.7 |           405 |                     0.42 |          0 |                     0
+ Heap2            |     6 |              4.2 |         23573 |                    24.25 |      22532 |                 25.73
+ Standby          |     5 |              3.5 |           242 |                     0.25 |          0 |                     0
+ XLOG             |     2 |              1.4 |           144 |                     0.15 |          0 |                     0
+(7 rows)
+</screen>
+     </para>
+
+      <para>
+       By default, use of this function is restricted to superusers and members
+       of the <literal>pg_monitor</literal> role. Access may be granted by
+       superusers to others using <command>GRANT</command>.
+      </para>
+    </listitem>
+   </varlistentry>
+
+  </variablelist>
+ </sect2>
+
+ <sect2>
+  <title>Author</title>
+
+  <para>
+   Bharath Rupireddy <email>bharath.rupireddyforpostgres@gmail.com</email>
+  </para>
+ </sect2>
+
+</sect1>
-- 
2.25.1

