Don't use _mdfd_getseg() in mdsyncfiletag().
authorThomas Munro <tmunro@postgresql.org>
Sat, 14 Dec 2019 02:54:31 +0000 (15:54 +1300)
committerThomas Munro <tmunro@postgresql.org>
Sat, 14 Dec 2019 03:32:03 +0000 (16:32 +1300)
_mdfd_getseg() opens all segments up to the requested one.  That
causes problems for mdsyncfiletag(), if mdunlinkfork() has
already unlinked other segment files.  Open the file we want
directly by name instead, if it's not already open.

The consequence of this bug was a rare panic in the checkpointer,
made more likely if you saturated the sync request queue so that
the SYNC_FORGET_REQUEST messages for a given relation were more
likely to be absorbed in separate cycles by the checkpointer.

Back-patch to 12.  Defect in commit 3eb77eba.

Author: Thomas Munro
Reported-by: Justin Pryzby
Discussion: https://postgr.es/m/20191119115759.GI30362%40telsasoft.com

src/backend/storage/smgr/md.c

index 8a9eaf643077f1449b9f711be593cc58f2e2777d..b110518a860d7bd05a12cc6a836e990d91807ea0 100644 (file)
@@ -1280,25 +1280,48 @@ int
 mdsyncfiletag(const FileTag *ftag, char *path)
 {
    SMgrRelation reln = smgropen(ftag->rnode, InvalidBackendId);
-   MdfdVec    *v;
-   char       *p;
+   int         fd,
+               result,
+               save_errno;
+   bool        need_to_close;
 
-   /* Provide the path for informational messages. */
-   p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
-   strlcpy(path, p, MAXPGPATH);
-   pfree(p);
+   /* See if we already have the file open, or need to open it. */
+   if (ftag->segno < reln->md_num_open_segs[ftag->forknum])
+   {
+       File        file;
+
+       file = reln->md_seg_fds[ftag->forknum][ftag->segno].mdfd_vfd;
+       strlcpy(path, FilePathName(file), MAXPGPATH);
+       fd = FileGetRawDesc(file);
+       need_to_close = false;
+   }
+   else
+   {
+       char       *p;
+
+       p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
+       strlcpy(path, p, MAXPGPATH);
+       pfree(p);
+
+       fd = OpenTransientFile(path, O_RDWR);
+       if (fd < 0)
+           return -1;
+       need_to_close = true;
+   }
+
+   /* Sync the file. */
+   pgstat_report_wait_start(WAIT_EVENT_DATA_FILE_SYNC);
+   result = pg_fsync(fd);
+   save_errno = errno;
+   pgstat_report_wait_end();
+
+   if (need_to_close && CloseTransientFile(fd) != 0)
+       ereport(WARNING,
+               (errcode_for_file_access(),
+                errmsg("could not close file \"%s\": %m", path)));
+   errno = save_errno;
 
-   /* Try to open the requested segment. */
-   v = _mdfd_getseg(reln,
-                    ftag->forknum,
-                    ftag->segno * (BlockNumber) RELSEG_SIZE,
-                    false,
-                    EXTENSION_RETURN_NULL | EXTENSION_DONT_CHECK_SIZE);
-   if (v == NULL)
-       return -1;
-
-   /* Try to fsync the file. */
-   return FileSync(v->mdfd_vfd, WAIT_EVENT_DATA_FILE_SYNC);
+   return result;
 }
 
 /*