diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index 2981b41..3774fb0 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -1159,6 +1159,7 @@ mdsync(void) while ((segno = bms_first_member(requests)) >= 0) { int failures; + bool opened_directly = false; /* * If fsync is off then we don't have to bother opening the @@ -1223,6 +1224,23 @@ mdsync(void) INSTR_TIME_SET_CURRENT(sync_start); + /* + * _mdfd_getseg() will only open segments which aren't + * preceded by non-truncated segments (c.f. notes about + * RELSEG_SIZE at the top of this file). But there are + * some cases, e.g. mdtruncate, where truncated segments + * are to be fsynced: Thus open those explicitly here. We + * avoid always using _mdfd_openseg() because using + * _mdfd_getseg() avoids some open()/close() calls if the + * underlying files are already open. + */ + if (seg == NULL) + { + seg = _mdfd_openseg(reln, forknum, segno, + EXTENSION_RETURN_NULL); + opened_directly = true; + } + if (seg != NULL && FileSync(seg->mdfd_vfd) >= 0) { @@ -1241,6 +1259,13 @@ mdsync(void) FilePathName(seg->mdfd_vfd), (double) elapsed / 1000); + /* free resources if explicitly opened above */ + if (opened_directly) + { + if (seg->mdfd_vfd >= 0) + FileClose(seg->mdfd_vfd); + pfree(seg); + } break; /* out of retry loop */ }