|
| 1 | +# |
| 2 | +# Here we mostly do sanity checks and verify, that ptrack public API works |
| 3 | +# as expected. Data integrity after incremental backups taken via ptrack |
| 4 | +# is tested on the pg_probackup side. |
| 5 | +# |
| 6 | + |
| 7 | +use strict; |
| 8 | +use warnings; |
| 9 | +use PostgresNode; |
| 10 | +use TestLib; |
| 11 | +use Test::More; |
| 12 | + |
| 13 | +plan tests => 23; |
| 14 | + |
| 15 | +my $node; |
| 16 | +my $res; |
| 17 | +my $res_stdout; |
| 18 | +my $res_stderr; |
| 19 | + |
| 20 | +# Initialize node |
| 21 | +$node = get_new_node('node'); |
| 22 | +$node->init; |
| 23 | +$node->start; |
| 24 | + |
| 25 | +# Could not load ptrack module after postmaster start |
| 26 | +($res, $res_stdout, $res_stderr) = $node->psql("postgres", "CREATE EXTENSION ptrack"); |
| 27 | +is($res, 3, 'errors out without shared_preload_libraries = \'ptrack\''); |
| 28 | +like( |
| 29 | + $res_stderr, |
| 30 | + qr/ptrack module must be initialized by Postmaster/, |
| 31 | + 'errors out without shared_preload_libraries = \'ptrack\''); |
| 32 | + |
| 33 | +# Load ptrack library |
| 34 | +$node->append_conf( |
| 35 | + 'postgresql.conf', q{ |
| 36 | +wal_level = 'minimal' |
| 37 | +shared_preload_libraries = 'ptrack' |
| 38 | +log_min_messages = debug1 |
| 39 | +}); |
| 40 | +$node->restart; |
| 41 | + |
| 42 | +$node->safe_psql("postgres", "CREATE EXTENSION ptrack"); |
| 43 | + |
| 44 | +# Check some static functions |
| 45 | +$node->safe_psql("postgres", "SELECT ptrack_version()"); |
| 46 | + |
| 47 | +# Could not use ptrack if disabled |
| 48 | +($res, $res_stdout, $res_stderr) = $node->psql("postgres", "SELECT ptrack_get_pagemapset('0/0')"); |
| 49 | +is($res, 3, 'errors out if ptrack is disabled'); |
| 50 | +like( |
| 51 | + $res_stderr, |
| 52 | + qr/ptrack is disabled/, |
| 53 | + 'errors out if ptrack is disabled'); |
| 54 | +($res, $res_stdout, $res_stderr) = $node->psql("postgres", "SELECT ptrack_init_lsn()"); |
| 55 | +is($res, 0, 'only warning if ptrack is disabled'); |
| 56 | +like( |
| 57 | + $res_stdout, |
| 58 | + qr/0\/0/, |
| 59 | + 'should print init LSN 0/0 if disabled'); |
| 60 | +like( |
| 61 | + $res_stderr, |
| 62 | + qr/ptrack is disabled/, |
| 63 | + 'warning if ptrack is disabled'); |
| 64 | + |
| 65 | +# Actually enable ptrack |
| 66 | +$node->append_conf( |
| 67 | + 'postgresql.conf', q{ |
| 68 | +ptrack.map_size = 13 |
| 69 | +}); |
| 70 | +$node->stop; |
| 71 | +$res = $node->start(fail_ok => 1); |
| 72 | +is($res, 0, 'could not start with wal_level = \'minimal\''); |
| 73 | +$node->append_conf( |
| 74 | + 'postgresql.conf', q{ |
| 75 | +wal_level = 'replica' |
| 76 | +}); |
| 77 | +$node->start; |
| 78 | + |
| 79 | +# Do checkpoint (test ptrack hook) |
| 80 | +$node->safe_psql("postgres", "CHECKPOINT"); |
| 81 | + |
| 82 | +# Remember pg_current_wal_flush_lsn() value |
| 83 | +my $flush_lsn = $node->safe_psql("postgres", "SELECT pg_current_wal_flush_lsn()"); |
| 84 | + |
| 85 | +# Remember ptrack init_lsn |
| 86 | +my $init_lsn = $node->safe_psql("postgres", "SELECT ptrack_init_lsn()"); |
| 87 | +unlike( |
| 88 | + $init_lsn, |
| 89 | + qr/0\/0/, |
| 90 | + 'ptrack init LSN should not be 0/0 after CHECKPOINT'); |
| 91 | + |
| 92 | +# Ptrack map should survive crash |
| 93 | +$node->stop('immediate'); |
| 94 | +$node->start; |
| 95 | +$res_stdout = $node->safe_psql("postgres", "SELECT ptrack_init_lsn()"); |
| 96 | +is($res_stdout, $init_lsn, 'ptrack init_lsn should be the same after crash recovery'); |
| 97 | + |
| 98 | +# Do some stuff, which hits ptrack |
| 99 | +$node->safe_psql("postgres", "CREATE DATABASE ptrack_test"); |
| 100 | +$node->safe_psql("postgres", "CREATE TABLE ptrack_test AS SELECT i AS id FROM generate_series(0, 1000) i"); |
| 101 | + |
| 102 | +# Remember DB and relation oids |
| 103 | +my $db_oid = $node->safe_psql("postgres", "SELECT oid FROM pg_database WHERE datname = 'ptrack_test'"); |
| 104 | +my $rel_oid = $node->safe_psql("postgres", "SELECT relfilenode FROM pg_class WHERE relname = 'ptrack_test'"); |
| 105 | + |
| 106 | +# Data should survive clean restart |
| 107 | +$node->restart; |
| 108 | +$res_stdout = $node->safe_psql("postgres", "SELECT ptrack_get_pagemapset('$flush_lsn')"); |
| 109 | +like( |
| 110 | + $res_stdout, |
| 111 | + qr/base\/$db_oid/, |
| 112 | + 'ptrack pagemapset should contain new database oid'); |
| 113 | +like( |
| 114 | + $res_stdout, |
| 115 | + qr/$rel_oid/, |
| 116 | + 'ptrack pagemapset should contain new relation oid'); |
| 117 | + |
| 118 | +# We should be able to change ptrack map size (but loose all changes) |
| 119 | +$node->append_conf( |
| 120 | + 'postgresql.conf', q{ |
| 121 | +ptrack.map_size = 14 |
| 122 | +}); |
| 123 | +$node->restart; |
| 124 | + |
| 125 | +$node->safe_psql("postgres", "CHECKPOINT"); |
| 126 | +$res_stdout = $node->safe_psql("postgres", "SELECT ptrack_init_lsn()"); |
| 127 | +unlike( |
| 128 | + $res_stdout, |
| 129 | + qr/0\/0/, |
| 130 | + 'ptrack init LSN should not be 0/0 after CHECKPOINT'); |
| 131 | +ok($res_stdout ne $init_lsn, 'ptrack init_lsn should not be the same after map resize'); |
| 132 | +$res_stdout = $node->safe_psql("postgres", "SELECT ptrack_get_pagemapset('$flush_lsn')"); |
| 133 | +unlike( |
| 134 | + $res_stdout, |
| 135 | + qr/base\/$db_oid/, |
| 136 | + 'we should loose changes after ptrack map resize'); |
| 137 | + |
| 138 | +# We should be able to turn off ptrack and clean up all files by stting ptrack.map_size = 0 |
| 139 | +$node->append_conf( |
| 140 | + 'postgresql.conf', q{ |
| 141 | +ptrack.map_size = 0 |
| 142 | +}); |
| 143 | +$node->restart; |
| 144 | + |
| 145 | +# Check that we have lost everything |
| 146 | +ok(! -f $node->data_dir . "/global/ptrack.map", "ptrack.map should be cleaned up"); |
| 147 | +ok(! -f $node->data_dir . "/global/ptrack.map.tmp", "ptrack.map.tmp should be cleaned up"); |
| 148 | +ok(! -f $node->data_dir . "/global/ptrack.map.mmap", "ptrack.map.mmap should be cleaned up"); |
| 149 | + |
| 150 | +($res, $res_stdout, $res_stderr) = $node->psql("postgres", "SELECT ptrack_get_pagemapset('0/0')"); |
| 151 | +is($res, 3, 'errors out if ptrack is disabled'); |
| 152 | +like( |
| 153 | + $res_stderr, |
| 154 | + qr/ptrack is disabled/, |
| 155 | + 'errors out if ptrack is disabled'); |
| 156 | +($res, $res_stdout, $res_stderr) = $node->psql("postgres", "SELECT ptrack_init_lsn()"); |
| 157 | +is($res, 0, 'only warning if ptrack is disabled'); |
| 158 | +like( |
| 159 | + $res_stdout, |
| 160 | + qr/0\/0/, |
| 161 | + 'should print init LSN 0/0 if disabled'); |
| 162 | +like( |
| 163 | + $res_stderr, |
| 164 | + qr/ptrack is disabled/, |
| 165 | + 'warning if ptrack is disabled'); |
| 166 | + |
| 167 | +$node->stop; |
| 168 | + |
| 169 | +done_testing; |
0 commit comments