>From 070f598f5e0998c719c877295f18f36183eefccc Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Mon, 28 Aug 2017 10:21:53 +0500
Subject: [PATCH] hooks to watch for changed pages

---
 src/backend/access/transam/xlog.c       | 11 +++++++++++
 src/backend/access/transam/xloginsert.c |  7 +++++++
 src/include/access/xloginsert.h         |  6 ++++++
 3 files changed, 24 insertions(+)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index df4843f409..ffbd87e94e 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7024,6 +7024,7 @@ StartupXLOG(void)
 			do
 			{
 				bool		switchedTLI = false;
+				int nblock;
 
 #ifdef WAL_DEBUG
 				if (XLOG_DEBUG ||
@@ -7186,6 +7187,16 @@ StartupXLOG(void)
 				/* Pop the error context stack */
 				error_context_stack = errcallback.previous;
 
+				if (xlog_insert_buffer_hook)
+					for(nblock = 0; nblock < xlogreader->max_block_id; nblock++)
+					{
+						if(xlogreader->blocks[nblock].forknum == MAIN_FORKNUM)
+						{
+							xlog_insert_buffer_hook(xlogreader->blocks[nblock].blkno,
+											xlogreader->blocks[nblock].rnode, true);
+						}
+					}
+
 				/*
 				 * Update lastReplayedEndRecPtr after this record has been
 				 * successfully replayed.
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index 3af03ecdb1..26dd72c21e 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -205,6 +205,9 @@ XLogResetInsertion(void)
 	begininsert_called = false;
 }
 
+/* Hook for plugins to get control in during page insertion into xlog */
+PGDLLIMPORT xlog_insert_buffer_hook_type xlog_insert_buffer_hook = NULL;
+
 /*
  * Register a reference to a buffer with the WAL record being constructed.
  * This must be called for every page that the WAL-logged operation modifies.
@@ -256,6 +259,10 @@ XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
 #endif
 
 	regbuf->in_use = true;
+	if (xlog_insert_buffer_hook && regbuf->forkno == MAIN_FORKNUM)
+	{
+		xlog_insert_buffer_hook(regbuf->block, regbuf->rnode, false);
+	}
 }
 
 /*
diff --git a/src/include/access/xloginsert.h b/src/include/access/xloginsert.h
index 174c88677f..c367669fa5 100644
--- a/src/include/access/xloginsert.h
+++ b/src/include/access/xloginsert.h
@@ -58,4 +58,10 @@ extern XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std);
 
 extern void InitXLogInsert(void);
 
+/* Hook for plugins to get control in during page insertion into xlog */
+typedef void (*xlog_insert_buffer_hook_type) (BlockNumber block_number, RelFileNode rel, bool recovery);
+
+/* in xloginsert.c */
+extern PGDLLIMPORT xlog_insert_buffer_hook_type xlog_insert_buffer_hook;
+
 #endif							/* XLOGINSERT_H */
-- 
2.11.0 (Apple Git-81)

