Skip to content

Commit 95c4571

Browse files
Have BufFileSize() ereport() on FileSize() failure.
Move the responsibility for checking for and reporting a failure from the only current BufFileSize() caller, logtape.c, to BufFileSize() itself. Code within buffile.c is generally responsible for interfacing with fd.c to report irrecoverable failures. This seems like a convention that's worth sticking to. Reorganizing things this way makes it easy to make the error message raised in the event of BufFileSize() failure descriptive of the underlying problem. We're now clear on the distinction between temporary file name and BufFile name, and can show errno, confident that its value actually relates to the error being reported. In passing, an existing, similar buffile.c ereport() + errcode_for_file_access() site is changed to follow the same conventions. The API of the function BufFileSize() is changed by this commit, despite already being in a stable release (Postgres 11). This seems acceptable, since the BufFileSize() ABI was changed by commit aa55183, which hasn't made it into a point release yet. Besides, it's difficult to imagine a third party BufFileSize() caller not just raising an error anyway, since BufFile state should be considered corrupt when BufFileSize() fails. Per complaint from Tom Lane. Discussion: https://postgr.es/m/26974.1540826748@sss.pgh.pa.us Backpatch: 11-, where shared BufFiles were introduced.
1 parent 48cf918 commit 95c4571

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/backend/storage/file/buffile.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name)
314314
if (nfiles == 0)
315315
ereport(ERROR,
316316
(errcode_for_file_access(),
317-
errmsg("could not open BufFile \"%s\"", name)));
317+
errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
318+
segment_name, name)));
318319

319320
file = makeBufFileCommon(nfiles);
320321
file->files = files;
@@ -793,20 +794,26 @@ BufFileTellBlock(BufFile *file)
793794
#endif
794795

795796
/*
796-
* Return the current file size.
797+
* Return the current shared BufFile size.
797798
*
798799
* Counts any holes left behind by BufFileAppend as part of the size.
799-
* Returns -1 on error.
800+
* ereport()s on failure.
800801
*/
801802
int64
802803
BufFileSize(BufFile *file)
803804
{
804805
int64 lastFileSize;
805806

807+
Assert(file->fileset != NULL);
808+
806809
/* Get the size of the last physical file by seeking to end. */
807810
lastFileSize = FileSeek(file->files[file->numFiles - 1], 0, SEEK_END);
808811
if (lastFileSize < 0)
809-
return -1;
812+
ereport(ERROR,
813+
(errcode_for_file_access(),
814+
errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
815+
FilePathName(file->files[file->numFiles - 1]),
816+
file->name)));
810817
file->offsets[file->numFiles - 1] = lastFileSize;
811818

812819
return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +

src/backend/utils/sort/logtape.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,6 @@ ltsConcatWorkerTapes(LogicalTapeSet *lts, TapeShare *shared,
433433
pg_itoa(i, filename);
434434
file = BufFileOpenShared(fileset, filename);
435435
filesize = BufFileSize(file);
436-
if (filesize < 0)
437-
ereport(ERROR,
438-
(errcode_for_file_access(),
439-
errmsg("could not determine size of temporary file \"%s\"", filename)));
440436

441437
/*
442438
* Stash first BufFile, and concatenate subsequent BufFiles to that.

0 commit comments

Comments
 (0)