agora inbox for pgsql-hackers@postgresql.orghelp / color / mirror / Atom feed
[PATCH 4/5] Use only xlogreader.c:XLogRead() 8+ messages / 1 participants [nested] [flat]
* [PATCH 4/5] Use only xlogreader.c:XLogRead() @ 2019-07-09 09:54 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-07-09 09:54 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 128 ++++++++++++++++++++++++++++++++ src/backend/access/transam/xlogutils.c | 40 ++++++++-- src/backend/replication/walsender.c | 125 ++++++++++++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 59 ++++++++++++++- src/include/access/xlogreader.h | 19 +++++ 5 files changed, 359 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index bbe383240d..3f8db2f25d 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -26,6 +28,7 @@ #include "replication/origin.h" #ifndef FRONTEND +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -1009,7 +1012,132 @@ XLogSegmentInit(XLogSegment *seg, int size) seg->tli = 0; seg->dir = NULL; seg->size = size; + seg->last_req = 0; +} + +/* + * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'. If + * tli is passed, get the data from timeline *tli. 'pos' is the current + * position in the XLOG file and openSegment is a callback that opens the next + * segment for reading. + * + * Returns true if the call succeeded, false if it failed. Caller should check + * errno in the case of failure. seg->last_req might also be useful for error + * messages. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +bool +XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli, XLogSegment *seg, XLogOpenSegment openSegment) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int readbytes; + + seg->off = XLogSegmentOffset(recptr, seg->size); + + if (seg->file < 0 || + !XLByteInSeg(recptr, seg->num, seg->size) || + (tli != NULL && *tli != seg->tli)) + { + XLogSegNo nextSegNo; + + /* Switch to another logfile segment */ + if (seg->file >= 0) + close(seg->file); + + XLByteToSeg(recptr, nextSegNo, seg->size); + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, tli, seg); + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set both "num" and "off". However we need to care + * about them too because the function can also be used directly, + * see walsender.c. + */ + seg->num = nextSegNo; + seg->off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (seg->size - seg->off)) + seg->last_req = seg->size - seg->off; + else + seg->last_req = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->file, p, seg->last_req, seg->off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + return false; + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set this field. However we need to care about it too + * because the function can also be used directly (see walsender.c). + */ + seg->off += readbytes; + } + + return true; +} + +#ifndef FRONTEND +/* + * Backend-specific code to handle errors encountered by XLogRead(). + */ +void +XLogReadProcessError(XLogSegment *seg) +{ + if (errno != 0) + { + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) seg->last_req))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) seg->last_req))); + } } +#endif /* ---------------------------------------- * Functions for decoding the data and block references in a record. diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 836c2e2927..899bf1b551 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,35 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_open_segment(XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg) +{ + char path[MAXPGPATH]; + + XLogFilePath(path, *tli, nextSegNo, seg->size); + seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (seg->file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + seg->tli = *tli; +} + /* * read_page callback for reading local xlog files * @@ -1022,10 +1051,9 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->seg.size, state->seg.tli, targetPagePtr, - XLOG_BLCKSZ); - state->seg.tli = pageTLI; - + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, &pageTLI, + &state->seg, read_local_xlog_page_open_segment)) + XLogReadProcessError(&state->seg); /* number of valid bytes in the buffer */ return count; } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 3b5fe2cb94..c64a4bf9f8 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -247,7 +247,9 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(char *buf, XLogRecPtr startptr, Size count); +static void WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg); +static void XLogReadOld(char *buf, XLogRecPtr startptr, Size count); /* Initialize walsender process before entering the main command loop */ @@ -782,7 +784,9 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ); + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, NULL, sendSeg, + WalSndOpenSegment)) + XLogReadProcessError(sendSeg); return count; } @@ -2359,7 +2363,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2532,6 +2536,76 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +{ + char path[MAXPGPATH]; + + /* + * The timeline is determined below, caller should not do anything about + * it. + */ + Assert(tli == NULL); + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + seg->tli = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, seg->size); + if (seg->num == endSegNo) + seg->tli = sendTimeLineNextTLI; + } + + XLogFilePath(path, seg->tli, nextSegNo, seg->size); + seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (seg->file < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(seg->tli, seg->num)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2549,6 +2623,7 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2764,7 +2839,49 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(&output_message.data[output_message.len], startptr, nbytes); + +retry: + if (!XLogRead(&output_message.data[output_message.len], startptr, nbytes, + NULL, /* WalSndOpenSegment will determine TLI */ + sendSeg, + WalSndOpenSegment)) + XLogReadProcessError(sendSeg); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(startptr, segno, wal_segment_size); + CheckXLogRemoved(segno, ThisTimeLineID); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->file >= 0) + { + close(sendSeg->file); + sendSeg->file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index a16793bb8b..3e09519f88 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -296,6 +296,45 @@ identify_target_directory(XLogDumpPrivate *private, char *directory, fatal_error("could not find any WAL file"); } +static void +XLogDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +{ + char fname[MAXPGPATH]; + int tries; + + XLogFileName(fname, *tli, nextSegNo, seg->size); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + seg->file = open_file_in_directory(seg->dir, fname); + if (seg->file >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (seg->file < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); + seg->tli = *tli; +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -441,8 +480,24 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(private->inpath, private->timeline, targetPagePtr, - readBuff, count); + if (!XLogRead(readBuff, targetPagePtr, count, &private->timeline, + &state->seg, XLogDumpOpenSegment)) + { + XLogSegment *seg = &state->seg; + int err = errno; + char fname[MAXPGPATH]; + int save_errno = errno; + + XLogFileName(fname, seg->tli, seg->num, seg->size); + errno = save_errno; + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->off, (Size) seg->last_req, strerror(err)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->off, (Size) seg->last_req); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index 26da3613f7..856e1b2e00 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -39,6 +39,7 @@ typedef struct XLogSegment char *dir; /* directory (only needed by frontends) */ int size; /* segment size */ + int last_req; /* the amount of data requested last time */ } XLogSegment; typedef struct XLogReaderState XLogReaderState; @@ -221,7 +222,25 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Callback to open the specified XLOG segment nextSegNo in timeline *tli for + * reading, and assign the descriptor to ->file. BasicOpenFile() is the + * preferred way to open the segment file in backend code, whereas open(2) + * should be used in frontend. + * + * If NULL is passed for tli, the callback must determine the timeline + * itself. In any case it's supposed to eventually set ->tli. + */ +typedef void (*XLogOpenSegment) (XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg); + extern void XLogSegmentInit(XLogSegment *seg, int size); +extern bool XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli, XLogSegment *seg, + XLogOpenSegment openSegment); +#ifndef FRONTEND +void XLogReadProcessError(XLogSegment *seg); +#endif /* Functions for decoding an XLogRecord */ -- 2.16.4 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v04-0005-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 3/4] Use only xlogreader.c:XLogRead() @ 2019-09-09 09:53 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-09-09 09:53 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 128 ++++++++++++++++++++++++ src/backend/access/transam/xlogutils.c | 40 ++++++-- src/backend/replication/walsender.c | 125 ++++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 59 ++++++++++- src/include/access/xlogreader.h | 19 ++++ 5 files changed, 359 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index 7b4ec81493..2a80bc5823 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -27,6 +29,7 @@ #ifndef FRONTEND #include "miscadmin.h" +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -1011,7 +1014,132 @@ XLogSegmentInit(XLogSegment *seg, int size) seg->tli = 0; seg->dir = NULL; seg->size = size; + seg->last_req = 0; +} + +/* + * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'. If + * tli is passed, get the data from timeline *tli. 'pos' is the current + * position in the XLOG file and openSegment is a callback that opens the next + * segment for reading. + * + * Returns true if the call succeeded, false if it failed. Caller should check + * errno in the case of failure. seg->last_req might also be useful for error + * messages. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +bool +XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli, XLogSegment *seg, XLogOpenSegment openSegment) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int readbytes; + + seg->off = XLogSegmentOffset(recptr, seg->size); + + if (seg->file < 0 || + !XLByteInSeg(recptr, seg->num, seg->size) || + (tli != NULL && *tli != seg->tli)) + { + XLogSegNo nextSegNo; + + /* Switch to another logfile segment */ + if (seg->file >= 0) + close(seg->file); + + XLByteToSeg(recptr, nextSegNo, seg->size); + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, tli, seg); + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set both "num" and "off". However we need to care + * about them too because the function can also be used directly, + * see walsender.c. + */ + seg->num = nextSegNo; + seg->off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (seg->size - seg->off)) + seg->last_req = seg->size - seg->off; + else + seg->last_req = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->file, p, seg->last_req, seg->off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + return false; + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set this field. However we need to care about it too + * because the function can also be used directly (see walsender.c). + */ + seg->off += readbytes; + } + + return true; +} + +#ifndef FRONTEND +/* + * Backend-specific code to handle errors encountered by XLogRead(). + */ +void +XLogReadProcessError(XLogSegment *seg) +{ + if (errno != 0) + { + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) seg->last_req))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) seg->last_req))); + } } +#endif /* ---------------------------------------- * Functions for decoding the data and block references in a record. diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 424bb06919..83b014e04c 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,35 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_open_segment(XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg) +{ + char path[MAXPGPATH]; + + XLogFilePath(path, *tli, nextSegNo, seg->size); + seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (seg->file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + seg->tli = *tli; +} + /* * read_page callback for reading local xlog files * @@ -1022,10 +1051,9 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->seg.size, state->seg.tli, targetPagePtr, - XLOG_BLCKSZ); - state->seg.tli = pageTLI; - + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, &pageTLI, + &state->seg, read_local_xlog_page_open_segment)) + XLogReadProcessError(&state->seg); /* number of valid bytes in the buffer */ return count; } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index f5630a63cf..0685c320b4 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -247,7 +247,9 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(char *buf, XLogRecPtr startptr, Size count); +static void WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg); +static void XLogReadOld(char *buf, XLogRecPtr startptr, Size count); /* Initialize walsender process before entering the main command loop */ @@ -782,7 +784,9 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ); + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, NULL, sendSeg, + WalSndOpenSegment)) + XLogReadProcessError(sendSeg); return count; } @@ -2359,7 +2363,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2532,6 +2536,76 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +{ + char path[MAXPGPATH]; + + /* + * The timeline is determined below, caller should not do anything about + * it. + */ + Assert(tli == NULL); + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + seg->tli = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, seg->size); + if (seg->num == endSegNo) + seg->tli = sendTimeLineNextTLI; + } + + XLogFilePath(path, seg->tli, nextSegNo, seg->size); + seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (seg->file < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(seg->tli, seg->num)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2549,6 +2623,7 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2764,7 +2839,49 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(&output_message.data[output_message.len], startptr, nbytes); + +retry: + if (!XLogRead(&output_message.data[output_message.len], startptr, nbytes, + NULL, /* WalSndOpenSegment will determine TLI */ + sendSeg, + WalSndOpenSegment)) + XLogReadProcessError(sendSeg); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(startptr, segno, wal_segment_size); + CheckXLogRemoved(segno, ThisTimeLineID); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->file >= 0) + { + close(sendSeg->file); + sendSeg->file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index a16793bb8b..3e09519f88 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -296,6 +296,45 @@ identify_target_directory(XLogDumpPrivate *private, char *directory, fatal_error("could not find any WAL file"); } +static void +XLogDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +{ + char fname[MAXPGPATH]; + int tries; + + XLogFileName(fname, *tli, nextSegNo, seg->size); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + seg->file = open_file_in_directory(seg->dir, fname); + if (seg->file >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (seg->file < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); + seg->tli = *tli; +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -441,8 +480,24 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(private->inpath, private->timeline, targetPagePtr, - readBuff, count); + if (!XLogRead(readBuff, targetPagePtr, count, &private->timeline, + &state->seg, XLogDumpOpenSegment)) + { + XLogSegment *seg = &state->seg; + int err = errno; + char fname[MAXPGPATH]; + int save_errno = errno; + + XLogFileName(fname, seg->tli, seg->num, seg->size); + errno = save_errno; + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->off, (Size) seg->last_req, strerror(err)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->off, (Size) seg->last_req); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index c2724fff74..4731023ccc 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -43,6 +43,7 @@ typedef struct XLogSegment char *dir; /* directory (only needed by frontends) */ int size; /* segment size */ + int last_req; /* the amount of data requested last time */ } XLogSegment; typedef struct XLogReaderState XLogReaderState; @@ -225,7 +226,25 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Callback to open the specified XLOG segment nextSegNo in timeline *tli for + * reading, and assign the descriptor to ->file. BasicOpenFile() is the + * preferred way to open the segment file in backend code, whereas open(2) + * should be used in frontend. + * + * If NULL is passed for tli, the callback must determine the timeline + * itself. In any case it's supposed to eventually set ->tli. + */ +typedef void (*XLogOpenSegment) (XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg); + extern void XLogSegmentInit(XLogSegment *seg, int size); +extern bool XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli, XLogSegment *seg, + XLogOpenSegment openSegment); +#ifndef FRONTEND +void XLogReadProcessError(XLogSegment *seg); +#endif /* Functions for decoding an XLogRecord */ -- 2.22.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v05-0005-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 3/4] Use only xlogreader.c:XLogRead() @ 2019-09-09 09:53 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-09-09 09:53 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 128 ++++++++++++++++++++++++ src/backend/access/transam/xlogutils.c | 40 ++++++-- src/backend/replication/walsender.c | 125 ++++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 59 ++++++++++- src/include/access/xlogreader.h | 19 ++++ 5 files changed, 359 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index 7b4ec81493..2a80bc5823 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -27,6 +29,7 @@ #ifndef FRONTEND #include "miscadmin.h" +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -1011,7 +1014,132 @@ XLogSegmentInit(XLogSegment *seg, int size) seg->tli = 0; seg->dir = NULL; seg->size = size; + seg->last_req = 0; +} + +/* + * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'. If + * tli is passed, get the data from timeline *tli. 'pos' is the current + * position in the XLOG file and openSegment is a callback that opens the next + * segment for reading. + * + * Returns true if the call succeeded, false if it failed. Caller should check + * errno in the case of failure. seg->last_req might also be useful for error + * messages. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +bool +XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli, XLogSegment *seg, XLogOpenSegment openSegment) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int readbytes; + + seg->off = XLogSegmentOffset(recptr, seg->size); + + if (seg->file < 0 || + !XLByteInSeg(recptr, seg->num, seg->size) || + (tli != NULL && *tli != seg->tli)) + { + XLogSegNo nextSegNo; + + /* Switch to another logfile segment */ + if (seg->file >= 0) + close(seg->file); + + XLByteToSeg(recptr, nextSegNo, seg->size); + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, tli, seg); + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set both "num" and "off". However we need to care + * about them too because the function can also be used directly, + * see walsender.c. + */ + seg->num = nextSegNo; + seg->off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (seg->size - seg->off)) + seg->last_req = seg->size - seg->off; + else + seg->last_req = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->file, p, seg->last_req, seg->off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + return false; + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set this field. However we need to care about it too + * because the function can also be used directly (see walsender.c). + */ + seg->off += readbytes; + } + + return true; +} + +#ifndef FRONTEND +/* + * Backend-specific code to handle errors encountered by XLogRead(). + */ +void +XLogReadProcessError(XLogSegment *seg) +{ + if (errno != 0) + { + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) seg->last_req))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) seg->last_req))); + } } +#endif /* ---------------------------------------- * Functions for decoding the data and block references in a record. diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 424bb06919..83b014e04c 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,35 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_open_segment(XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg) +{ + char path[MAXPGPATH]; + + XLogFilePath(path, *tli, nextSegNo, seg->size); + seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (seg->file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + seg->tli = *tli; +} + /* * read_page callback for reading local xlog files * @@ -1022,10 +1051,9 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->seg.size, state->seg.tli, targetPagePtr, - XLOG_BLCKSZ); - state->seg.tli = pageTLI; - + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, &pageTLI, + &state->seg, read_local_xlog_page_open_segment)) + XLogReadProcessError(&state->seg); /* number of valid bytes in the buffer */ return count; } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index f5630a63cf..0685c320b4 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -247,7 +247,9 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(char *buf, XLogRecPtr startptr, Size count); +static void WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg); +static void XLogReadOld(char *buf, XLogRecPtr startptr, Size count); /* Initialize walsender process before entering the main command loop */ @@ -782,7 +784,9 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ); + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, NULL, sendSeg, + WalSndOpenSegment)) + XLogReadProcessError(sendSeg); return count; } @@ -2359,7 +2363,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2532,6 +2536,76 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +{ + char path[MAXPGPATH]; + + /* + * The timeline is determined below, caller should not do anything about + * it. + */ + Assert(tli == NULL); + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + seg->tli = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, seg->size); + if (seg->num == endSegNo) + seg->tli = sendTimeLineNextTLI; + } + + XLogFilePath(path, seg->tli, nextSegNo, seg->size); + seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (seg->file < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(seg->tli, seg->num)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2549,6 +2623,7 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2764,7 +2839,49 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(&output_message.data[output_message.len], startptr, nbytes); + +retry: + if (!XLogRead(&output_message.data[output_message.len], startptr, nbytes, + NULL, /* WalSndOpenSegment will determine TLI */ + sendSeg, + WalSndOpenSegment)) + XLogReadProcessError(sendSeg); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(startptr, segno, wal_segment_size); + CheckXLogRemoved(segno, ThisTimeLineID); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->file >= 0) + { + close(sendSeg->file); + sendSeg->file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index a16793bb8b..3e09519f88 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -296,6 +296,45 @@ identify_target_directory(XLogDumpPrivate *private, char *directory, fatal_error("could not find any WAL file"); } +static void +XLogDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +{ + char fname[MAXPGPATH]; + int tries; + + XLogFileName(fname, *tli, nextSegNo, seg->size); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + seg->file = open_file_in_directory(seg->dir, fname); + if (seg->file >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (seg->file < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); + seg->tli = *tli; +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -441,8 +480,24 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(private->inpath, private->timeline, targetPagePtr, - readBuff, count); + if (!XLogRead(readBuff, targetPagePtr, count, &private->timeline, + &state->seg, XLogDumpOpenSegment)) + { + XLogSegment *seg = &state->seg; + int err = errno; + char fname[MAXPGPATH]; + int save_errno = errno; + + XLogFileName(fname, seg->tli, seg->num, seg->size); + errno = save_errno; + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->off, (Size) seg->last_req, strerror(err)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->off, (Size) seg->last_req); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index c2724fff74..4731023ccc 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -43,6 +43,7 @@ typedef struct XLogSegment char *dir; /* directory (only needed by frontends) */ int size; /* segment size */ + int last_req; /* the amount of data requested last time */ } XLogSegment; typedef struct XLogReaderState XLogReaderState; @@ -225,7 +226,25 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Callback to open the specified XLOG segment nextSegNo in timeline *tli for + * reading, and assign the descriptor to ->file. BasicOpenFile() is the + * preferred way to open the segment file in backend code, whereas open(2) + * should be used in frontend. + * + * If NULL is passed for tli, the callback must determine the timeline + * itself. In any case it's supposed to eventually set ->tli. + */ +typedef void (*XLogOpenSegment) (XLogSegNo nextSegNo, TimeLineID *tli, + XLogSegment *seg); + extern void XLogSegmentInit(XLogSegment *seg, int size); +extern bool XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli, XLogSegment *seg, + XLogOpenSegment openSegment); +#ifndef FRONTEND +void XLogReadProcessError(XLogSegment *seg); +#endif /* Functions for decoding an XLogRecord */ -- 2.22.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v05-0005-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 5/6] Use only xlogreader.c:XLogRead() @ 2019-09-23 05:40 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-09-23 05:40 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 157 ++++++++++++++++++++++++ src/backend/access/transam/xlogutils.c | 46 ++++++- src/backend/replication/walsender.c | 139 ++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 64 +++++++++- src/include/access/xlogreader.h | 42 +++++++ 5 files changed, 436 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index 4de5530b3e..7f77fa95cb 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -27,6 +29,7 @@ #ifndef FRONTEND #include "miscadmin.h" +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -1015,6 +1018,160 @@ WALOpenSegmentInit(WALOpenSegment *seg, int size) #endif } +/* + * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'. If + * tli_p is passed, get the data from timeline *tli_p. 'pos' is the current + * position in the XLOG file and openSegment is a callback that opens the next + * segment for reading. + * + * Returns error information if the data could not be read or NULL if + * succeeded. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +XLogReadError * +XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli_p, WALOpenSegment *seg, WALSegmentOpen openSegment) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int segbytes; + int readbytes; + + seg->off = XLogSegmentOffset(recptr, seg->size); + + if (seg->file < 0 || + !XLByteInSeg(recptr, seg->num, seg->size) || + (tli_p != NULL && *tli_p != seg->tli)) + { + XLogSegNo nextSegNo; + TimeLineID tli = InvalidTimeLineID; + int file; + + /* Switch to another logfile segment */ + if (seg->file >= 0) + close(seg->file); + + XLByteToSeg(recptr, nextSegNo, seg->size); + + /* If we have the TLI, let's pass it to the callback. */ + if (tli_p != NULL) + tli = *tli_p; + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, &tli, &file, seg); + + /* + * If we passed InvalidTimeLineID, the callback should have + * determined the correct TLI and returned it. + */ + Assert(tli != InvalidTimeLineID); + + /* Update the open segment info. */ + seg->tli = tli; + seg->file = file; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set both "num" and "off". However we need to care + * about them too because the function can also be used directly, + * see walsender.c. + */ + seg->num = nextSegNo; + seg->off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (seg->size - seg->off)) + segbytes = seg->size - seg->off; + else + segbytes = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->file, p, segbytes, seg->off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + { + XLogReadError *errinfo; + + errinfo = (XLogReadError *) palloc(sizeof(XLogReadError)); + errinfo->read_errno = errno; + errinfo->readbytes = readbytes; + errinfo->reqbytes = segbytes; + errinfo->seg = seg; + + return errinfo; + } + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set this field. However we need to care about it too + * because the function can also be used directly (see walsender.c). + */ + seg->off += readbytes; + } + + return NULL; +} + +#ifndef FRONTEND +/* + * Backend-specific convenience code to handle read errors encountered by + * XLogRead(). + */ +void +XLogReadProcessError(XLogReadError *errinfo) +{ + WALOpenSegment *seg = errinfo->seg; + + if (errinfo->readbytes < 0) + { + errno = errinfo->read_errno; + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) errinfo->reqbytes))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) errinfo->reqbytes))); + } +} +#endif + /* ---------------------------------------- * Functions for decoding the data and block references in a record. * ---------------------------------------- diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 424bb06919..38c3196168 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,39 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_segment_open(XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg) +{ + TimeLineID tli = *tli_p; + char path[MAXPGPATH]; + int file; + + Assert(tli != InvalidTimeLineID); + + XLogFilePath(path, tli, nextSegNo, seg->size); + file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + *file_p = file; +} + /* * read_page callback for reading local xlog files * @@ -915,6 +948,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, loc; int count; TimeLineID pageTLI; + XLogReadError *errinfo; loc = targetPagePtr + reqLen; @@ -1022,10 +1056,10 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->seg.size, state->seg.tli, targetPagePtr, - XLOG_BLCKSZ); - state->seg.tli = pageTLI; - + if ((errinfo = XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, &pageTLI, + &state->seg, + read_local_xlog_page_segment_open)) != NULL) + XLogReadProcessError(errinfo); /* number of valid bytes in the buffer */ return count; } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index a617e20ab6..a7b3e0ecbe 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -247,9 +247,12 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(char *buf, XLogRecPtr startptr, Size count); +static void WalSndSegmentOpen(XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg); +static void XLogReadOld(char *buf, XLogRecPtr startptr, Size count); + /* Initialize walsender process before entering the main command loop */ void InitWalSender(void) @@ -763,6 +766,7 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req { XLogRecPtr flushptr; int count; + XLogReadError *errinfo; XLogReadDetermineTimeline(state, targetPagePtr, reqLen); sendTimeLineIsHistoric = (state->currTLI != ThisTimeLineID); @@ -783,7 +787,13 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ); + if ((errinfo = XLogRead(cur_page, + targetPagePtr, + XLOG_BLCKSZ, + NULL, /* WalSndSegmentOpen will determine TLI */ + sendSeg, + WalSndSegmentOpen)) != NULL) + XLogReadProcessError(errinfo); return count; } @@ -2360,7 +2370,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2533,6 +2543,81 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndSegmentOpen(XLogSegNo nextSegNo, TimeLineID *tli_p, int *file_p, + WALOpenSegment *seg) +{ + TimeLineID tli = *tli_p; + char path[MAXPGPATH]; + int file; + + /* + * The timeline is determined below, caller should not pass it. + */ + Assert(tli == InvalidTimeLineID); + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + tli = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, seg->size); + if (seg->num == endSegNo) + tli = sendTimeLineNextTLI; + } + + XLogFilePath(path, tli, nextSegNo, seg->size); + file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (file < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(tli, nextSegNo)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + *file_p = file; + *tli_p = tli; +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2550,6 +2635,8 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; + XLogReadError *errinfo; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2765,7 +2852,51 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(&output_message.data[output_message.len], startptr, nbytes); + +retry: + if ((errinfo = XLogRead(&output_message.data[output_message.len], + startptr, + nbytes, + NULL, /* WalSndSegmentOpen will determine TLI */ + sendSeg, + WalSndSegmentOpen)) != NULL) + XLogReadProcessError(errinfo); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(startptr, segno, wal_segment_size); + CheckXLogRemoved(segno, ThisTimeLineID); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->file >= 0) + { + close(sendSeg->file); + sendSeg->file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index a16793bb8b..cd5f589f03 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -296,6 +296,51 @@ identify_target_directory(XLogDumpPrivate *private, char *directory, fatal_error("could not find any WAL file"); } +static void +WALDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli_p, int *file_p, + WALOpenSegment *seg) +{ + TimeLineID tli = *tli_p; + char fname[MAXPGPATH]; + int file; + int tries; + + Assert(tli != InvalidTimeLineID); + + XLogFileName(fname, tli, nextSegNo, seg->size); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + file = open_file_in_directory(seg->dir, fname); + if (file >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (file < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); + + *file_p = file; +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -427,6 +472,7 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, { XLogDumpPrivate *private = state->private_data; int count = XLOG_BLCKSZ; + XLogReadError *errinfo; if (private->endptr != InvalidXLogRecPtr) { @@ -441,8 +487,22 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(private->inpath, private->timeline, targetPagePtr, - readBuff, count); + if ((errinfo = XLogRead(readBuff, targetPagePtr, count, &private->timeline, + &state->seg, WALDumpOpenSegment)) != NULL) + { + WALOpenSegment *seg = errinfo->seg; + char fname[MAXPGPATH]; + + XLogFileName(fname, seg->tli, seg->num, seg->size); + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->off, (Size) errinfo->reqbytes, + strerror(errinfo->read_errno)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->off, (Size) errinfo->reqbytes); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index b9d99d524e..3d9742b81b 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -227,8 +227,50 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Callback to open the specified WAL segment for reading. + * + * "nextSegNo" is the number of the segment to be opened. + * + * "tli_p" is an input/output argument. If *tli_p is valid, it's the timeline + * the new segment should be in. If *tli_p==InvalidTimeLineID, the callback + * needs to determine the timeline itself and put the result into *tli_p. + * + * "file_p" points to an address the segment file descriptor should be stored + * at. + * + * "seg" provides information on the currently open segment. The callback is + * not supposed to change this info. + * + * BasicOpenFile() is the preferred way to open the segment file in backend + * code, whereas open(2) should be used in frontend. + */ +typedef void (*WALSegmentOpen) (XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg); + extern void WALOpenSegmentInit(WALOpenSegment *seg, int size); +/* + * Error information that both backend and frontend caller can process. + * + * XXX Should the name be WALReadError? If so, we probably need to rename + * XLogRead() and XLogReadProcessError() too. + */ +typedef struct XLogReadError +{ + int read_errno; /* errno set by the last read(). */ + int readbytes; /* Bytes read by the last read(). */ + int reqbytes; /* Bytes requested to be read. */ + WALOpenSegment *seg; /* Segment we tried to read from. */ +} XLogReadError; + +extern XLogReadError *XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli_p, WALOpenSegment *seg, + WALSegmentOpen openSegment); +#ifndef FRONTEND +void XLogReadProcessError(XLogReadError *errinfo); +#endif + /* Functions for decoding an XLogRecord */ extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, -- 2.20.1 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v06-0006-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 5/6] Use only xlogreader.c:XLogRead() @ 2019-09-23 05:40 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-09-23 05:40 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 157 ++++++++++++++++++++++++ src/backend/access/transam/xlogutils.c | 46 ++++++- src/backend/replication/walsender.c | 139 ++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 64 +++++++++- src/include/access/xlogreader.h | 42 +++++++ 5 files changed, 436 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index 4de5530b3e..7f77fa95cb 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -27,6 +29,7 @@ #ifndef FRONTEND #include "miscadmin.h" +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -1015,6 +1018,160 @@ WALOpenSegmentInit(WALOpenSegment *seg, int size) #endif } +/* + * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'. If + * tli_p is passed, get the data from timeline *tli_p. 'pos' is the current + * position in the XLOG file and openSegment is a callback that opens the next + * segment for reading. + * + * Returns error information if the data could not be read or NULL if + * succeeded. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +XLogReadError * +XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli_p, WALOpenSegment *seg, WALSegmentOpen openSegment) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int segbytes; + int readbytes; + + seg->off = XLogSegmentOffset(recptr, seg->size); + + if (seg->file < 0 || + !XLByteInSeg(recptr, seg->num, seg->size) || + (tli_p != NULL && *tli_p != seg->tli)) + { + XLogSegNo nextSegNo; + TimeLineID tli = InvalidTimeLineID; + int file; + + /* Switch to another logfile segment */ + if (seg->file >= 0) + close(seg->file); + + XLByteToSeg(recptr, nextSegNo, seg->size); + + /* If we have the TLI, let's pass it to the callback. */ + if (tli_p != NULL) + tli = *tli_p; + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, &tli, &file, seg); + + /* + * If we passed InvalidTimeLineID, the callback should have + * determined the correct TLI and returned it. + */ + Assert(tli != InvalidTimeLineID); + + /* Update the open segment info. */ + seg->tli = tli; + seg->file = file; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set both "num" and "off". However we need to care + * about them too because the function can also be used directly, + * see walsender.c. + */ + seg->num = nextSegNo; + seg->off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (seg->size - seg->off)) + segbytes = seg->size - seg->off; + else + segbytes = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->file, p, segbytes, seg->off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + { + XLogReadError *errinfo; + + errinfo = (XLogReadError *) palloc(sizeof(XLogReadError)); + errinfo->read_errno = errno; + errinfo->readbytes = readbytes; + errinfo->reqbytes = segbytes; + errinfo->seg = seg; + + return errinfo; + } + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set this field. However we need to care about it too + * because the function can also be used directly (see walsender.c). + */ + seg->off += readbytes; + } + + return NULL; +} + +#ifndef FRONTEND +/* + * Backend-specific convenience code to handle read errors encountered by + * XLogRead(). + */ +void +XLogReadProcessError(XLogReadError *errinfo) +{ + WALOpenSegment *seg = errinfo->seg; + + if (errinfo->readbytes < 0) + { + errno = errinfo->read_errno; + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) errinfo->reqbytes))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->tli, seg->num), seg->off, + (Size) errinfo->reqbytes))); + } +} +#endif + /* ---------------------------------------- * Functions for decoding the data and block references in a record. * ---------------------------------------- diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 424bb06919..38c3196168 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,39 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_segment_open(XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg) +{ + TimeLineID tli = *tli_p; + char path[MAXPGPATH]; + int file; + + Assert(tli != InvalidTimeLineID); + + XLogFilePath(path, tli, nextSegNo, seg->size); + file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + *file_p = file; +} + /* * read_page callback for reading local xlog files * @@ -915,6 +948,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, loc; int count; TimeLineID pageTLI; + XLogReadError *errinfo; loc = targetPagePtr + reqLen; @@ -1022,10 +1056,10 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->seg.size, state->seg.tli, targetPagePtr, - XLOG_BLCKSZ); - state->seg.tli = pageTLI; - + if ((errinfo = XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, &pageTLI, + &state->seg, + read_local_xlog_page_segment_open)) != NULL) + XLogReadProcessError(errinfo); /* number of valid bytes in the buffer */ return count; } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index a617e20ab6..a7b3e0ecbe 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -247,9 +247,12 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(char *buf, XLogRecPtr startptr, Size count); +static void WalSndSegmentOpen(XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg); +static void XLogReadOld(char *buf, XLogRecPtr startptr, Size count); + /* Initialize walsender process before entering the main command loop */ void InitWalSender(void) @@ -763,6 +766,7 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req { XLogRecPtr flushptr; int count; + XLogReadError *errinfo; XLogReadDetermineTimeline(state, targetPagePtr, reqLen); sendTimeLineIsHistoric = (state->currTLI != ThisTimeLineID); @@ -783,7 +787,13 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ); + if ((errinfo = XLogRead(cur_page, + targetPagePtr, + XLOG_BLCKSZ, + NULL, /* WalSndSegmentOpen will determine TLI */ + sendSeg, + WalSndSegmentOpen)) != NULL) + XLogReadProcessError(errinfo); return count; } @@ -2360,7 +2370,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2533,6 +2543,81 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndSegmentOpen(XLogSegNo nextSegNo, TimeLineID *tli_p, int *file_p, + WALOpenSegment *seg) +{ + TimeLineID tli = *tli_p; + char path[MAXPGPATH]; + int file; + + /* + * The timeline is determined below, caller should not pass it. + */ + Assert(tli == InvalidTimeLineID); + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + tli = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, seg->size); + if (seg->num == endSegNo) + tli = sendTimeLineNextTLI; + } + + XLogFilePath(path, tli, nextSegNo, seg->size); + file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (file < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(tli, nextSegNo)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + *file_p = file; + *tli_p = tli; +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2550,6 +2635,8 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; + XLogReadError *errinfo; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2765,7 +2852,51 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(&output_message.data[output_message.len], startptr, nbytes); + +retry: + if ((errinfo = XLogRead(&output_message.data[output_message.len], + startptr, + nbytes, + NULL, /* WalSndSegmentOpen will determine TLI */ + sendSeg, + WalSndSegmentOpen)) != NULL) + XLogReadProcessError(errinfo); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(startptr, segno, wal_segment_size); + CheckXLogRemoved(segno, ThisTimeLineID); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->file >= 0) + { + close(sendSeg->file); + sendSeg->file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index a16793bb8b..cd5f589f03 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -296,6 +296,51 @@ identify_target_directory(XLogDumpPrivate *private, char *directory, fatal_error("could not find any WAL file"); } +static void +WALDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli_p, int *file_p, + WALOpenSegment *seg) +{ + TimeLineID tli = *tli_p; + char fname[MAXPGPATH]; + int file; + int tries; + + Assert(tli != InvalidTimeLineID); + + XLogFileName(fname, tli, nextSegNo, seg->size); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + file = open_file_in_directory(seg->dir, fname); + if (file >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (file < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); + + *file_p = file; +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -427,6 +472,7 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, { XLogDumpPrivate *private = state->private_data; int count = XLOG_BLCKSZ; + XLogReadError *errinfo; if (private->endptr != InvalidXLogRecPtr) { @@ -441,8 +487,22 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(private->inpath, private->timeline, targetPagePtr, - readBuff, count); + if ((errinfo = XLogRead(readBuff, targetPagePtr, count, &private->timeline, + &state->seg, WALDumpOpenSegment)) != NULL) + { + WALOpenSegment *seg = errinfo->seg; + char fname[MAXPGPATH]; + + XLogFileName(fname, seg->tli, seg->num, seg->size); + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->off, (Size) errinfo->reqbytes, + strerror(errinfo->read_errno)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->off, (Size) errinfo->reqbytes); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index b9d99d524e..3d9742b81b 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -227,8 +227,50 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Callback to open the specified WAL segment for reading. + * + * "nextSegNo" is the number of the segment to be opened. + * + * "tli_p" is an input/output argument. If *tli_p is valid, it's the timeline + * the new segment should be in. If *tli_p==InvalidTimeLineID, the callback + * needs to determine the timeline itself and put the result into *tli_p. + * + * "file_p" points to an address the segment file descriptor should be stored + * at. + * + * "seg" provides information on the currently open segment. The callback is + * not supposed to change this info. + * + * BasicOpenFile() is the preferred way to open the segment file in backend + * code, whereas open(2) should be used in frontend. + */ +typedef void (*WALSegmentOpen) (XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg); + extern void WALOpenSegmentInit(WALOpenSegment *seg, int size); +/* + * Error information that both backend and frontend caller can process. + * + * XXX Should the name be WALReadError? If so, we probably need to rename + * XLogRead() and XLogReadProcessError() too. + */ +typedef struct XLogReadError +{ + int read_errno; /* errno set by the last read(). */ + int readbytes; /* Bytes read by the last read(). */ + int reqbytes; /* Bytes requested to be read. */ + WALOpenSegment *seg; /* Segment we tried to read from. */ +} XLogReadError; + +extern XLogReadError *XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID *tli_p, WALOpenSegment *seg, + WALSegmentOpen openSegment); +#ifndef FRONTEND +void XLogReadProcessError(XLogReadError *errinfo); +#endif + /* Functions for decoding an XLogRecord */ extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, -- 2.20.1 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v06-0006-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 1/2] Use only xlogreader.c:XLogRead() @ 2019-09-26 11:51 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-09-26 11:51 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 153 ++++++++++++++++++++++++ src/backend/access/transam/xlogutils.c | 45 ++++++- src/backend/replication/walsender.c | 138 ++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 60 +++++++++- src/include/access/xlogreader.h | 47 ++++++++ 5 files changed, 433 insertions(+), 10 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index c8b0d2303d..a58c5d1633 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -27,6 +29,7 @@ #ifndef FRONTEND #include "miscadmin.h" +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -1016,6 +1019,156 @@ out: #endif /* FRONTEND */ +/* + * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'. If + * tli_p is passed, get the data from timeline *tli_p. 'pos' is the current + * position in the XLOG file and openSegment is a callback that opens the next + * segment for reading. + * + * Returns error information if the data could not be read or NULL if + * succeeded. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +XLogReadError * +XLogRead(char *buf, XLogRecPtr startptr, Size count, TimeLineID *tli_p, + WALOpenSegment *seg, WALSegmentContext *segcxt, + WALSegmentOpen openSegment) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + static XLogReadError errinfo; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int segbytes; + int readbytes; + + seg->ws_off = XLogSegmentOffset(recptr, segcxt->ws_segsize); + + if (seg->ws_file < 0 || + !XLByteInSeg(recptr, seg->ws_segno, segcxt->ws_segsize) || + (tli_p != NULL && *tli_p != seg->ws_tli)) + { + XLogSegNo nextSegNo; + TimeLineID tli; + int file; + + /* Switch to another logfile segment */ + if (seg->ws_file >= 0) + close(seg->ws_file); + + XLByteToSeg(recptr, nextSegNo, segcxt->ws_segsize); + + /* + * If we have the TLI, let's pass it to the callback. If NULL is + * passed, the callback has to find the TLI itself. + */ + if (tli_p != NULL) + tli = *tli_p; + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, &tli, &file, seg, segcxt); + + /* Update the open segment info. */ + seg->ws_tli = tli; + seg->ws_file = file; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set both "ws_segno" and "ws_off", however the XLOG + * reader is not necessarily involved. Furthermore, we need to set + * the current values for this function to work. + */ + seg->ws_segno = nextSegNo; + seg->ws_off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (segcxt->ws_segsize - seg->ws_off)) + segbytes = segcxt->ws_segsize - seg->ws_off; + else + segbytes = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->ws_file, p, segbytes, seg->ws_off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + { + errinfo.read_errno = errno; + errinfo.readbytes = readbytes; + errinfo.reqbytes = segbytes; + errinfo.seg = seg; + return &errinfo; + } + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* + * If the function is called by the XLOG reader, the reader will + * eventually set this field. However we need to care about it too + * because the function can also be used directly (see walsender.c). + */ + seg->ws_off += readbytes; + } + + return NULL; +} + +#ifndef FRONTEND +/* + * Backend-specific convenience code to handle read errors encountered by + * XLogRead(). + */ +void +XLogReadProcessError(XLogReadError *errinfo) +{ + WALOpenSegment *seg = errinfo->seg; + + if (errinfo->readbytes < 0) + { + errno = errinfo->read_errno; + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->ws_tli, seg->ws_segno), + seg->ws_off, (Size) errinfo->reqbytes))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->ws_tli, seg->ws_segno), + seg->ws_off, + (Size) errinfo->reqbytes))); + } +} +#endif + /* ---------------------------------------- * Functions for decoding the data and block references in a record. * ---------------------------------------- diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 5f1e5ba75d..09d42d3112 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,38 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_segment_open(XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg, + WALSegmentContext *segcxt) +{ + TimeLineID tli = *tli_p; + char path[MAXPGPATH]; + int file; + + XLogFilePath(path, tli, nextSegNo, segcxt->ws_segsize); + file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + *file_p = file; +} + /* * read_page callback for reading local xlog files * @@ -914,6 +946,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, XLogRecPtr read_upto, loc; int count; + XLogReadError *errinfo; loc = targetPagePtr + reqLen; @@ -1020,8 +1053,12 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->segcxt.ws_segsize, state->seg.ws_tli, targetPagePtr, - XLOG_BLCKSZ); + if ((errinfo = XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, + &state->seg.ws_tli, + &state->seg, + &state->segcxt, + read_local_xlog_page_segment_open)) != NULL) + XLogReadProcessError(errinfo); /* number of valid bytes in the buffer */ return count; diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index eb4a98cc91..dcb84693fc 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -248,9 +248,14 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(WALSegmentContext *segcxt, char *buf, XLogRecPtr startptr, Size count); +static void WalSndSegmentOpen(XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg, + WALSegmentContext *segcxt); +static void XLogReadOld(WALSegmentContext *segcxt, char *buf, + XLogRecPtr startptr, Size count); + /* Initialize walsender process before entering the main command loop */ void InitWalSender(void) @@ -766,6 +771,8 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req { XLogRecPtr flushptr; int count; + XLogReadError *errinfo; + XLogSegNo segno; XLogReadDetermineTimeline(state, targetPagePtr, reqLen); sendTimeLineIsHistoric = (state->currTLI != ThisTimeLineID); @@ -786,7 +793,24 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(sendCxt, cur_page, targetPagePtr, XLOG_BLCKSZ); + if ((errinfo = XLogRead(cur_page, + targetPagePtr, + XLOG_BLCKSZ, + NULL, /* WalSndSegmentOpen will determine TLI */ + sendSeg, + sendCxt, + WalSndSegmentOpen)) != NULL) + XLogReadProcessError(errinfo); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(targetPagePtr, segno, sendCxt->ws_segsize); + CheckXLogRemoved(segno, sendSeg->ws_tli); return count; } @@ -2363,7 +2387,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(WALSegmentContext *segcxt, char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(WALSegmentContext *segcxt, char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2536,6 +2560,71 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndSegmentOpen(XLogSegNo nextSegNo, TimeLineID *tli_p, int *file_p, + WALOpenSegment *seg, WALSegmentContext *segcxt) +{ + char path[MAXPGPATH]; + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + *tli_p = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, segcxt->ws_segsize); + if (seg->ws_segno == endSegNo) + *tli_p = sendTimeLineNextTLI; + } + + XLogFilePath(path, *tli_p, nextSegNo, segcxt->ws_segsize); + *file_p = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (*file_p < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(*tli_p, nextSegNo)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2553,6 +2642,8 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; + XLogReadError *errinfo; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2768,7 +2859,46 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(sendCxt, &output_message.data[output_message.len], startptr, nbytes); + +retry: + if ((errinfo = XLogRead(&output_message.data[output_message.len], + startptr, + nbytes, + NULL, /* WalSndSegmentOpen will determine TLI */ + sendSeg, + sendCxt, + WalSndSegmentOpen)) != NULL) + XLogReadProcessError(errinfo); + + /* See logical_read_xlog_page(). */ + XLByteToSeg(startptr, segno, sendCxt->ws_segsize); + CheckXLogRemoved(segno, sendSeg->ws_tli); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->ws_file >= 0) + { + close(sendSeg->ws_file); + sendSeg->ws_file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index b79208cd73..c97376101c 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -281,6 +281,46 @@ identify_target_directory(char *directory, char *fname) return NULL; /* not reached */ } +static void +WALDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli_p, int *file_p, + WALOpenSegment *seg, WALSegmentContext *segcxt) +{ + TimeLineID tli = *tli_p; + char fname[MAXPGPATH]; + int tries; + + XLogFileName(fname, tli, nextSegNo, segcxt->ws_segsize); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + *file_p = open_file_in_directory(segcxt->ws_dir, fname); + if (*file_p >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (*file_p < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -412,6 +452,7 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, { XLogDumpPrivate *private = state->private_data; int count = XLOG_BLCKSZ; + XLogReadError *errinfo; if (private->endptr != InvalidXLogRecPtr) { @@ -426,8 +467,23 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(state->segcxt.ws_dir, private->timeline, targetPagePtr, - readBuff, count); + if ((errinfo = XLogRead(readBuff, targetPagePtr, count, &private->timeline, + &state->seg, &state->segcxt, WALDumpOpenSegment)) != NULL) + { + WALOpenSegment *seg = errinfo->seg; + char fname[MAXPGPATH]; + + XLogFileName(fname, seg->ws_tli, seg->ws_segno, + state->segcxt.ws_segsize); + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->ws_off, (Size) errinfo->reqbytes, + strerror(errinfo->read_errno)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->ws_off, (Size) errinfo->reqbytes); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index 1bbee386e8..bf4d105de3 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -218,6 +218,31 @@ extern XLogReaderState *XLogReaderAllocate(int wal_segment_size, extern void XLogReaderFree(XLogReaderState *state); /* Initialize supporting structures */ +/* + * Callback to open the specified WAL segment for reading. + * + * "nextSegNo" is the number of the segment to be opened. + * + * "tli_p" is an input/output argument. If *tli_p is valid, it's the timeline + * the new segment should be in. If *tli_p==InvalidTimeLineID, the callback + * needs to determine the timeline itself and put the result into *tli_p. + * + * "file_p" points to an address the segment file descriptor should be stored + * at. + * + * "seg" provides information on the currently open segment. The callback is + * not supposed to change this info. + * + * "segcxt" is additional information about the segment, which logically does + * not fit into "seg". + * + * BasicOpenFile() is the preferred way to open the segment file in backend + * code, whereas open(2) should be used in frontend. + */ +typedef void (*WALSegmentOpen) (XLogSegNo nextSegNo, TimeLineID *tli_p, + int *file_p, WALOpenSegment *seg, + WALSegmentContext *segcxt); + extern void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir); @@ -232,6 +257,28 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, #ifdef FRONTEND extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Error information that both backend and frontend caller can process. + * + * XXX Should the name be WALReadError? If so, we probably need to rename + * XLogRead() and XLogReadProcessError() too. + */ +typedef struct XLogReadError +{ + int read_errno; /* errno set by the last read(). */ + int readbytes; /* Bytes read by the last read(). */ + int reqbytes; /* Bytes requested to be read. */ + WALOpenSegment *seg; /* Segment we tried to read from. */ +} XLogReadError; + +extern XLogReadError *XLogRead(char *buf, XLogRecPtr startptr, + Size count, TimeLineID *tli_p, + WALOpenSegment *seg, WALSegmentContext *segcxt, + WALSegmentOpen openSegment); +#ifndef FRONTEND +void XLogReadProcessError(XLogReadError *errinfo); +#endif + /* Functions for decoding an XLogRecord */ extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, -- 2.20.1 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v07-0006-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 1/2] Use only xlogreader.c:XLogRead() @ 2019-10-04 10:07 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2019-10-04 10:07 UTC (permalink / raw) The implementations in xlogutils.c and walsender.c are just renamed now, to be removed by the following diff. --- src/backend/access/transam/xlogreader.c | 141 ++++++++++++++++++++++- src/backend/access/transam/xlogutils.c | 61 ++++++++-- src/backend/replication/walsender.c | 143 +++++++++++++++++++++++- src/bin/pg_waldump/pg_waldump.c | 60 +++++++++- src/include/access/xlogreader.h | 42 +++++++ 5 files changed, 428 insertions(+), 19 deletions(-) diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index c8b0d2303d..3e2167ca5a 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -17,6 +17,8 @@ */ #include "postgres.h" +#include <unistd.h> + #include "access/transam.h" #include "access/xlogrecord.h" #include "access/xlog_internal.h" @@ -27,6 +29,7 @@ #ifndef FRONTEND #include "miscadmin.h" +#include "pgstat.h" #include "utils/memutils.h" #endif @@ -626,7 +629,13 @@ ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen) if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr)) goto err; - /* update read state information */ + /* + * Update read state information. + * + * Note that XLogRead(), if used, should have updated the "seg" too for + * its own reasons, however we cannot rely on ->read_page() to call + * XLogRead(). + */ state->seg.ws_segno = targetSegNo; state->seg.ws_off = targetPageOff; state->readLen = readLen; @@ -1016,6 +1025,136 @@ out: #endif /* FRONTEND */ +/* + * Read 'count' bytes from WAL fetched from timeline 'tli' into 'buf', + * starting at location 'startptr'. 'seg' is the last segment used, + * 'openSegment' is a callback to opens the next segment if needed and + * 'segcxt' is additional segment info that does not fit into 'seg'. + * + * 'errinfo' should point to XLogReadError structure which will receive error + * details in case the read fails. + * + * Returns true if succeeded, false if failed. + * + * XXX probably this should be improved to suck data directly from the + * WAL buffers when possible. + */ +bool +XLogRead(char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, + WALOpenSegment *seg, WALSegmentContext *segcxt, + WALSegmentOpen openSegment, XLogReadError *errinfo) +{ + char *p; + XLogRecPtr recptr; + Size nbytes; + + p = buf; + recptr = startptr; + nbytes = count; + + while (nbytes > 0) + { + int segbytes; + int readbytes; + + seg->ws_off = XLogSegmentOffset(recptr, segcxt->ws_segsize); + + if (seg->ws_file < 0 || + !XLByteInSeg(recptr, seg->ws_segno, segcxt->ws_segsize) || + tli != seg->ws_tli) + { + XLogSegNo nextSegNo; + + /* Switch to another logfile segment */ + if (seg->ws_file >= 0) + close(seg->ws_file); + + XLByteToSeg(recptr, nextSegNo, segcxt->ws_segsize); + + /* Open the next segment in the caller's way. */ + openSegment(nextSegNo, segcxt, &tli, &seg->ws_file); + + /* Update the current segment info. */ + seg->ws_tli = tli; + seg->ws_segno = nextSegNo; + seg->ws_off = 0; + } + + /* How many bytes are within this segment? */ + if (nbytes > (segcxt->ws_segsize - seg->ws_off)) + segbytes = segcxt->ws_segsize - seg->ws_off; + else + segbytes = nbytes; + +#ifndef FRONTEND + pgstat_report_wait_start(WAIT_EVENT_WAL_READ); +#endif + + /* + * Failure to read the data does not necessarily imply non-zero errno. + * Set it to zero so that caller can distinguish the failure that does + * not affect errno. + */ + errno = 0; + + readbytes = pg_pread(seg->ws_file, p, segbytes, seg->ws_off); + +#ifndef FRONTEND + pgstat_report_wait_end(); +#endif + + if (readbytes <= 0) + { + errinfo->read_errno = errno; + errinfo->readbytes = readbytes; + errinfo->reqbytes = segbytes; + errinfo->seg = seg; + return false; + } + + /* Update state for read */ + recptr += readbytes; + nbytes -= readbytes; + p += readbytes; + + /* Update the current segment info. */ + seg->ws_off += readbytes; + } + + return true; +} + +#ifndef FRONTEND +/* + * Backend-specific convenience code to handle read errors encountered by + * XLogRead(). + */ +void +XLogReadProcessError(XLogReadError *errinfo) +{ + WALOpenSegment *seg = errinfo->seg; + + if (errinfo->readbytes < 0) + { + errno = errinfo->read_errno; + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not read from log segment %s, offset %u, length %zu: %m", + XLogFileNameP(seg->ws_tli, seg->ws_segno), + seg->ws_off, (Size) errinfo->reqbytes))); + } + else + { + ereport(ERROR, + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("could not read from log segment %s, offset %u: length %zu", + XLogFileNameP(seg->ws_tli, seg->ws_segno), + seg->ws_off, + (Size) errinfo->reqbytes))); + } +} +#endif + /* ---------------------------------------- * Functions for decoding the data and block references in a record. * ---------------------------------------- diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 5f1e5ba75d..007974ea99 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -653,8 +653,8 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, * frontend). Probably these should be merged at some point. */ static void -XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, - Size count) +XLogReadOld(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr, + Size count) { char *p; XLogRecPtr recptr; @@ -896,6 +896,39 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa } } +/* + * Callback for XLogRead() to open the next segment. + */ +static void +read_local_xlog_page_segment_open(XLogSegNo nextSegNo, + WALSegmentContext *segcxt, + TimeLineID *tli_p, + int *file_p) +{ + TimeLineID tli = *tli_p; + char path[MAXPGPATH]; + int file; + + XLogFilePath(path, tli, nextSegNo, segcxt->ws_segsize); + file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (file < 0) + { + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + path))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } + + *file_p = file; +} + /* * read_page callback for reading local xlog files * @@ -913,7 +946,9 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, { XLogRecPtr read_upto, loc; + TimeLineID tli; int count; + XLogReadError errinfo; loc = targetPagePtr + reqLen; @@ -932,7 +967,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, read_upto = GetFlushRecPtr(); else read_upto = GetXLogReplayRecPtr(&ThisTimeLineID); - state->seg.ws_tli = ThisTimeLineID; + tli = ThisTimeLineID; /* * Check which timeline to get the record from. @@ -982,14 +1017,14 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, read_upto = state->currTLIValidUntil; /* - * Setting ws_tli to our wanted record's TLI is slightly wrong; - * the page might begin on an older timeline if it contains a - * timeline switch, since its xlog segment will have been copied - * from the prior timeline. This is pretty harmless though, as - * nothing cares so long as the timeline doesn't go backwards. We - * should read the page header instead; FIXME someday. + * Setting tli to our wanted record's TLI is slightly wrong; the + * page might begin on an older timeline if it contains a timeline + * switch, since its xlog segment will have been copied from the + * prior timeline. This is pretty harmless though, as nothing + * cares so long as the timeline doesn't go backwards. We should + * read the page header instead; FIXME someday. */ - state->seg.ws_tli = state->currTLI; + tli = state->currTLI; /* No need to wait on a historical timeline */ break; @@ -1020,8 +1055,10 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, * as 'count', read the whole page anyway. It's guaranteed to be * zero-padded up to the page boundary if it's incomplete. */ - XLogRead(cur_page, state->segcxt.ws_segsize, state->seg.ws_tli, targetPagePtr, - XLOG_BLCKSZ); + if (!XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ, tli, &state->seg, + &state->segcxt, read_local_xlog_page_segment_open, + &errinfo)) + XLogReadProcessError(&errinfo); /* number of valid bytes in the buffer */ return count; diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index eb4a98cc91..b21b143f99 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -248,9 +248,13 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time); static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); -static void XLogRead(WALSegmentContext *segcxt, char *buf, XLogRecPtr startptr, Size count); +static void WalSndSegmentOpen(XLogSegNo nextSegNo, WALSegmentContext *segcxt, + TimeLineID *tli_p, int *file_p); +static void XLogReadOld(WALSegmentContext *segcxt, char *buf, + XLogRecPtr startptr, Size count); + /* Initialize walsender process before entering the main command loop */ void InitWalSender(void) @@ -766,6 +770,8 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req { XLogRecPtr flushptr; int count; + XLogReadError errinfo; + XLogSegNo segno; XLogReadDetermineTimeline(state, targetPagePtr, reqLen); sendTimeLineIsHistoric = (state->currTLI != ThisTimeLineID); @@ -786,7 +792,27 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req count = flushptr - targetPagePtr; /* part of the page available */ /* now actually read the data, we know it's there */ - XLogRead(sendCxt, cur_page, targetPagePtr, XLOG_BLCKSZ); + if (!XLogRead(cur_page, + targetPagePtr, + XLOG_BLCKSZ, + sendSeg->ws_tli, /* Pass the current TLI because only + * WalSndSegmentOpen controls whether new + * TLI is needed. */ + sendSeg, + sendCxt, + WalSndSegmentOpen, + &errinfo)) + XLogReadProcessError(&errinfo); + + /* + * After reading into the buffer, check that what we read was valid. We do + * this after reading, because even though the segment was present when we + * opened it, it might get recycled or removed while we read it. The + * read() succeeds in that case, but the data we tried to read might + * already have been overwritten with new WAL records. + */ + XLByteToSeg(targetPagePtr, segno, sendCxt->ws_segsize); + CheckXLogRemoved(segno, sendSeg->ws_tli); return count; } @@ -2363,7 +2389,7 @@ WalSndKill(int code, Datum arg) * more than one. */ static void -XLogRead(WALSegmentContext *segcxt, char *buf, XLogRecPtr startptr, Size count) +XLogReadOld(WALSegmentContext *segcxt, char *buf, XLogRecPtr startptr, Size count) { char *p; XLogRecPtr recptr; @@ -2536,6 +2562,71 @@ retry: } } +/* + * Callback for XLogRead() to open the next segment. + */ +void +WalSndSegmentOpen(XLogSegNo nextSegNo, WALSegmentContext *segcxt, + TimeLineID *tli_p, int *file_p) +{ + char path[MAXPGPATH]; + + /*------- + * When reading from a historic timeline, and there is a timeline switch + * within this segment, read from the WAL segment belonging to the new + * timeline. + * + * For example, imagine that this server is currently on timeline 5, and + * we're streaming timeline 4. The switch from timeline 4 to 5 happened at + * 0/13002088. In pg_wal, we have these files: + * + * ... + * 000000040000000000000012 + * 000000040000000000000013 + * 000000050000000000000013 + * 000000050000000000000014 + * ... + * + * In this situation, when requested to send the WAL from segment 0x13, on + * timeline 4, we read the WAL from file 000000050000000000000013. Archive + * recovery prefers files from newer timelines, so if the segment was + * restored from the archive on this server, the file belonging to the old + * timeline, 000000040000000000000013, might not exist. Their contents are + * equal up to the switchpoint, because at a timeline switch, the used + * portion of the old segment is copied to the new file. ------- + */ + *tli_p = sendTimeLine; + if (sendTimeLineIsHistoric) + { + XLogSegNo endSegNo; + + XLByteToSeg(sendTimeLineValidUpto, endSegNo, segcxt->ws_segsize); + if (sendSeg->ws_segno == endSegNo) + *tli_p = sendTimeLineNextTLI; + } + + XLogFilePath(path, *tli_p, nextSegNo, segcxt->ws_segsize); + *file_p = BasicOpenFile(path, O_RDONLY | PG_BINARY); + + if (*file_p < 0) + { + /* + * If the file is not found, assume it's because the standby asked for + * a too old WAL segment that has already been removed or recycled. + */ + if (errno == ENOENT) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("requested WAL segment %s has already been removed", + XLogFileNameP(*tli_p, nextSegNo)))); + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", + path))); + } +} + /* * Send out the WAL in its normal physical/stored form. * @@ -2553,6 +2644,8 @@ XLogSendPhysical(void) XLogRecPtr startptr; XLogRecPtr endptr; Size nbytes; + XLogSegNo segno; + XLogReadError errinfo; /* If requested switch the WAL sender to the stopping state. */ if (got_STOPPING) @@ -2768,7 +2861,49 @@ XLogSendPhysical(void) * calls. */ enlargeStringInfo(&output_message, nbytes); - XLogRead(sendCxt, &output_message.data[output_message.len], startptr, nbytes); + +retry: + if (!XLogRead(&output_message.data[output_message.len], + startptr, + nbytes, + sendSeg->ws_tli, /* Pass the current TLI because only + * WalSndSegmentOpen controls whether new + * TLI is needed. */ + sendSeg, + sendCxt, + WalSndSegmentOpen, + &errinfo)) + XLogReadProcessError(&errinfo); + + /* See logical_read_xlog_page(). */ + XLByteToSeg(startptr, segno, sendCxt->ws_segsize); + CheckXLogRemoved(segno, sendSeg->ws_tli); + + /* + * During recovery, the currently-open WAL file might be replaced with the + * file of the same name retrieved from archive. So we always need to + * check what we read was valid after reading into the buffer. If it's + * invalid, we try to open and read the file again. + */ + if (am_cascading_walsender) + { + WalSnd *walsnd = MyWalSnd; + bool reload; + + SpinLockAcquire(&walsnd->mutex); + reload = walsnd->needreload; + walsnd->needreload = false; + SpinLockRelease(&walsnd->mutex); + + if (reload && sendSeg->ws_file >= 0) + { + close(sendSeg->ws_file); + sendSeg->ws_file = -1; + + goto retry; + } + } + output_message.len += nbytes; output_message.data[output_message.len] = '\0'; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index b79208cd73..4c49d1acf4 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -281,6 +281,46 @@ identify_target_directory(char *directory, char *fname) return NULL; /* not reached */ } +static void +WALDumpOpenSegment(XLogSegNo nextSegNo, WALSegmentContext *segcxt, + TimeLineID *tli_p, int *file_p) +{ + TimeLineID tli = *tli_p; + char fname[MAXPGPATH]; + int tries; + + XLogFileName(fname, tli, nextSegNo, segcxt->ws_segsize); + + /* + * In follow mode there is a short period of time after the server has + * written the end of the previous file before the new file is available. + * So we loop for 5 seconds looking for the file to appear before giving + * up. + */ + for (tries = 0; tries < 10; tries++) + { + *file_p = open_file_in_directory(segcxt->ws_dir, fname); + if (*file_p >= 0) + break; + if (errno == ENOENT) + { + int save_errno = errno; + + /* File not there yet, try again */ + pg_usleep(500 * 1000); + + errno = save_errno; + continue; + } + /* Any other error, fall through and fail */ + break; + } + + if (*file_p < 0) + fatal_error("could not find file \"%s\": %s", + fname, strerror(errno)); +} + /* * Read count bytes from a segment file in the specified directory, for the * given timeline, containing the specified record pointer; store the data in @@ -412,6 +452,7 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, { XLogDumpPrivate *private = state->private_data; int count = XLOG_BLCKSZ; + XLogReadError errinfo; if (private->endptr != InvalidXLogRecPtr) { @@ -426,8 +467,23 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } } - XLogDumpXLogRead(state->segcxt.ws_dir, private->timeline, targetPagePtr, - readBuff, count); + if (!XLogRead(readBuff, targetPagePtr, count, private->timeline, + &state->seg, &state->segcxt, WALDumpOpenSegment, &errinfo)) + { + WALOpenSegment *seg = errinfo.seg; + char fname[MAXPGPATH]; + + XLogFileName(fname, seg->ws_tli, seg->ws_segno, + state->segcxt.ws_segsize); + + if (errno != 0) + fatal_error("could not read from log file %s, offset %u, length %zu: %s", + fname, seg->ws_off, (Size) errinfo.reqbytes, + strerror(errinfo.read_errno)); + else + fatal_error("could not read from log file %s, offset %u: length: %zu", + fname, seg->ws_off, (Size) errinfo.reqbytes); + } return count; } diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index 1bbee386e8..f066b6255d 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -218,6 +218,26 @@ extern XLogReaderState *XLogReaderAllocate(int wal_segment_size, extern void XLogReaderFree(XLogReaderState *state); /* Initialize supporting structures */ +/* + * Callback to open the specified WAL segment for reading. + * + * "nextSegNo" is the number of the segment to be opened. + * + * "segcxt" is additional information about the segment. + * + * "tli_p" is an input/output argument. XLogRead() uses it to pass the + * timeline in which the new segment should be found, but the callback can use + * it to return the TLI that it actually opened. + * + * "file_p" points to an address the segment file descriptor should be stored + * at. + * + * BasicOpenFile() is the preferred way to open the segment file in backend + * code, whereas open(2) should be used in frontend. + */ +typedef void (*WALSegmentOpen) (XLogSegNo nextSegNo, WALSegmentContext *segcxt, + TimeLineID *tli_p, int *file_p); + extern void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir); @@ -232,6 +252,28 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state, #ifdef FRONTEND extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ +/* + * Error information that both backend and frontend caller can process. + * + * XXX Should the name be WALReadError? If so, we probably need to rename + * XLogRead() and XLogReadProcessError() too. + */ +typedef struct XLogReadError +{ + int read_errno; /* errno set by the last read(). */ + int readbytes; /* Bytes read by the last read(). */ + int reqbytes; /* Bytes requested to be read. */ + WALOpenSegment *seg; /* Segment we tried to read from. */ +} XLogReadError; + +extern bool XLogRead(char *buf, XLogRecPtr startptr, Size count, + TimeLineID tli, WALOpenSegment *seg, + WALSegmentContext *segcxt, WALSegmentOpen openSegment, + XLogReadError *errinfo); +#ifndef FRONTEND +void XLogReadProcessError(XLogReadError *errinfo); +#endif + /* Functions for decoding an XLogRecord */ extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, -- 2.20.1 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v08-0006-Remove-the-old-implemenations-of-XLogRead.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
* [PATCH 2/5] Refactor index_concurrently_create_copy() for use with REPACK (CONCURRENTLY). @ 2026-02-27 18:01 Antonin Houska <ah@cybertec.at> 0 siblings, 0 replies; 8+ messages in thread From: Antonin Houska @ 2026-02-27 18:01 UTC (permalink / raw) This patch moves the code to index_create_copy() and adds a "concurrently" parameter so it can be used by REPACK (CONCURRENTLY). With the CONCURRENTLY option, REPACK cannot simply swap the heap file and rebuild its indexes. Instead, it needs to build a separate set of indexes (including system catalog entries) *before* the actual swap, to reduce the time AccessExclusiveLock needs to be held for. --- src/backend/catalog/index.c | 54 +++++++++++++++++++++++--------- src/backend/commands/indexcmds.c | 6 ++-- src/backend/nodes/makefuncs.c | 9 +++--- src/include/catalog/index.h | 3 ++ src/include/nodes/makefuncs.h | 4 ++- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 5ee6389d39c..f8e6c3d804e 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -1288,15 +1288,32 @@ index_create(Relation heapRelation, /* * index_concurrently_create_copy * - * Create concurrently an index based on the definition of the one provided by - * caller. The index is inserted into catalogs and needs to be built later - * on. This is called during concurrent reindex processing. - * - * "tablespaceOid" is the tablespace to use for this index. + * Variant of index_create_copy(), called during concurrent reindex + * processing. */ Oid index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, Oid tablespaceOid, const char *newName) +{ + return index_create_copy(heapRelation, oldIndexId, tablespaceOid, newName, + true); +} + +/* + * index_create_copy + * + * Create an index based on the definition of the one provided by caller. The + * index is inserted into catalogs. If 'concurrently' is TRUE, it needs to be + * built later on, otherwise it's built immediately. + * + * "tablespaceOid" is the tablespace to use for this index. + * + * The actual implementation of index_concurrently_create_copy(), reusable for + * other purposes. + */ +Oid +index_create_copy(Relation heapRelation, Oid oldIndexId, Oid tablespaceOid, + const char *newName, bool concurrently) { Relation indexRelation; IndexInfo *oldInfo, @@ -1315,6 +1332,7 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, List *indexColNames = NIL; List *indexExprs = NIL; List *indexPreds = NIL; + int flags = 0; indexRelation = index_open(oldIndexId, RowExclusiveLock); @@ -1325,7 +1343,7 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, * Concurrent build of an index with exclusion constraints is not * supported. */ - if (oldInfo->ii_ExclusionOps != NULL) + if (oldInfo->ii_ExclusionOps != NULL && concurrently) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("concurrent index creation for exclusion constraints is not supported"))); @@ -1381,9 +1399,7 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, } /* - * Build the index information for the new index. Note that rebuild of - * indexes with exclusion constraints is not supported, hence there is no - * need to fill all the ii_Exclusion* fields. + * Build the index information for the new index. */ newInfo = makeIndexInfo(oldInfo->ii_NumIndexAttrs, oldInfo->ii_NumIndexKeyAttrs, @@ -1392,10 +1408,13 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, indexPreds, oldInfo->ii_Unique, oldInfo->ii_NullsNotDistinct, - false, /* not ready for inserts */ - true, + !concurrently, /* isready */ + concurrently, /* concurrent */ indexRelation->rd_indam->amsummarizing, - oldInfo->ii_WithoutOverlaps); + oldInfo->ii_WithoutOverlaps, + oldInfo->ii_ExclusionOps, + oldInfo->ii_ExclusionProcs, + oldInfo->ii_ExclusionStrats); /* * Extract the list of column names and the column numbers for the new @@ -1433,6 +1452,9 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, stattargets[i].isnull = isnull; } + if (concurrently) + flags = INDEX_CREATE_SKIP_BUILD | INDEX_CREATE_CONCURRENT; + /* * Now create the new index. * @@ -1456,7 +1478,7 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, indcoloptions->values, stattargets, reloptionsDatum, - INDEX_CREATE_SKIP_BUILD | INDEX_CREATE_CONCURRENT, + flags, 0, true, /* allow table to be a system catalog? */ false, /* is_internal? */ @@ -2450,7 +2472,8 @@ BuildIndexInfo(Relation index) indexStruct->indisready, false, index->rd_indam->amsummarizing, - indexStruct->indisexclusion && indexStruct->indisunique); + indexStruct->indisexclusion && indexStruct->indisunique, + NULL, NULL, NULL); /* fill in attribute numbers */ for (i = 0; i < numAtts; i++) @@ -2510,7 +2533,8 @@ BuildDummyIndexInfo(Relation index) indexStruct->indisready, false, index->rd_indam->amsummarizing, - indexStruct->indisexclusion && indexStruct->indisunique); + indexStruct->indisexclusion && indexStruct->indisunique, + NULL, NULL, NULL); /* fill in attribute numbers */ for (i = 0; i < numAtts; i++) diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 635679cc1f2..34209bd1393 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -243,7 +243,8 @@ CheckIndexCompatible(Oid oldId, */ indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes, accessMethodId, NIL, NIL, false, false, - false, false, amsummarizing, isWithoutOverlaps); + false, false, amsummarizing, isWithoutOverlaps, + NULL, NULL, NULL); typeIds = palloc_array(Oid, numberOfAttributes); collationIds = palloc_array(Oid, numberOfAttributes); opclassIds = palloc_array(Oid, numberOfAttributes); @@ -930,7 +931,8 @@ DefineIndex(ParseState *pstate, !concurrent, concurrent, amissummarizing, - stmt->iswithoutoverlaps); + stmt->iswithoutoverlaps, + NULL, NULL, NULL); typeIds = palloc_array(Oid, numberOfAttributes); collationIds = palloc_array(Oid, numberOfAttributes); diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c index 2caec621d73..ca7e21e8349 100644 --- a/src/backend/nodes/makefuncs.c +++ b/src/backend/nodes/makefuncs.c @@ -834,7 +834,8 @@ IndexInfo * makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, List *predicates, bool unique, bool nulls_not_distinct, bool isready, bool concurrent, bool summarizing, - bool withoutoverlaps) + bool withoutoverlaps, Oid *exclusion_ops, Oid *exclusion_procs, + uint16 *exclusion_strats) { IndexInfo *n = makeNode(IndexInfo); @@ -863,9 +864,9 @@ makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, n->ii_PredicateState = NULL; /* exclusion constraints */ - n->ii_ExclusionOps = NULL; - n->ii_ExclusionProcs = NULL; - n->ii_ExclusionStrats = NULL; + n->ii_ExclusionOps = exclusion_ops; + n->ii_ExclusionProcs = exclusion_procs; + n->ii_ExclusionStrats = exclusion_strats; /* speculative inserts */ n->ii_UniqueOps = NULL; diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index b259c4141ed..3426087b445 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -99,6 +99,9 @@ extern Oid index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, Oid tablespaceOid, const char *newName); +extern Oid index_create_copy(Relation heapRelation, Oid oldIndexId, + Oid tablespaceOid, const char *newName, + bool concurrently); extern void index_concurrently_build(Oid heapRelationId, Oid indexRelationId); diff --git a/src/include/nodes/makefuncs.h b/src/include/nodes/makefuncs.h index 982ec25ae14..dcea148ae1a 100644 --- a/src/include/nodes/makefuncs.h +++ b/src/include/nodes/makefuncs.h @@ -99,7 +99,9 @@ extern IndexInfo *makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, List *predicates, bool unique, bool nulls_not_distinct, bool isready, bool concurrent, - bool summarizing, bool withoutoverlaps); + bool summarizing, bool withoutoverlaps, + Oid *exclusion_ops, Oid *exclusion_procs, + uint16 *exclusion_strats); extern Node *makeStringConst(char *str, int location); extern DefElem *makeDefElem(char *name, Node *arg, int location); -- 2.47.3 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v36-0003-Move-conversion-of-a-historic-to-MVCC-snapshot-to-a-.patch ^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2026-02-27 18:01 UTC | newest] Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2019-07-09 09:54 [PATCH 4/5] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2019-09-09 09:53 [PATCH 3/4] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2019-09-09 09:53 [PATCH 3/4] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2019-09-23 05:40 [PATCH 5/6] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2019-09-23 05:40 [PATCH 5/6] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2019-09-26 11:51 [PATCH 1/2] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2019-10-04 10:07 [PATCH 1/2] Use only xlogreader.c:XLogRead() Antonin Houska <ah@cybertec.at> 2026-02-27 18:01 [PATCH 2/5] Refactor index_concurrently_create_copy() for use with REPACK (CONCURRENTLY). Antonin Houska <ah@cybertec.at>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox