summaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2020-06-16 13:50:56 +1200
committerThomas Munro <tmunro@postgresql.org>2020-06-16 17:01:07 +1200
commit02b71f06be751ff9809f48742f15e1905a1a6d83 (patch)
tree6246b8022a18271a1f93aff445a931ad30a3fe17 /src/backend/utils
parent5c1bfd627fdc0b9ab07c9ee88b4677147919bf1b (diff)
Fix buffile.c error handling.
Convert buffile.c error handling to use ereport. This fixes cases where I/O errors were indistinguishable from EOF or not reported. Also remove "%m" from error messages where errno would be bogus. While we're modifying those strings, add block numbers and short read byte counts where appropriate. Back-patch to all supported releases. Reported-by: Amit Khandekar <amitdkhan.pg@gmail.com> Reviewed-by: Melanie Plageman <melanieplageman@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@2ndquadrant.com> Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Ibrar Ahmed <ibrar.ahmad@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CA%2BhUKGJE04G%3D8TLK0DLypT_27D9dR8F1RQgNp0jK6qR0tZGWOw%40mail.gmail.com
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/sort/logtape.c19
-rw-r--r--src/backend/utils/sort/tuplestore.c56
2 files changed, 38 insertions, 37 deletions
diff --git a/src/backend/utils/sort/logtape.c b/src/backend/utils/sort/logtape.c
index 774520752f0..7396b15f5ac 100644
--- a/src/backend/utils/sort/logtape.c
+++ b/src/backend/utils/sort/logtape.c
@@ -202,12 +202,12 @@ static void ltsDumpBuffer(LogicalTapeSet *lts, LogicalTape *lt);
static void
ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
{
- if (BufFileSeekBlock(lts->pfile, blocknum) != 0 ||
- BufFileWrite(lts->pfile, buffer, BLCKSZ) != BLCKSZ)
+ if (BufFileSeekBlock(lts->pfile, blocknum) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not write block %ld of temporary file: %m",
+ errmsg("could not seek to block %ld of temporary file",
blocknum)));
+ BufFileWrite(lts->pfile, buffer, BLCKSZ);
}
/*
@@ -219,12 +219,19 @@ ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
static void
ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
{
- if (BufFileSeekBlock(lts->pfile, blocknum) != 0 ||
- BufFileRead(lts->pfile, buffer, BLCKSZ) != BLCKSZ)
+ size_t nread;
+
+ if (BufFileSeekBlock(lts->pfile, blocknum) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not read block %ld of temporary file: %m",
+ errmsg("could not seek to block %ld of temporary file",
blocknum)));
+ nread = BufFileRead(lts->pfile, buffer, BLCKSZ);
+ if (nread != BLCKSZ)
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not read block %ld of temporary file: read only %zu of %zu bytes",
+ blocknum, nread, (size_t) BLCKSZ)));
}
/*
diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c
index 1347fc4520a..c77d3f85fe3 100644
--- a/src/backend/utils/sort/tuplestore.c
+++ b/src/backend/utils/sort/tuplestore.c
@@ -512,7 +512,7 @@ tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
}
else
{
@@ -522,7 +522,7 @@ tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
}
break;
default:
@@ -849,7 +849,7 @@ tuplestore_puttuple_common(Tuplestorestate *state, void *tuple)
SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
state->status = TSS_WRITEFILE;
/*
@@ -953,7 +953,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
state->status = TSS_READFILE;
/* FALL THRU into READFILE case */
@@ -1017,7 +1017,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
SEEK_CUR) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
Assert(!state->truncated);
return NULL;
}
@@ -1034,7 +1034,7 @@ tuplestore_gettuple(Tuplestorestate *state, bool forward,
SEEK_CUR) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
tup = READTUP(state, tuplen);
return tup;
@@ -1236,7 +1236,7 @@ tuplestore_rescan(Tuplestorestate *state)
if (BufFileSeek(state->myfile, 0, 0L, SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
break;
default:
elog(ERROR, "invalid tuplestore state");
@@ -1301,7 +1301,7 @@ tuplestore_copy_read_pointer(Tuplestorestate *state,
SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
}
else
{
@@ -1310,7 +1310,7 @@ tuplestore_copy_read_pointer(Tuplestorestate *state,
SEEK_SET) != 0)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not seek in tuplestore temporary file: %m")));
+ errmsg("could not seek in tuplestore temporary file")));
}
}
else if (srcptr == state->activeptr)
@@ -1457,7 +1457,8 @@ getlen(Tuplestorestate *state, bool eofOK)
if (nbytes != 0 || !eofOK)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not read from tuplestore temporary file: %m")));
+ errmsg("could not read from tuplestore temporary file: read only %zu of %zu bytes",
+ nbytes, sizeof(len))));
return 0;
}
@@ -1494,22 +1495,10 @@ writetup_heap(Tuplestorestate *state, void *tup)
/* total on-disk footprint: */
unsigned int tuplen = tupbodylen + sizeof(int);
- if (BufFileWrite(state->myfile, (void *) &tuplen,
- sizeof(tuplen)) != sizeof(tuplen))
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not write to tuplestore temporary file: %m")));
- if (BufFileWrite(state->myfile, (void *) tupbody,
- tupbodylen) != (size_t) tupbodylen)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not write to tuplestore temporary file: %m")));
+ BufFileWrite(state->myfile, (void *) &tuplen, sizeof(tuplen));
+ BufFileWrite(state->myfile, (void *) tupbody, tupbodylen);
if (state->backward) /* need trailing length word? */
- if (BufFileWrite(state->myfile, (void *) &tuplen,
- sizeof(tuplen)) != sizeof(tuplen))
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not write to tuplestore temporary file: %m")));
+ BufFileWrite(state->myfile, (void *) &tuplen, sizeof(tuplen));
FREEMEM(state, GetMemoryChunkSpace(tuple));
heap_free_minimal_tuple(tuple);
@@ -1522,20 +1511,25 @@ readtup_heap(Tuplestorestate *state, unsigned int len)
unsigned int tuplen = tupbodylen + MINIMAL_TUPLE_DATA_OFFSET;
MinimalTuple tuple = (MinimalTuple) palloc(tuplen);
char *tupbody = (char *) tuple + MINIMAL_TUPLE_DATA_OFFSET;
+ size_t nread;
USEMEM(state, GetMemoryChunkSpace(tuple));
/* read in the tuple proper */
tuple->t_len = tuplen;
- if (BufFileRead(state->myfile, (void *) tupbody,
- tupbodylen) != (size_t) tupbodylen)
+ nread = BufFileRead(state->myfile, (void *) tupbody, tupbodylen);
+ if (nread != (size_t) tupbodylen)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not read from tuplestore temporary file: %m")));
+ errmsg("could not read from tuplestore temporary file: read only %zu of %zu bytes",
+ nread, (size_t) tupbodylen)));
if (state->backward) /* need trailing length word? */
- if (BufFileRead(state->myfile, (void *) &tuplen,
- sizeof(tuplen)) != sizeof(tuplen))
+ {
+ nread = BufFileRead(state->myfile, (void *) &tuplen, sizeof(tuplen));
+ if (nread != sizeof(tuplen))
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not read from tuplestore temporary file: %m")));
+ errmsg("could not read from tuplestore temporary file: read only %zu of %zu bytes",
+ nread, sizeof(tuplen))));
+ }
return (void *) tuple;
}