Debian Codegraph @

src/test/recovery/t/033_replay_tsp_drops.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test replay of tablespace/database creation/drop
5
6 use strict;
7 use warnings FATAL => 'all';
8
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12 use Time::HiRes qw(usleep);
13
14 sub test_tablespace
15 {
16 my ($strategy) = @_;
17
18 my $node_primary = PostgreSQL::Test::Cluster->new("primary1_$strategy");
19 $node_primary->init(allows_streaming => 1);
20 $node_primary->start;
21 $node_primary->psql(
22 'postgres',
23 qq[
24 SET allow_in_place_tablespaces=on;
25 CREATE TABLESPACE dropme_ts1 LOCATION '';
26 CREATE TABLESPACE dropme_ts2 LOCATION '';
27 CREATE TABLESPACE source_ts LOCATION '';
28 CREATE TABLESPACE target_ts LOCATION '';
29 CREATE DATABASE template_db IS_TEMPLATE = true;
30 SELECT pg_create_physical_replication_slot('slot', true);
31 ]);
32 my $backup_name = 'my_backup';
33 $node_primary->backup($backup_name);
34
35 my $node_standby = PostgreSQL::Test::Cluster->new("standby2_$strategy");
36 $node_standby->init_from_backup($node_primary, $backup_name,
37 has_streaming => 1);
38 $node_standby->append_conf('postgresql.conf',
39 "allow_in_place_tablespaces = on");
40 $node_standby->append_conf('postgresql.conf', "primary_slot_name = slot");
41 $node_standby->start;
42
43 # Make sure the connection is made
44 $node_primary->wait_for_catchup($node_standby, 'write');
45
46 # Do immediate shutdown just after a sequence of CREATE DATABASE / DROP
47 # DATABASE / DROP TABLESPACE. This causes CREATE DATABASE WAL records
48 # to be applied to already-removed directories.
49 my $query = q[
50 CREATE DATABASE dropme_db1 WITH TABLESPACE dropme_ts1 STRATEGY=<STRATEGY>;
51 CREATE TABLE t (a int) TABLESPACE dropme_ts2;
52 CREATE DATABASE dropme_db2 WITH TABLESPACE dropme_ts2 STRATEGY=<STRATEGY>;
53 CREATE DATABASE moveme_db TABLESPACE source_ts STRATEGY=<STRATEGY>;
54 ALTER DATABASE moveme_db SET TABLESPACE target_ts;
55 CREATE DATABASE newdb TEMPLATE template_db STRATEGY=<STRATEGY>;
56 ALTER DATABASE template_db IS_TEMPLATE = false;
57 DROP DATABASE dropme_db1;
58 DROP TABLE t;
59 DROP DATABASE dropme_db2; DROP TABLESPACE dropme_ts2;
60 DROP TABLESPACE source_ts;
61 DROP DATABASE template_db;
62 ];
63 $query =~ s/<STRATEGY>/$strategy/g;
64
65 $node_primary->safe_psql('postgres', $query);
66 $node_primary->wait_for_catchup($node_standby, 'write');
67
68 # show "create missing directory" log message
69 $node_standby->safe_psql('postgres',
70 "ALTER SYSTEM SET log_min_messages TO debug1;");
71 $node_standby->stop('immediate');
72 # Should restart ignoring directory creation error.
73 is($node_standby->start(fail_ok => 1),
74 1, "standby node started for $strategy");
75 $node_standby->stop('immediate');
76 }
77
78 test_tablespace("FILE_COPY");
79 test_tablespace("WAL_LOG");
80
81 # Ensure that a missing tablespace directory during create database
82 # replay immediately causes panic if the standby has already reached
83 # consistent state (archive recovery is in progress). This is
84 # effective only for CREATE DATABASE WITH STRATEGY=FILE_COPY.
85
86 my $node_primary = PostgreSQL::Test::Cluster->new('primary2');
87 $node_primary->init(allows_streaming => 1);
88 $node_primary->start;
89
90 # Create tablespace
91 $node_primary->safe_psql(
92 'postgres', q[
93 SET allow_in_place_tablespaces=on;
94 CREATE TABLESPACE ts1 LOCATION ''
95 ]);
96 $node_primary->safe_psql('postgres',
97 "CREATE DATABASE db1 WITH TABLESPACE ts1 STRATEGY=FILE_COPY");
98
99 # Take backup
100 my $backup_name = 'my_backup';
101 $node_primary->backup($backup_name);
102 my $node_standby = PostgreSQL::Test::Cluster->new('standby3');
103 $node_standby->init_from_backup($node_primary, $backup_name,
104 has_streaming => 1);
105 $node_standby->append_conf('postgresql.conf',
106 "allow_in_place_tablespaces = on");
107 $node_standby->start;
108
109 # Make sure standby reached consistency and starts accepting connections
110 $node_standby->poll_query_until('postgres', 'SELECT 1', '1');
111
112 # Remove standby tablespace directory so it will be missing when
113 # replay resumes.
114 my $tspoid = $node_standby->safe_psql('postgres',
115 "SELECT oid FROM pg_tablespace WHERE spcname = 'ts1';");
116 my $tspdir = $node_standby->data_dir . "/pg_tblspc/$tspoid";
117 File::Path::rmtree($tspdir);
118
119 my $logstart = -s $node_standby->logfile;
120
121 # Create a database in the tablespace and a table in default tablespace
122 $node_primary->safe_psql(
123 'postgres',
124 q[
125 CREATE TABLE should_not_replay_insertion(a int);
126 CREATE DATABASE db2 WITH TABLESPACE ts1 STRATEGY=FILE_COPY;
127 INSERT INTO should_not_replay_insertion VALUES (1);
128 ]);
129
130 # Standby should fail and should not silently skip replaying the wal
131 # In this test, PANIC turns into WARNING by allow_in_place_tablespaces.
132 # Check the log messages instead of confirming standby failure.
133 my $max_attempts = $PostgreSQL::Test::Utils::timeout_default * 10;
134 while ($max_attempts-- >= 0)
135 {
136 last
137 if (
138 $node_standby->log_contains(
139 qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: pg_tblspc/!,
140 $logstart));
141 usleep(100_000);
142 }
143 ok($max_attempts > 0, "invalid directory creation is detected");
144
145 done_testing();