92 #define INIT_MD_FILETAG(a,xx_rlocator,xx_forknum,xx_segno) \
94 memset(&(a), 0, sizeof(FileTag)), \
95 (a).handler = SYNC_HANDLER_MD, \
96 (a).rlocator = (xx_rlocator), \
97 (a).forknum = (xx_forknum), \
98 (a).segno = (xx_segno) \
104 #define EXTENSION_FAIL (1 << 0)
106 #define EXTENSION_RETURN_NULL (1 << 1)
108 #define EXTENSION_CREATE (1 << 2)
110 #define EXTENSION_CREATE_RECOVERY (1 << 3)
118 #define EXTENSION_DONT_CHECK_SIZE (1 << 4)
120 #define EXTENSION_DONT_OPEN (1 << 5)
222 int save_errno = errno;
232 errmsg(
"could not create file \"%s\": %m", path)));
314 for (forknum = 0; forknum <=
MAX_FORKNUM; forknum++)
333 if (ret < 0 && errno != ENOENT)
338 errmsg(
"could not truncate file \"%s\": %m", path)));
352 path =
relpath(rlocator, forknum);
375 if (ret >= 0 || errno != ENOENT)
378 if (ret < 0 && errno != ENOENT)
383 errmsg(
"could not remove file \"%s\": %m", path)));
411 if (ret >= 0 || errno != ENOENT)
413 char *segpath = (
char *)
palloc(strlen(path) + 12);
416 for (segno = 1;; segno++)
418 sprintf(segpath,
"%s.%u", path, segno);
436 if (unlink(segpath) < 0)
442 errmsg(
"could not remove file \"%s\": %m", segpath)));
463 const void *buffer,
bool skipFsync)
474 #ifdef CHECK_WRITE_VS_EXTEND
486 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
487 errmsg(
"cannot extend file \"%s\" beyond %u blocks",
493 seekpos = (off_t) BLCKSZ * (blocknum % ((
BlockNumber) RELSEG_SIZE));
495 Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
497 if ((nbytes =
FileWrite(v->
mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
502 errmsg(
"could not extend file \"%s\": %m",
504 errhint(
"Check free disk space.")));
508 errmsg(
"could not extend file \"%s\": wrote only %d of %d bytes at block %u",
510 nbytes, BLCKSZ, blocknum),
511 errhint(
"Check free disk space.")));
532 int remblocks = nblocks;
537 #ifdef CHECK_WRITE_VS_EXTEND
548 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
549 errmsg(
"cannot extend file \"%s\" beyond %u blocks",
553 while (remblocks > 0)
556 off_t seekpos = (off_t) BLCKSZ * segstartblock;
559 if (segstartblock + remblocks > RELSEG_SIZE)
560 numblocks = RELSEG_SIZE - segstartblock;
562 numblocks = remblocks;
566 Assert(segstartblock < RELSEG_SIZE);
567 Assert(segstartblock + numblocks <= RELSEG_SIZE);
585 seekpos, (off_t) BLCKSZ * numblocks,
586 WAIT_EVENT_DATA_FILE_EXTEND);
591 errmsg(
"could not extend file \"%s\" with FileFallocate(): %m",
593 errhint(
"Check free disk space."));
608 seekpos, (off_t) BLCKSZ * numblocks,
609 WAIT_EVENT_DATA_FILE_EXTEND);
613 errmsg(
"could not extend file \"%s\": %m",
615 errhint(
"Check free disk space."));
623 remblocks -= numblocks;
624 curblocknum += numblocks;
663 errmsg(
"could not open file \"%s\": %m", path)));
685 for (
int forknum = 0; forknum <=
MAX_FORKNUM; forknum++)
702 while (nopensegs > 0)
729 seekpos = (off_t) BLCKSZ * (blocknum % ((
BlockNumber) RELSEG_SIZE));
731 Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
754 TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
763 seekpos = (off_t) BLCKSZ * (blocknum % ((
BlockNumber) RELSEG_SIZE));
765 Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
767 nbytes =
FileRead(v->
mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_READ);
769 TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
777 if (nbytes != BLCKSZ)
782 errmsg(
"could not read block %u in file \"%s\": %m",
794 MemSet(buffer, 0, BLCKSZ);
798 errmsg(
"could not read block %u in file \"%s\": read only %d of %d bytes",
813 const void *buffer,
bool skipFsync)
824 #ifdef CHECK_WRITE_VS_EXTEND
828 TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
837 seekpos = (off_t) BLCKSZ * (blocknum % ((
BlockNumber) RELSEG_SIZE));
839 Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
841 nbytes =
FileWrite(v->
mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_WRITE);
843 TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
851 if (nbytes != BLCKSZ)
856 errmsg(
"could not write block %u in file \"%s\": %m",
861 errmsg(
"could not write block %u in file \"%s\": wrote only %d of %d bytes",
865 errhint(
"Check free disk space.")));
910 segnum_start = blocknum / RELSEG_SIZE;
913 segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
914 if (segnum_start != segnum_end)
915 nflush = RELSEG_SIZE - (blocknum % ((
BlockNumber) RELSEG_SIZE));
918 Assert(nflush <= nblocks);
920 seekpos = (off_t) BLCKSZ * (blocknum % ((
BlockNumber) RELSEG_SIZE));
971 return (segno * ((
BlockNumber) RELSEG_SIZE)) + nblocks;
1006 if (nblocks > curnblk)
1012 (
errmsg(
"could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
1014 nblocks, curnblk)));
1016 if (nblocks == curnblk)
1024 while (curopensegs > 0)
1028 priorblocks = (curopensegs - 1) * RELSEG_SIZE;
1030 v = &reln->
md_seg_fds[forknum][curopensegs - 1];
1032 if (priorblocks > nblocks)
1041 errmsg(
"could not truncate file \"%s\": %m",
1053 else if (priorblocks + ((
BlockNumber) RELSEG_SIZE) > nblocks)
1062 BlockNumber lastsegblocks = nblocks - priorblocks;
1064 if (
FileTruncate(v->
mdfd_vfd, (off_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
1067 errmsg(
"could not truncate file \"%s\" to %u blocks: %m",
1100 int min_inactive_seg;
1135 errmsg(
"could not fsync file \"%s\": %m",
1139 if (segno > min_inactive_seg)
1173 (
errmsg_internal(
"could not forward fsync request because request queue is full")));
1180 errmsg(
"could not fsync file \"%s\": %m",
1239 rlocator.
dbOid = dbid;
1258 for (
i = 0;
i < ndelrels;
i++)
1274 for (
i = 0;
i < ndelrels;
i++)
1331 fullpath =
psprintf(
"%s.%u", path, segno);
1391 bool skipFsync,
int behavior)
1405 if (targetseg < reln->md_num_open_segs[forknum])
1432 nextsegno <= targetseg; nextsegno++)
1466 zerobuf, skipFsync);
1494 errmsg(
"could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1508 errmsg(
"could not open file \"%s\" (target block %u): %m",
1529 errmsg(
"could not seek to end of file \"%s\": %m",
1556 need_to_close =
false;
1569 need_to_close =
true;
1575 result =
FileSync(file, WAIT_EVENT_DATA_FILE_SYNC);
1605 return unlink(path);
void TablespaceCreateDbspace(Oid spcOid, Oid dbOid, bool isRedo)
#define InvalidBlockNumber
#define TYPEALIGN(ALIGNVAL, LEN)
#define MemSet(start, val, len)
elog(ERROR, "%s: %s", p2, msg)
int errmsg_internal(const char *fmt,...)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
int FileSync(File file, uint32 wait_event_info)
void FileClose(File file)
int FileFallocate(File file, off_t offset, off_t amount, uint32 wait_event_info)
int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info)
int data_sync_elevel(int elevel)
File PathNameOpenFile(const char *fileName, int fileFlags)
int FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
int FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
char * FilePathName(File file)
int FileZero(File file, off_t offset, off_t amount, uint32 wait_event_info)
off_t FileSize(File file)
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
int pg_truncate(const char *path, off_t length)
#define FILE_POSSIBLY_DELETED(err)
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * repalloc(void *pointer, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * palloc_aligned(Size size, Size alignto, int flags)
void mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum, BlockNumber segno)
#define EXTENSION_CREATE_RECOVERY
static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
static void mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
bool mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
bool mdexists(SMgrRelation reln, ForkNumber forknum)
static void register_unlink_segment(RelFileLocatorBackend rlocator, ForkNumber forknum, BlockNumber segno)
#define EXTENSION_DONT_OPEN
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
int mdunlinkfiletag(const FileTag *ftag, char *path)
static MemoryContext MdCxt
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
static int do_truncate(const char *path)
void mdclose(SMgrRelation reln, ForkNumber forknum)
void mdzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
static MdfdVec * _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno, int oflags)
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
int mdsyncfiletag(const FileTag *ftag, char *path)
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
static MdfdVec * _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno, bool skipFsync, int behavior)
#define EXTENSION_RETURN_NULL
static char * _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
void mdopen(SMgrRelation reln)
static int _mdfd_open_flags(void)
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
#define INIT_MD_FILETAG(a, xx_rlocator, xx_forknum, xx_segno)
#define EXTENSION_DONT_CHECK_SIZE
static MdfdVec * mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
void DropRelationFiles(RelFileLocator *delrels, int ndelrels, bool isRedo)
static void _fdvec_resize(SMgrRelation reln, ForkNumber forknum, int nseg)
void ForgetDatabaseSyncRequests(Oid dbid)
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define ERRCODE_DATA_CORRUPTED
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt)
instr_time pgstat_prepare_io_time(void)
size_t strlcpy(char *dst, const char *src, size_t siz)
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
#define RelFileLocatorBackendIsTemp(rlocator)
#define relpath(rlocator, forknum)
#define relpathperm(rlocator, forknum)
void smgrclose(SMgrRelation reln)
SMgrRelation smgropen(RelFileLocator rlocator, BackendId backend)
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
int md_num_open_segs[MAX_FORKNUM+1]
struct _MdfdVec * md_seg_fds[MAX_FORKNUM+1]
RelFileLocatorBackend smgr_rlocator
bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type, bool retryOnError)
void XLogDropRelation(RelFileLocator rlocator, ForkNumber forknum)