From: "Andrey V. Lepikhov" Date: Mon, 1 Apr 2019 08:33:46 +0500 Subject: [PATCH 1/4] Relation-into-WAL-function --- src/backend/access/transam/generic_xlog.c | 48 +++++++++++++++++++++++ src/include/access/generic_xlog.h | 3 ++ 2 files changed, 51 insertions(+) diff --git a/src/backend/access/transam/generic_xlog.c b/src/backend/access/transam/generic_xlog.c index 5b00b7275b..c22e361747 100644 --- a/src/backend/access/transam/generic_xlog.c +++ b/src/backend/access/transam/generic_xlog.c @@ -542,3 +542,51 @@ generic_mask(char *page, BlockNumber blkno) mask_unused_space(page); } + +/* + * Function to write generic xlog for every existing block of a relation. + * Caller is responsible for locking the relation exclusively. + */ +void +generic_log_relation(Relation rel) +{ + BlockNumber blkno; + BlockNumber nblocks; + int npbuf = 0; + GenericXLogState *state = NULL; + Buffer bufpack[MAX_GENERIC_XLOG_PAGES]; + + CHECK_FOR_INTERRUPTS(); + nblocks = RelationGetNumberOfBlocks(rel); + + /* + * Iterate over all index pages and WAL-logging it. Pages are grouping into + * the packages before adding to a WAL-record. Zero-pages are + * not logged. + */ + for (blkno = 0; blkno < nblocks; blkno++) + { + Buffer buf; + + buf = ReadBuffer(rel, blkno); + if (!PageIsNew(BufferGetPage(buf))) + { + if (npbuf == 0) + state = GenericXLogStart(rel); + + LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); + GenericXLogRegisterBuffer(state, buf, GENERIC_XLOG_FULL_IMAGE); + bufpack[npbuf++] = buf; + } + else + ReleaseBuffer(buf); + + if ((npbuf == MAX_GENERIC_XLOG_PAGES) || (blkno == nblocks-1)) + { + GenericXLogFinish(state); + + for (; npbuf > 0; npbuf--) + UnlockReleaseBuffer(bufpack[npbuf-1]); + } + } +} diff --git a/src/include/access/generic_xlog.h b/src/include/access/generic_xlog.h index cb5b5b713a..e3bbf014cc 100644 --- a/src/include/access/generic_xlog.h +++ b/src/include/access/generic_xlog.h @@ -42,4 +42,7 @@ extern const char *generic_identify(uint8 info); extern void generic_desc(StringInfo buf, XLogReaderState *record); extern void generic_mask(char *pagedata, BlockNumber blkno); +/* other utils */ +extern void generic_log_relation(Relation rel); + #endif /* GENERIC_XLOG_H */ -- 2.17.1 --------------5B85550EC82B346D9FBFC34E Content-Type: text/x-patch; name="v2_0002-GIN-Optimal-WAL-Usage.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2_0002-GIN-Optimal-WAL-Usage.patch"