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..ac2e251158 --- /dev/null +++ b/src/test/recovery/t/014_unlogged_reinit.pl @@ -0,0 +1,117 @@ +# 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'); + +# The following tests test symlinks. Windows doesn't have symlinks, so +# skip on Windows. +my $tablespaceDir = undef; +my $ts1UnloggedPath = undef; + +SKIP: +{ + skip "symlinks not supported on Windows", 2 if ($windows_os); + + # Create unlogged tables in a tablespace + $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}\""; + +# The following tests test symlinks. Windows doesn't have symlinks, so +# skip on Windows. +SKIP: +{ + skip "symlinks not supported on Windows", 2 if ($windows_os); + + # 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'); + +# The following tests test symlinks. Windows doesn't have symlinks, so +# skip on Windows. +SKIP: +{ + skip "symlinks not supported on Windows", 4 if ($windows_os); + + # 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\""; +}