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
|
/*-------------------------------------------------------------------------
*
* checksumhelper.c
* Background worker to walk the database and write checksums to pages
*
* When enabling data checksums on a database at initdb time, no extra process
* is required as each page is checksummed, and verified, at accesses. When
* enabling checksums on an already running cluster, which was not initialized
* with checksums, this helper worker will ensure that all pages are
* checksummed before verification of the checksums is turned on.
*
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/postmaster/checksumhelper.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/pg_database.h"
#include "commands/vacuum.h"
#include "common/relpath.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/bgworker.h"
#include "postmaster/bgwriter.h"
#include "postmaster/checksumhelper.h"
#include "storage/bufmgr.h"
#include "storage/checksum.h"
#include "storage/lmgr.h"
#include "storage/ipc.h"
#include "storage/procarray.h"
#include "storage/smgr.h"
#include "tcop/tcopprot.h"
#include "utils/lsyscache.h"
#include "utils/ps_status.h"
typedef enum
{
SUCCESSFUL = 0,
ABORTED,
FAILED
} ChecksumHelperResult;
typedef struct ChecksumHelperShmemStruct
{
pg_atomic_flag launcher_started;
ChecksumHelperResult success;
bool process_shared_catalogs;
bool abort;
/* Parameter values set on start */
int cost_delay;
int cost_limit;
} ChecksumHelperShmemStruct;
/* Shared memory segment for checksumhelper */
static ChecksumHelperShmemStruct * ChecksumHelperShmem;
/* Bookkeeping for work to do */
typedef struct ChecksumHelperDatabase
{
Oid dboid;
char *dbname;
} ChecksumHelperDatabase;
typedef struct ChecksumHelperRelation
{
Oid reloid;
char relkind;
} ChecksumHelperRelation;
/* Prototypes */
static List *BuildDatabaseList(void);
static List *BuildRelationList(bool include_shared);
static List *BuildTempTableList(void);
static ChecksumHelperResult ProcessDatabase(ChecksumHelperDatabase * db);
static void launcher_cancel_handler(SIGNAL_ARGS);
/*
* Main entry point for checksumhelper launcher process.
*/
bool
StartChecksumHelperLauncher(int cost_delay, int cost_limit)
{
BackgroundWorker bgw;
BackgroundWorkerHandle *bgw_handle;
if (ChecksumHelperShmem->abort)
{
ereport(ERROR,
(errmsg("could not start checksumhelper: has been cancelled")));
}
ChecksumHelperShmem->cost_delay = cost_delay;
ChecksumHelperShmem->cost_limit = cost_limit;
memset(&bgw, 0, sizeof(bgw));
bgw.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
bgw.bgw_start_time = BgWorkerStart_RecoveryFinished;
snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ChecksumHelperLauncherMain");
snprintf(bgw.bgw_name, BGW_MAXLEN, "checksumhelper launcher");
snprintf(bgw.bgw_type, BGW_MAXLEN, "checksumhelper launcher");
bgw.bgw_restart_time = BGW_NEVER_RESTART;
bgw.bgw_notify_pid = MyProcPid;
bgw.bgw_main_arg = (Datum) 0;
if (!pg_atomic_test_set_flag(&ChecksumHelperShmem->launcher_started))
{
/* Failed to set means somebody else started */
ereport(ERROR,
(errmsg("could not start checksumhelper: already running")));
}
if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
{
pg_atomic_clear_flag(&ChecksumHelperShmem->launcher_started);
return false;
}
return true;
}
/*
* ShutdownChecksumHelperIfRunning
* Request shutdown of the checksumhelper
*
* This does not turn off processing immediately, it signals the checksum
* process to end when done with the current block.
*/
void
ShutdownChecksumHelperIfRunning(void)
{
/* If the launcher isn't started, there is nothing to shut down */
if (pg_atomic_unlocked_test_flag(&ChecksumHelperShmem->launcher_started))
return;
/*
* We don't need an atomic variable for aborting, setting it multiple
* times will not change the handling.
*/
ChecksumHelperShmem->abort = true;
}
/*
* ProcessSingleRelationFork
* Enable checksums in a single relation/fork.
*
* Returns true if successful, and false if *aborted*. On error, an actual
* error is raised in the lower levels.
*/
static bool
ProcessSingleRelationFork(Relation reln, ForkNumber forkNum, BufferAccessStrategy strategy)
{
BlockNumber numblocks = RelationGetNumberOfBlocksInFork(reln, forkNum);
BlockNumber b;
char activity[NAMEDATALEN * 2 + 128];
for (b = 0; b < numblocks; b++)
{
Buffer buf = ReadBufferExtended(reln, forkNum, b, RBM_NORMAL, strategy);
/*
* Report to pgstat every 100 blocks (so as not to "spam")
*/
if ((b % 100) == 0)
{
snprintf(activity, sizeof(activity) - 1, "processing: %s.%s (%s block %d/%d)",
get_namespace_name(RelationGetNamespace(reln)), RelationGetRelationName(reln),
forkNames[forkNum], b, numblocks);
pgstat_report_activity(STATE_RUNNING, activity);
}
/* Need to get an exclusive lock before we can flag as dirty */
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
/*
* Mark the buffer as dirty and force a full page write. We have to
* re-write the page to WAL even if the checksum hasn't changed,
* because if there is a replica it might have a slightly different
* version of the page with an invalid checksum, caused by unlogged
* changes (e.g. hintbits) on the master happening while checksums
* were off. This can happen if there was a valid checksum on the page
* at one point in the past, so only when checksums are first on, then
* off, and then turned on again.
*/
START_CRIT_SECTION();
MarkBufferDirty(buf);
log_newpage_buffer(buf, false);
END_CRIT_SECTION();
UnlockReleaseBuffer(buf);
/*
* This is the only place where we check if we are asked to abort, the
* abortion will bubble up from here.
*/
if (ChecksumHelperShmem->abort)
return false;
vacuum_delay_point();
}
return true;
}
/*
* ProcessSingleRelationByOid
* Process a single relation based on oid.
*
* Returns true if successful, and false if *aborted*. On error, an actual error
* is raised in the lower levels.
*/
static bool
ProcessSingleRelationByOid(Oid relationId, BufferAccessStrategy strategy)
{
Relation rel;
ForkNumber fnum;
bool aborted = false;
StartTransactionCommand();
elog(DEBUG2, "Checksumhelper starting to process relation %d", relationId);
rel = try_relation_open(relationId, AccessShareLock);
if (rel == NULL)
{
/*
* Relation no longer exist. We consider this a success, since there
* are no pages in it that need checksums, and thus return true.
*/
elog(DEBUG1, "Checksumhelper skipping relation %d as it no longer exists", relationId);
CommitTransactionCommand();
pgstat_report_activity(STATE_IDLE, NULL);
return true;
}
RelationOpenSmgr(rel);
for (fnum = 0; fnum <= MAX_FORKNUM; fnum++)
{
if (smgrexists(rel->rd_smgr, fnum))
{
if (!ProcessSingleRelationFork(rel, fnum, strategy))
{
aborted = true;
break;
}
}
}
relation_close(rel, AccessShareLock);
elog(DEBUG2, "Checksumhelper done with relation %d: %s",
relationId, (aborted ? "aborted" : "finished"));
CommitTransactionCommand();
pgstat_report_activity(STATE_IDLE, NULL);
return !aborted;
}
/*
* ProcessDatabase
* Enable checksums in a single database.
*
* We do this by launching a dynamic background worker into this database, and
* waiting for it to finish. We have to do this in a separate worker, since
* each process can only be connected to one database during its lifetime.
*/
static ChecksumHelperResult
ProcessDatabase(ChecksumHelperDatabase * db)
{
BackgroundWorker bgw;
BackgroundWorkerHandle *bgw_handle;
BgwHandleStatus status;
pid_t pid;
char activity[NAMEDATALEN + 64];
ChecksumHelperShmem->success = FAILED;
memset(&bgw, 0, sizeof(bgw));
bgw.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
bgw.bgw_start_time = BgWorkerStart_RecoveryFinished;
snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ChecksumHelperWorkerMain");
snprintf(bgw.bgw_name, BGW_MAXLEN, "checksumhelper worker");
snprintf(bgw.bgw_type, BGW_MAXLEN, "checksumhelper worker");
bgw.bgw_restart_time = BGW_NEVER_RESTART;
bgw.bgw_notify_pid = MyProcPid;
bgw.bgw_main_arg = ObjectIdGetDatum(db->dboid);
if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
{
ereport(LOG,
(errmsg("failed to start worker for checksumhelper in \"%s\"",
db->dbname)));
return FAILED;
}
status = WaitForBackgroundWorkerStartup(bgw_handle, &pid);
if (status != BGWH_STARTED)
{
ereport(LOG,
(errmsg("failed to wait for worker startup for checksumhelper in \"%s\"",
db->dbname)));
return FAILED;
}
ereport(DEBUG1,
(errmsg("started background worker for checksums in \"%s\"",
db->dbname)));
snprintf(activity, sizeof(activity) - 1,
"Waiting for worker in database %s (pid %d)", db->dbname, pid);
pgstat_report_activity(STATE_RUNNING, activity);
status = WaitForBackgroundWorkerShutdown(bgw_handle);
if (status != BGWH_STOPPED)
{
ereport(LOG,
(errmsg("failed to wait for worker shutdown for checksumhelper in \"%s\"",
db->dbname)));
return FAILED;
}
if (ChecksumHelperShmem->success == ABORTED)
ereport(LOG,
(errmsg("checksumhelper was aborted during processing in \"%s\"",
db->dbname)));
ereport(DEBUG1,
(errmsg("background worker for checksums in \"%s\" completed",
db->dbname)));
pgstat_report_activity(STATE_IDLE, NULL);
return ChecksumHelperShmem->success;
}
static void
launcher_exit(int code, Datum arg)
{
ChecksumHelperShmem->abort = false;
pg_atomic_clear_flag(&ChecksumHelperShmem->launcher_started);
}
static void
launcher_cancel_handler(SIGNAL_ARGS)
{
ChecksumHelperShmem->abort = true;
}
static void
WaitForAllTransactionsToFinish(void)
{
TransactionId waitforxid;
LWLockAcquire(XidGenLock, LW_SHARED);
waitforxid = ShmemVariableCache->nextXid;
LWLockRelease(XidGenLock);
while (true)
{
TransactionId oldestxid = GetOldestActiveTransactionId();
elog(DEBUG1, "Checking old transactions");
if (TransactionIdPrecedes(oldestxid, waitforxid))
{
char activity[64];
/* Oldest running xid is older than us, so wait */
snprintf(activity, sizeof(activity), "Waiting for current transactions to finish (waiting for %d)", waitforxid);
pgstat_report_activity(STATE_RUNNING, activity);
/* Retry every 5 seconds */
ResetLatch(MyLatch);
(void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT,
5000,
WAIT_EVENT_PG_SLEEP);
}
else
{
pgstat_report_activity(STATE_IDLE, NULL);
return;
}
}
}
void
ChecksumHelperLauncherMain(Datum arg)
{
List *DatabaseList;
List *remaining = NIL;
ListCell *lc,
*lc2;
List *CurrentDatabases = NIL;
bool found_failed = false;
on_shmem_exit(launcher_exit, 0);
ereport(DEBUG1,
(errmsg("checksumhelper launcher started")));
pqsignal(SIGTERM, die);
pqsignal(SIGINT, launcher_cancel_handler);
BackgroundWorkerUnblockSignals();
init_ps_display(pgstat_get_backend_desc(B_CHECKSUMHELPER_LAUNCHER), "", "", "");
/*
* Initialize a connection to shared catalogs only.
*/
BackgroundWorkerInitializeConnection(NULL, NULL, 0);
/*
* Set up so first run processes shared catalogs, but not once in every
* db.
*/
ChecksumHelperShmem->process_shared_catalogs = true;
/*
* Wait for all existing transactions to finish. This will make sure that
* we can see all tables all databases, so we don't miss any. Anything
* created after this point is known to have checksums on all pages
* already, so we don't have to care about those.
*/
WaitForAllTransactionsToFinish();
/*
* Create a database list. We don't need to concern ourselves with
* rebuilding this list during runtime since any database created after
* this process started will be running with checksums turned on from the
* start.
*/
DatabaseList = BuildDatabaseList();
/*
* If there are no databases at all to checksum, we can exit immediately
* as there is no work to do.
*/
if (DatabaseList == NIL || list_length(DatabaseList) == 0)
return;
foreach(lc, DatabaseList)
{
ChecksumHelperDatabase *db = (ChecksumHelperDatabase *) lfirst(lc);
ChecksumHelperResult processing;
processing = ProcessDatabase(db);
if (processing == SUCCESSFUL)
{
pfree(db->dbname);
pfree(db);
if (ChecksumHelperShmem->process_shared_catalogs)
/*
* Now that one database has completed shared catalogs, we
* don't have to process them again.
*/
ChecksumHelperShmem->process_shared_catalogs = false;
}
else if (processing == FAILED)
{
/*
* Put failed databases on the remaining list.
*/
remaining = lappend(remaining, db);
}
else
/* aborted */
return;
}
list_free(DatabaseList);
/*
* remaining now has all databases not yet processed. This can be because
* they failed for some reason, or because the database was dropped
* between us getting the database list and trying to process it. Get a
* fresh list of databases to detect the second case where the database
* was dropped before we had started processing it. If a database still
* exists, but enabling checksums failed then we fail the entire
* checksumming process and exit with an error.
*/
CurrentDatabases = BuildDatabaseList();
foreach(lc, remaining)
{
ChecksumHelperDatabase *db = (ChecksumHelperDatabase *) lfirst(lc);
bool found = false;
foreach(lc2, CurrentDatabases)
{
ChecksumHelperDatabase *db2 = (ChecksumHelperDatabase *) lfirst(lc2);
if (db->dboid == db2->dboid)
{
found = true;
ereport(WARNING,
(errmsg("failed to enable checksums in \"%s\"",
db->dbname)));
break;
}
}
if (found)
found_failed = true;
else
{
ereport(LOG,
(errmsg("database \"%s\" has been dropped, skipping",
db->dbname)));
}
pfree(db->dbname);
pfree(db);
}
list_free(remaining);
/* Free the extra list of databases */
foreach(lc, CurrentDatabases)
{
ChecksumHelperDatabase *db = (ChecksumHelperDatabase *) lfirst(lc);
pfree(db->dbname);
pfree(db);
}
list_free(CurrentDatabases);
if (found_failed)
{
/* Disable checksums on cluster, because we failed */
SetDataChecksumsOff();
ereport(ERROR,
(errmsg("checksumhelper failed to enable checksums in all databases, aborting")));
}
/*
* Force a checkpoint to get everything out to disk.
*/
RequestCheckpoint(CHECKPOINT_FORCE | CHECKPOINT_WAIT | CHECKPOINT_IMMEDIATE);
/*
* Everything has been processed, so flag checksums enabled.
*/
SetDataChecksumsOn();
ereport(LOG,
(errmsg("checksums enabled, checksumhelper launcher shutting down")));
}
/*
* ChecksumHelperShmemSize
* Compute required space for checksumhelper-related shared memory
*/
Size
ChecksumHelperShmemSize(void)
{
Size size;
size = sizeof(ChecksumHelperShmemStruct);
size = MAXALIGN(size);
return size;
}
/*
* ChecksumHelperShmemInit
* Allocate and initialize checksumhelper-related shared memory
*/
void
ChecksumHelperShmemInit(void)
{
bool found;
ChecksumHelperShmem = (ChecksumHelperShmemStruct *)
ShmemInitStruct("ChecksumHelper Data",
ChecksumHelperShmemSize(),
&found);
if (!found)
{
MemSet(ChecksumHelperShmem, 0, ChecksumHelperShmemSize());
pg_atomic_init_flag(&ChecksumHelperShmem->launcher_started);
}
}
/*
* BuildDatabaseList
* Compile a list of all currently available databases in the cluster
*
* This creates the list of databases for the checksumhelper workers to add
* checksums to.
*/
static List *
BuildDatabaseList(void)
{
List *DatabaseList = NIL;
Relation rel;
HeapScanDesc scan;
HeapTuple tup;
MemoryContext ctx = CurrentMemoryContext;
MemoryContext oldctx;
StartTransactionCommand();
rel = heap_open(DatabaseRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 0, NULL);
while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
Form_pg_database pgdb = (Form_pg_database) GETSTRUCT(tup);
ChecksumHelperDatabase *db;
oldctx = MemoryContextSwitchTo(ctx);
db = (ChecksumHelperDatabase *) palloc(sizeof(ChecksumHelperDatabase));
db->dboid = HeapTupleGetOid(tup);
db->dbname = pstrdup(NameStr(pgdb->datname));
DatabaseList = lappend(DatabaseList, db);
MemoryContextSwitchTo(oldctx);
}
heap_endscan(scan);
heap_close(rel, AccessShareLock);
CommitTransactionCommand();
return DatabaseList;
}
/*
* BuildRelationList
* Compile a list of all relations in the database
*
* If shared is true, both shared relations and local ones are returned, else
* all non-shared relations are returned.
* Temp tables are not included.
*/
static List *
BuildRelationList(bool include_shared)
{
List *RelationList = NIL;
Relation rel;
HeapScanDesc scan;
HeapTuple tup;
MemoryContext ctx = CurrentMemoryContext;
MemoryContext oldctx;
StartTransactionCommand();
rel = heap_open(RelationRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 0, NULL);
while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
Form_pg_class pgc = (Form_pg_class) GETSTRUCT(tup);
ChecksumHelperRelation *relentry;
if (pgc->relpersistence == 't')
continue;
if (pgc->relisshared && !include_shared)
continue;
/*
* Foreign tables have by definition no local storage that can be
* checksummed, so skip.
*/
if (pgc->relkind == RELKIND_FOREIGN_TABLE)
continue;
oldctx = MemoryContextSwitchTo(ctx);
relentry = (ChecksumHelperRelation *) palloc(sizeof(ChecksumHelperRelation));
relentry->reloid = HeapTupleGetOid(tup);
relentry->relkind = pgc->relkind;
RelationList = lappend(RelationList, relentry);
MemoryContextSwitchTo(oldctx);
}
heap_endscan(scan);
heap_close(rel, AccessShareLock);
CommitTransactionCommand();
return RelationList;
}
/*
* BuildTempTableList
* Compile a list of all temporary tables in database
*
* Returns a List of oids.
*/
static List *
BuildTempTableList(void)
{
List *RelationList = NIL;
Relation rel;
HeapScanDesc scan;
HeapTuple tup;
MemoryContext ctx = CurrentMemoryContext;
MemoryContext oldctx;
StartTransactionCommand();
rel = heap_open(RelationRelationId, AccessShareLock);
scan = heap_beginscan_catalog(rel, 0, NULL);
while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
{
Form_pg_class pgc = (Form_pg_class) GETSTRUCT(tup);
if (pgc->relpersistence != 't')
continue;
oldctx = MemoryContextSwitchTo(ctx);
RelationList = lappend_oid(RelationList, HeapTupleGetOid(tup));
MemoryContextSwitchTo(oldctx);
}
heap_endscan(scan);
heap_close(rel, AccessShareLock);
CommitTransactionCommand();
return RelationList;
}
/*
* Main function for enabling checksums in a single database
*/
void
ChecksumHelperWorkerMain(Datum arg)
{
Oid dboid = DatumGetObjectId(arg);
List *RelationList = NIL;
List *InitialTempTableList = NIL;
ListCell *lc;
BufferAccessStrategy strategy;
bool aborted = false;
pqsignal(SIGTERM, die);
BackgroundWorkerUnblockSignals();
init_ps_display(pgstat_get_backend_desc(B_CHECKSUMHELPER_WORKER), "", "", "");
ereport(DEBUG1,
(errmsg("checksum worker starting for database oid %d", dboid)));
BackgroundWorkerInitializeConnectionByOid(dboid, InvalidOid, BGWORKER_BYPASS_ALLOWCONN);
/*
* Get a list of all temp tables present as we start in this database. We
* need to wait until they are all gone until we are done, since we cannot
* access those files and modify them.
*/
InitialTempTableList = BuildTempTableList();
/*
* Enable vacuum cost delay, if any.
*/
VacuumCostDelay = ChecksumHelperShmem->cost_delay;
VacuumCostLimit = ChecksumHelperShmem->cost_limit;
VacuumCostActive = (VacuumCostDelay > 0);
VacuumCostBalance = 0;
VacuumPageHit = 0;
VacuumPageMiss = 0;
VacuumPageDirty = 0;
/*
* Create and set the vacuum strategy as our buffer strategy.
*/
strategy = GetAccessStrategy(BAS_VACUUM);
RelationList = BuildRelationList(ChecksumHelperShmem->process_shared_catalogs);
foreach(lc, RelationList)
{
ChecksumHelperRelation *rel = (ChecksumHelperRelation *) lfirst(lc);
if (!ProcessSingleRelationByOid(rel->reloid, strategy))
{
aborted = true;
break;
}
}
list_free_deep(RelationList);
if (aborted)
{
ChecksumHelperShmem->success = ABORTED;
ereport(DEBUG1,
(errmsg("checksum worker aborted in database oid %d", dboid)));
return;
}
/*
* Wait for all temp tables that existed when we started to go away. This
* is necessary since we cannot "reach" them to enable checksums. Any temp
* tables created after we started will already have checksums in them
* (due to the inprogress state), so those are safe.
*/
while (true)
{
List *CurrentTempTables;
ListCell *lc;
int numleft;
char activity[64];
CurrentTempTables = BuildTempTableList();
numleft = 0;
foreach(lc, InitialTempTableList)
{
if (list_member_oid(CurrentTempTables, lfirst_oid(lc)))
numleft++;
}
list_free(CurrentTempTables);
if (numleft == 0)
break;
/* At least one temp table left to wait for */
snprintf(activity, sizeof(activity), "Waiting for %d temp tables to be removed", numleft);
pgstat_report_activity(STATE_RUNNING, activity);
/* Retry every 5 seconds */
ResetLatch(MyLatch);
(void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT,
5000,
WAIT_EVENT_PG_SLEEP);
}
list_free(InitialTempTableList);
ChecksumHelperShmem->success = SUCCESSFUL;
ereport(DEBUG1,
(errmsg("checksum worker completed in database oid %d", dboid)));
}
|