]> granicus.if.org Git - postgresql/commitdiff
Fix handling of files that source server removes during pg_rewind is running.
authorFujii Masao <fujii@postgresql.org>
Wed, 28 Mar 2018 19:00:21 +0000 (04:00 +0900)
committerFujii Masao <fujii@postgresql.org>
Wed, 28 Mar 2018 19:02:34 +0000 (04:02 +0900)
After processing the filemap to build the list of chunks that will be
fetched from the source to rewing the target server, it is possible that
a file which was previously processed is removed from the source.  A
simple example of such an occurence is a WAL segment which gets recycled
on the target in-between.  When the filemap is processed, files not
categorized as relation files are first truncated to prepare for its
full copy of which is going to be taken from the source, divided into a
set of junks.  However, for a recycled WAL segment, this would result in
a segment which has a zero-byte size.  With such an empty file,
post-rewind recovery thinks that records are saved but they are actually
not because of the truncation which happened when processing the
filemap, resulting in data loss.

In order to fix the problem, make sure that files which are found as
removed on the source when receiving chunks of them are as well deleted
on the target server for consistency.

Back-patch to 9.5 where pg_rewind was added.

Author: Tsunakawa Takayuki
Reviewed-by: Michael Paquier
Reported-by: Tsunakawa Takayuki
Discussion: https://postgr.es/m/0A3221C70F24FB45833433255569204D1F8DAAA2%40G01JPEXMBYT05

src/bin/pg_rewind/file_ops.c
src/bin/pg_rewind/file_ops.h
src/bin/pg_rewind/libpq_fetch.c

index fceb5edd0273b2dc93991ac6ec84794aea6ef4b8..4353a0ab06f5bd0dbdf5b6f2f94e977fbdf13df9 100644 (file)
@@ -30,7 +30,6 @@
 static int     dstfd = -1;
 static char dstpath[MAXPGPATH] = "";
 
-static void remove_target_file(const char *path);
 static void create_target_dir(const char *path);
 static void remove_target_dir(const char *path);
 static void create_target_symlink(const char *path, const char *link);
@@ -135,7 +134,7 @@ remove_target(file_entry_t *entry)
                        break;
 
                case FILE_TYPE_REGULAR:
-                       remove_target_file(entry->path);
+                       remove_target_file(entry->path, false);
                        break;
 
                case FILE_TYPE_SYMLINK:
@@ -166,8 +165,12 @@ create_target(file_entry_t *entry)
        }
 }
 
-static void
-remove_target_file(const char *path)
+/*
+ * Remove a file from target data directory.  If missing_ok is true, it
+ * is fine for the target file to not exist.
+ */
+void
+remove_target_file(const char *path, bool missing_ok)
 {
        char            dstpath[MAXPGPATH];
 
@@ -176,8 +179,13 @@ remove_target_file(const char *path)
 
        snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
        if (unlink(dstpath) != 0)
+       {
+               if (errno == ENOENT && missing_ok)
+                       return;
+
                pg_fatal("could not remove file \"%s\": %s\n",
                                 dstpath, strerror(errno));
+       }
 }
 
 void
index f68c71df4abb72f3b923057b7824f3b996211479..f9437c9a1e35f030beee645a1e76b310ba2d4263 100644 (file)
@@ -15,6 +15,7 @@
 extern void open_target_file(const char *path, bool trunc);
 extern void write_target_range(char *buf, off_t begin, size_t size);
 extern void close_target_file(void);
+extern void remove_target_file(const char *path, bool missing_ok);
 extern void truncate_target_file(const char *path, off_t newsize);
 extern void create_target(file_entry_t *t);
 extern void remove_target(file_entry_t *t);
index 22b99642794d930ed055c70298b7ccbe51a63f97..8d9690577476e0685544bb62888dcda50e976aea 100644 (file)
@@ -337,15 +337,19 @@ receiveFileChunks(const char *sql)
                chunk = PQgetvalue(res, 0, 2);
 
                /*
-                * It's possible that the file was deleted on remote side after we
-                * created the file map. In this case simply ignore it, as if it was
-                * not there in the first place, and move on.
+                * If a file has been deleted on the source, remove it on the target
+                * as well.  Note that multiple unlink() calls may happen on the same
+                * file if multiple data chunks are associated with it, hence ignore
+                * unconditionally anything missing.  If this file is not a relation
+                * data file, then it has been already truncated when creating the
+                * file chunk list at the previous execution of the filemap.
                 */
                if (PQgetisnull(res, 0, 2))
                {
                        pg_log(PG_DEBUG,
                          "received null value for chunk for file \"%s\", file has been deleted\n",
                                   filename);
+                       remove_target_file(filename, true);
                        pg_free(filename);
                        PQclear(res);
                        continue;