| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Tests that unlogged tables are properly reinitialized after a crash. |
| 5 |
# |
| 6 |
# The behavior should be the same when restoring from a backup, but |
| 7 |
# that is not tested here. |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings FATAL => 'all'; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
use PostgreSQL::Test::Utils; |
| 13 |
use Test::More; |
| 14 |
|
| 15 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 16 |
|
| 17 |
$node->init; |
| 18 |
$node->start; |
| 19 |
my $pgdata = $node->data_dir; |
| 20 |
|
| 21 |
# Create an unlogged table and an unlogged sequence to test that forks |
| 22 |
# other than init are not copied. |
| 23 |
$node->safe_psql('postgres', 'CREATE UNLOGGED TABLE base_unlogged (id int)'); |
| 24 |
$node->safe_psql('postgres', 'CREATE UNLOGGED SEQUENCE seq_unlogged'); |
| 25 |
|
| 26 |
my $baseUnloggedPath = $node->safe_psql('postgres', |
| 27 |
q{select pg_relation_filepath('base_unlogged')}); |
| 28 |
my $seqUnloggedPath = $node->safe_psql('postgres', |
| 29 |
q{select pg_relation_filepath('seq_unlogged')}); |
| 30 |
|
| 31 |
# Test that main and init forks exist. |
| 32 |
ok(-f "$pgdata/${baseUnloggedPath}_init", 'table init fork exists'); |
| 33 |
ok(-f "$pgdata/$baseUnloggedPath", 'table main fork exists'); |
| 34 |
ok(-f "$pgdata/${seqUnloggedPath}_init", 'sequence init fork exists'); |
| 35 |
ok(-f "$pgdata/$seqUnloggedPath", 'sequence main fork exists'); |
| 36 |
|
| 37 |
# Test the sequence |
| 38 |
is($node->safe_psql('postgres', "SELECT nextval('seq_unlogged')"), |
| 39 |
1, 'sequence nextval'); |
| 40 |
is($node->safe_psql('postgres', "SELECT nextval('seq_unlogged')"), |
| 41 |
2, 'sequence nextval again'); |
| 42 |
|
| 43 |
# Create an unlogged table in a tablespace. |
| 44 |
|
| 45 |
my $tablespaceDir = PostgreSQL::Test::Utils::tempdir; |
| 46 |
|
| 47 |
$node->safe_psql('postgres', |
| 48 |
"CREATE TABLESPACE ts1 LOCATION '$tablespaceDir'"); |
| 49 |
$node->safe_psql('postgres', |
| 50 |
'CREATE UNLOGGED TABLE ts1_unlogged (id int) TABLESPACE ts1'); |
| 51 |
|
| 52 |
my $ts1UnloggedPath = $node->safe_psql('postgres', |
| 53 |
q{select pg_relation_filepath('ts1_unlogged')}); |
| 54 |
|
| 55 |
# Test that main and init forks exist. |
| 56 |
ok(-f "$pgdata/${ts1UnloggedPath}_init", 'init fork in tablespace exists'); |
| 57 |
ok(-f "$pgdata/$ts1UnloggedPath", 'main fork in tablespace exists'); |
| 58 |
|
| 59 |
# Create more unlogged sequences for testing. |
| 60 |
$node->safe_psql('postgres', 'CREATE UNLOGGED SEQUENCE seq_unlogged2'); |
| 61 |
# This rewrites the sequence relation in AlterSequence(). |
| 62 |
$node->safe_psql('postgres', 'ALTER SEQUENCE seq_unlogged2 INCREMENT 2'); |
| 63 |
$node->safe_psql('postgres', "SELECT nextval('seq_unlogged2')"); |
| 64 |
|
| 65 |
$node->safe_psql('postgres', |
| 66 |
'CREATE UNLOGGED TABLE tab_seq_unlogged3 (a int GENERATED ALWAYS AS IDENTITY)' |
| 67 |
); |
| 68 |
# This rewrites the sequence relation in ResetSequence(). |
| 69 |
$node->safe_psql('postgres', 'TRUNCATE tab_seq_unlogged3 RESTART IDENTITY'); |
| 70 |
$node->safe_psql('postgres', 'INSERT INTO tab_seq_unlogged3 DEFAULT VALUES'); |
| 71 |
|
| 72 |
# Crash the postmaster. |
| 73 |
$node->stop('immediate'); |
| 74 |
|
| 75 |
# Write fake forks to test that they are removed during recovery. |
| 76 |
append_to_file("$pgdata/${baseUnloggedPath}_vm", 'TEST_VM'); |
| 77 |
append_to_file("$pgdata/${baseUnloggedPath}_fsm", 'TEST_FSM'); |
| 78 |
|
| 79 |
# Remove main fork to test that it is recopied from init. |
| 80 |
unlink("$pgdata/${baseUnloggedPath}") |
| 81 |
or BAIL_OUT("could not remove \"${baseUnloggedPath}\": $!"); |
| 82 |
unlink("$pgdata/${seqUnloggedPath}") |
| 83 |
or BAIL_OUT("could not remove \"${seqUnloggedPath}\": $!"); |
| 84 |
|
| 85 |
# the same for the tablespace |
| 86 |
append_to_file("$pgdata/${ts1UnloggedPath}_vm", 'TEST_VM'); |
| 87 |
append_to_file("$pgdata/${ts1UnloggedPath}_fsm", 'TEST_FSM'); |
| 88 |
unlink("$pgdata/${ts1UnloggedPath}") |
| 89 |
or BAIL_OUT("could not remove \"${ts1UnloggedPath}\": $!"); |
| 90 |
|
| 91 |
$node->start; |
| 92 |
|
| 93 |
# check unlogged table in base |
| 94 |
ok( -f "$pgdata/${baseUnloggedPath}_init", |
| 95 |
'table init fork in base still exists'); |
| 96 |
ok(-f "$pgdata/$baseUnloggedPath", |
| 97 |
'table main fork in base recreated at startup'); |
| 98 |
ok(!-f "$pgdata/${baseUnloggedPath}_vm", |
| 99 |
'vm fork in base removed at startup'); |
| 100 |
ok( !-f "$pgdata/${baseUnloggedPath}_fsm", |
| 101 |
'fsm fork in base removed at startup'); |
| 102 |
|
| 103 |
# check unlogged sequence |
| 104 |
ok(-f "$pgdata/${seqUnloggedPath}_init", 'sequence init fork still exists'); |
| 105 |
ok(-f "$pgdata/$seqUnloggedPath", 'sequence main fork recreated at startup'); |
| 106 |
|
| 107 |
# Test the sequence after restart |
| 108 |
is($node->safe_psql('postgres', "SELECT nextval('seq_unlogged')"), |
| 109 |
1, 'sequence nextval after restart'); |
| 110 |
is($node->safe_psql('postgres', "SELECT nextval('seq_unlogged')"), |
| 111 |
2, 'sequence nextval after restart again'); |
| 112 |
|
| 113 |
# check unlogged table in tablespace |
| 114 |
ok( -f "$pgdata/${ts1UnloggedPath}_init", |
| 115 |
'init fork still exists in tablespace'); |
| 116 |
ok(-f "$pgdata/$ts1UnloggedPath", |
| 117 |
'main fork in tablespace recreated at startup'); |
| 118 |
ok( !-f "$pgdata/${ts1UnloggedPath}_vm", |
| 119 |
'vm fork in tablespace removed at startup'); |
| 120 |
ok( !-f "$pgdata/${ts1UnloggedPath}_fsm", |
| 121 |
'fsm fork in tablespace removed at startup'); |
| 122 |
|
| 123 |
# Test other sequences |
| 124 |
is($node->safe_psql('postgres', "SELECT nextval('seq_unlogged2')"), |
| 125 |
1, 'altered sequence nextval after restart'); |
| 126 |
is($node->safe_psql('postgres', "SELECT nextval('seq_unlogged2')"), |
| 127 |
3, 'altered sequence nextval after restart again'); |
| 128 |
|
| 129 |
$node->safe_psql('postgres', |
| 130 |
"INSERT INTO tab_seq_unlogged3 VALUES (DEFAULT), (DEFAULT)"); |
| 131 |
is($node->safe_psql('postgres', "SELECT * FROM tab_seq_unlogged3"), |
| 132 |
"1\n2", 'reset sequence nextval after restart'); |
| 133 |
|
| 134 |
done_testing(); |