1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
|
/*-------------------------------------------------------------------------
*
* walsummarizer.c
*
* Background process to perform WAL summarization, if it is enabled.
* It continuously scans the write-ahead log and periodically emits a
* summary file which indicates which blocks in which relation forks
* were modified by WAL records in the LSN range covered by the summary
* file. See walsummary.c and blkreftable.c for more details on the
* naming and contents of WAL summary files.
*
* If configured to do, this background process will also remove WAL
* summary files when the file timestamp is older than a configurable
* threshold (but only if the WAL has been removed first).
*
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/backend/postmaster/walsummarizer.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/timeline.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "backup/walsummary.h"
#include "catalog/storage_xlog.h"
#include "common/blkreftable.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "postmaster/interrupt.h"
#include "postmaster/walsummarizer.h"
#include "replication/walreceiver.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/latch.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/wait_event.h"
/*
* Data in shared memory related to WAL summarization.
*/
typedef struct
{
/*
* These fields are protected by WALSummarizerLock.
*
* Until we've discovered what summary files already exist on disk and
* stored that information in shared memory, initialized is false and the
* other fields here contain no meaningful information. After that has
* been done, initialized is true.
*
* summarized_tli and summarized_lsn indicate the last LSN and TLI at
* which the next summary file will start. Normally, these are the LSN and
* TLI at which the last file ended; in such case, lsn_is_exact is true.
* If, however, the LSN is just an approximation, then lsn_is_exact is
* false. This can happen if, for example, there are no existing WAL
* summary files at startup. In that case, we have to derive the position
* at which to start summarizing from the WAL files that exist on disk,
* and so the LSN might point to the start of the next file even though
* that might happen to be in the middle of a WAL record.
*
* summarizer_pgprocno is the proc number of the summarizer process, if
* one is running, or else INVALID_PROC_NUMBER.
*
* pending_lsn is used by the summarizer to advertise the ending LSN of a
* record it has recently read. It shouldn't ever be less than
* summarized_lsn, but might be greater, because the summarizer buffers
* data for a range of LSNs in memory before writing out a new file.
*/
bool initialized;
TimeLineID summarized_tli;
XLogRecPtr summarized_lsn;
bool lsn_is_exact;
ProcNumber summarizer_pgprocno;
XLogRecPtr pending_lsn;
/*
* This field handles its own synchronization.
*/
ConditionVariable summary_file_cv;
} WalSummarizerData;
/*
* Private data for our xlogreader's page read callback.
*/
typedef struct
{
TimeLineID tli;
bool historic;
XLogRecPtr read_upto;
bool end_of_wal;
} SummarizerReadLocalXLogPrivate;
/* Pointer to shared memory state. */
static WalSummarizerData *WalSummarizerCtl;
/*
* When we reach end of WAL and need to read more, we sleep for a number of
* milliseconds that is a integer multiple of MS_PER_SLEEP_QUANTUM. This is
* the multiplier. It should vary between 1 and MAX_SLEEP_QUANTA, depending
* on system activity. See summarizer_wait_for_wal() for how we adjust this.
*/
static long sleep_quanta = 1;
/*
* The sleep time will always be a multiple of 200ms and will not exceed
* thirty seconds (150 * 200 = 30 * 1000). Note that the timeout here needs
* to be substantially less than the maximum amount of time for which an
* incremental backup will wait for this process to catch up. Otherwise, an
* incremental backup might time out on an idle system just because we sleep
* for too long.
*/
#define MAX_SLEEP_QUANTA 150
#define MS_PER_SLEEP_QUANTUM 200
/*
* This is a count of the number of pages of WAL that we've read since the
* last time we waited for more WAL to appear.
*/
static long pages_read_since_last_sleep = 0;
/*
* Most recent RedoRecPtr value observed by MaybeRemoveOldWalSummaries.
*/
static XLogRecPtr redo_pointer_at_last_summary_removal = InvalidXLogRecPtr;
/*
* GUC parameters
*/
bool summarize_wal = false;
int wal_summary_keep_time = 10 * 24 * 60;
static void WalSummarizerShutdown(int code, Datum arg);
static XLogRecPtr GetLatestLSN(TimeLineID *tli);
static void HandleWalSummarizerInterrupts(void);
static XLogRecPtr SummarizeWAL(TimeLineID tli, XLogRecPtr start_lsn,
bool exact, XLogRecPtr switch_lsn,
XLogRecPtr maximum_lsn);
static void SummarizeSmgrRecord(XLogReaderState *xlogreader,
BlockRefTable *brtab);
static void SummarizeXactRecord(XLogReaderState *xlogreader,
BlockRefTable *brtab);
static bool SummarizeXlogRecord(XLogReaderState *xlogreader);
static int summarizer_read_local_xlog_page(XLogReaderState *state,
XLogRecPtr targetPagePtr,
int reqLen,
XLogRecPtr targetRecPtr,
char *cur_page);
static void summarizer_wait_for_wal(void);
static void MaybeRemoveOldWalSummaries(void);
/*
* Amount of shared memory required for this module.
*/
Size
WalSummarizerShmemSize(void)
{
return sizeof(WalSummarizerData);
}
/*
* Create or attach to shared memory segment for this module.
*/
void
WalSummarizerShmemInit(void)
{
bool found;
WalSummarizerCtl = (WalSummarizerData *)
ShmemInitStruct("Wal Summarizer Ctl", WalSummarizerShmemSize(),
&found);
if (!found)
{
/*
* First time through, so initialize.
*
* We're just filling in dummy values here -- the real initialization
* will happen when GetOldestUnsummarizedLSN() is called for the first
* time.
*/
WalSummarizerCtl->initialized = false;
WalSummarizerCtl->summarized_tli = 0;
WalSummarizerCtl->summarized_lsn = InvalidXLogRecPtr;
WalSummarizerCtl->lsn_is_exact = false;
WalSummarizerCtl->summarizer_pgprocno = INVALID_PROC_NUMBER;
WalSummarizerCtl->pending_lsn = InvalidXLogRecPtr;
ConditionVariableInit(&WalSummarizerCtl->summary_file_cv);
}
}
/*
* Entry point for walsummarizer process.
*/
void
WalSummarizerMain(void)
{
sigjmp_buf local_sigjmp_buf;
MemoryContext context;
/*
* Within this function, 'current_lsn' and 'current_tli' refer to the
* point from which the next WAL summary file should start. 'exact' is
* true if 'current_lsn' is known to be the start of a WAL record or WAL
* segment, and false if it might be in the middle of a record someplace.
*
* 'switch_lsn' and 'switch_tli', if set, are the LSN at which we need to
* switch to a new timeline and the timeline to which we need to switch.
* If not set, we either haven't figured out the answers yet or we're
* already on the latest timeline.
*/
XLogRecPtr current_lsn;
TimeLineID current_tli;
bool exact;
XLogRecPtr switch_lsn = InvalidXLogRecPtr;
TimeLineID switch_tli = 0;
ereport(DEBUG1,
(errmsg_internal("WAL summarizer started")));
/*
* Properly accept or ignore signals the postmaster might send us
*
* We have no particular use for SIGINT at the moment, but seems
* reasonable to treat like SIGTERM.
*/
pqsignal(SIGHUP, SignalHandlerForConfigReload);
pqsignal(SIGINT, SignalHandlerForShutdownRequest);
pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
/* SIGQUIT handler was already set up by InitPostmasterChild */
pqsignal(SIGALRM, SIG_IGN);
pqsignal(SIGPIPE, SIG_IGN);
pqsignal(SIGUSR1, procsignal_sigusr1_handler);
pqsignal(SIGUSR2, SIG_IGN); /* not used */
/* Advertise ourselves. */
on_shmem_exit(WalSummarizerShutdown, (Datum) 0);
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
WalSummarizerCtl->summarizer_pgprocno = MyProcNumber;
LWLockRelease(WALSummarizerLock);
/* Create and switch to a memory context that we can reset on error. */
context = AllocSetContextCreate(TopMemoryContext,
"Wal Summarizer",
ALLOCSET_DEFAULT_SIZES);
MemoryContextSwitchTo(context);
/*
* Reset some signals that are accepted by postmaster but not here
*/
pqsignal(SIGCHLD, SIG_DFL);
/*
* If an exception is encountered, processing resumes here.
*/
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
{
/* Since not using PG_TRY, must reset error stack by hand */
error_context_stack = NULL;
/* Prevent interrupts while cleaning up */
HOLD_INTERRUPTS();
/* Report the error to the server log */
EmitErrorReport();
/* Release resources we might have acquired. */
LWLockReleaseAll();
ConditionVariableCancelSleep();
pgstat_report_wait_end();
ReleaseAuxProcessResources(false);
AtEOXact_Files(false);
AtEOXact_HashTables(false);
/*
* Now return to normal top-level context and clear ErrorContext for
* next time.
*/
MemoryContextSwitchTo(context);
FlushErrorState();
/* Flush any leaked data in the top-level context */
MemoryContextReset(context);
/* Now we can allow interrupts again */
RESUME_INTERRUPTS();
/*
* Sleep for 10 seconds before attempting to resume operations in
* order to avoid excessive logging.
*
* Many of the likely error conditions are things that will repeat
* every time. For example, if the WAL can't be read or the summary
* can't be written, only administrator action will cure the problem.
* So a really fast retry time doesn't seem to be especially
* beneficial, and it will clutter the logs.
*/
(void) WaitLatch(MyLatch,
WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
10000,
WAIT_EVENT_WAL_SUMMARIZER_ERROR);
}
/* We can now handle ereport(ERROR) */
PG_exception_stack = &local_sigjmp_buf;
/*
* Unblock signals (they were blocked when the postmaster forked us)
*/
sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
/*
* Fetch information about previous progress from shared memory, and ask
* GetOldestUnsummarizedLSN to reset pending_lsn to summarized_lsn. We
* might be recovering from an error, and if so, pending_lsn might have
* advanced past summarized_lsn, but any WAL we read previously has been
* lost and will need to be reread.
*
* If we discover that WAL summarization is not enabled, just exit.
*/
current_lsn = GetOldestUnsummarizedLSN(¤t_tli, &exact, true);
if (XLogRecPtrIsInvalid(current_lsn))
proc_exit(0);
/*
* Loop forever
*/
for (;;)
{
XLogRecPtr latest_lsn;
TimeLineID latest_tli;
XLogRecPtr end_of_summary_lsn;
/* Flush any leaked data in the top-level context */
MemoryContextReset(context);
/* Process any signals received recently. */
HandleWalSummarizerInterrupts();
/* If it's time to remove any old WAL summaries, do that now. */
MaybeRemoveOldWalSummaries();
/* Find the LSN and TLI up to which we can safely summarize. */
latest_lsn = GetLatestLSN(&latest_tli);
/*
* If we're summarizing a historic timeline and we haven't yet
* computed the point at which to switch to the next timeline, do that
* now.
*
* Note that if this is a standby, what was previously the current
* timeline could become historic at any time.
*
* We could try to make this more efficient by caching the results of
* readTimeLineHistory when latest_tli has not changed, but since we
* only have to do this once per timeline switch, we probably wouldn't
* save any significant amount of work in practice.
*/
if (current_tli != latest_tli && XLogRecPtrIsInvalid(switch_lsn))
{
List *tles = readTimeLineHistory(latest_tli);
switch_lsn = tliSwitchPoint(current_tli, tles, &switch_tli);
ereport(DEBUG1,
errmsg("switch point from TLI %u to TLI %u is at %X/%X",
current_tli, switch_tli, LSN_FORMAT_ARGS(switch_lsn)));
}
/*
* If we've reached the switch LSN, we can't summarize anything else
* on this timeline. Switch to the next timeline and go around again.
*/
if (!XLogRecPtrIsInvalid(switch_lsn) && current_lsn >= switch_lsn)
{
current_tli = switch_tli;
switch_lsn = InvalidXLogRecPtr;
switch_tli = 0;
continue;
}
/* Summarize WAL. */
end_of_summary_lsn = SummarizeWAL(current_tli,
current_lsn, exact,
switch_lsn, latest_lsn);
Assert(!XLogRecPtrIsInvalid(end_of_summary_lsn));
Assert(end_of_summary_lsn >= current_lsn);
/*
* Update state for next loop iteration.
*
* Next summary file should start from exactly where this one ended.
*/
current_lsn = end_of_summary_lsn;
exact = true;
/* Update state in shared memory. */
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
Assert(WalSummarizerCtl->pending_lsn <= end_of_summary_lsn);
WalSummarizerCtl->summarized_lsn = end_of_summary_lsn;
WalSummarizerCtl->summarized_tli = current_tli;
WalSummarizerCtl->lsn_is_exact = true;
WalSummarizerCtl->pending_lsn = end_of_summary_lsn;
LWLockRelease(WALSummarizerLock);
/* Wake up anyone waiting for more summary files to be written. */
ConditionVariableBroadcast(&WalSummarizerCtl->summary_file_cv);
}
}
/*
* Get information about the state of the WAL summarizer.
*/
void
GetWalSummarizerState(TimeLineID *summarized_tli, XLogRecPtr *summarized_lsn,
XLogRecPtr *pending_lsn, int *summarizer_pid)
{
LWLockAcquire(WALSummarizerLock, LW_SHARED);
if (!WalSummarizerCtl->initialized)
{
/*
* If initialized is false, the rest of the structure contents are
* undefined.
*/
*summarized_tli = 0;
*summarized_lsn = InvalidXLogRecPtr;
*pending_lsn = InvalidXLogRecPtr;
*summarizer_pid = -1;
}
else
{
int summarizer_pgprocno = WalSummarizerCtl->summarizer_pgprocno;
*summarized_tli = WalSummarizerCtl->summarized_tli;
*summarized_lsn = WalSummarizerCtl->summarized_lsn;
if (summarizer_pgprocno == INVALID_PROC_NUMBER)
{
/*
* If the summarizer has exited, the fact that it had processed
* beyond summarized_lsn is irrelevant now.
*/
*pending_lsn = WalSummarizerCtl->summarized_lsn;
*summarizer_pid = -1;
}
else
{
*pending_lsn = WalSummarizerCtl->pending_lsn;
/*
* We're not fussed about inexact answers here, since they could
* become stale instantly, so we don't bother taking the lock, but
* make sure that invalid PID values are normalized to -1.
*/
*summarizer_pid = GetPGProcByNumber(summarizer_pgprocno)->pid;
if (*summarizer_pid <= 0)
*summarizer_pid = -1;
}
}
LWLockRelease(WALSummarizerLock);
}
/*
* Get the oldest LSN in this server's timeline history that has not yet been
* summarized.
*
* If *tli != NULL, it will be set to the TLI for the LSN that is returned.
*
* If *lsn_is_exact != NULL, it will be set to true if the returned LSN is
* necessarily the start of a WAL record and false if it's just the beginning
* of a WAL segment.
*
* If reset_pending_lsn is true, resets the pending_lsn in shared memory to
* be equal to the summarized_lsn.
*/
XLogRecPtr
GetOldestUnsummarizedLSN(TimeLineID *tli, bool *lsn_is_exact,
bool reset_pending_lsn)
{
TimeLineID latest_tli;
LWLockMode mode = reset_pending_lsn ? LW_EXCLUSIVE : LW_SHARED;
int n;
List *tles;
XLogRecPtr unsummarized_lsn = InvalidXLogRecPtr;
TimeLineID unsummarized_tli = 0;
bool should_make_exact = false;
List *existing_summaries;
ListCell *lc;
/* If not summarizing WAL, do nothing. */
if (!summarize_wal)
return InvalidXLogRecPtr;
/*
* Unless we need to reset the pending_lsn, we initially acquire the lock
* in shared mode and try to fetch the required information. If we acquire
* in shared mode and find that the data structure hasn't been
* initialized, we reacquire the lock in exclusive mode so that we can
* initialize it. However, if someone else does that first before we get
* the lock, then we can just return the requested information after all.
*/
while (1)
{
LWLockAcquire(WALSummarizerLock, mode);
if (WalSummarizerCtl->initialized)
{
unsummarized_lsn = WalSummarizerCtl->summarized_lsn;
if (tli != NULL)
*tli = WalSummarizerCtl->summarized_tli;
if (lsn_is_exact != NULL)
*lsn_is_exact = WalSummarizerCtl->lsn_is_exact;
if (reset_pending_lsn)
WalSummarizerCtl->pending_lsn =
WalSummarizerCtl->summarized_lsn;
LWLockRelease(WALSummarizerLock);
return unsummarized_lsn;
}
if (mode == LW_EXCLUSIVE)
break;
LWLockRelease(WALSummarizerLock);
mode = LW_EXCLUSIVE;
}
/*
* The data structure needs to be initialized, and we are the first to
* obtain the lock in exclusive mode, so it's our job to do that
* initialization.
*
* So, find the oldest timeline on which WAL still exists, and the
* earliest segment for which it exists.
*/
(void) GetLatestLSN(&latest_tli);
tles = readTimeLineHistory(latest_tli);
for (n = list_length(tles) - 1; n >= 0; --n)
{
TimeLineHistoryEntry *tle = list_nth(tles, n);
XLogSegNo oldest_segno;
oldest_segno = XLogGetOldestSegno(tle->tli);
if (oldest_segno != 0)
{
/* Compute oldest LSN that still exists on disk. */
XLogSegNoOffsetToRecPtr(oldest_segno, 0, wal_segment_size,
unsummarized_lsn);
unsummarized_tli = tle->tli;
break;
}
}
/* It really should not be possible for us to find no WAL. */
if (unsummarized_tli == 0)
ereport(ERROR,
errcode(ERRCODE_INTERNAL_ERROR),
errmsg_internal("no WAL found on timeline %u", latest_tli));
/*
* Don't try to summarize anything older than the end LSN of the newest
* summary file that exists for this timeline.
*/
existing_summaries =
GetWalSummaries(unsummarized_tli,
InvalidXLogRecPtr, InvalidXLogRecPtr);
foreach(lc, existing_summaries)
{
WalSummaryFile *ws = lfirst(lc);
if (ws->end_lsn > unsummarized_lsn)
{
unsummarized_lsn = ws->end_lsn;
should_make_exact = true;
}
}
/* Update shared memory with the discovered values. */
WalSummarizerCtl->initialized = true;
WalSummarizerCtl->summarized_lsn = unsummarized_lsn;
WalSummarizerCtl->summarized_tli = unsummarized_tli;
WalSummarizerCtl->lsn_is_exact = should_make_exact;
WalSummarizerCtl->pending_lsn = unsummarized_lsn;
/* Also return the to the caller as required. */
if (tli != NULL)
*tli = WalSummarizerCtl->summarized_tli;
if (lsn_is_exact != NULL)
*lsn_is_exact = WalSummarizerCtl->lsn_is_exact;
LWLockRelease(WALSummarizerLock);
return unsummarized_lsn;
}
/*
* Attempt to set the WAL summarizer's latch.
*
* This might not work, because there's no guarantee that the WAL summarizer
* process was successfully started, and it also might have started but
* subsequently terminated. So, under normal circumstances, this will get the
* latch set, but there's no guarantee.
*/
void
SetWalSummarizerLatch(void)
{
ProcNumber pgprocno;
if (WalSummarizerCtl == NULL)
return;
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
pgprocno = WalSummarizerCtl->summarizer_pgprocno;
LWLockRelease(WALSummarizerLock);
if (pgprocno != INVALID_PROC_NUMBER)
SetLatch(&ProcGlobal->allProcs[pgprocno].procLatch);
}
/*
* Wait until WAL summarization reaches the given LSN, but not longer than
* the given timeout.
*
* The return value is the first still-unsummarized LSN. If it's greater than
* or equal to the passed LSN, then that LSN was reached. If not, we timed out.
*
* Either way, *pending_lsn is set to the value taken from WalSummarizerCtl.
*/
XLogRecPtr
WaitForWalSummarization(XLogRecPtr lsn, long timeout, XLogRecPtr *pending_lsn)
{
TimestampTz start_time = GetCurrentTimestamp();
TimestampTz deadline = TimestampTzPlusMilliseconds(start_time, timeout);
XLogRecPtr summarized_lsn;
Assert(!XLogRecPtrIsInvalid(lsn));
Assert(timeout > 0);
while (1)
{
TimestampTz now;
long remaining_timeout;
/*
* If the LSN summarized on disk has reached the target value, stop.
*/
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
summarized_lsn = WalSummarizerCtl->summarized_lsn;
*pending_lsn = WalSummarizerCtl->pending_lsn;
LWLockRelease(WALSummarizerLock);
if (summarized_lsn >= lsn)
break;
/* Timeout reached? If yes, stop. */
now = GetCurrentTimestamp();
remaining_timeout = TimestampDifferenceMilliseconds(now, deadline);
if (remaining_timeout <= 0)
break;
/* Wait and see. */
ConditionVariableTimedSleep(&WalSummarizerCtl->summary_file_cv,
remaining_timeout,
WAIT_EVENT_WAL_SUMMARY_READY);
}
return summarized_lsn;
}
/*
* On exit, update shared memory to make it clear that we're no longer
* running.
*/
static void
WalSummarizerShutdown(int code, Datum arg)
{
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
WalSummarizerCtl->summarizer_pgprocno = INVALID_PROC_NUMBER;
LWLockRelease(WALSummarizerLock);
}
/*
* Get the latest LSN that is eligible to be summarized, and set *tli to the
* corresponding timeline.
*/
static XLogRecPtr
GetLatestLSN(TimeLineID *tli)
{
if (!RecoveryInProgress())
{
/* Don't summarize WAL before it's flushed. */
return GetFlushRecPtr(tli);
}
else
{
XLogRecPtr flush_lsn;
TimeLineID flush_tli;
XLogRecPtr replay_lsn;
TimeLineID replay_tli;
/*
* What we really want to know is how much WAL has been flushed to
* disk, but the only flush position available is the one provided by
* the walreceiver, which may not be running, because this could be
* crash recovery or recovery via restore_command. So use either the
* WAL receiver's flush position or the replay position, whichever is
* further ahead, on the theory that if the WAL has been replayed then
* it must also have been flushed to disk.
*/
flush_lsn = GetWalRcvFlushRecPtr(NULL, &flush_tli);
replay_lsn = GetXLogReplayRecPtr(&replay_tli);
if (flush_lsn > replay_lsn)
{
*tli = flush_tli;
return flush_lsn;
}
else
{
*tli = replay_tli;
return replay_lsn;
}
}
}
/*
* Interrupt handler for main loop of WAL summarizer process.
*/
static void
HandleWalSummarizerInterrupts(void)
{
if (ProcSignalBarrierPending)
ProcessProcSignalBarrier();
if (ConfigReloadPending)
{
ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP);
}
if (ShutdownRequestPending || !summarize_wal)
{
ereport(DEBUG1,
errmsg_internal("WAL summarizer shutting down"));
proc_exit(0);
}
/* Perform logging of memory contexts of this process */
if (LogMemoryContextPending)
ProcessLogMemoryContextInterrupt();
}
/*
* Summarize a range of WAL records on a single timeline.
*
* 'tli' is the timeline to be summarized.
*
* 'start_lsn' is the point at which we should start summarizing. If this
* value comes from the end LSN of the previous record as returned by the
* xlogreader machinery, 'exact' should be true; otherwise, 'exact' should
* be false, and this function will search forward for the start of a valid
* WAL record.
*
* 'switch_lsn' is the point at which we should switch to a later timeline,
* if we're summarizing a historic timeline.
*
* 'maximum_lsn' identifies the point beyond which we can't count on being
* able to read any more WAL. It should be the switch point when reading a
* historic timeline, or the most-recently-measured end of WAL when reading
* the current timeline.
*
* The return value is the LSN at which the WAL summary actually ends. Most
* often, a summary file ends because we notice that a checkpoint has
* occurred and reach the redo pointer of that checkpoint, but sometimes
* we stop for other reasons, such as a timeline switch.
*/
static XLogRecPtr
SummarizeWAL(TimeLineID tli, XLogRecPtr start_lsn, bool exact,
XLogRecPtr switch_lsn, XLogRecPtr maximum_lsn)
{
SummarizerReadLocalXLogPrivate *private_data;
XLogReaderState *xlogreader;
XLogRecPtr summary_start_lsn;
XLogRecPtr summary_end_lsn = switch_lsn;
char temp_path[MAXPGPATH];
char final_path[MAXPGPATH];
WalSummaryIO io;
BlockRefTable *brtab = CreateEmptyBlockRefTable();
/* Initialize private data for xlogreader. */
private_data = (SummarizerReadLocalXLogPrivate *)
palloc0(sizeof(SummarizerReadLocalXLogPrivate));
private_data->tli = tli;
private_data->historic = !XLogRecPtrIsInvalid(switch_lsn);
private_data->read_upto = maximum_lsn;
/* Create xlogreader. */
xlogreader = XLogReaderAllocate(wal_segment_size, NULL,
XL_ROUTINE(.page_read = &summarizer_read_local_xlog_page,
.segment_open = &wal_segment_open,
.segment_close = &wal_segment_close),
private_data);
if (xlogreader == NULL)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed while allocating a WAL reading processor.")));
/*
* When exact = false, we're starting from an arbitrary point in the WAL
* and must search forward for the start of the next record.
*
* When exact = true, start_lsn should be either the LSN where a record
* begins, or the LSN of a page where the page header is immediately
* followed by the start of a new record. XLogBeginRead should tolerate
* either case.
*
* We need to allow for both cases because the behavior of xlogreader
* varies. When a record spans two or more xlog pages, the ending LSN
* reported by xlogreader will be the starting LSN of the following
* record, but when an xlog page boundary falls between two records, the
* end LSN for the first will be reported as the first byte of the
* following page. We can't know until we read that page how large the
* header will be, but we'll have to skip over it to find the next record.
*/
if (exact)
{
/*
* Even if start_lsn is the beginning of a page rather than the
* beginning of the first record on that page, we should still use it
* as the start LSN for the summary file. That's because we detect
* missing summary files by looking for cases where the end LSN of one
* file is less than the start LSN of the next file. When only a page
* header is skipped, nothing has been missed.
*/
XLogBeginRead(xlogreader, start_lsn);
summary_start_lsn = start_lsn;
}
else
{
summary_start_lsn = XLogFindNextRecord(xlogreader, start_lsn);
if (XLogRecPtrIsInvalid(summary_start_lsn))
{
/*
* If we hit end-of-WAL while trying to find the next valid
* record, we must be on a historic timeline that has no valid
* records that begin after start_lsn and before end of WAL.
*/
if (private_data->end_of_wal)
{
ereport(DEBUG1,
errmsg_internal("could not read WAL from timeline %u at %X/%X: end of WAL at %X/%X",
tli,
LSN_FORMAT_ARGS(start_lsn),
LSN_FORMAT_ARGS(private_data->read_upto)));
/*
* The timeline ends at or after start_lsn, without containing
* any records. Thus, we must make sure the main loop does not
* iterate. If start_lsn is the end of the timeline, then we
* won't actually emit an empty summary file, but otherwise,
* we must, to capture the fact that the LSN range in question
* contains no interesting WAL records.
*/
summary_start_lsn = start_lsn;
summary_end_lsn = private_data->read_upto;
switch_lsn = xlogreader->EndRecPtr;
}
else
ereport(ERROR,
(errmsg("could not find a valid record after %X/%X",
LSN_FORMAT_ARGS(start_lsn))));
}
/* We shouldn't go backward. */
Assert(summary_start_lsn >= start_lsn);
}
/*
* Main loop: read xlog records one by one.
*/
while (1)
{
int block_id;
char *errormsg;
XLogRecord *record;
bool stop_requested = false;
HandleWalSummarizerInterrupts();
/* We shouldn't go backward. */
Assert(summary_start_lsn <= xlogreader->EndRecPtr);
/* Now read the next record. */
record = XLogReadRecord(xlogreader, &errormsg);
if (record == NULL)
{
if (private_data->end_of_wal)
{
/*
* This timeline must be historic and must end before we were
* able to read a complete record.
*/
ereport(DEBUG1,
errmsg_internal("could not read WAL from timeline %u at %X/%X: end of WAL at %X/%X",
tli,
LSN_FORMAT_ARGS(xlogreader->EndRecPtr),
LSN_FORMAT_ARGS(private_data->read_upto)));
/* Summary ends at end of WAL. */
summary_end_lsn = private_data->read_upto;
break;
}
if (errormsg)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not read WAL from timeline %u at %X/%X: %s",
tli, LSN_FORMAT_ARGS(xlogreader->EndRecPtr),
errormsg)));
else
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not read WAL from timeline %u at %X/%X",
tli, LSN_FORMAT_ARGS(xlogreader->EndRecPtr))));
}
/* We shouldn't go backward. */
Assert(summary_start_lsn <= xlogreader->EndRecPtr);
if (!XLogRecPtrIsInvalid(switch_lsn) &&
xlogreader->ReadRecPtr >= switch_lsn)
{
/*
* Whoops! We've read a record that *starts* after the switch LSN,
* contrary to our goal of reading only until we hit the first
* record that ends at or after the switch LSN. Pretend we didn't
* read it after all by bailing out of this loop right here,
* before we do anything with this record.
*
* This can happen because the last record before the switch LSN
* might be continued across multiple pages, and then we might
* come to a page with XLP_FIRST_IS_OVERWRITE_CONTRECORD set. In
* that case, the record that was continued across multiple pages
* is incomplete and will be disregarded, and the read will
* restart from the beginning of the page that is flagged
* XLP_FIRST_IS_OVERWRITE_CONTRECORD.
*
* If this case occurs, we can fairly say that the current summary
* file ends at the switch LSN exactly. The first record on the
* page marked XLP_FIRST_IS_OVERWRITE_CONTRECORD will be
* discovered when generating the next summary file.
*/
summary_end_lsn = switch_lsn;
break;
}
/* Special handling for particular types of WAL records. */
switch (XLogRecGetRmid(xlogreader))
{
case RM_SMGR_ID:
SummarizeSmgrRecord(xlogreader, brtab);
break;
case RM_XACT_ID:
SummarizeXactRecord(xlogreader, brtab);
break;
case RM_XLOG_ID:
stop_requested = SummarizeXlogRecord(xlogreader);
break;
default:
break;
}
/*
* If we've been told that it's time to end this WAL summary file, do
* so. As an exception, if there's nothing included in this WAL
* summary file yet, then stopping doesn't make any sense, and we
* should wait until the next stop point instead.
*/
if (stop_requested && xlogreader->ReadRecPtr > summary_start_lsn)
{
summary_end_lsn = xlogreader->ReadRecPtr;
break;
}
/* Feed block references from xlog record to block reference table. */
for (block_id = 0; block_id <= XLogRecMaxBlockId(xlogreader);
block_id++)
{
RelFileLocator rlocator;
ForkNumber forknum;
BlockNumber blocknum;
if (!XLogRecGetBlockTagExtended(xlogreader, block_id, &rlocator,
&forknum, &blocknum, NULL))
continue;
/*
* As we do elsewhere, ignore the FSM fork, because it's not fully
* WAL-logged.
*/
if (forknum != FSM_FORKNUM)
BlockRefTableMarkBlockModified(brtab, &rlocator, forknum,
blocknum);
}
/* Update our notion of where this summary file ends. */
summary_end_lsn = xlogreader->EndRecPtr;
/* Also update shared memory. */
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
Assert(summary_end_lsn >= WalSummarizerCtl->pending_lsn);
Assert(summary_end_lsn >= WalSummarizerCtl->summarized_lsn);
WalSummarizerCtl->pending_lsn = summary_end_lsn;
LWLockRelease(WALSummarizerLock);
/*
* If we have a switch LSN and have reached it, stop before reading
* the next record.
*/
if (!XLogRecPtrIsInvalid(switch_lsn) &&
xlogreader->EndRecPtr >= switch_lsn)
break;
}
/* Destroy xlogreader. */
pfree(xlogreader->private_data);
XLogReaderFree(xlogreader);
/*
* If a timeline switch occurs, we may fail to make any progress at all
* before exiting the loop above. If that happens, we don't write a WAL
* summary file at all.
*/
if (summary_end_lsn > summary_start_lsn)
{
/* Generate temporary and final path name. */
snprintf(temp_path, MAXPGPATH,
XLOGDIR "/summaries/temp.summary");
snprintf(final_path, MAXPGPATH,
XLOGDIR "/summaries/%08X%08X%08X%08X%08X.summary",
tli,
LSN_FORMAT_ARGS(summary_start_lsn),
LSN_FORMAT_ARGS(summary_end_lsn));
/* Open the temporary file for writing. */
io.filepos = 0;
io.file = PathNameOpenFile(temp_path, O_WRONLY | O_CREAT | O_TRUNC);
if (io.file < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create file \"%s\": %m", temp_path)));
/* Write the data. */
WriteBlockRefTable(brtab, WriteWalSummary, &io);
/* Close temporary file and shut down xlogreader. */
FileClose(io.file);
/* Tell the user what we did. */
ereport(DEBUG1,
errmsg("summarized WAL on TLI %u from %X/%X to %X/%X",
tli,
LSN_FORMAT_ARGS(summary_start_lsn),
LSN_FORMAT_ARGS(summary_end_lsn)));
/* Durably rename the new summary into place. */
durable_rename(temp_path, final_path, ERROR);
}
return summary_end_lsn;
}
/*
* Special handling for WAL records with RM_SMGR_ID.
*/
static void
SummarizeSmgrRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
{
uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
if (info == XLOG_SMGR_CREATE)
{
xl_smgr_create *xlrec;
/*
* If a new relation fork is created on disk, there is no point
* tracking anything about which blocks have been modified, because
* the whole thing will be new. Hence, set the limit block for this
* fork to 0.
*
* Ignore the FSM fork, which is not fully WAL-logged.
*/
xlrec = (xl_smgr_create *) XLogRecGetData(xlogreader);
if (xlrec->forkNum != FSM_FORKNUM)
BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
xlrec->forkNum, 0);
}
else if (info == XLOG_SMGR_TRUNCATE)
{
xl_smgr_truncate *xlrec;
xlrec = (xl_smgr_truncate *) XLogRecGetData(xlogreader);
/*
* If a relation fork is truncated on disk, there is no point in
* tracking anything about block modifications beyond the truncation
* point.
*
* We ignore SMGR_TRUNCATE_FSM here because the FSM isn't fully
* WAL-logged and thus we can't track modified blocks for it anyway.
*/
if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
MAIN_FORKNUM, xlrec->blkno);
if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0)
BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
VISIBILITYMAP_FORKNUM, xlrec->blkno);
}
}
/*
* Special handling for WAL records with RM_XACT_ID.
*/
static void
SummarizeXactRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
{
uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
uint8 xact_info = info & XLOG_XACT_OPMASK;
if (xact_info == XLOG_XACT_COMMIT ||
xact_info == XLOG_XACT_COMMIT_PREPARED)
{
xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(xlogreader);
xl_xact_parsed_commit parsed;
int i;
/*
* Don't track modified blocks for any relations that were removed on
* commit.
*/
ParseCommitRecord(XLogRecGetInfo(xlogreader), xlrec, &parsed);
for (i = 0; i < parsed.nrels; ++i)
{
ForkNumber forknum;
for (forknum = 0; forknum <= MAX_FORKNUM; ++forknum)
if (forknum != FSM_FORKNUM)
BlockRefTableSetLimitBlock(brtab, &parsed.xlocators[i],
forknum, 0);
}
}
else if (xact_info == XLOG_XACT_ABORT ||
xact_info == XLOG_XACT_ABORT_PREPARED)
{
xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(xlogreader);
xl_xact_parsed_abort parsed;
int i;
/*
* Don't track modified blocks for any relations that were removed on
* abort.
*/
ParseAbortRecord(XLogRecGetInfo(xlogreader), xlrec, &parsed);
for (i = 0; i < parsed.nrels; ++i)
{
ForkNumber forknum;
for (forknum = 0; forknum <= MAX_FORKNUM; ++forknum)
if (forknum != FSM_FORKNUM)
BlockRefTableSetLimitBlock(brtab, &parsed.xlocators[i],
forknum, 0);
}
}
}
/*
* Special handling for WAL records with RM_XLOG_ID.
*/
static bool
SummarizeXlogRecord(XLogReaderState *xlogreader)
{
uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
if (info == XLOG_CHECKPOINT_REDO || info == XLOG_CHECKPOINT_SHUTDOWN)
{
/*
* This is an LSN at which redo might begin, so we'd like
* summarization to stop just before this WAL record.
*/
return true;
}
return false;
}
/*
* Similar to read_local_xlog_page, but limited to read from one particular
* timeline. If the end of WAL is reached, it will wait for more if reading
* from the current timeline, or give up if reading from a historic timeline.
* In the latter case, it will also set private_data->end_of_wal = true.
*
* Caller must set private_data->tli to the TLI of interest,
* private_data->read_upto to the lowest LSN that is not known to be safe
* to read on that timeline, and private_data->historic to true if and only
* if the timeline is not the current timeline. This function will update
* private_data->read_upto and private_data->historic if more WAL appears
* on the current timeline or if the current timeline becomes historic.
*/
static int
summarizer_read_local_xlog_page(XLogReaderState *state,
XLogRecPtr targetPagePtr, int reqLen,
XLogRecPtr targetRecPtr, char *cur_page)
{
int count;
WALReadError errinfo;
SummarizerReadLocalXLogPrivate *private_data;
HandleWalSummarizerInterrupts();
private_data = (SummarizerReadLocalXLogPrivate *)
state->private_data;
while (1)
{
if (targetPagePtr + XLOG_BLCKSZ <= private_data->read_upto)
{
/*
* more than one block available; read only that block, have
* caller come back if they need more.
*/
count = XLOG_BLCKSZ;
break;
}
else if (targetPagePtr + reqLen > private_data->read_upto)
{
/* We don't seem to have enough data. */
if (private_data->historic)
{
/*
* This is a historic timeline, so there will never be any
* more data than we have currently.
*/
private_data->end_of_wal = true;
return -1;
}
else
{
XLogRecPtr latest_lsn;
TimeLineID latest_tli;
/*
* This is - or at least was up until very recently - the
* current timeline, so more data might show up. Delay here
* so we don't tight-loop.
*/
HandleWalSummarizerInterrupts();
summarizer_wait_for_wal();
/* Recheck end-of-WAL. */
latest_lsn = GetLatestLSN(&latest_tli);
if (private_data->tli == latest_tli)
{
/* Still the current timeline, update max LSN. */
Assert(latest_lsn >= private_data->read_upto);
private_data->read_upto = latest_lsn;
}
else
{
List *tles = readTimeLineHistory(latest_tli);
XLogRecPtr switchpoint;
/*
* The timeline we're scanning is no longer the latest
* one. Figure out when it ended.
*/
private_data->historic = true;
switchpoint = tliSwitchPoint(private_data->tli, tles,
NULL);
/*
* Allow reads up to exactly the switch point.
*
* It's possible that this will cause read_upto to move
* backwards, because walreceiver might have read a
* partial record and flushed it to disk, and we'd view
* that data as safe to read. However, the
* XLOG_END_OF_RECOVERY record will be written at the end
* of the last complete WAL record, not at the end of the
* WAL that we've flushed to disk.
*
* So switchpoint < private->read_upto is possible here,
* but switchpoint < state->EndRecPtr should not be.
*/
Assert(switchpoint >= state->EndRecPtr);
private_data->read_upto = switchpoint;
/* Debugging output. */
ereport(DEBUG1,
errmsg("timeline %u became historic, can read up to %X/%X",
private_data->tli, LSN_FORMAT_ARGS(private_data->read_upto)));
}
/* Go around and try again. */
}
}
else
{
/* enough bytes available to satisfy the request */
count = private_data->read_upto - targetPagePtr;
break;
}
}
if (!WALRead(state, cur_page, targetPagePtr, count,
private_data->tli, &errinfo))
WALReadRaiseError(&errinfo);
/* Track that we read a page, for sleep time calculation. */
++pages_read_since_last_sleep;
/* number of valid bytes in the buffer */
return count;
}
/*
* Sleep for long enough that we believe it's likely that more WAL will
* be available afterwards.
*/
static void
summarizer_wait_for_wal(void)
{
if (pages_read_since_last_sleep == 0)
{
/*
* No pages were read since the last sleep, so double the sleep time,
* but not beyond the maximum allowable value.
*/
sleep_quanta = Min(sleep_quanta * 2, MAX_SLEEP_QUANTA);
}
else if (pages_read_since_last_sleep > 1)
{
/*
* Multiple pages were read since the last sleep, so reduce the sleep
* time.
*
* A large burst of activity should be able to quickly reduce the
* sleep time to the minimum, but we don't want a handful of extra WAL
* records to provoke a strong reaction. We choose to reduce the sleep
* time by 1 quantum for each page read beyond the first, which is a
* fairly arbitrary way of trying to be reactive without overreacting.
*/
if (pages_read_since_last_sleep > sleep_quanta - 1)
sleep_quanta = 1;
else
sleep_quanta -= pages_read_since_last_sleep;
}
/* OK, now sleep. */
(void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
sleep_quanta * MS_PER_SLEEP_QUANTUM,
WAIT_EVENT_WAL_SUMMARIZER_WAL);
ResetLatch(MyLatch);
/* Reset count of pages read. */
pages_read_since_last_sleep = 0;
}
/*
* Remove WAL summaries whose mtimes are older than wal_summary_keep_time.
*/
static void
MaybeRemoveOldWalSummaries(void)
{
XLogRecPtr redo_pointer = GetRedoRecPtr();
List *wslist;
time_t cutoff_time;
/* If WAL summary removal is disabled, don't do anything. */
if (wal_summary_keep_time == 0)
return;
/*
* If the redo pointer has not advanced, don't do anything.
*
* This has the effect that we only try to remove old WAL summary files
* once per checkpoint cycle.
*/
if (redo_pointer == redo_pointer_at_last_summary_removal)
return;
redo_pointer_at_last_summary_removal = redo_pointer;
/*
* Files should only be removed if the last modification time precedes the
* cutoff time we compute here.
*/
cutoff_time = time(NULL) - 60 * wal_summary_keep_time;
/* Get all the summaries that currently exist. */
wslist = GetWalSummaries(0, InvalidXLogRecPtr, InvalidXLogRecPtr);
/* Loop until all summaries have been considered for removal. */
while (wslist != NIL)
{
ListCell *lc;
XLogSegNo oldest_segno;
XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
TimeLineID selected_tli;
HandleWalSummarizerInterrupts();
/*
* Pick a timeline for which some summary files still exist on disk,
* and find the oldest LSN that still exists on disk for that
* timeline.
*/
selected_tli = ((WalSummaryFile *) linitial(wslist))->tli;
oldest_segno = XLogGetOldestSegno(selected_tli);
if (oldest_segno != 0)
XLogSegNoOffsetToRecPtr(oldest_segno, 0, wal_segment_size,
oldest_lsn);
/* Consider each WAL file on the selected timeline in turn. */
foreach(lc, wslist)
{
WalSummaryFile *ws = lfirst(lc);
HandleWalSummarizerInterrupts();
/* If it's not on this timeline, it's not time to consider it. */
if (selected_tli != ws->tli)
continue;
/*
* If the WAL doesn't exist any more, we can remove it if the file
* modification time is old enough.
*/
if (XLogRecPtrIsInvalid(oldest_lsn) || ws->end_lsn <= oldest_lsn)
RemoveWalSummaryIfOlderThan(ws, cutoff_time);
/*
* Whether we removed the file or not, we need not consider it
* again.
*/
wslist = foreach_delete_current(wslist, lc);
pfree(ws);
}
}
}
|