walrcv_timestamp.patch
text/x-patch
Filename: walrcv_timestamp.patch
Type: text/x-patch
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: context
| File | + | − |
|---|---|---|
| src/backend/access/transam/xlog.c | 7 | 0 |
| src/backend/replication/walreceiver.c | 5 | 0 |
| src/backend/replication/walreceiverfuncs.c | 32 | 0 |
| src/backend/storage/ipc/standby.c | 2 | 0 |
| src/include/replication/walreceiver.h | 3 | 0 |
*** a/src/backend/access/transam/xlog.c
--- b/src/backend/access/transam/xlog.c
***************
*** 9281,9287 **** retry:
--- 9281,9294 ----
sources);
switched_segment = true;
if (readFile != -1)
+ {
+ /*
+ * Nudge forwards the WAL receive timestamp when
+ * we are relying on WAL files.
+ */
+ SetWalRcvTimestamp();
break;
+ }
/*
* Nope, not found in archive and/or pg_xlog.
*** a/src/backend/replication/walreceiver.c
--- b/src/backend/replication/walreceiver.c
***************
*** 515,521 **** XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
}
}
! /* Flush the log to disk */
static void
XLogWalRcvFlush(void)
{
--- 515,521 ----
}
}
! /* Flush the log to disk and update shared memory pointer and timestamp */
static void
XLogWalRcvFlush(void)
{
***************
*** 524,537 **** XLogWalRcvFlush(void)
--- 524,541 ----
/* use volatile pointer to prevent code rearrangement */
volatile WalRcvData *walrcv = WalRcv;
char activitymsg[50];
+ TimestampTz receivedTimestamp;
issue_xlog_fsync(recvFile, recvId, recvSeg);
LogstreamResult.Flush = LogstreamResult.Write;
+ receivedTimestamp = GetCurrentTimestamp();
+
/* Update shared-memory status */
SpinLockAcquire(&walrcv->mutex);
walrcv->receivedUpto = LogstreamResult.Flush;
+ walrcv->receivedTimestamp = receivedTimestamp;
SpinLockRelease(&walrcv->mutex);
/* Report XLOG streaming progress in PS display */
*** a/src/backend/replication/walreceiverfuncs.c
--- b/src/backend/replication/walreceiverfuncs.c
***************
*** 220,222 **** GetWalRcvWriteRecPtr(void)
--- 220,254 ----
return recptr;
}
+
+ /*
+ * Returns the timestamp of last walreceiver write
+ */
+ TimestampTz
+ GetWalRcvTimestamp(void)
+ {
+ /* use volatile pointer to prevent code rearrangement */
+ volatile WalRcvData *walrcv = WalRcv;
+ TimestampTz receivedTimestamp;
+
+ SpinLockAcquire(&walrcv->mutex);
+ receivedTimestamp = walrcv->receivedTimestamp;
+ SpinLockRelease(&walrcv->mutex);
+
+ return receivedTimestamp;
+ }
+
+ /*
+ * Sets the timestamp for when receiving WAL files
+ */
+ void
+ SetWalRcvTimestamp(void)
+ {
+ /* use volatile pointer to prevent code rearrangement */
+ volatile WalRcvData *walrcv = WalRcv;
+ TimestampTz receivedTimestamp = GetCurrentTimestamp();
+
+ SpinLockAcquire(&walrcv->mutex);
+ walrcv->receivedTimestamp = receivedTimestamp;
+ SpinLockRelease(&walrcv->mutex);
+ }
*** a/src/backend/storage/ipc/standby.c
--- b/src/backend/storage/ipc/standby.c
***************
*** 22,27 ****
--- 22,28 ----
#include "access/xlog.h"
#include "miscadmin.h"
#include "pgstat.h"
+ #include "replication/walreceiver.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
***************
*** 126,132 **** WaitExceedsMaxStandbyDelay(void)
{
/* Are we past max_standby_delay? */
if (MaxStandbyDelay >= 0 &&
! TimestampDifferenceExceeds(GetLatestXLogTime(), GetCurrentTimestamp(),
MaxStandbyDelay))
return true;
--- 127,133 ----
{
/* Are we past max_standby_delay? */
if (MaxStandbyDelay >= 0 &&
! TimestampDifferenceExceeds(GetWalRcvTimestamp(), GetCurrentTimestamp(),
MaxStandbyDelay))
return true;
*** a/src/include/replication/walreceiver.h
--- b/src/include/replication/walreceiver.h
***************
*** 58,63 **** typedef struct
--- 58,64 ----
* walreceiver updates this whenever it flushes the received WAL.
*/
XLogRecPtr receivedUpto;
+ TimestampTz receivedTimestamp;
slock_t mutex; /* locks shared variables shown above */
} WalRcvData;
***************
*** 83,87 **** extern bool WalRcvInProgress(void);
--- 84,90 ----
extern XLogRecPtr WaitNextXLogAvailable(XLogRecPtr recptr, bool *finished);
extern void RequestXLogStreaming(XLogRecPtr recptr, const char *conninfo);
extern XLogRecPtr GetWalRcvWriteRecPtr(void);
+ extern TimestampTz GetWalRcvTimestamp(void);
+ extern void SetWalRcvTimestamp(void);
#endif /* _WALRECEIVER_H */