Skip to content

Commit a7eadaa

Browse files
committed
Fix pg_rewind when rewinding new database with tables included
This fixes an issue introduced by 266b6ac, which has added filters to exclude file patterns on the target and source data directories to reduce the number of files transferred. Filters get applied to both the target and source data files, and include pg_internal.init which is present for each database once relations are created on it. However, if the target differed from the source with at least one new database with relations, the rewind would fail due to the exclusion filters applied on the target files, causing pg_internal.init to still be present on the target database folder, while its contents should have been completely removed so as there is nothing remaining inside at the time of the folder deletion. Applying exclusion filters on the source files is fine, because this way the amount of data copied from the source to the target is reduced. And actually, not applying the filters on the target is what pg_rewind should do, because this causes such files to be automatically removed during the rewind on the target. Exclusion filters apply to paths which are removed or recreated automatically at startup, so removing all those files on the target during the rewind is a win. The existing set of TAP tests already stresses the rewind of databases, but it did not include any tables on those newly-created databases. Creating extra tables in this case is enough to reproduce the failure, so the existing tests are extended to close the gap. Reported-by: Mithun Cy Author: Michael Paquier Discussion: https://postgr.es/m/CADq3xVYt6_pO7ZzmjOqPgY9HWsL=kLd-_tNyMtdfjKqEALDyTA@mail.gmail.com Backpatch-through: 11
1 parent fa33956 commit a7eadaa

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/bin/pg_rewind/filemap.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ process_source_file(const char *path, file_type_t type, size_t newsize,
147147

148148
Assert(map->array == NULL);
149149

150-
/* ignore any path matching the exclusion filters */
150+
/*
151+
* Skip any files matching the exclusion filters. This has the effect to
152+
* remove all those files on the target.
153+
*/
151154
if (check_file_excluded(path, true))
152155
return;
153156

@@ -334,12 +337,10 @@ process_target_file(const char *path, file_type_t type, size_t oldsize,
334337
file_entry_t *entry;
335338

336339
/*
337-
* Ignore any path matching the exclusion filters. This is not actually
338-
* mandatory for target files, but this does not hurt and let's be
339-
* consistent with the source processing.
340+
* Do not apply any exclusion filters here. This has advantage to remove
341+
* from the target data folder all paths which have been filtered out from
342+
* the source data folder when processing the source files.
340343
*/
341-
if (check_file_excluded(path, false))
342-
return;
343344

344345
snprintf(localpath, sizeof(localpath), "%s/%s", datadir_target, path);
345346
if (lstat(localpath, &statbuf) < 0)

src/bin/pg_rewind/t/002_databases.pl

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@ sub run_test
1515
RewindTest::setup_cluster($test_mode, ['-g']);
1616
RewindTest::start_master();
1717

18-
# Create a database in master.
18+
# Create a database in master with a table.
1919
master_psql('CREATE DATABASE inmaster');
20+
master_psql('CREATE TABLE inmaster_tab (a int)', 'inmaster');
2021

2122
RewindTest::create_standby($test_mode);
2223

23-
# Create another database, the creation is replicated to the standby
24+
# Create another database with another table, the creation is
25+
# replicated to the standby.
2426
master_psql('CREATE DATABASE beforepromotion');
27+
master_psql('CREATE TABLE beforepromotion_tab (a int)',
28+
'beforepromotion');
2529

2630
RewindTest::promote_standby();
2731

2832
# Create databases in the old master and the new promoted standby.
2933
master_psql('CREATE DATABASE master_afterpromotion');
34+
master_psql('CREATE TABLE master_promotion_tab (a int)',
35+
'master_afterpromotion');
3036
standby_psql('CREATE DATABASE standby_afterpromotion');
37+
standby_psql('CREATE TABLE standby_promotion_tab (a int)',
38+
'standby_afterpromotion');
3139

3240
# The clusters are now diverged.
3341

src/bin/pg_rewind/t/RewindTest.pm

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,20 @@ our $node_standby;
6868
sub master_psql
6969
{
7070
my $cmd = shift;
71+
my $dbname = shift || 'postgres';
7172

7273
system_or_bail 'psql', '-q', '--no-psqlrc', '-d',
73-
$node_master->connstr('postgres'), '-c', "$cmd";
74+
$node_master->connstr($dbname), '-c', "$cmd";
7475
return;
7576
}
7677

7778
sub standby_psql
7879
{
7980
my $cmd = shift;
81+
my $dbname = shift || 'postgres';
8082

8183
system_or_bail 'psql', '-q', '--no-psqlrc', '-d',
82-
$node_standby->connstr('postgres'), '-c', "$cmd";
84+
$node_standby->connstr($dbname), '-c', "$cmd";
8385
return;
8486
}
8587

0 commit comments

Comments
 (0)