From: Michael Paquier Date: Thu, 25 Dec 2025 23:41:56 +0000 (+0900) Subject: Upgrade BufFile to use int64 for byte positions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bde3a4616072534fd03c75f9019d5165665ed211;p=thirdparty%2Fpostgresql.git Upgrade BufFile to use int64 for byte positions This change has the advantage of removing some weird type casts, caused by offset calculations based on pgoff_t but saved as int (on older branches we use off_t, which could be 4 or 8 bytes depending on the environment). These are safe currently because capped by MAX_PHYSICAL_FILESIZE, but we would run into problems when to make MAX_PHYSICAL_FILESIZE larger or allow callers of these routines to use a larger physical max size on demand. While on it, this improves BufFileDumpBuffer() so as we do not use an offset for "availbytes". It is not a file offset per-set, but a number of available bytes. This change should lead to no functional changes. Author: Chao Li Discussion: https://postgr.es/m/aUStrqoOCDRFAq1M@paquier.xyz --- diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 85b316d879d..9fc37092132 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -93,8 +93,8 @@ struct BufFile */ int curFile; /* file index (0..n) part of current pos */ pgoff_t curOffset; /* offset part of current pos */ - int pos; /* next read/write position in buffer */ - int nbytes; /* total # of valid bytes in buffer */ + int64 pos; /* next read/write position in buffer */ + int64 nbytes; /* total # of valid bytes in buffer */ /* * XXX Should ideally use PGIOAlignedBlock, but might need a way to avoid @@ -493,8 +493,8 @@ BufFileLoadBuffer(BufFile *file) static void BufFileDumpBuffer(BufFile *file) { - int wpos = 0; - int bytestowrite; + int64 wpos = 0; + int64 bytestowrite; File thisfile; /* @@ -503,7 +503,7 @@ BufFileDumpBuffer(BufFile *file) */ while (wpos < file->nbytes) { - pgoff_t availbytes; + int64 availbytes; instr_time io_start; instr_time io_time; @@ -524,8 +524,8 @@ BufFileDumpBuffer(BufFile *file) bytestowrite = file->nbytes - wpos; availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset; - if ((pgoff_t) bytestowrite > availbytes) - bytestowrite = (int) availbytes; + if (bytestowrite > availbytes) + bytestowrite = availbytes; thisfile = file->files[file->curFile]; @@ -794,7 +794,7 @@ BufFileSeek(BufFile *file, int fileno, pgoff_t offset, int whence) * whether reading or writing, but buffer remains dirty if we were * writing. */ - file->pos = (int) (newOffset - file->curOffset); + file->pos = (int64) (newOffset - file->curOffset); return 0; } /* Otherwise, must reposition buffer, so flush any dirty data */ @@ -983,10 +983,10 @@ BufFileTruncateFileSet(BufFile *file, int fileno, pgoff_t offset) { /* No need to reset the current pos if the new pos is greater. */ if (newOffset <= file->curOffset + file->pos) - file->pos = (int) (newOffset - file->curOffset); + file->pos = (int64) newOffset - file->curOffset; /* Adjust the nbytes for the current buffer. */ - file->nbytes = (int) (newOffset - file->curOffset); + file->nbytes = (int64) newOffset - file->curOffset; } else if (newFile == file->curFile && newOffset < file->curOffset)