Skip to content

Commit df411e7

Browse files
committed
Add tests for reinit.c
This tests how the different forks of unlogged tables behave in crash recovery. Author: David Steele <david@pgmasters.net>
1 parent 3b7ab43 commit df411e7

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Tests that unlogged tables are properly reinitialized after a crash.
2+
#
3+
# The behavior should be the same when restoring from a backup, but
4+
# that is not tested here.
5+
6+
use strict;
7+
use warnings;
8+
use PostgresNode;
9+
use TestLib;
10+
use Test::More tests => 12;
11+
12+
my $node = get_new_node('main');
13+
14+
$node->init;
15+
$node->start;
16+
my $pgdata = $node->data_dir;
17+
18+
# Create an unlogged table to test that forks other than init are not
19+
# copied.
20+
$node->safe_psql('postgres', 'CREATE UNLOGGED TABLE base_unlogged (id int)');
21+
22+
my $baseUnloggedPath = $node->safe_psql('postgres',
23+
q{select pg_relation_filepath('base_unlogged')});
24+
25+
# Test that main and init forks exist.
26+
ok(-f "$pgdata/${baseUnloggedPath}_init", 'init fork in base exists');
27+
ok(-f "$pgdata/$baseUnloggedPath", 'main fork in base exists');
28+
29+
# Create an unlogged table in a tablespace.
30+
31+
my $tablespaceDir = TestLib::tempdir;
32+
33+
$node->safe_psql('postgres',
34+
"CREATE TABLESPACE ts1 LOCATION '$tablespaceDir'");
35+
$node->safe_psql('postgres',
36+
'CREATE UNLOGGED TABLE ts1_unlogged (id int) TABLESPACE ts1');
37+
38+
my $ts1UnloggedPath = $node->safe_psql('postgres',
39+
q{select pg_relation_filepath('ts1_unlogged')});
40+
41+
# Test that main and init forks exist.
42+
ok(-f "$pgdata/${ts1UnloggedPath}_init", 'init fork in tablespace exists');
43+
ok(-f "$pgdata/$ts1UnloggedPath", 'main fork in tablespace exists');
44+
45+
# Crash the postmaster.
46+
$node->stop('immediate');
47+
48+
# Write fake forks to test that they are removed during recovery.
49+
append_to_file("$pgdata/${baseUnloggedPath}_vm", 'TEST_VM');
50+
append_to_file("$pgdata/${baseUnloggedPath}_fsm", 'TEST_FSM');
51+
52+
# Remove main fork to test that it is recopied from init.
53+
unlink("$pgdata/${baseUnloggedPath}")
54+
or BAIL_OUT("could not remove \"${baseUnloggedPath}\": $!");
55+
56+
# the same for the tablespace
57+
append_to_file("$pgdata/${ts1UnloggedPath}_vm", 'TEST_VM');
58+
append_to_file("$pgdata/${ts1UnloggedPath}_fsm", 'TEST_FSM');
59+
unlink("$pgdata/${ts1UnloggedPath}")
60+
or BAIL_OUT("could not remove \"${ts1UnloggedPath}\": $!");
61+
62+
$node->start;
63+
64+
# check unlogged table in base
65+
ok(-f "$pgdata/${baseUnloggedPath}_init",
66+
'init fork in base still exists');
67+
ok(-f "$pgdata/$baseUnloggedPath",
68+
'main fork in base recreated at startup');
69+
ok( !-f "$pgdata/${baseUnloggedPath}_vm",
70+
'vm fork in base removed at startup');
71+
ok( !-f "$pgdata/${baseUnloggedPath}_fsm",
72+
'fsm fork in base removed at startup');
73+
74+
# check unlogged table in tablespace
75+
ok( -f "$pgdata/${ts1UnloggedPath}_init",
76+
'init fork still exists in tablespace');
77+
ok(-f "$pgdata/$ts1UnloggedPath",
78+
'main fork in tablespace recreated at startup');
79+
ok( !-f "$pgdata/${ts1UnloggedPath}_vm",
80+
'vm fork in tablespace removed at startup');
81+
ok( !-f "$pgdata/${ts1UnloggedPath}_fsm",
82+
'fsm fork in tablespace removed at startup');

0 commit comments

Comments
 (0)