pg_checksums.optimize.writes.and.always.sync.patch
application/octet-stream
Filename: pg_checksums.optimize.writes.and.always.sync.patch
Type: application/octet-stream
Part: 0
Patch
Format: unified
| File | + | − |
|---|---|---|
| doc/src/sgml/ref/pg_checksums.sgml | 2 | 1 |
| src/bin/pg_checksums/pg_checksums.c | 24 | 2 |
commit 086b33ab9858ed658d001744c0d501257c9fcd7c
Author: Greg Sabino Mullane <greg@turnstep.com>
Date: Wed Jun 2 10:14:57 2021 -0400
Skip rewriting the file if the checksums already match.
diff --git a/doc/src/sgml/ref/pg_checksums.sgml b/doc/src/sgml/ref/pg_checksums.sgml
index c84bc5c5b2..9a97a5e92f 100644
--- a/doc/src/sgml/ref/pg_checksums.sgml
+++ b/doc/src/sgml/ref/pg_checksums.sgml
@@ -47,7 +47,8 @@ PostgreSQL documentation
<para>
When verifying checksums, every file in the cluster is scanned. When
- enabling checksums, every file in the cluster is rewritten in-place.
+ enabling checksums, every file in the cluster with a changed checksum is
+ rewritten in-place.
Disabling checksums only updates the file <filename>pg_control</filename>.
</para>
</refsect1>
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 831cf42d3a..afd3e598eb 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -32,7 +32,9 @@
static int64 files = 0;
+static int64 total_files_modified = 0;
static int64 blocks = 0;
+static int64 total_blocks_modified = 0;
static int64 badblocks = 0;
static ControlFileData *ControlFile;
@@ -195,6 +197,7 @@ scan_file(const char *fn, BlockNumber segmentno)
int f;
BlockNumber blockno;
int flags;
+ int64 blocks_modified = 0;
Assert(mode == PG_MODE_ENABLE ||
mode == PG_MODE_CHECK);
@@ -256,6 +259,13 @@ scan_file(const char *fn, BlockNumber segmentno)
{
int w;
+ /* Do not rewrite if the checksum is already set to the expected value */
+ if (header->pd_checksum == csum) {
+ continue;
+ }
+
+ blocks_modified++;
+
/* Set checksum in page header */
header->pd_checksum = csum;
@@ -292,6 +302,12 @@ scan_file(const char *fn, BlockNumber segmentno)
pg_log_info("checksums enabled in file \"%s\"", fn);
}
+ if (blocks_modified)
+ {
+ total_files_modified++;
+ total_blocks_modified += blocks_modified;
+ }
+
close(f);
}
@@ -637,8 +653,8 @@ main(int argc, char *argv[])
progress_report(true);
printf(_("Checksum operation completed\n"));
- printf(_("Files scanned: %s\n"), psprintf(INT64_FORMAT, files));
- printf(_("Blocks scanned: %s\n"), psprintf(INT64_FORMAT, blocks));
+ printf(_("Files scanned: %s\n"), psprintf(INT64_FORMAT, files));
+ printf(_("Blocks scanned: %s\n"), psprintf(INT64_FORMAT, blocks));
if (mode == PG_MODE_CHECK)
{
printf(_("Bad checksums: %s\n"), psprintf(INT64_FORMAT, badblocks));
@@ -647,6 +663,12 @@ main(int argc, char *argv[])
if (badblocks > 0)
exit(1);
}
+ else if (mode == PG_MODE_ENABLE)
+ {
+ printf(_("Files modified: %s\n"), psprintf(INT64_FORMAT, total_files_modified));
+ printf(_("Blocks modified: %s\n"), psprintf(INT64_FORMAT, total_blocks_modified));
+ }
+
}
/*