Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d9ef650

Browse files
committed
Add new function pg_get_wal_summarizer_state().
This makes it possible to access information about the progress of WAL summarization from SQL. The previously-added functions pg_available_wal_summaries() and pg_wal_summary_contents() only examine on-disk state, but this function exposes information from the server's shared memory. Discussion: http://postgr.es/m/CA+Tgmobvqqj-DW9F7uUzT-cQqs6wcVb-Xhs=w=hzJnXSE-kRGw@mail.gmail.com
1 parent 544bcb5 commit d9ef650

File tree

6 files changed

+146
-1
lines changed

6 files changed

+146
-1
lines changed

doc/src/sgml/func.sgml

+28
Original file line numberDiff line numberDiff line change
@@ -26554,6 +26554,34 @@ SELECT collation for ('foo' COLLATE "de_DE");
2655426554
<literal>relblocknumber</literal> will be zero.
2655526555
</para></entry>
2655626556
</row>
26557+
26558+
<row>
26559+
<entry role="func_table_entry"><para role="func_signature">
26560+
<indexterm>
26561+
<primary>pg_get_wal_summarizer_state</primary>
26562+
</indexterm>
26563+
<function>pg_get_wal_summarizer_state</function> ()
26564+
<returnvalue>record</returnvalue>
26565+
( <parameter>summarized_tli</parameter> <type>bigint</type>,
26566+
<parameter>summarized_lsn</parameter> <type>pg_lsn</type>,
26567+
<parameter>pending_lsn</parameter> <type>pg_lsn</type>,
26568+
<parameter>summarizer_pid</parameter> <type>int</type> )
26569+
</para>
26570+
<para>
26571+
Returns information about the progress of the WAL summarizer. If the
26572+
WAL summarizer has never run since the instance was started, then
26573+
<literal>summarized_tli</literal> and <literal>summarized_lsn</literal>
26574+
will be <literal>0</literal> and <literal>0/0</literal> respectively;
26575+
otherwise, they will be the TLI and ending LSN of the last WAL summary
26576+
file written to disk. If the WAL summarizer is currently running,
26577+
<literal>pending_lsn</literal> will be the ending LSN of the last
26578+
record that it has consumed, which must always be greater than or
26579+
equal to <literal>summarized_lsn</literal>; if the WAL summarizer is
26580+
not running, it will be equal to <literal>summarized_lsn</literal>.
26581+
<literal>summarized_pid</literal> is the PID of the WAL summarizer
26582+
process, if it is running, and otherwise NULL.
26583+
</para></entry>
26584+
</row>
2655726585
</tbody>
2655826586
</tgroup>
2655926587
</table>

src/backend/backup/walsummaryfuncs.c

+39
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
#include "common/blkreftable.h"
1717
#include "funcapi.h"
1818
#include "miscadmin.h"
19+
#include "postmaster/walsummarizer.h"
1920
#include "utils/fmgrprotos.h"
2021
#include "utils/pg_lsn.h"
2122

2223
#define NUM_WS_ATTS 3
2324
#define NUM_SUMMARY_ATTS 6
25+
#define NUM_STATE_ATTS 4
2426
#define MAX_BLOCKS_PER_CALL 256
2527

2628
/*
@@ -167,3 +169,40 @@ pg_wal_summary_contents(PG_FUNCTION_ARGS)
167169

168170
return (Datum) 0;
169171
}
172+
173+
/*
174+
* Returns information about the state of the WAL summarizer process.
175+
*/
176+
Datum
177+
pg_get_wal_summarizer_state(PG_FUNCTION_ARGS)
178+
{
179+
Datum values[NUM_STATE_ATTS];
180+
bool nulls[NUM_STATE_ATTS];
181+
TimeLineID summarized_tli;
182+
XLogRecPtr summarized_lsn;
183+
XLogRecPtr pending_lsn;
184+
int summarizer_pid;
185+
TupleDesc tupdesc;
186+
HeapTuple htup;
187+
188+
GetWalSummarizerState(&summarized_tli, &summarized_lsn, &pending_lsn,
189+
&summarizer_pid);
190+
191+
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
192+
elog(ERROR, "return type must be a row type");
193+
194+
memset(nulls, 0, sizeof(nulls));
195+
196+
values[0] = Int64GetDatum((int64) summarized_tli);
197+
values[1] = LSNGetDatum(summarized_lsn);
198+
values[2] = LSNGetDatum(pending_lsn);
199+
200+
if (summarizer_pid < 0)
201+
nulls[3] = true;
202+
else
203+
values[3] = Int32GetDatum(summarizer_pid);
204+
205+
htup = heap_form_tuple(tupdesc, values, nulls);
206+
207+
PG_RETURN_DATUM(HeapTupleGetDatum(htup));
208+
}

src/backend/postmaster/walsummarizer.c

+65
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ static XLogRecPtr redo_pointer_at_last_summary_removal = InvalidXLogRecPtr;
142142
bool summarize_wal = false;
143143
int wal_summary_keep_time = 10 * 24 * 60;
144144

145+
static void WalSummarizerShutdown(int code, Datum arg);
145146
static XLogRecPtr GetLatestLSN(TimeLineID *tli);
146147
static void HandleWalSummarizerInterrupts(void);
147148
static XLogRecPtr SummarizeWAL(TimeLineID tli, XLogRecPtr start_lsn,
@@ -245,6 +246,7 @@ WalSummarizerMain(void)
245246
pqsignal(SIGUSR2, SIG_IGN); /* not used */
246247

247248
/* Advertise ourselves. */
249+
on_shmem_exit(WalSummarizerShutdown, (Datum) 0);
248250
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
249251
WalSummarizerCtl->summarizer_pgprocno = MyProc->pgprocno;
250252
LWLockRelease(WALSummarizerLock);
@@ -417,6 +419,57 @@ WalSummarizerMain(void)
417419
}
418420
}
419421

422+
/*
423+
* Get information about the state of the WAL summarizer.
424+
*/
425+
void
426+
GetWalSummarizerState(TimeLineID *summarized_tli, XLogRecPtr *summarized_lsn,
427+
XLogRecPtr *pending_lsn, int *summarizer_pid)
428+
{
429+
LWLockAcquire(WALSummarizerLock, LW_SHARED);
430+
if (!WalSummarizerCtl->initialized)
431+
{
432+
/*
433+
* If initialized is false, the rest of the structure contents are
434+
* undefined.
435+
*/
436+
*summarized_tli = 0;
437+
*summarized_lsn = InvalidXLogRecPtr;
438+
*pending_lsn = InvalidXLogRecPtr;
439+
*summarizer_pid = -1;
440+
}
441+
else
442+
{
443+
int summarizer_pgprocno = WalSummarizerCtl->summarizer_pgprocno;
444+
445+
*summarized_tli = WalSummarizerCtl->summarized_tli;
446+
*summarized_lsn = WalSummarizerCtl->summarized_lsn;
447+
if (summarizer_pgprocno == INVALID_PGPROCNO)
448+
{
449+
/*
450+
* If the summarizer has exited, the fact that it had processed
451+
* beyond summarized_lsn is irrelevant now.
452+
*/
453+
*pending_lsn = WalSummarizerCtl->summarized_lsn;
454+
*summarizer_pid = -1;
455+
}
456+
else
457+
{
458+
*pending_lsn = WalSummarizerCtl->pending_lsn;
459+
460+
/*
461+
* We're not fussed about inexact answers here, since they could
462+
* become stale instantly, so we don't bother taking the lock, but
463+
* make sure that invalid PID values are normalized to -1.
464+
*/
465+
*summarizer_pid = GetPGProcByNumber(summarizer_pgprocno)->pid;
466+
if (*summarizer_pid <= 0)
467+
*summarizer_pid = -1;
468+
}
469+
}
470+
LWLockRelease(WALSummarizerLock);
471+
}
472+
420473
/*
421474
* Get the oldest LSN in this server's timeline history that has not yet been
422475
* summarized.
@@ -622,6 +675,18 @@ WaitForWalSummarization(XLogRecPtr lsn, long timeout, XLogRecPtr *pending_lsn)
622675
return summarized_lsn;
623676
}
624677

678+
/*
679+
* On exit, update shared memory to make it clear that we're no longer
680+
* running.
681+
*/
682+
static void
683+
WalSummarizerShutdown(int code, Datum arg)
684+
{
685+
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
686+
WalSummarizerCtl->summarizer_pgprocno = INVALID_PGPROCNO;
687+
LWLockRelease(WALSummarizerLock);
688+
}
689+
625690
/*
626691
* Get the latest LSN that is eligible to be summarized, and set *tli to the
627692
* corresponding timeline.

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202401041
60+
#define CATALOG_VERSION_NO 202401111
6161

6262
#endif

src/include/catalog/pg_proc.dat

+9
Original file line numberDiff line numberDiff line change
@@ -12142,5 +12142,14 @@
1214212142
proargmodes => '{i,i,i,o,o,o,o,o,o}',
1214312143
proargnames => '{tli,start_lsn,end_lsn,relfilenode,reltablespace,reldatabase,relforknumber,relblocknumber,is_limit_block}',
1214412144
prosrc => 'pg_wal_summary_contents' },
12145+
{ oid => '8438',
12146+
descr => 'WAL summarizer state',
12147+
proname => 'pg_get_wal_summarizer_state',
12148+
provolatile => 'v', proparallel => 's',
12149+
prorettype => 'record', proargtypes => '',
12150+
proallargtypes => '{int8,pg_lsn,pg_lsn,int4}',
12151+
proargmodes => '{o,o,o,o}',
12152+
proargnames => '{summarized_tli,summarized_lsn,pending_lsn,summarizer_pid}',
12153+
prosrc => 'pg_get_wal_summarizer_state' },
1214512154

1214612155
]

src/include/postmaster/walsummarizer.h

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ extern Size WalSummarizerShmemSize(void);
2323
extern void WalSummarizerShmemInit(void);
2424
extern void WalSummarizerMain(void) pg_attribute_noreturn();
2525

26+
extern void GetWalSummarizerState(TimeLineID *summarized_tli,
27+
XLogRecPtr *summarized_lsn,
28+
XLogRecPtr *pending_lsn,
29+
int *summarizer_pid);
2630
extern XLogRecPtr GetOldestUnsummarizedLSN(TimeLineID *tli,
2731
bool *lsn_is_exact,
2832
bool reset_pending_lsn);

0 commit comments

Comments
 (0)