|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use PostgreSQL::Test::Cluster; |
| 4 | +use PostgreSQL::Test::Utils; |
| 5 | +use Test::More; |
| 6 | +use File::Basename; |
| 7 | + |
| 8 | + |
| 9 | +my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 10 | +$node_primary->init(allows_streaming => 1); |
| 11 | +$node_primary->append_conf('postgresql.conf', q[ |
| 12 | +allow_in_place_tablespaces = true |
| 13 | +log_connections=on |
| 14 | +# to avoid "repairing" corruption |
| 15 | +full_page_writes=off |
| 16 | +log_min_messages=debug2 |
| 17 | +autovacuum_naptime=1s |
| 18 | +shared_buffers=1MB |
| 19 | +]); |
| 20 | +$node_primary->start; |
| 21 | + |
| 22 | + |
| 23 | +# Create streaming standby linking to primary |
| 24 | +my $backup_name = 'my_backup'; |
| 25 | +$node_primary->backup($backup_name); |
| 26 | +my $node_standby = PostgreSQL::Test::Cluster->new('standby'); |
| 27 | +$node_standby->init_from_backup($node_primary, $backup_name, |
| 28 | + has_streaming => 1); |
| 29 | +$node_standby->start; |
| 30 | + |
| 31 | +# To avoid hanging while expecting some specific input from a psql |
| 32 | +# instance being driven by us, add a timeout high enough that it |
| 33 | +# should never trigger even on very slow machines, unless something |
| 34 | +# is really wrong. |
| 35 | +my $psql_timeout = IPC::Run::timer(300); |
| 36 | + |
| 37 | +my %psql_primary = (stdin => '', stdout => '', stderr => ''); |
| 38 | +$psql_primary{run} = IPC::Run::start( |
| 39 | + [ 'psql', '-XA', '-f', '-', '-d', $node_primary->connstr('postgres') ], |
| 40 | + '<', |
| 41 | + \$psql_primary{stdin}, |
| 42 | + '>', |
| 43 | + \$psql_primary{stdout}, |
| 44 | + '2>', |
| 45 | + \$psql_primary{stderr}, |
| 46 | + $psql_timeout); |
| 47 | + |
| 48 | +my %psql_standby = ('stdin' => '', 'stdout' => '', 'stderr' => ''); |
| 49 | +$psql_standby{run} = IPC::Run::start( |
| 50 | + [ 'psql', '-XA', '-f', '-', '-d', $node_standby->connstr('postgres') ], |
| 51 | + '<', |
| 52 | + \$psql_standby{stdin}, |
| 53 | + '>', |
| 54 | + \$psql_standby{stdout}, |
| 55 | + '2>', |
| 56 | + \$psql_standby{stderr}, |
| 57 | + $psql_timeout); |
| 58 | + |
| 59 | + |
| 60 | +# Create template database with a table that we'll update, to trigger dirty |
| 61 | +# rows. Using a template database + preexisting rows makes it a bit easier to |
| 62 | +# reproduce, because there's no cache invalidations generated. |
| 63 | + |
| 64 | +$node_primary->safe_psql('postgres', "CREATE DATABASE conflict_db_template OID = 50000;"); |
| 65 | +$node_primary->safe_psql('conflict_db_template', q[ |
| 66 | + CREATE TABLE large(id serial primary key, dataa text, datab text); |
| 67 | + INSERT INTO large(dataa, datab) SELECT g.i::text, 1 FROM generate_series(1, 4000) g(i);]); |
| 68 | +$node_primary->safe_psql('postgres', "CREATE DATABASE conflict_db TEMPLATE conflict_db_template OID = 50001;"); |
| 69 | + |
| 70 | +$node_primary->safe_psql('postgres', q[ |
| 71 | + CREATE EXTENSION pg_prewarm; |
| 72 | + CREATE TABLE replace_sb(data text); |
| 73 | + INSERT INTO replace_sb(data) SELECT random()::text FROM generate_series(1, 15000);]); |
| 74 | + |
| 75 | +# Use longrunning transactions, so that AtEOXact_SMgr doesn't close files |
| 76 | +send_query_and_wait( |
| 77 | + \%psql_primary, |
| 78 | + q[BEGIN;], |
| 79 | + qr/BEGIN/m); |
| 80 | +send_query_and_wait( |
| 81 | + \%psql_standby, |
| 82 | + q[BEGIN;], |
| 83 | + qr/BEGIN/m); |
| 84 | + |
| 85 | +# Cause lots of dirty rows in shared_buffers |
| 86 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 1;"); |
| 87 | + |
| 88 | +# Now do a bunch of work in another database. That will end up needing to |
| 89 | +# write back dirty data from the previous step, opening the relevant file |
| 90 | +# descriptors |
| 91 | +cause_eviction(\%psql_primary, \%psql_standby); |
| 92 | + |
| 93 | +# drop and recreate database |
| 94 | +$node_primary->safe_psql('postgres', "DROP DATABASE conflict_db;"); |
| 95 | +$node_primary->safe_psql('postgres', "CREATE DATABASE conflict_db TEMPLATE conflict_db_template OID = 50001;"); |
| 96 | + |
| 97 | +verify($node_primary, $node_standby, 1, |
| 98 | + "initial contents as expected"); |
| 99 | + |
| 100 | +# Again cause lots of dirty rows in shared_buffers, but use a different update |
| 101 | +# value so we can check everything is OK |
| 102 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 2;"); |
| 103 | + |
| 104 | +# Again cause a lot of IO. That'll again write back dirty data, but uses (XXX |
| 105 | +# adjust after bugfix) the already opened file descriptor. |
| 106 | +# FIXME |
| 107 | +cause_eviction(\%psql_primary, \%psql_standby); |
| 108 | + |
| 109 | +verify($node_primary, $node_standby, 2, |
| 110 | + "update to reused relfilenode (due to DB oid conflict) is not lost"); |
| 111 | + |
| 112 | + |
| 113 | +$node_primary->safe_psql('conflict_db', "VACUUM FULL large;"); |
| 114 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 3;"); |
| 115 | + |
| 116 | +verify($node_primary, $node_standby, 3, |
| 117 | + "restored contents as expected"); |
| 118 | + |
| 119 | +# Test for old filehandles after moving a database in / out of tablespace |
| 120 | +$node_primary->safe_psql('postgres', q[CREATE TABLESPACE test_tablespace LOCATION '']); |
| 121 | + |
| 122 | +# cause dirty buffers |
| 123 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 4;"); |
| 124 | +# cause files to be opened in backend in other database |
| 125 | +cause_eviction(\%psql_primary, \%psql_standby); |
| 126 | + |
| 127 | +# move database back / forth |
| 128 | +$node_primary->safe_psql('postgres', 'ALTER DATABASE conflict_db SET TABLESPACE test_tablespace'); |
| 129 | +$node_primary->safe_psql('postgres', 'ALTER DATABASE conflict_db SET TABLESPACE pg_default'); |
| 130 | + |
| 131 | +# cause dirty buffers |
| 132 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 5;"); |
| 133 | +cause_eviction(\%psql_primary, \%psql_standby); |
| 134 | + |
| 135 | +verify($node_primary, $node_standby, 5, |
| 136 | + "post move contents as expected"); |
| 137 | + |
| 138 | +$node_primary->safe_psql('postgres', 'ALTER DATABASE conflict_db SET TABLESPACE test_tablespace'); |
| 139 | + |
| 140 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 7;"); |
| 141 | +cause_eviction(\%psql_primary, \%psql_standby); |
| 142 | +$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 8;"); |
| 143 | +$node_primary->safe_psql('postgres', 'DROP DATABASE conflict_db'); |
| 144 | +$node_primary->safe_psql('postgres', 'DROP TABLESPACE test_tablespace'); |
| 145 | + |
| 146 | +$node_primary->safe_psql('postgres', 'REINDEX TABLE pg_database'); |
| 147 | + |
| 148 | + |
| 149 | +# explicitly shut down psql instances gracefully - to avoid hangs |
| 150 | +# or worse on windows |
| 151 | +$psql_primary{stdin} .= "\\q\n"; |
| 152 | +$psql_primary{run}->finish; |
| 153 | +$psql_standby{stdin} .= "\\q\n"; |
| 154 | +$psql_standby{run}->finish; |
| 155 | + |
| 156 | +$node_primary->stop(); |
| 157 | +$node_standby->stop(); |
| 158 | + |
| 159 | +# Make sure that there weren't crashes during shutdown |
| 160 | + |
| 161 | +command_like([ 'pg_controldata', $node_primary->data_dir ], |
| 162 | + qr/Database cluster state:\s+shut down\n/, 'primary shut down ok'); |
| 163 | +command_like([ 'pg_controldata', $node_standby->data_dir ], |
| 164 | + qr/Database cluster state:\s+shut down in recovery\n/, 'standby shut down ok'); |
| 165 | +done_testing(); |
| 166 | + |
| 167 | +sub verify |
| 168 | +{ |
| 169 | + my ($primary, $standby, $counter, $message) = @_; |
| 170 | + |
| 171 | + my $query = "SELECT datab, count(*) FROM large GROUP BY 1 ORDER BY 1 LIMIT 10"; |
| 172 | + is($primary->safe_psql('conflict_db', $query), |
| 173 | + "$counter|4000", |
| 174 | + "primary: $message"); |
| 175 | + |
| 176 | + $primary->wait_for_catchup($standby); |
| 177 | + is($standby->safe_psql('conflict_db', $query), |
| 178 | + "$counter|4000", |
| 179 | + "standby: $message"); |
| 180 | +} |
| 181 | + |
| 182 | +sub cause_eviction |
| 183 | +{ |
| 184 | + my ($psql_primary, $psql_standby) = @_; |
| 185 | + |
| 186 | + send_query_and_wait( |
| 187 | + $psql_primary, |
| 188 | + q[SELECT SUM(pg_prewarm(oid)) warmed_buffers FROM pg_class WHERE pg_relation_filenode(oid) != 0;], |
| 189 | + qr/warmed_buffers/m); |
| 190 | + |
| 191 | + send_query_and_wait( |
| 192 | + $psql_standby, |
| 193 | + q[SELECT SUM(pg_prewarm(oid)) warmed_buffers FROM pg_class WHERE pg_relation_filenode(oid) != 0;], |
| 194 | + qr/warmed_buffers/m); |
| 195 | +} |
| 196 | + |
| 197 | +# Send query, wait until string matches |
| 198 | +sub send_query_and_wait |
| 199 | +{ |
| 200 | + my ($psql, $query, $untl) = @_; |
| 201 | + my $ret; |
| 202 | + |
| 203 | + # send query |
| 204 | + $$psql{stdin} .= $query; |
| 205 | + $$psql{stdin} .= "\n"; |
| 206 | + |
| 207 | + # wait for query results |
| 208 | + $$psql{run}->pump_nb(); |
| 209 | + while (1) |
| 210 | + { |
| 211 | + last if $$psql{stdout} =~ /$untl/; |
| 212 | + |
| 213 | + if ($psql_timeout->is_expired) |
| 214 | + { |
| 215 | + BAIL_OUT("aborting wait: program timed out\n" |
| 216 | + . "stream contents: >>$$psql{stdout}<<\n" |
| 217 | + . "pattern searched for: $untl\n"); |
| 218 | + return 0; |
| 219 | + } |
| 220 | + if (not $$psql{run}->pumpable()) |
| 221 | + { |
| 222 | + BAIL_OUT("aborting wait: program died\n" |
| 223 | + . "stream contents: >>$$psql{stdout}<<\n" |
| 224 | + . "pattern searched for: $untl\n"); |
| 225 | + return 0; |
| 226 | + } |
| 227 | + $$psql{run}->pump(); |
| 228 | + } |
| 229 | + |
| 230 | + $$psql{stdout} = ''; |
| 231 | + |
| 232 | + return 1; |
| 233 | +} |
0 commit comments