| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# This test case aims to verify that client-side backup compression work |
| 4 |
# properly, and it also aims to verify that pg_verifybackup can verify a base |
| 5 |
# backup that didn't start out in plain format. |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
use File::Path qw(rmtree); |
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 15 |
$primary->init(allows_streaming => 1); |
| 16 |
$primary->start; |
| 17 |
|
| 18 |
# Create file with some random data and an arbitrary size, useful to check |
| 19 |
# the solidity of the compression and decompression logic. The size of the |
| 20 |
# file is chosen to be around 640kB. This has proven to be large enough to |
| 21 |
# detect some issues related to LZ4, and low enough to not impact the runtime |
| 22 |
# of the test significantly. |
| 23 |
my $junk_data = $primary->safe_psql( |
| 24 |
'postgres', qq( |
| 25 |
SELECT string_agg(encode(sha256(i::bytea), 'hex'), '') |
| 26 |
FROM generate_series(1, 10240) s(i);)); |
| 27 |
my $data_dir = $primary->data_dir; |
| 28 |
my $junk_file = "$data_dir/junk"; |
| 29 |
open my $jf, '>', $junk_file |
| 30 |
or die "Could not create junk file: $!"; |
| 31 |
print $jf $junk_data; |
| 32 |
close $jf; |
| 33 |
|
| 34 |
my $backup_path = $primary->backup_dir . '/client-backup'; |
| 35 |
my $extract_path = $primary->backup_dir . '/extracted-backup'; |
| 36 |
|
| 37 |
my @test_configuration = ( |
| 38 |
{ |
| 39 |
'compression_method' => 'none', |
| 40 |
'backup_flags' => [], |
| 41 |
'backup_archive' => 'base.tar', |
| 42 |
'enabled' => 1 |
| 43 |
}, |
| 44 |
{ |
| 45 |
'compression_method' => 'gzip', |
| 46 |
'backup_flags' => [ '--compress', 'client-gzip:5' ], |
| 47 |
'backup_archive' => 'base.tar.gz', |
| 48 |
'enabled' => check_pg_config("#define HAVE_LIBZ 1") |
| 49 |
}, |
| 50 |
{ |
| 51 |
'compression_method' => 'lz4', |
| 52 |
'backup_flags' => [ '--compress', 'client-lz4:5' ], |
| 53 |
'backup_archive' => 'base.tar.lz4', |
| 54 |
'enabled' => check_pg_config("#define USE_LZ4 1") |
| 55 |
}, |
| 56 |
{ |
| 57 |
'compression_method' => 'lz4', |
| 58 |
'backup_flags' => [ '--compress', 'client-lz4:1' ], |
| 59 |
'backup_archive' => 'base.tar.lz4', |
| 60 |
'enabled' => check_pg_config("#define USE_LZ4 1") |
| 61 |
}, |
| 62 |
{ |
| 63 |
'compression_method' => 'zstd', |
| 64 |
'backup_flags' => [ '--compress', 'client-zstd:5' ], |
| 65 |
'backup_archive' => 'base.tar.zst', |
| 66 |
'enabled' => check_pg_config("#define USE_ZSTD 1") |
| 67 |
}, |
| 68 |
{ |
| 69 |
'compression_method' => 'zstd', |
| 70 |
'backup_flags' => [ '--compress', 'client-zstd:level=1,long' ], |
| 71 |
'backup_archive' => 'base.tar.zst', |
| 72 |
'enabled' => check_pg_config("#define USE_ZSTD 1") |
| 73 |
}, |
| 74 |
{ |
| 75 |
'compression_method' => 'parallel zstd', |
| 76 |
'backup_flags' => [ '--compress', 'client-zstd:workers=3' ], |
| 77 |
'backup_archive' => 'base.tar.zst', |
| 78 |
'enabled' => check_pg_config("#define USE_ZSTD 1"), |
| 79 |
'possibly_unsupported' => |
| 80 |
qr/could not set compression worker count to 3: Unsupported parameter/ |
| 81 |
}); |
| 82 |
|
| 83 |
for my $tc (@test_configuration) |
| 84 |
{ |
| 85 |
my $method = $tc->{'compression_method'}; |
| 86 |
|
| 87 |
SKIP: |
| 88 |
{ |
| 89 |
skip "$method compression not supported by this build", 3 |
| 90 |
if !$tc->{'enabled'}; |
| 91 |
skip "no decompressor available for $method", 3 |
| 92 |
if exists $tc->{'decompress_program'} |
| 93 |
&& (!defined $tc->{'decompress_program'} |
| 94 |
|| $tc->{'decompress_program'} eq ''); |
| 95 |
|
| 96 |
# Take a client-side backup. |
| 97 |
my $backup_stdout = ''; |
| 98 |
my $backup_stderr = ''; |
| 99 |
my $backup_result = $primary->run_log( |
| 100 |
[ |
| 101 |
'pg_basebackup', '--no-sync', |
| 102 |
'--pgdata' => $backup_path, |
| 103 |
'--wal-method' => 'fetch', |
| 104 |
'--checkpoint' => 'fast', |
| 105 |
'--format' => 'tar', |
| 106 |
@{ $tc->{'backup_flags'} } |
| 107 |
], |
| 108 |
'>' => \$backup_stdout, |
| 109 |
'2>' => \$backup_stderr); |
| 110 |
if ($backup_stdout ne '') |
| 111 |
{ |
| 112 |
print "# standard output was:\n$backup_stdout"; |
| 113 |
} |
| 114 |
if ($backup_stderr ne '') |
| 115 |
{ |
| 116 |
print "# standard error was:\n$backup_stderr"; |
| 117 |
} |
| 118 |
if ( !$backup_result |
| 119 |
&& $tc->{'possibly_unsupported'} |
| 120 |
&& $backup_stderr =~ /$tc->{'possibly_unsupported'}/) |
| 121 |
{ |
| 122 |
skip "compression with $method not supported by this build", 3; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
ok($backup_result, "client side backup, compression $method"); |
| 127 |
} |
| 128 |
|
| 129 |
# Verify that the we got the files we expected. |
| 130 |
my $backup_files = join(',', |
| 131 |
sort grep { $_ ne '.' && $_ ne '..' } slurp_dir($backup_path)); |
| 132 |
my $expected_backup_files = |
| 133 |
join(',', sort ('backup_manifest', $tc->{'backup_archive'})); |
| 134 |
is($backup_files, $expected_backup_files, |
| 135 |
"found expected backup files, compression $method"); |
| 136 |
|
| 137 |
# Verify tar backup. |
| 138 |
$primary->command_ok( |
| 139 |
[ |
| 140 |
'pg_verifybackup', '--no-parse-wal', |
| 141 |
'--exit-on-error', $backup_path, |
| 142 |
], |
| 143 |
"verify backup, compression $method"); |
| 144 |
|
| 145 |
# Cleanup. |
| 146 |
rmtree($extract_path); |
| 147 |
rmtree($backup_path); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
done_testing(); |