pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedSwitch buffile.c/h to use pgoff_t
13+ messages / 3 participants
[nested] [flat]
Commits from this thread:
2025-12-25 bde3a46160 Upgrade BufFile to use int64 for byte positions
2025-12-22 e5f3839af6 Switch buffile.c/h to use pgoff_t instead of off_t
* Switch buffile.c/h to use pgoff_t
@ 2025-12-19 01:43 Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2025-12-19 01:43 UTC (permalink / raw)
To: Postgres hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Bryan Green <dbryan.green@gmail.com>
Hi all,
(Added Bryan in CC as he has been looking at this stuff previously.)
An mentioned on this thread and as a part of the quest to remove more
of long in the tree, buffile.c and buffile.h still rely on an
unportable off_t, which is signed 4 bytes on Windows:
https://www.postgresql.org/message-id/0f238ff4-c442-42f5-adb8-01b762c94ca1@gmail.com
Please find attached a patch to do the switch. I was surprised to see
that the amount of code to adapt was limited, the routines of
buffile.h changed in this commit being used in other places that keep
track of offsets. Hence these other files just need to do a off_t =>
pgoff_t flip in a couple of structures to be updated, as far as I can
see.
This removes a couple of extra long casts, as well as one comment in
BufFileSeek() that relates to overflows for large offsets, that would
not exist with this switch, which is nice.
Thanks,
--
Michael
Attachments:
0001-Switch-buffile.c-h-to-use-portable-pgoff_t.patch (text/x-diff, 8.5K) download
From 566b2e685e819a6b3356702c9b00698d11a1ee35 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 19 Dec 2025 10:36:04 +0900
Subject: [PATCH] Switch buffile.c/h to use portable pgoff_t
off_t was used previously, which is 4 bytes on Windows, hence limiting
the backend code to a hard limit for files longer than 2GB.
---
src/include/storage/buffile.h | 6 +++---
src/backend/replication/logical/worker.c | 8 ++++----
src/backend/storage/file/buffile.c | 23 +++++++++++------------
src/backend/utils/sort/tuplestore.c | 12 ++++++------
4 files changed, 24 insertions(+), 25 deletions(-)
diff --git a/src/include/storage/buffile.h b/src/include/storage/buffile.h
index a2f4821f240b..4908dc56c7be 100644
--- a/src/include/storage/buffile.h
+++ b/src/include/storage/buffile.h
@@ -42,8 +42,8 @@ pg_nodiscard extern size_t BufFileRead(BufFile *file, void *ptr, size_t size);
extern void BufFileReadExact(BufFile *file, void *ptr, size_t size);
extern size_t BufFileReadMaybeEOF(BufFile *file, void *ptr, size_t size, bool eofOK);
extern void BufFileWrite(BufFile *file, const void *ptr, size_t size);
-extern int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence);
-extern void BufFileTell(BufFile *file, int *fileno, off_t *offset);
+extern int BufFileSeek(BufFile *file, int fileno, pgoff_t offset, int whence);
+extern void BufFileTell(BufFile *file, int *fileno, pgoff_t *offset);
extern int BufFileSeekBlock(BufFile *file, int64 blknum);
extern int64 BufFileSize(BufFile *file);
extern int64 BufFileAppend(BufFile *target, BufFile *source);
@@ -54,6 +54,6 @@ extern BufFile *BufFileOpenFileSet(FileSet *fileset, const char *name,
int mode, bool missing_ok);
extern void BufFileDeleteFileSet(FileSet *fileset, const char *name,
bool missing_ok);
-extern void BufFileTruncateFileSet(BufFile *file, int fileno, off_t offset);
+extern void BufFileTruncateFileSet(BufFile *file, int fileno, pgoff_t offset);
#endif /* BUFFILE_H */
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index fc64476a9ef1..d1ee0261c646 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -530,7 +530,7 @@ typedef struct SubXactInfo
{
TransactionId xid; /* XID of the subxact */
int fileno; /* file number in the buffile */
- off_t offset; /* offset in the file */
+ pgoff_t offset; /* offset in the file */
} SubXactInfo;
/* Sub-transaction data for the current streaming transaction */
@@ -2226,12 +2226,12 @@ apply_handle_stream_abort(StringInfo s)
*/
static void
ensure_last_message(FileSet *stream_fileset, TransactionId xid, int fileno,
- off_t offset)
+ pgoff_t offset)
{
char path[MAXPGPATH];
BufFile *fd;
int last_fileno;
- off_t last_offset;
+ pgoff_t last_offset;
Assert(!IsTransactionState());
@@ -2266,7 +2266,7 @@ apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
MemoryContext oldcxt;
ResourceOwner oldowner;
int fileno;
- off_t offset;
+ pgoff_t offset;
if (!am_parallel_apply_worker())
maybe_start_skipping_changes(lsn);
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 4e520065ae00..85b316d879dc 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -92,7 +92,7 @@ struct BufFile
* Position as seen by user of BufFile is (curFile, curOffset + pos).
*/
int curFile; /* file index (0..n) part of current pos */
- off_t curOffset; /* offset 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 */
@@ -503,7 +503,7 @@ BufFileDumpBuffer(BufFile *file)
*/
while (wpos < file->nbytes)
{
- off_t availbytes;
+ pgoff_t availbytes;
instr_time io_start;
instr_time io_time;
@@ -524,7 +524,7 @@ BufFileDumpBuffer(BufFile *file)
bytestowrite = file->nbytes - wpos;
availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
- if ((off_t) bytestowrite > availbytes)
+ if ((pgoff_t) bytestowrite > availbytes)
bytestowrite = (int) availbytes;
thisfile = file->files[file->curFile];
@@ -729,7 +729,7 @@ BufFileFlush(BufFile *file)
* BufFileSeek
*
* Like fseek(), except that target position needs two values in order to
- * work when logical filesize exceeds maximum value representable by off_t.
+ * work when logical filesize exceeds maximum value representable by pgoff_t.
* We do not support relative seeks across more than that, however.
* I/O errors are reported by ereport().
*
@@ -737,10 +737,10 @@ BufFileFlush(BufFile *file)
* impossible seek is attempted.
*/
int
-BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
+BufFileSeek(BufFile *file, int fileno, pgoff_t offset, int whence)
{
int newFile;
- off_t newOffset;
+ pgoff_t newOffset;
switch (whence)
{
@@ -754,8 +754,7 @@ BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
/*
* Relative seek considers only the signed offset, ignoring
- * fileno. Note that large offsets (> 1 GB) risk overflow in this
- * add, unless we have 64-bit off_t.
+ * fileno.
*/
newFile = file->curFile;
newOffset = (file->curOffset + file->pos) + offset;
@@ -830,7 +829,7 @@ BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
}
void
-BufFileTell(BufFile *file, int *fileno, off_t *offset)
+BufFileTell(BufFile *file, int *fileno, pgoff_t *offset)
{
*fileno = file->curFile;
*offset = file->curOffset + file->pos;
@@ -852,7 +851,7 @@ BufFileSeekBlock(BufFile *file, int64 blknum)
{
return BufFileSeek(file,
(int) (blknum / BUFFILE_SEG_SIZE),
- (off_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
+ (pgoff_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
SEEK_SET);
}
@@ -925,11 +924,11 @@ BufFileAppend(BufFile *target, BufFile *source)
* and the offset.
*/
void
-BufFileTruncateFileSet(BufFile *file, int fileno, off_t offset)
+BufFileTruncateFileSet(BufFile *file, int fileno, pgoff_t offset)
{
int numFiles = file->numFiles;
int newFile = fileno;
- off_t newOffset = file->curOffset;
+ pgoff_t newOffset = file->curOffset;
char segment_name[MAXPGPATH];
int i;
diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c
index def945b04541..9701b8133602 100644
--- a/src/backend/utils/sort/tuplestore.c
+++ b/src/backend/utils/sort/tuplestore.c
@@ -94,7 +94,7 @@ typedef struct
bool eof_reached; /* read has reached EOF */
int current; /* next array index to read */
int file; /* temp file# */
- off_t offset; /* byte offset in file */
+ pgoff_t offset; /* byte offset in file */
} TSReadPointer;
/*
@@ -179,7 +179,7 @@ struct Tuplestorestate
int readptrsize; /* allocated length of readptrs array */
int writepos_file; /* file# (valid if READFILE state) */
- off_t writepos_offset; /* offset (valid if READFILE state) */
+ pgoff_t writepos_offset; /* offset (valid if READFILE state) */
};
#define COPYTUP(state,tup) ((*(state)->copytup) (state, tup))
@@ -1051,7 +1051,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
* Back up to fetch previously-returned tuple's ending length
* word. If seek fails, assume we are at start of file.
*/
- if (BufFileSeek(state->myfile, 0, -(long) sizeof(unsigned int),
+ if (BufFileSeek(state->myfile, 0, -(pgoff_t) sizeof(unsigned int),
SEEK_CUR) != 0)
{
/* even a failed backwards fetch gets you out of eof state */
@@ -1072,7 +1072,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
* Back up to get ending length word of tuple before it.
*/
if (BufFileSeek(state->myfile, 0,
- -(long) (tuplen + 2 * sizeof(unsigned int)),
+ -(pgoff_t) (tuplen + 2 * sizeof(unsigned int)),
SEEK_CUR) != 0)
{
/*
@@ -1082,7 +1082,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
* what in-memory case does).
*/
if (BufFileSeek(state->myfile, 0,
- -(long) (tuplen + sizeof(unsigned int)),
+ -(pgoff_t) (tuplen + sizeof(unsigned int)),
SEEK_CUR) != 0)
ereport(ERROR,
(errcode_for_file_access(),
@@ -1099,7 +1099,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
* length word of the tuple, so back up to that point.
*/
if (BufFileSeek(state->myfile, 0,
- -(long) tuplen,
+ -(pgoff_t) tuplen,
SEEK_CUR) != 0)
ereport(ERROR,
(errcode_for_file_access(),
--
2.51.0
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-19 03:00 ` Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:19 ` Re: Switch buffile.c/h to use pgoff_t Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
0 siblings, 2 replies; 13+ messages in thread
From: Chao Li @ 2025-12-19 03:00 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
> On Dec 19, 2025, at 09:43, Michael Paquier <michael@paquier.xyz> wrote:
>
> Hi all,
> (Added Bryan in CC as he has been looking at this stuff previously.)
>
> An mentioned on this thread and as a part of the quest to remove more
> of long in the tree, buffile.c and buffile.h still rely on an
> unportable off_t, which is signed 4 bytes on Windows:
> https://www.postgresql.org/message-id/0f238ff4-c442-42f5-adb8-01b762c94ca1@gmail.com
>
> Please find attached a patch to do the switch. I was surprised to see
> that the amount of code to adapt was limited, the routines of
> buffile.h changed in this commit being used in other places that keep
> track of offsets. Hence these other files just need to do a off_t =>
> pgoff_t flip in a couple of structures to be updated, as far as I can
> see.
>
> This removes a couple of extra long casts, as well as one comment in
> BufFileSeek() that relates to overflows for large offsets, that would
> not exist with this switch, which is nice.
>
> Thanks,
> --
> Michael
> <0001-Switch-buffile.c-h-to-use-portable-pgoff_t.patch>
```
while (wpos < file->nbytes)
{
- off_t availbytes;
+ pgoff_t availbytes;
instr_time io_start;
instr_time io_time;
@@ -524,7 +524,7 @@ BufFileDumpBuffer(BufFile *file)
bytestowrite = file->nbytes - wpos;
availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
- if ((off_t) bytestowrite > availbytes)
+ if ((pgoff_t) bytestowrite > availbytes)
bytestowrite = (int) availbytes;
```
bytestowrite is of type int, loosing it to pgoff_t then compare with availbytes, if bytestowrite > availbytes, then availbytes must be within the range of int, so the next assignment “bytestowrite = (int) availbytes” is safe, but makes reading difficult.
Given MAX_PHYSICAL_FILESIZE is just 1G (2^30), why availbytes has to be pgoff_t instead of just int?
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
@ 2025-12-19 05:19 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
1 sibling, 0 replies; 13+ messages in thread
From: Bertrand Drouvot @ 2025-12-19 05:19 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
Hi,
On Fri, Dec 19, 2025 at 11:00:54AM +0800, Chao Li wrote:
> Given MAX_PHYSICAL_FILESIZE is just 1G (2^30), why availbytes has to be pgoff_t instead of just int?
I agree that int would work, but maybe it's using pgoff_t just to be on the safe
side of things should MAX_PHYSICAL_FILESIZE become 2^31 or higher one day?
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
@ 2025-12-19 05:22 ` Michael Paquier <michael@paquier.xyz>
2025-12-22 22:49 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
1 sibling, 2 replies; 13+ messages in thread
From: Michael Paquier @ 2025-12-19 05:22 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Fri, Dec 19, 2025 at 11:00:54AM +0800, Chao Li wrote:
> Given MAX_PHYSICAL_FILESIZE is just 1G (2^30), why availbytes has to
> be pgoff_t instead of just int?
The point of such changes would be to lift this barrier at some point,
which is what the other thread I am mentioning upthread is also
pointing at. It does not change the fact that this code is currently
not portable as written: off_t can be 4 or 8 bytes depending on the
environment, and pgoff_t exists to be a stable alternative. This
relates as well to the use of long in the tree, all coming down to
WIN32.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-22 22:49 ` Michael Paquier <michael@paquier.xyz>
1 sibling, 0 replies; 13+ messages in thread
From: Michael Paquier @ 2025-12-22 22:49 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Fri, Dec 19, 2025 at 02:22:02PM +0900, Michael Paquier wrote:
> The point of such changes would be to lift this barrier at some point,
> which is what the other thread I am mentioning upthread is also
> pointing at. It does not change the fact that this code is currently
> not portable as written: off_t can be 4 or 8 bytes depending on the
> environment, and pgoff_t exists to be a stable alternative. This
> relates as well to the use of long in the tree, all coming down to
> WIN32.
Getting rid of a couple more long assumptions while removing one
portability comment from buffile.c is appealing while the change is
not invasive, so applied.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-23 02:59 ` Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
1 sibling, 1 reply; 13+ messages in thread
From: Chao Li @ 2025-12-23 02:59 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Fri, Dec 19, 2025 at 1:22 PM Michael Paquier <michael@paquier.xyz> wrote:
> On Fri, Dec 19, 2025 at 11:00:54AM +0800, Chao Li wrote:
> > Given MAX_PHYSICAL_FILESIZE is just 1G (2^30), why availbytes has to
> > be pgoff_t instead of just int?
>
> The point of such changes would be to lift this barrier at some point,
> which is what the other thread I am mentioning upthread is also
> pointing at. It does not change the fact that this code is currently
> not portable as written: off_t can be 4 or 8 bytes depending on the
> environment, and pgoff_t exists to be a stable alternative. This
> relates as well to the use of long in the tree, all coming down to
> WIN32.
> --
> Michael
>
Sorry, I didn’t explain myself clearly earlier. My main point was to avoid
the awkward mixed-type casts here:
```
if ((pgoff_t) bytestowrite > availbytes)
bytestowrite = (int) availbytes;
```
I agree that changing availbytes to int would not be a good choice.
Instead, I tried making bytestowrite a pgoff_t, so that the comparison and
assignment can be done without casts, while still keeping the code correct
if MAX_PHYSICAL_FILESIZE is lifted in the future.
I’ve attached a small patch along these lines. It compiles without
warnings, and "make check" passes on my side. What do you think?
Best regards,
==
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v1-0001-buffile-use-pgoff_t-for-bytestowrite-in-BufFileDu.patch (application/octet-stream, 1.5K) download
From fa73b95da37af8c2ec00a07201d6c21826d9fb87 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 23 Dec 2025 10:44:27 +0800
Subject: [PATCH v1] buffile: use pgoff_t for bytestowrite in BufFileDumpBuffer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Avoid mixed-width arithmetic when computing the number of bytes to write
in BufFileDumpBuffer() by using pgoff_t for bytestowrite.
This removes unnecessary casts, makes the comparison clearer, and keeps
file-offset arithmetic using PostgreSQL’s portable offset type.
Author: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/aUStrqoOCDRFAq1M@paquier.xyz
---
src/backend/storage/file/buffile.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 85b316d879d..b24560d670c 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -494,7 +494,7 @@ static void
BufFileDumpBuffer(BufFile *file)
{
int wpos = 0;
- int bytestowrite;
+ pgoff_t bytestowrite;
File thisfile;
/*
@@ -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];
--
2.39.5 (Apple Git-154)
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
@ 2025-12-24 06:15 ` Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2025-12-24 06:15 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Tue, Dec 23, 2025 at 10:59:45AM +0800, Chao Li wrote:
> I’ve attached a small patch along these lines. It compiles without
> warnings, and "make check" passes on my side. What do you think?
I don't think it is right. bytestowrite is not a file offset, and the
code has been using an int due to BufFile->nbytes. This leads to a
more confusing result.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-24 09:01 ` Chao Li <li.evan.chao@gmail.com>
2025-12-24 23:42 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Chao Li @ 2025-12-24 09:01 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Wed, Dec 24, 2025 at 2:15 PM Michael Paquier <michael@paquier.xyz> wrote:
> On Tue, Dec 23, 2025 at 10:59:45AM +0800, Chao Li wrote:
> > I’ve attached a small patch along these lines. It compiles without
> > warnings, and "make check" passes on my side. What do you think?
>
> I don't think it is right. bytestowrite is not a file offset, and the
> code has been using an int due to BufFile->nbytes. This leads to a
> more confusing result.
> --
> Michael
>
Make sense, bytestowrite is not a file offset. So, in the current code,
availbytes is not a file offset either, but it is defined as pgoff_t, which
has the same confusion, right? Also bytestowrite is casted to pgoff_t, it's
the same confusion again.
How about using "ssize_t" for both bytestowrite and availbytes? It's still
signed, broader than int, and the odd type casts are eliminated.
In win32_port.h:
```
#ifndef _WIN64
typedef long ssize_t;
#else
typedef __int64 ssize_t;
#endif
```
Best regards,
==
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v2-0001-buffile-use-ssize_t-for-bytestowrite-in-BufFileDu.patch (application/octet-stream, 1.7K) download
From c896554595c7615c94b01fc13cc6369272538043 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 23 Dec 2025 10:44:27 +0800
Subject: [PATCH v2] buffile: use ssize_t for bytestowrite in BufFileDumpBuffer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Avoid mixed-width arithmetic when computing the number of bytes to write
in BufFileDumpBuffer() by using ssize_t for bytestowrite.
This removes unnecessary casts, makes the comparison clearer, and keeps
file-offset arithmetic using PostgreSQL’s portable offset type.
Author: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/aUStrqoOCDRFAq1M@paquier.xyz
---
src/backend/storage/file/buffile.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 85b316d879d..4e1f3667307 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -494,7 +494,7 @@ static void
BufFileDumpBuffer(BufFile *file)
{
int wpos = 0;
- int bytestowrite;
+ ssize_t bytestowrite;
File thisfile;
/*
@@ -503,7 +503,7 @@ BufFileDumpBuffer(BufFile *file)
*/
while (wpos < file->nbytes)
{
- pgoff_t availbytes;
+ ssize_t 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];
--
2.39.5 (Apple Git-154)
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
@ 2025-12-24 23:42 ` Michael Paquier <michael@paquier.xyz>
2025-12-24 23:45 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-25 02:40 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
0 siblings, 2 replies; 13+ messages in thread
From: Michael Paquier @ 2025-12-24 23:42 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Wed, Dec 24, 2025 at 05:01:57PM +0800, Chao Li wrote:
> Make sense, bytestowrite is not a file offset. So, in the current code,
> availbytes is not a file offset either, but it is defined as pgoff_t, which
> has the same confusion, right? Also bytestowrite is casted to pgoff_t, it's
> the same confusion again.
Yeah, actually this suggestion makes more sense. availbytes is a
computation made of a maximal size and an offset, so defining it as an
offset from the start is kind of weird.
Now I don't think that your suggested set of changes could become more
consistent with a few more changes. For example, what about pos and
nbytes in BufFile? While ssize_t is more consistent with FileRead()
and FileWrite(), this code is written to care about signedness while
ssize_t has a stricter range per posix, hence could int64 be a better
choice for the whole interface? int64 is already what we use for
BufFileSize(), which is due to the limit of MAX_PHYSICAL_FILESIZE of
course.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 23:42 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-24 23:45 ` Michael Paquier <michael@paquier.xyz>
1 sibling, 0 replies; 13+ messages in thread
From: Michael Paquier @ 2025-12-24 23:45 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Thu, Dec 25, 2025 at 08:42:04AM +0900, Michael Paquier wrote:
> Now I don't think that your suggested set of changes could become more
> consistent with a few more changes.
Cough. "I think that your suggested set of changes could become more
consistent with a few more changes." Cough.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 23:42 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-25 02:40 ` Chao Li <li.evan.chao@gmail.com>
2025-12-25 23:45 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
1 sibling, 1 reply; 13+ messages in thread
From: Chao Li @ 2025-12-25 02:40 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Thu, Dec 25, 2025 at 7:42 AM Michael Paquier <michael@paquier.xyz> wrote:
> On Wed, Dec 24, 2025 at 05:01:57PM +0800, Chao Li wrote:
> > Make sense, bytestowrite is not a file offset. So, in the current code,
> > availbytes is not a file offset either, but it is defined as pgoff_t,
> which
> > has the same confusion, right? Also bytestowrite is casted to pgoff_t,
> it's
> > the same confusion again.
>
> Yeah, actually this suggestion makes more sense. availbytes is a
> computation made of a maximal size and an offset, so defining it as an
> offset from the start is kind of weird.
>
> Now I think that your suggested set of changes could become more
> consistent with a few more changes. For example, what about pos and
> nbytes in BufFile? While ssize_t is more consistent with FileRead()
> and FileWrite(), this code is written to care about signedness while
> ssize_t has a stricter range per posix, hence could int64 be a better
> choice for the whole interface? int64 is already what we use for
> BufFileSize(), which is due to the limit of MAX_PHYSICAL_FILESIZE of
> course.
> --
> Michael
>
Yeah, int64 feels a better fit.
WRT. changing pos and nbytes in BufFile to int64, I agree that also makes
sense. BufFile is a private struct in buffile.c, though it's indirectly
exposed by:
```
typedef struct BufFile BufFile;
```
None of the callers accesses its fields directly. Changing the two fields
from int to int64 will bump the structure size 16 bytes, but likely there
is not a case where numerous BufFile objects need to be created, thus
runtime impact should be minimal.
In attached v3, I have applied int64 to the 2 struct fields and
corresponding local variables. I ran a clean build, no warning was
introduced, and "make check" passed.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v3-0001-Refactor-BufFile-buffer-fields-to-eliminate-odd-t.patch (application/octet-stream, 2.6K) download
From 3ac6ca495869e35d766dcbdfe93b4e0e0b3d3be1 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 23 Dec 2025 10:44:27 +0800
Subject: [PATCH v3] Refactor BufFile buffer fields to eliminate odd type casts
Use int64 for BufFile buffer position and length fields, and adjust
related local variables accordingly. This avoids repeated and confusing
casts between int and wider types when computing buffer offsets and
write sizes.
No functional change intended.
Author: Chao Li <lic@highgo.com>
Discussion: https://postgr.es/m/aUStrqoOCDRFAq1M@paquier.xyz
---
src/backend/storage/file/buffile.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 85b316d879d..d42b630d8cc 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];
@@ -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 = newOffset - file->curOffset;
/* Adjust the nbytes for the current buffer. */
- file->nbytes = (int) (newOffset - file->curOffset);
+ file->nbytes = newOffset - file->curOffset;
}
else if (newFile == file->curFile &&
newOffset < file->curOffset)
--
2.39.5 (Apple Git-154)
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 23:42 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-25 02:40 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
@ 2025-12-25 23:45 ` Michael Paquier <michael@paquier.xyz>
2025-12-26 01:11 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2025-12-25 23:45 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
On Thu, Dec 25, 2025 at 10:40:02AM +0800, Chao Li wrote:
> In attached v3, I have applied int64 to the 2 struct fields and
> corresponding local variables. I ran a clean build, no warning was
> introduced, and "make check" passed.
The advantage of being able to make the code transparently more
pluggable for max large sizes while we already use 8-byte offsets is
kind of nice, I guess. "availbytes" is a clarification bonus as it is
not an offset per-se. I can see that you have missed on cast spot, so
adjusted things a bit, then applied the result.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: Switch buffile.c/h to use pgoff_t
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:22 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-24 23:42 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-25 02:40 ` Re: Switch buffile.c/h to use pgoff_t Chao Li <li.evan.chao@gmail.com>
2025-12-25 23:45 ` Re: Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
@ 2025-12-26 01:11 ` Chao Li <li.evan.chao@gmail.com>
0 siblings, 0 replies; 13+ messages in thread
From: Chao Li @ 2025-12-26 01:11 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Bryan Green <dbryan.green@gmail.com>
> On Dec 26, 2025, at 07:45, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Thu, Dec 25, 2025 at 10:40:02AM +0800, Chao Li wrote:
>> In attached v3, I have applied int64 to the 2 struct fields and
>> corresponding local variables. I ran a clean build, no warning was
>> introduced, and "make check" passed.
>
> The advantage of being able to make the code transparently more
> pluggable for max large sizes while we already use 8-byte offsets is
> kind of nice, I guess. "availbytes" is a clarification bonus as it is
> not an offset per-se. I can see that you have missed on cast spot, so
> adjusted things a bit, then applied the result.
> --
> Michael
Thanks a lot for pushing.
WRT to the original (int) casts, I intentionally removed them, because file->nbytes, newOffset and file->curOffset are all signed 64 bit integers now. Compiling with -Wextra won’t get a warning, so technically, the type cast is no longer needed.
I saw you changed to:
```
- file->pos = (int) (newOffset - file->curOffset);
+ file->pos = (int64) (newOffset - file->curOffset);
- file->pos = (int) (newOffset - file->curOffset);
+ file->pos = (int64) newOffset - file->curOffset;
- file->nbytes = (int) (newOffset - file->curOffset);
+ file->nbytes = (int64) newOffset - file->curOffset;
```
The latter two places missed (), but that should also work, just a little inconsistent.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
^ permalink raw reply [nested|flat] 13+ messages in thread
end of thread, other threads:[~2025-12-26 01:11 UTC | newest]
Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-12-19 01:43 Switch buffile.c/h to use pgoff_t Michael Paquier <michael@paquier.xyz>
2025-12-19 03:00 ` Chao Li <li.evan.chao@gmail.com>
2025-12-19 05:19 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-12-19 05:22 ` Michael Paquier <michael@paquier.xyz>
2025-12-22 22:49 ` Michael Paquier <michael@paquier.xyz>
2025-12-23 02:59 ` Chao Li <li.evan.chao@gmail.com>
2025-12-24 06:15 ` Michael Paquier <michael@paquier.xyz>
2025-12-24 09:01 ` Chao Li <li.evan.chao@gmail.com>
2025-12-24 23:42 ` Michael Paquier <michael@paquier.xyz>
2025-12-24 23:45 ` Michael Paquier <michael@paquier.xyz>
2025-12-25 02:40 ` Chao Li <li.evan.chao@gmail.com>
2025-12-25 23:45 ` Michael Paquier <michael@paquier.xyz>
2025-12-26 01:11 ` Chao Li <li.evan.chao@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox