Debian Codegraph @

src/bin/pg_combinebackup/t/010_hardlink.pl

raw
1 # Copyright (c) 2025, PostgreSQL Global Development Group
2 #
3 # This test aims to validate that hard links are created as expected in the
4 # output directory, when running pg_combinebackup with --link mode.
5
6 use strict;
7 use warnings FATAL => 'all';
8 use PostgreSQL::Test::Cluster;
9 use PostgreSQL::Test::Utils;
10 use Test::More;
11
12 # Set up a new database instance.
13 my $primary = PostgreSQL::Test::Cluster->new('primary');
14 $primary->init(has_archiving => 1, allows_streaming => 1);
15 $primary->append_conf('postgresql.conf', 'summarize_wal = on');
16 # We disable autovacuum to prevent "something else" to modify our test tables.
17 $primary->append_conf('postgresql.conf', 'autovacuum = off');
18 $primary->start;
19
20 # Create a couple of tables (~264KB each).
21 # Note: Cirrus CI runs some tests with a very small segment size, so, in that
22 # environment, a single table of 264KB would have both a segment with a link
23 # count of 1 and also one with a link count of 2. But in a normal installation,
24 # segment size is 1GB. Therefore, we use 2 different tables here: for test_1,
25 # all segments (or the only one) will have two hard links; for test_2, the
26 # last segment (or the only one) will have 1 hard link, and any others will
27 # have 2.
28 my $query = <<'EOM';
29 CREATE TABLE test_%s AS
30 SELECT x.id::bigint,
31 repeat('a', 1600) AS value
32 FROM generate_series(1, 100) AS x(id);
33 EOM
34
35 $primary->safe_psql('postgres', sprintf($query, '1'));
36 $primary->safe_psql('postgres', sprintf($query, '2'));
37
38 # Fetch information about the data files.
39 $query = <<'EOM';
40 SELECT pg_relation_filepath(oid)
41 FROM pg_class
42 WHERE relname = 'test_%s';
43 EOM
44
45 my $test_1_path = $primary->safe_psql('postgres', sprintf($query, '1'));
46 note "test_1 path is $test_1_path";
47
48 my $test_2_path = $primary->safe_psql('postgres', sprintf($query, '2'));
49 note "test_2 path is $test_2_path";
50
51 # Take a full backup.
52 my $backup1path = $primary->backup_dir . '/backup1';
53 $primary->command_ok(
54 [
55 'pg_basebackup',
56 '--pgdata' => $backup1path,
57 '--no-sync',
58 '--checkpoint' => 'fast',
59 '--wal-method' => 'none'
60 ],
61 "full backup");
62
63 # Perform an insert that touches a page of the last segment of the data file of
64 # table test_2.
65 $primary->safe_psql('postgres', <<EOM);
66 INSERT INTO test_2 (id, value) VALUES (101, repeat('a', 1600));
67 EOM
68
69 # Take an incremental backup.
70 my $backup2path = $primary->backup_dir . '/backup2';
71 $primary->command_ok(
72 [
73 'pg_basebackup',
74 '--pgdata' => $backup2path,
75 '--no-sync',
76 '--checkpoint' => 'fast',
77 '--wal-method' => 'none',
78 '--incremental' => $backup1path . '/backup_manifest'
79 ],
80 "incremental backup");
81
82 # Restore the incremental backup and use it to create a new node.
83 my $restore = PostgreSQL::Test::Cluster->new('restore');
84 $restore->init_from_backup(
85 $primary, 'backup2',
86 combine_with_prior => ['backup1'],
87 combine_mode => '--link');
88
89 # Ensure files have the expected count of hard links. We expect all data files
90 # from test_1 to contain 2 hard links, because they were not touched between the
91 # full and incremental backups, and the last data file of table test_2 to
92 # contain a single hard link because of changes in its last page.
93 my $test_1_full_path = join('/', $restore->data_dir, $test_1_path);
94 check_data_file($test_1_full_path, 2);
95
96 my $test_2_full_path = join('/', $restore->data_dir, $test_2_path);
97 check_data_file($test_2_full_path, 1);
98
99 # OK, that's all.
100 done_testing();
101
102
103 # Given the path to the first segment of a data file, inspect its parent
104 # directory to find all the segments of that data file, and make sure all the
105 # segments contain 2 hard links. The last one must have the given number of hard
106 # links.
107 #
108 # Parameters:
109 # * data_file: path to the first segment of a data file, as per the output of
110 # pg_relation_filepath.
111 # * last_segment_nlinks: the number of hard links expected in the last segment
112 # of the given data file.
113 sub check_data_file
114 {
115 my ($data_file, $last_segment_nlinks) = @_;
116
117 my @data_file_segments = ($data_file);
118
119 # Start checking for additional segments
120 my $segment_number = 1;
121
122 while (1)
123 {
124 my $next_segment = $data_file . '.' . $segment_number;
125
126 # If the file exists and is a regular file, add it to the list
127 if (-f $next_segment)
128 {
129 push @data_file_segments, $next_segment;
130 $segment_number++;
131 }
132 # Stop the loop if the file doesn't exist
133 else
134 {
135 last;
136 }
137 }
138
139 # All segments of the given data file should contain 2 hard links, except
140 # for the last one, which should match the given number of links.
141 my $last_segment = pop @data_file_segments;
142
143 for my $segment (@data_file_segments)
144 {
145 # Get the file's stat information of each segment
146 my $nlink_count = get_hard_link_count($segment);
147 ok($nlink_count == 2, "File '$segment' has 2 hard links");
148 }
149
150 # Get the file's stat information of the last segment
151 my $nlink_count = get_hard_link_count($last_segment);
152 ok($nlink_count == $last_segment_nlinks,
153 "File '$last_segment' has $last_segment_nlinks hard link(s)");
154 }
155
156
157 # Subroutine to get hard link count of a given file.
158 # Receives the path to a file, and returns the number of hard links of
159 # that file.
160 sub get_hard_link_count
161 {
162 my ($file) = @_;
163
164 # Get file stats
165 my @stats = stat($file);
166 my $nlink = $stats[3]; # Number of hard links
167
168 return $nlink;
169 }