Debian Codegraph @

src/test/recovery/t/036_truncated_dropped.pl

raw
1 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
2
3 # Tests recovery scenarios where the files are shorter than in the common
4 # cases, e.g. due to replaying WAL records of a relation that was subsequently
5 # truncated or dropped.
6
7 use strict;
8 use warnings FATAL => 'all';
9 use PostgreSQL::Test::Cluster;
10 use Test::More;
11
12 my $node = PostgreSQL::Test::Cluster->new('n1');
13
14 $node->init();
15
16 # Disable autovacuum to guarantee VACUUM can remove rows / truncate relations
17 $node->append_conf(
18 'postgresql.conf', qq[
19 wal_level = 'replica'
20 autovacuum = off
21 ]);
22
23 $node->start();
24
25
26 # Test: Replay replay of PRUNE records for a pre-existing, then dropped,
27 # relation
28
29 $node->safe_psql(
30 'postgres', qq[
31 CREATE TABLE truncme(i int) WITH (fillfactor = 50);
32 INSERT INTO truncme SELECT generate_series(1, 1000);
33 UPDATE truncme SET i = 1;
34 CHECKPOINT; -- ensure relation exists at start of recovery
35 VACUUM truncme; -- generate prune records
36 DROP TABLE truncme;
37 ]);
38
39 $node->stop('immediate');
40
41 ok($node->start(),
42 'replay of PRUNE records for a pre-existing, then dropped, relation');
43
44
45 # Test: Replay of PRUNE records for a newly created, then dropped, relation
46
47 $node->safe_psql(
48 'postgres', qq[
49 CREATE TABLE truncme(i int) WITH (fillfactor = 50);
50 INSERT INTO truncme SELECT generate_series(1, 1000);
51 UPDATE truncme SET i = 1;
52 VACUUM truncme; -- generate prune records
53 DROP TABLE truncme;
54 ]);
55
56 $node->stop('immediate');
57
58 ok($node->start(),
59 'replay of PRUNE records for a newly created, then dropped, relation');
60
61
62 # Test: Replay of PRUNE records affecting truncated block. With FPIs used for
63 # PRUNE.
64
65 $node->safe_psql(
66 'postgres', qq[
67 CREATE TABLE truncme(i int) WITH (fillfactor = 50);
68 INSERT INTO truncme SELECT generate_series(1, 1000);
69 UPDATE truncme SET i = 1;
70 CHECKPOINT; -- generate FPIs
71 VACUUM truncme; -- generate prune records
72 TRUNCATE truncme; -- make blocks non-existing
73 INSERT INTO truncme SELECT generate_series(1, 10);
74 ]);
75
76 $node->stop('immediate');
77
78 ok($node->start(),
79 'replay of PRUNE records affecting truncated block (FPIs)');
80
81 is($node->safe_psql('postgres', 'select count(*), sum(i) FROM truncme'),
82 '10|55', 'table contents as expected after recovery');
83 $node->safe_psql('postgres', 'DROP TABLE truncme');
84
85
86 # Test replay of PRUNE records for blocks that are later truncated. Without
87 # FPIs used for PRUNE.
88
89 $node->safe_psql(
90 'postgres', qq[
91 CREATE TABLE truncme(i int) WITH (fillfactor = 50);
92 INSERT INTO truncme SELECT generate_series(1, 1000);
93 UPDATE truncme SET i = 1;
94 VACUUM truncme; -- generate prune records
95 TRUNCATE truncme; -- make blocks non-existing
96 INSERT INTO truncme SELECT generate_series(1, 10);
97 ]);
98
99 $node->stop('immediate');
100
101 ok($node->start(),
102 'replay of PRUNE records affecting truncated block (no FPIs)');
103
104 is($node->safe_psql('postgres', 'select count(*), sum(i) FROM truncme'),
105 '10|55', 'table contents as expected after recovery');
106 $node->safe_psql('postgres', 'DROP TABLE truncme');
107
108
109 # Test: Replay of partial truncation via VACUUM
110
111 $node->safe_psql(
112 'postgres', qq[
113 CREATE TABLE truncme(i int) WITH (fillfactor = 50);
114 INSERT INTO truncme SELECT generate_series(1, 1000);
115 UPDATE truncme SET i = i + 1;
116 -- ensure a mix of pre/post truncation rows
117 DELETE FROM truncme WHERE i > 500;
118
119 VACUUM truncme; -- should truncate relation
120
121 -- rows at TIDs that previously existed
122 INSERT INTO truncme SELECT generate_series(1000, 1010);
123 ]);
124
125 $node->stop('immediate');
126
127 ok($node->start(), 'replay of partial truncation via VACUUM');
128
129 is( $node->safe_psql(
130 'postgres', 'select count(*), sum(i), min(i), max(i) FROM truncme'),
131 '510|136304|2|1010',
132 'table contents as expected after recovery');
133 $node->safe_psql('postgres', 'DROP TABLE truncme');
134
135
136 done_testing();