Clarify some errors in pg_receivewal when closing WAL segments
authorMichael Paquier
Fri, 17 Sep 2021 06:53:01 +0000 (15:53 +0900)
committerMichael Paquier
Fri, 17 Sep 2021 06:53:01 +0000 (15:53 +0900)
A WAL segment closed during a WAL stream for pg_receivewal would
generate incorrect error messages depending on the context, as the file
name used when referring to a WAL segment ignored partial files or the
compression method used.  In such cases, the error message generated
(failure on close, seek or rename) would not match a physical file
name.  The same code paths are used by pg_basebackup, but it uses no
partial suffix so it is not impacted.

7fbe0c8 has introduced in walmethods.c a callback to get the exact
physical file name used for a given context, this commit makes use of it
to improve those error messages.  This could be extended to more code
paths of pg_basebackup/ in the future, if necessary.

Extracted from a larger patch by the same author.

Author: Georgios Kokolatos
Discussion: https://postgr.es/m/ZCm1J5vfyQ2E6dYvXz8si39HQ2gwxSZ3IpYaVgYa3lUwY88SLapx9EEnOf5uEwrddhx2twG7zYKjVeuP5MwZXCNPybtsGouDsAD1o2L_I5E=@pm.me

src/bin/pg_basebackup/receivelog.c

index 9601fd8d9cf9e5145df1cf54d9555734b1104b6a..72b8d9e31530de064552bf04d956e62345534837 100644 (file)
@@ -185,20 +185,27 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
 static bool
 close_walfile(StreamCtl *stream, XLogRecPtr pos)
 {
+   char       *fn;
    off_t       currpos;
    int         r;
 
    if (walfile == NULL)
        return true;
 
+   /* Note that this considers the compression used if necessary */
+   fn = stream->walmethod->get_file_name(current_walfile_name,
+                                         stream->partial_suffix);
+
    currpos = stream->walmethod->get_current_pos(walfile);
+
    if (currpos == -1)
    {
        pg_log_error("could not determine seek position in file \"%s\": %s",
-                    current_walfile_name, stream->walmethod->getlasterror());
+                    fn, stream->walmethod->getlasterror());
        stream->walmethod->close(walfile, CLOSE_UNLINK);
        walfile = NULL;
 
+       pg_free(fn);
        return false;
    }
 
@@ -208,8 +215,7 @@ close_walfile(StreamCtl *stream, XLogRecPtr pos)
            r = stream->walmethod->close(walfile, CLOSE_NORMAL);
        else
        {
-           pg_log_info("not renaming \"%s%s\", segment is not complete",
-                       current_walfile_name, stream->partial_suffix);
+           pg_log_info("not renaming \"%s\", segment is not complete", fn);
            r = stream->walmethod->close(walfile, CLOSE_NO_RENAME);
        }
    }
@@ -221,10 +227,14 @@ close_walfile(StreamCtl *stream, XLogRecPtr pos)
    if (r != 0)
    {
        pg_log_error("could not close file \"%s\": %s",
-                    current_walfile_name, stream->walmethod->getlasterror());
+                    fn, stream->walmethod->getlasterror());
+
+       pg_free(fn);
        return false;
    }
 
+   pg_free(fn);
+
    /*
     * Mark file as archived if requested by the caller - pg_basebackup needs
     * to do so as files can otherwise get archived again after promotion of a