diff options
author | Tomas Vondra <tomas.vondra@postgresql.org> | 2024-04-05 16:28:21 +0200 |
---|---|---|
committer | Tomas Vondra <tomas.vondra@postgresql.org> | 2024-04-05 16:30:01 +0200 |
commit | 10e3226ba13d184fc3165138c619eb7f2d52cdd2 (patch) | |
tree | f2c68c6fb6c9f3a4f9e4a868af0acd7e352d8e81 /src/backend/backup/basebackup_incremental.c | |
parent | ee1cbe806dad47674ded35427c6ba217531847d6 (diff) | |
download | postgresql-10e3226ba13d184fc3165138c619eb7f2d52cdd2.tar.gz postgresql-10e3226ba13d184fc3165138c619eb7f2d52cdd2.zip |
Align blocks in incremental backups to BLCKSZ
Align blocks stored in incremental files to BLCKSZ, so that the
incremental backups work well with CoW filesystems.
The header of the incremental file is padded with \0 to a multiple of
BLCKSZ, so that the block data (also BLCKSZ) is aligned to BLCKSZ. The
padding is added only to files containing block data, so files with just
the header remain small. This adds a bit of extra space, but as the
number of blocks increases the overhead gets negligible very quickly.
And as the padding is \0 bytes, it does compress extremely well.
The alignment is important for CoW filesystems that usually require the
blocks to be aligned to filesystem page size for features like block
sharing, deduplication etc. to work well. With the variable sized header
the blocks in the increments were not aligned at all, negating the
benefits of the CoW filesystems.
This matters even for non-CoW filesystems, for example when placed on a
RAID array. If the block is not aligned, it may easily span multiple
devices, causing read and write amplification.
It might be better to align the blocks to the filesystem page, not
BLCKSZ, but we have no good way to determine that. Even if we determine
the page size at the time of taking the backup, the backup may move. For
now the BLCKSZ seems sufficient - the filesystem page is usually 4K, so
the default BLCKSZ (8K by default) is aligned to that.
Author: Tomas Vondra
Reviewed-by: Robert Haas, Jakub Wartak
Discussion: https://postgr.es/m/3024283a-7491-4240-80d0-421575f6bb23%40enterprisedb.com
Diffstat (limited to 'src/backend/backup/basebackup_incremental.c')
-rw-r--r-- | src/backend/backup/basebackup_incremental.c | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c index 2970dfe6031..131598badef 100644 --- a/src/backend/backup/basebackup_incremental.c +++ b/src/backend/backup/basebackup_incremental.c @@ -929,6 +929,36 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path, } /* + * Compute the size for a header of an incremental file containing a given + * number of blocks. The header is rounded to a multiple of BLCKSZ, but + * only if the file will store some block data. + */ +extern size_t +GetIncrementalHeaderSize(unsigned num_blocks_required) +{ + size_t result; + + /* Make sure we're not going to overflow. */ + Assert(num_blocks_required <= RELSEG_SIZE); + + /* + * Three four byte quantities (magic number, truncation block length, + * block count) followed by block numbers. + */ + result = 3 * sizeof(uint32) + (sizeof(BlockNumber) * num_blocks_required); + + /* + * Round the header size to a multiple of BLCKSZ - when not a multiple of + * BLCKSZ, add the missing fraction of a block. But do this only if the + * file will store data for some blocks, otherwise keep it small. + */ + if ((num_blocks_required > 0) && (result % BLCKSZ != 0)) + result += BLCKSZ - (result % BLCKSZ); + + return result; +} + +/* * Compute the size for an incremental file containing a given number of blocks. */ extern size_t @@ -940,11 +970,12 @@ GetIncrementalFileSize(unsigned num_blocks_required) Assert(num_blocks_required <= RELSEG_SIZE); /* - * Three four byte quantities (magic number, truncation block length, - * block count) followed by block numbers followed by block contents. + * Header with three four byte quantities (magic number, truncation block + * length, block count) followed by block numbers, rounded to a multiple + * of BLCKSZ (for files with block data), followed by block contents. */ - result = 3 * sizeof(uint32); - result += (BLCKSZ + sizeof(BlockNumber)) * num_blocks_required; + result = GetIncrementalHeaderSize(num_blocks_required); + result += BLCKSZ * num_blocks_required; return result; } |