From 04550fb532dc4d3894a92d397f6303fddcaf75b2 Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 5 Mar 2018 12:21:53 -0500 Subject: [PATCH 1/1] Reinit.c regression tests. Add regression tests for reinit.c. These were originally written for a patch that refactored reinit.c. That refactor ended up not being needed, but it seems like a good idea to add the tests. --- src/test/recovery/t/014_unlogged_reinit.pl | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/test/recovery/t/014_unlogged_reinit.pl diff --git a/src/test/recovery/t/014_unlogged_reinit.pl b/src/test/recovery/t/014_unlogged_reinit.pl new file mode 100644 index 0000000000..290196291f --- /dev/null +++ b/src/test/recovery/t/014_unlogged_reinit.pl @@ -0,0 +1,96 @@ +# Tests that unlogged tables are properly reinitialized after a crash. +# +# The behavior should be the same when restoring from a backup but that is not +# tested here (yet). +use strict; +use warnings; +use PostgresNode; +use TestLib; +use Test::More tests => 16; + +# Initialize node without replication settings +my $node = get_new_node('main'); + +$node->init; +$node->start; +my $pgdata = $node->data_dir; + +# Create an unlogged table to test that forks other than init are not copied +$node->safe_psql('postgres', 'CREATE UNLOGGED TABLE base_unlogged (id int)'); + +my $baseUnloggedPath = $node->safe_psql('postgres', + q{select pg_relation_filepath('base_unlogged')}); + +# Make sure main and init forks exist +ok(-f "$pgdata/${baseUnloggedPath}_init", 'init fork in base'); +ok(-f "$pgdata/$baseUnloggedPath", 'main fork in base'); + +# Create unlogged tables in a tablespace +my $tablespaceDir = undef; +my $ts1UnloggedPath = undef; + +$tablespaceDir = TestLib::tempdir . "/ts1"; + +mkdir($tablespaceDir) + or die "unable to mkdir \"$tablespaceDir\""; + +$node->safe_psql('postgres', + "CREATE TABLESPACE ts1 LOCATION '$tablespaceDir'"); +$node->safe_psql('postgres', + 'CREATE UNLOGGED TABLE ts1_unlogged (id int) TABLESPACE ts1'); + + $ts1UnloggedPath = $node->safe_psql('postgres', + q{select pg_relation_filepath('ts1_unlogged')}); + +# Make sure main and init forks exist +ok(-f "$pgdata/${ts1UnloggedPath}_init", 'init fork in tablespace'); +ok(-f "$pgdata/$ts1UnloggedPath", 'main fork in tablespace'); + +# Crash the postmaster +$node->stop('immediate'); + +# Write forks to test that they are removed during recovery +$node->command_ok(['touch', "$pgdata/${baseUnloggedPath}_vm"], + 'touch vm fork in base'); +$node->command_ok(['touch', "$pgdata/${baseUnloggedPath}_fsm"], + 'touch fsm fork in base'); + +# Remove main fork to test that it is recopied from init +unlink("$pgdata/${baseUnloggedPath}") + or die "unable to remove \"${baseUnloggedPath}\""; + +# Write forks to test that they are removed by recovery +$node->command_ok(['touch', "$pgdata/${ts1UnloggedPath}_vm"], + 'touch vm fork in tablespace'); +$node->command_ok(['touch', "$pgdata/${ts1UnloggedPath}_fsm"], + 'touch fsm fork in tablespace'); + +# Remove main fork to test that it is recopied from init +unlink("$pgdata/${ts1UnloggedPath}") + or die "unable to remove \"${ts1UnloggedPath}\""; + +# Start the postmaster +$node->start; + +# Check unlogged table in base +ok(-f "$pgdata/${baseUnloggedPath}_init", 'init fork in base'); +ok(-f "$pgdata/$baseUnloggedPath", 'main fork in base'); +ok(!-f "$pgdata/${baseUnloggedPath}_vm", 'vm fork not in base'); +ok(!-f "$pgdata/${baseUnloggedPath}_fsm", 'fsm fork not in base'); + +# Drop unlogged table +$node->safe_psql('postgres', 'DROP TABLE base_unlogged'); + +# Check unlogged table in tablespace +ok(-f "$pgdata/${ts1UnloggedPath}_init", 'init fork in tablespace'); +ok(-f "$pgdata/$ts1UnloggedPath", 'main fork in tablespace'); +ok(!-f "$pgdata/${ts1UnloggedPath}_vm", 'vm fork not in tablespace'); +ok(!-f "$pgdata/${ts1UnloggedPath}_fsm", 'fsm fork not in tablespace'); + +# Drop unlogged table +$node->safe_psql('postgres', 'DROP TABLE ts1_unlogged'); + +# Drop tablespace +$node->safe_psql('postgres', 'DROP TABLESPACE ts1'); +rmdir($tablespaceDir) + or die "unable to rmdir \"$tablespaceDir\""; -- 2.14.3 (Apple Git-98)