summaryrefslogtreecommitdiff
path: root/src/backend/replication
diff options
context:
space:
mode:
authorPeter Eisentraut <peter@eisentraut.org>2023-01-16 09:20:44 +0100
committerPeter Eisentraut <peter@eisentraut.org>2023-01-16 11:01:31 +0100
commit20428d344a2964de6aaef9984fcd472f3c65d115 (patch)
tree12ec494bd3dd561e56417915bcf97da45eb1cb17 /src/backend/replication
parent1561612e3bf3264c31618b9455d0c1003b9271ec (diff)
downloadpostgresql-20428d344a2964de6aaef9984fcd472f3c65d115.tar.gz
Add BufFileRead variants with short read and EOF detection
Most callers of BufFileRead() want to check whether they read the full specified length. Checking this at every call site is very tedious. This patch provides additional variants BufFileReadExact() and BufFileReadMaybeEOF() that include the length checks. I considered changing BufFileRead() itself, but this function is also used in extensions, and so changing the behavior like this would create a lot of problems there. The new names are analogous to the existing LogicalTapeReadExact(). Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/f3501945-c591-8cc3-5ef0-b72a2e0eaa9c@enterprisedb.com
Diffstat (limited to 'src/backend/replication')
-rw-r--r--src/backend/replication/logical/worker.c32
1 files changed, 4 insertions, 28 deletions
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index f3856c9843..d8b8a374c6 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -2069,19 +2069,13 @@ apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
CHECK_FOR_INTERRUPTS();
/* read length of the on-disk record */
- nbytes = BufFileRead(stream_fd, &len, sizeof(len));
+ nbytes = BufFileReadMaybeEOF(stream_fd, &len, sizeof(len), true);
/* have we reached end of the file? */
if (nbytes == 0)
break;
/* do we have a correct length? */
- if (nbytes != sizeof(len))
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not read from streaming transaction's changes file \"%s\": read only %zu of %zu bytes",
- path, nbytes, sizeof(len))));
-
if (len <= 0)
elog(ERROR, "incorrect length %d in streaming transaction's changes file \"%s\"",
len, path);
@@ -2090,12 +2084,7 @@ apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
buffer = repalloc(buffer, len);
/* and finally read the data into the buffer */
- nbytes = BufFileRead(stream_fd, buffer, len);
- if (nbytes != len)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not read from streaming transaction's changes file \"%s\": read only %zu of %zu bytes",
- path, nbytes, (size_t) len)));
+ BufFileReadExact(stream_fd, buffer, len);
BufFileTell(stream_fd, &fileno, &offset);
@@ -3993,7 +3982,6 @@ static void
subxact_info_read(Oid subid, TransactionId xid)
{
char path[MAXPGPATH];
- size_t nread;
Size len;
BufFile *fd;
MemoryContext oldctx;
@@ -4013,12 +4001,7 @@ subxact_info_read(Oid subid, TransactionId xid)
return;
/* read number of subxact items */
- nread = BufFileRead(fd, &subxact_data.nsubxacts, sizeof(subxact_data.nsubxacts));
- if (nread != sizeof(subxact_data.nsubxacts))
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not read from streaming transaction's subxact file \"%s\": read only %zu of %zu bytes",
- path, nread, sizeof(subxact_data.nsubxacts))));
+ BufFileReadExact(fd, &subxact_data.nsubxacts, sizeof(subxact_data.nsubxacts));
len = sizeof(SubXactInfo) * subxact_data.nsubxacts;
@@ -4037,14 +4020,7 @@ subxact_info_read(Oid subid, TransactionId xid)
MemoryContextSwitchTo(oldctx);
if (len > 0)
- {
- nread = BufFileRead(fd, subxact_data.subxacts, len);
- if (nread != len)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not read from streaming transaction's subxact file \"%s\": read only %zu of %zu bytes",
- path, nread, len)));
- }
+ BufFileReadExact(fd, subxact_data.subxacts, len);
BufFileClose(fd);
}