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

Commit 4c9f50d

Browse files
committed
Optimize pg_checksums --enable where checksum is already set
This commit prevents pg_checksums to do a rewrite of a block if it has no need to, in the case where the computed checksum matches with what's already stored in the block read. This is helpful to accelerate successive runs of the tool when the previous ones got interrupted, for example. The number of blocks and files written is tracked and reported by the tool once finished. Note that the final flush of the data folder happens even if no blocks are written, as it could be possible that a previous interrupted run got stopped while doing a flush. Author: Greg Sabino Mullane Reviewed-by: Paquier Michael, Julien Rouhaud Discussion: https://postgr.es/m/CAKAnmmL+k6goxmVzQJB+0bAR0PN1sgo6GDUXJhyhUmVMze1QAw@mail.gmail.com
1 parent 178ec46 commit 4c9f50d

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

doc/src/sgml/ref/pg_checksums.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ PostgreSQL documentation
4747

4848
<para>
4949
When verifying checksums, every file in the cluster is scanned. When
50-
enabling checksums, every file in the cluster is rewritten in-place.
50+
enabling checksums, each relation file block with a changed checksum is
51+
rewritten in-place.
5152
Disabling checksums only updates the file <filename>pg_control</filename>.
5253
</para>
5354
</refsect1>

src/bin/pg_checksums/pg_checksums.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#include "storage/checksum_impl.h"
3232

3333

34-
static int64 files = 0;
35-
static int64 blocks = 0;
34+
static int64 files_scanned = 0;
35+
static int64 files_written = 0;
36+
static int64 blocks_scanned = 0;
37+
static int64 blocks_written = 0;
3638
static int64 badblocks = 0;
3739
static ControlFileData *ControlFile;
3840

@@ -195,6 +197,7 @@ scan_file(const char *fn, BlockNumber segmentno)
195197
int f;
196198
BlockNumber blockno;
197199
int flags;
200+
int64 blocks_written_in_file = 0;
198201

199202
Assert(mode == PG_MODE_ENABLE ||
200203
mode == PG_MODE_CHECK);
@@ -208,7 +211,7 @@ scan_file(const char *fn, BlockNumber segmentno)
208211
exit(1);
209212
}
210213

211-
files++;
214+
files_scanned++;
212215

213216
for (blockno = 0;; blockno++)
214217
{
@@ -227,7 +230,7 @@ scan_file(const char *fn, BlockNumber segmentno)
227230
blockno, fn, r, BLCKSZ);
228231
exit(1);
229232
}
230-
blocks++;
233+
blocks_scanned++;
231234

232235
/*
233236
* Since the file size is counted as total_size for progress status
@@ -256,6 +259,15 @@ scan_file(const char *fn, BlockNumber segmentno)
256259
{
257260
int w;
258261

262+
/*
263+
* Do not rewrite if the checksum is already set to the expected
264+
* value.
265+
*/
266+
if (header->pd_checksum == csum)
267+
continue;
268+
269+
blocks_written_in_file++;
270+
259271
/* Set checksum in page header */
260272
header->pd_checksum = csum;
261273

@@ -292,6 +304,13 @@ scan_file(const char *fn, BlockNumber segmentno)
292304
pg_log_info("checksums enabled in file \"%s\"", fn);
293305
}
294306

307+
/* Update write counters if any write activity has happened */
308+
if (blocks_written_in_file > 0)
309+
{
310+
files_written++;
311+
blocks_written += blocks_written_in_file;
312+
}
313+
295314
close(f);
296315
}
297316

@@ -637,8 +656,8 @@ main(int argc, char *argv[])
637656
progress_report(true);
638657

639658
printf(_("Checksum operation completed\n"));
640-
printf(_("Files scanned: %s\n"), psprintf(INT64_FORMAT, files));
641-
printf(_("Blocks scanned: %s\n"), psprintf(INT64_FORMAT, blocks));
659+
printf(_("Files scanned: %s\n"), psprintf(INT64_FORMAT, files_scanned));
660+
printf(_("Blocks scanned: %s\n"), psprintf(INT64_FORMAT, blocks_scanned));
642661
if (mode == PG_MODE_CHECK)
643662
{
644663
printf(_("Bad checksums: %s\n"), psprintf(INT64_FORMAT, badblocks));
@@ -647,6 +666,11 @@ main(int argc, char *argv[])
647666
if (badblocks > 0)
648667
exit(1);
649668
}
669+
else if (mode == PG_MODE_ENABLE)
670+
{
671+
printf(_("Files written: %s\n"), psprintf(INT64_FORMAT, files_written));
672+
printf(_("Blocks written: %s\n"), psprintf(INT64_FORMAT, blocks_written));
673+
}
650674
}
651675

652676
/*

0 commit comments

Comments
 (0)