Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWPWj-000ZBw-1r for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Dec 2025 01:43:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWPWi-005Ibn-0u for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Dec 2025 01:43:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWPWh-005Ibd-0x for pgsql-hackers@lists.postgresql.org; Fri, 19 Dec 2025 01:43:28 +0000 Received: from fout-b2-smtp.messagingengine.com ([202.12.124.145]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWPWf-001QPl-3C for pgsql-hackers@lists.postgresql.org; Fri, 19 Dec 2025 01:43:27 +0000 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfout.stl.internal (Postfix) with ESMTP id 507AC1D00103; Thu, 18 Dec 2025 20:43:24 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Thu, 18 Dec 2025 20:43:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm1; t=1766108604; x=1766195004; bh=ESdxbS+IBrVVqsMfNtasEbilXfHPhxDy DFthe1zoKNs=; b=bMzAgpDlqj+4at1IkJsmlks22OANQY7DM/UK0nl/eb227j8N RuhmpfZHAHxFJvIwM/aaP5N7ka8Ss7A5gbmkFRT3VOX+ACaCJ4pCrLMjmYIk4rH2 S6Mk03ag2HjVdRrWY6RWfB6Q42OZ1L7M1UwNigV3EHHgMKRcD2vKzUFpCx5LZxMq WnWXIrvEAKEJ+ZESmjxAMpX32+QDQaMzOuTa1ZpOYlNZv0LfOzlhXLTagHbrg6gl BYBDYgT5ZB81IgRKEUiv7RrEqYGtKjf081o33rHgbdjpcG5RKG8enXfoYjoZDuzB RwCdBS2VvKXH5tQW7IisCA9zBhk/JRuEjTRiNw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1766108604; x= 1766195004; bh=ESdxbS+IBrVVqsMfNtasEbilXfHPhxDyDFthe1zoKNs=; b=S NQFUIpIi1BKr0UQJ60Sc7+MsZSBc2CdVmWe467DVCoENnZiKATf0Cref9ypNT2EU ab50uBOjN1f0HDsO0Yp6VbTlYkX/1qSmVFugQbHuxnIC+Iy3IR2Fa7MRdq/fJdrT OiUMHRgRkx/mB9/p6HswaIq9acHytiUJOOXBINXrTfxnlkWj42IHYvOQuRBgmLEZ +Jc/3qZJo24olTPX+TW27bfP4UQIA2KDx5sDtCKQNYz4yfwnaI1Gv5D/Lqwdkwyp uQJWE+cEcB/46hJIPHDOPoMw3qyYF7PDQF+64sUoyEAUCLLqhZD9x0+tLJiA9+vj bxRIECEgVkSxvfyHlEu5A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgdegjedtudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlh cuvffnffculdefhedmnecujfgurhepfffhvfevuffkgggtugesghdtreertddtvdenucfh rhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprghquhhivg hrrdighiiiqeenucggtffrrghtthgvrhhnpefgtefhudfhhfdtieehfefffeeludelieek kedvgeffheegteejuddtfeefueduteenucffohhmrghinhepphhoshhtghhrvghsqhhlrd horhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep mhhitghhrggvlhesphgrqhhuihgvrhdrgiihiidpnhgspghrtghpthhtohepvddpmhhoug gvpehsmhhtphhouhhtpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrsheslhhishht shdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepuggsrhihrghnrdhgrhgvvg hnsehgmhgrihhlrdgtohhm X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Dec 2025 20:43:22 -0500 (EST) Date: Fri, 19 Dec 2025 10:43:10 +0900 From: Michael Paquier To: Postgres hackers Cc: Bryan Green Subject: Switch buffile.c/h to use pgoff_t Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="OrjCzxTzp8Dj0pJY" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --OrjCzxTzp8Dj0pJY Content-Type: multipart/mixed; boundary="77X2n5Y4UOAlCTR4" Content-Disposition: inline --77X2n5Y4UOAlCTR4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --77X2n5Y4UOAlCTR4 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Switch-buffile.c-h-to-use-portable-pgoff_t.patch" Content-Transfer-Encoding: quoted-printable =46rom 566b2e685e819a6b3356702c9b00698d11a1ee35 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier 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, voi= d *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, b= ool 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 when= ce); +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, cons= t 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 offs= et); =20 #endif /* BUFFILE_H */ diff --git a/src/backend/replication/logical/worker.c b/src/backend/replica= tion/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; =20 /* 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; =20 Assert(!IsTransactionState()); =20 @@ -2266,7 +2266,7 @@ apply_spooled_messages(FileSet *stream_fileset, Trans= actionId xid, MemoryContext oldcxt; ResourceOwner oldowner; int fileno; - off_t offset; + pgoff_t offset; =20 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 */ =20 @@ -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; =20 @@ -524,7 +524,7 @@ BufFileDumpBuffer(BufFile *file) bytestowrite =3D file->nbytes - wpos; availbytes =3D MAX_PHYSICAL_FILESIZE - file->curOffset; =20 - if ((off_t) bytestowrite > availbytes) + if ((pgoff_t) bytestowrite > availbytes) bytestowrite =3D (int) availbytes; =20 thisfile =3D 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; =20 switch (whence) { @@ -754,8 +754,7 @@ BufFileSeek(BufFile *file, int fileno, off_t offset, in= t whence) =20 /* * 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 =3D file->curFile; newOffset =3D (file->curOffset + file->pos) + offset; @@ -830,7 +829,7 @@ BufFileSeek(BufFile *file, int fileno, off_t offset, in= t whence) } =20 void -BufFileTell(BufFile *file, int *fileno, off_t *offset) +BufFileTell(BufFile *file, int *fileno, pgoff_t *offset) { *fileno =3D file->curFile; *offset =3D 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); } =20 @@ -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 =3D file->numFiles; int newFile =3D fileno; - off_t newOffset =3D file->curOffset; + pgoff_t newOffset =3D file->curOffset; char segment_name[MAXPGPATH]; int i; =20 diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/t= uplestore.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; =20 /* @@ -179,7 +179,7 @@ struct Tuplestorestate int readptrsize; /* allocated length of readptrs array */ =20 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) */ }; =20 #define COPYTUP(state,tup) ((*(state)->copytup) (state, tup)) @@ -1051,7 +1051,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forw= ard, * 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) !=3D 0) { /* even a failed backwards fetch gets you out of eof state */ @@ -1072,7 +1072,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forw= ard, * 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) !=3D 0) { /* @@ -1082,7 +1082,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forw= ard, * what in-memory case does). */ if (BufFileSeek(state->myfile, 0, - -(long) (tuplen + sizeof(unsigned int)), + -(pgoff_t) (tuplen + sizeof(unsigned int)), SEEK_CUR) !=3D 0) ereport(ERROR, (errcode_for_file_access(), @@ -1099,7 +1099,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forw= ard, * length word of the tuple, so back up to that point. */ if (BufFileSeek(state->myfile, 0, - -(long) tuplen, + -(pgoff_t) tuplen, SEEK_CUR) !=3D 0) ereport(ERROR, (errcode_for_file_access(), --=20 2.51.0 --77X2n5Y4UOAlCTR4-- --OrjCzxTzp8Dj0pJY Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmlEra4ACgkQnvQgOdby QH1eJBAAjDFj0AOuGxm7vRE6aoOlJl85gcU7/6vLS3YwTOaodOQBzKdOmEZWmbFd fJvwzoCrOKk8hfPEL478bZa3teOOCVHqjwQXjgTVt2jY2nJF1BT9DeHgdUaOykc0 Lu5HMbYOEOO0jCQlL0pso9b6Bb3GbvqQBn+tkz3bW7sSUVhPKCLVwLY236K16gWQ I2n6loUSXGw1DGMIUYBzvDRX2US9LS+CHy9YOgngiqLZisuEVxbx2jTZtL0fjfuK l7GVL5WReYYOVVZglgVpUiwMD3QeyRFGAw5+3ze7vC7RyBL4CMnxrrFoaHc/1DDE JO6OUOEYg/fQ6VIy04k52MXbvnDKxdj+UxTtUBJ/Rg8lBLBeHZhQIJC1DjXN/nMC vCXk3Z/Lwwp8/LDT6a/uKjBMUsEq0E4JEAh81D5fdVTIpaHmvr1uCEeUn+7ty6YG BEWC/zsVi3USrX8TPMIJORt1HjVdQ8wm0awfGhU75DHC+bzKLv2Pxo7DPZFXUiag JSXlcp6IiGIoWrBnrXqiM0kz6dUuNp2aFwuI/bvCNcfsBGNEoxsLSGLFixA8Rgcs EMtui0V5a5YHEV3xQKDtk996XiPdzJAYqkjGmo0XVBH+7p03Oi/oa2sIiDvEXZEw 0/VtDKFKHt/k+FPnzT5tBBxt+ctozZVXtEN0qVDXHOZXLDpmMEE= =REaP -----END PGP SIGNATURE----- --OrjCzxTzp8Dj0pJY--