From fda093937a21b8116c2ff0619ad0a1954050e266 Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Fri, 4 Dec 2026 14:24:37 +0100 Subject: [PATCH v1 2/4] pg_rewind: Copy databases created in the target in full A database that was created in the target after the divergence is usually not present in the source, so its files are removed. If the source has created a different database with the same OID, however, the files exist in both systems. pg_rewind did not copy such files as a whole, relying on replay of the source's CREATE DATABASE record to copy the database again. Remember the databases created in the target while reading its WAL, and copy their files in full. This is the same technique the WAL summarizer applies to such databases for incremental backups, and it allows replay to keep a database directory that already exists. Discussion: https://postgr.es/m/CANVD+MB_JL5ydxD8NUUGJsdEGB1+nW56-PnS8QTfUtW+w_V9+A@mail.gmail.com --- src/bin/pg_rewind/filemap.c | 45 +++++++++++++++++++++++++++++++++++ src/bin/pg_rewind/filemap.h | 1 + src/bin/pg_rewind/parsexlog.c | 27 ++++++++++++--------- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c index eeb26c5f816..c77a5a3d946 100644 --- a/src/bin/pg_rewind/filemap.c +++ b/src/bin/pg_rewind/filemap.c @@ -30,8 +30,10 @@ #include "catalog/pg_tablespace_d.h" #include "common/file_utils.h" #include "common/hashfn_unstable.h" +#include "common/relpath.h" #include "common/string.h" #include "datapagemap.h" +#include "fe_utils/simple_list.h" #include "filemap.h" #include "pg_rewind.h" @@ -55,6 +57,9 @@ static filehash_hash *filehash; +/* Directories of the databases created in the target since the divergence */ +static SimpleStringList target_created_dbs = {NULL, NULL}; + static file_content_type_t getFileContentType(const char *path); static char *datasegpath(RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno); @@ -340,6 +345,36 @@ process_target_file(const char *path, file_type_t type, size_t size, entry->target_link_target = link_target ? pg_strdup(link_target) : NULL; } +/* + * This callback gets called while we read the WAL in the target, for every + * database that has been created in the target system. + */ +void +process_target_wal_database_create(Oid tablespace_id, Oid db_id) +{ + char *path = GetDatabasePath(db_id, tablespace_id); + + simple_string_list_append(&target_created_dbs, path); + pfree(path); +} + +/* + * Is the file in a database that was created in the target system? + */ +static bool +in_target_created_database(const char *path) +{ + for (SimpleStringListCell *cell = target_created_dbs.head; cell; cell = cell->next) + { + size_t len = strlen(cell->val); + + if (strncmp(path, cell->val, len) == 0 && path[len] == '/') + return true; + } + + return false; +} + /* * This callback gets called while we read the WAL in the target, for every * block that has changed in the target system. It decides if the given @@ -870,6 +905,16 @@ decide_file_action(file_entry_t *entry, XLogSegNo last_common_segno) */ return FILE_ACTION_COPY; } + else if (in_target_created_database(path)) + { + /* + * It's a data file of a database that was created in the + * target, and a database with the same OID exists in the + * source. Neither the size of the file nor the target's WAL + * tells which blocks differ, so copy it in toto. + */ + return FILE_ACTION_COPY; + } else { /* diff --git a/src/bin/pg_rewind/filemap.h b/src/bin/pg_rewind/filemap.h index e6252bb9db4..d608a357560 100644 --- a/src/bin/pg_rewind/filemap.h +++ b/src/bin/pg_rewind/filemap.h @@ -113,6 +113,7 @@ extern void process_target_file(const char *path, file_type_t type, extern void process_target_wal_block_change(ForkNumber forknum, RelFileLocator rlocator, BlockNumber blkno); +extern void process_target_wal_database_create(Oid tablespace_id, Oid db_id); extern filemap_t *decide_file_actions(XLogSegNo last_common_segno); extern void calculate_totals(filemap_t *filemap); diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c index a8a5b31dc1e..3f532b8c9b6 100644 --- a/src/bin/pg_rewind/parsexlog.c +++ b/src/bin/pg_rewind/parsexlog.c @@ -396,21 +396,26 @@ extractPageInfo(XLogReaderState *record) if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_CREATE_FILE_COPY) { /* - * New databases can be safely ignored. It won't be present in the - * source system, so it will be deleted. There's one corner-case, - * though: if a new, different, database is also created in the source - * system, we'll see that the files already exist and not copy them. - * That's OK, though; WAL replay of creating the new database, from - * the source systems's WAL, will re-copy the new database, - * overwriting the database created in the target system. + * A new database is normally not present in the source system, so it + * will be deleted. There's one corner-case, though: if a new, + * different, database with the same OID is also created in the source + * system, its files exist in both systems. Remember the database, so + * that these files are copied in toto. */ + xl_dbase_create_file_copy_rec *xlrec = + (xl_dbase_create_file_copy_rec *) XLogRecGetData(record); + + process_target_wal_database_create(xlrec->tablespace_id, + xlrec->db_id); } else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_CREATE_WAL_LOG) { - /* - * New databases can be safely ignored. It won't be present in the - * source system, so it will be deleted. - */ + /* As above. */ + xl_dbase_create_wal_log_rec *xlrec = + (xl_dbase_create_wal_log_rec *) XLogRecGetData(record); + + process_target_wal_database_create(xlrec->tablespace_id, + xlrec->db_id); } else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_DROP) { -- 2.47.3