v27-0003-Error-or-Assert-before-START_CRIT_SECTION-for-WA.patch
application/x-patch
Filename: v27-0003-Error-or-Assert-before-START_CRIT_SECTION-for-WA.patch
Type: application/x-patch
Part: 3
Message:
Re: [Patch] ALTER SYSTEM READ ONLY
Patch
Format: format-patch
Series: patch v27-0003
Subject: Error or Assert before START_CRIT_SECTION for WAL write
| File | + | − |
|---|---|---|
| contrib/pg_surgery/heap_surgery.c | 8 | 2 |
| src/backend/access/brin/brin.c | 4 | 0 |
| src/backend/access/brin/brin_pageops.c | 18 | 3 |
| src/backend/access/brin/brin_revmap.c | 9 | 1 |
| src/backend/access/gin/ginbtree.c | 12 | 3 |
| src/backend/access/gin/gindatapage.c | 15 | 3 |
| src/backend/access/gin/ginfast.c | 9 | 2 |
| src/backend/access/gin/gininsert.c | 4 | 0 |
| src/backend/access/gin/ginutil.c | 8 | 1 |
| src/backend/access/gin/ginvacuum.c | 10 | 1 |
| src/backend/access/gist/gist.c | 20 | 5 |
| src/backend/access/gist/gistvacuum.c | 11 | 2 |
| src/backend/access/hash/hash.c | 16 | 3 |
| src/backend/access/hash/hashinsert.c | 8 | 1 |
| src/backend/access/hash/hashovfl.c | 18 | 4 |
| src/backend/access/hash/hashpage.c | 9 | 0 |
| src/backend/access/heap/heapam.c | 25 | 1 |
| src/backend/access/heap/pruneheap.c | 9 | 3 |
| src/backend/access/heap/vacuumlazy.c | 18 | 4 |
| src/backend/access/heap/visibilitymap.c | 20 | 2 |
| src/backend/access/nbtree/nbtdedup.c | 3 | 0 |
| src/backend/access/nbtree/nbtinsert.c | 9 | 1 |
| src/backend/access/nbtree/nbtpage.c | 30 | 4 |
| src/backend/access/spgist/spgdoinsert.c | 13 | 0 |
| src/backend/access/spgist/spgvacuum.c | 19 | 3 |
| src/backend/access/transam/multixact.c | 4 | 1 |
| src/backend/access/transam/twophase.c | 9 | 0 |
| src/backend/access/transam/varsup.c | 4 | 0 |
| src/backend/access/transam/walprohibit.c | 10 | 0 |
| src/backend/access/transam/xact.c | 6 | 0 |
| src/backend/access/transam/xlog.c | 18 | 8 |
| src/backend/access/transam/xloginsert.c | 11 | 2 |
| src/backend/commands/sequence.c | 16 | 0 |
| src/backend/commands/variable.c | 6 | 3 |
| src/backend/postmaster/checkpointer.c | 4 | 0 |
| src/backend/storage/buffer/bufmgr.c | 6 | 4 |
| src/backend/storage/freespace/freespace.c | 9 | 1 |
| src/backend/utils/cache/relmapper.c | 3 | 0 |
| src/include/access/walprohibit.h | 46 | 0 |
| src/include/miscadmin.h | 27 | 0 |
From 7e6c6036123c2967d2ead33269e23acafb0abd94 Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Mon, 27 Jul 2020 02:13:36 -0400
Subject: [PATCH v27 3/4] Error or Assert before START_CRIT_SECTION for WAL
write
Based on the following criteria added the Assert or the Error when system is
prohibited:
- Added ERROR for the function which can be reachable without valid XID in case
of VACUUM or CONCURRENT CREATE INDEX. For that added common static inline
function CheckWALPermitted().
- Added ASSERT for the function which cannot be reached without valid XID;
Assert to ensure XID validation. For that added AssertWALPermitted_HaveXID().
To enforce the rule to have aforesaid assert or error check before entering a
critical section for WAL write, a new walpermit_checked_state assert only flag
is added. If these check is missing then XLogBeginInsert() will have an
assertion if it is in critical section.
If we are not doing WAL insert inside the critical section then the above
checking is not necessary, we can rely on XLogBeginInsert() for that check &
report an error.
---
contrib/pg_surgery/heap_surgery.c | 10 ++++-
src/backend/access/brin/brin.c | 4 ++
src/backend/access/brin/brin_pageops.c | 21 +++++++++--
src/backend/access/brin/brin_revmap.c | 10 ++++-
src/backend/access/gin/ginbtree.c | 15 ++++++--
src/backend/access/gin/gindatapage.c | 18 +++++++--
src/backend/access/gin/ginfast.c | 11 +++++-
src/backend/access/gin/gininsert.c | 4 ++
src/backend/access/gin/ginutil.c | 9 ++++-
src/backend/access/gin/ginvacuum.c | 11 +++++-
src/backend/access/gist/gist.c | 25 +++++++++---
src/backend/access/gist/gistvacuum.c | 13 ++++++-
src/backend/access/hash/hash.c | 19 ++++++++--
src/backend/access/hash/hashinsert.c | 9 ++++-
src/backend/access/hash/hashovfl.c | 22 +++++++++--
src/backend/access/hash/hashpage.c | 9 +++++
src/backend/access/heap/heapam.c | 26 ++++++++++++-
src/backend/access/heap/pruneheap.c | 12 ++++--
src/backend/access/heap/vacuumlazy.c | 22 +++++++++--
src/backend/access/heap/visibilitymap.c | 22 ++++++++++-
src/backend/access/nbtree/nbtdedup.c | 3 ++
src/backend/access/nbtree/nbtinsert.c | 10 ++++-
src/backend/access/nbtree/nbtpage.c | 34 +++++++++++++++--
src/backend/access/spgist/spgdoinsert.c | 13 +++++++
src/backend/access/spgist/spgvacuum.c | 22 +++++++++--
src/backend/access/transam/multixact.c | 5 ++-
src/backend/access/transam/twophase.c | 9 +++++
src/backend/access/transam/varsup.c | 4 ++
src/backend/access/transam/walprohibit.c | 10 +++++
src/backend/access/transam/xact.c | 6 +++
src/backend/access/transam/xlog.c | 26 +++++++++----
src/backend/access/transam/xloginsert.c | 13 ++++++-
src/backend/commands/sequence.c | 16 ++++++++
src/backend/commands/variable.c | 9 +++--
src/backend/postmaster/checkpointer.c | 4 ++
src/backend/storage/buffer/bufmgr.c | 10 +++--
src/backend/storage/freespace/freespace.c | 10 ++++-
src/backend/utils/cache/relmapper.c | 3 ++
src/include/access/walprohibit.h | 46 +++++++++++++++++++++++
src/include/miscadmin.h | 27 +++++++++++++
40 files changed, 504 insertions(+), 68 deletions(-)
diff --git a/contrib/pg_surgery/heap_surgery.c b/contrib/pg_surgery/heap_surgery.c
index d31e5f31fd4..2938b937f75 100644
--- a/contrib/pg_surgery/heap_surgery.c
+++ b/contrib/pg_surgery/heap_surgery.c
@@ -14,6 +14,7 @@
#include "access/heapam.h"
#include "access/visibilitymap.h"
+#include "access/walprohibit.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_proc_d.h"
#include "miscadmin.h"
@@ -89,6 +90,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
OffsetNumber curr_start_ptr,
next_start_ptr;
bool include_this_tid[MaxHeapTuplesPerPage];
+ bool needwal;
if (RecoveryInProgress())
ereport(ERROR,
@@ -100,6 +102,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
sanity_check_tid_array(ta, &ntids);
rel = relation_open(relid, RowExclusiveLock);
+ needwal = RelationNeedsWAL(rel);
/* Check target relation. */
sanity_check_relation(rel);
@@ -217,6 +220,9 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
if (heap_force_opt == HEAP_FORCE_KILL && PageIsAllVisible(page))
visibilitymap_pin(rel, blkno, &vmbuf);
+ if (needwal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) from here until all the changes are logged. */
START_CRIT_SECTION();
@@ -297,12 +303,12 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
MarkBufferDirty(buf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
log_newpage_buffer(buf, true);
}
/* WAL log the VM page if it was modified. */
- if (did_modify_vm && RelationNeedsWAL(rel))
+ if (did_modify_vm && needwal)
log_newpage_buffer(vmbuf, false);
END_CRIT_SECTION();
diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index c23ea44866a..75d808a460b 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -24,6 +24,7 @@
#include "access/relscan.h"
#include "access/table.h"
#include "access/tableam.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
@@ -900,6 +901,9 @@ brinbuildempty(Relation index)
ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
+ /* Building indexes will have an XID */
+ AssertWALPermittedHaveXID();
+
/* Initialize and xlog metabuffer. */
START_CRIT_SECTION();
brin_metapage_init(BufferGetPage(metabuf), BrinGetPagesPerRange(index),
diff --git a/src/backend/access/brin/brin_pageops.c b/src/backend/access/brin/brin_pageops.c
index df9ffc2fb86..270a881ab12 100644
--- a/src/backend/access/brin/brin_pageops.c
+++ b/src/backend/access/brin/brin_pageops.c
@@ -14,6 +14,7 @@
#include "access/brin_pageops.h"
#include "access/brin_revmap.h"
#include "access/brin_xlog.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -65,6 +66,7 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
Buffer newbuf;
BlockNumber newblk = InvalidBlockNumber;
bool extended;
+ bool needwal = RelationNeedsWAL(idxrel);
Assert(newsz == MAXALIGN(newsz));
@@ -176,13 +178,16 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
if (((BrinPageFlags(oldpage) & BRIN_EVACUATE_PAGE) == 0) &&
brin_can_do_samepage_update(oldbuf, origsz, newsz))
{
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
if (!PageIndexTupleOverwrite(oldpage, oldoff, (Item) unconstify(BrinTuple *, newtup), newsz))
elog(ERROR, "failed to replace BRIN tuple");
MarkBufferDirty(oldbuf);
/* XLOG stuff */
- if (RelationNeedsWAL(idxrel))
+ if (needwal)
{
xl_brin_samepage_update xlrec;
XLogRecPtr recptr;
@@ -240,6 +245,9 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
revmapbuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/*
@@ -267,7 +275,7 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
MarkBufferDirty(revmapbuf);
/* XLOG stuff */
- if (RelationNeedsWAL(idxrel))
+ if (needwal)
{
xl_brin_update xlrec;
XLogRecPtr recptr;
@@ -351,6 +359,7 @@ brin_doinsert(Relation idxrel, BlockNumber pagesPerRange,
Buffer revmapbuf;
ItemPointerData tid;
bool extended;
+ bool needwal;
Assert(itemsz == MAXALIGN(itemsz));
@@ -405,6 +414,10 @@ brin_doinsert(Relation idxrel, BlockNumber pagesPerRange,
page = BufferGetPage(*buffer);
blk = BufferGetBlockNumber(*buffer);
+ needwal = RelationNeedsWAL(idxrel);
+ if (needwal)
+ CheckWALPermitted();
+
/* Execute the actual insertion */
START_CRIT_SECTION();
if (extended)
@@ -424,7 +437,7 @@ brin_doinsert(Relation idxrel, BlockNumber pagesPerRange,
MarkBufferDirty(revmapbuf);
/* XLOG stuff */
- if (RelationNeedsWAL(idxrel))
+ if (needwal)
{
xl_brin_insert xlrec;
XLogRecPtr recptr;
@@ -881,6 +894,8 @@ brin_initialize_empty_new_buffer(Relation idxrel, Buffer buffer)
"brin_initialize_empty_new_buffer: initializing blank page %u",
BufferGetBlockNumber(buffer)));
+ CheckWALPermitted();
+
START_CRIT_SECTION();
page = BufferGetPage(buffer);
brin_page_init(page, BRIN_PAGETYPE_REGULAR);
diff --git a/src/backend/access/brin/brin_revmap.c b/src/backend/access/brin/brin_revmap.c
index bab2a88ee3f..70d846d5121 100644
--- a/src/backend/access/brin/brin_revmap.c
+++ b/src/backend/access/brin/brin_revmap.c
@@ -26,6 +26,7 @@
#include "access/brin_tuple.h"
#include "access/brin_xlog.h"
#include "access/rmgr.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -340,6 +341,7 @@ brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
OffsetNumber revmapOffset;
OffsetNumber regOffset;
ItemId lp;
+ bool needwal;
revmap = brinRevmapInitialize(idxrel, &pagesPerRange, NULL);
@@ -404,6 +406,10 @@ brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
* crashed or aborted summarization; remove them silently.
*/
+ needwal = RelationNeedsWAL(idxrel);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
ItemPointerSetInvalid(&invalidIptr);
@@ -415,7 +421,7 @@ brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
MarkBufferDirty(regBuf);
MarkBufferDirty(revmapBuf);
- if (RelationNeedsWAL(idxrel))
+ if (needwal)
{
xl_brin_desummarize xlrec;
XLogRecPtr recptr;
@@ -613,6 +619,8 @@ revmap_physical_extend(BrinRevmap *revmap)
return;
}
+ AssertWALPermittedHaveXID();
+
/*
* Ok, we have now locked the metapage and the target block. Re-initialize
* the target block as a revmap page, and update the metapage.
diff --git a/src/backend/access/gin/ginbtree.c b/src/backend/access/gin/ginbtree.c
index 482cf10877c..0d6dacfe84a 100644
--- a/src/backend/access/gin/ginbtree.c
+++ b/src/backend/access/gin/ginbtree.c
@@ -16,6 +16,7 @@
#include "access/gin_private.h"
#include "access/ginxlog.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "miscadmin.h"
#include "storage/predicate.h"
@@ -332,6 +333,7 @@ ginPlaceToPage(GinBtree btree, GinBtreeStack *stack,
{
Page page = BufferGetPage(stack->buffer);
bool result;
+ bool needwal;
GinPlaceToPageRC rc;
uint16 xlflags = 0;
Page childpage = NULL;
@@ -377,6 +379,7 @@ ginPlaceToPage(GinBtree btree, GinBtreeStack *stack,
insertdata, updateblkno,
&ptp_workspace,
&newlpage, &newrpage);
+ needwal = RelationNeedsWAL(btree->index) && !btree->isBuild;
if (rc == GPTP_NO_WORK)
{
@@ -385,10 +388,13 @@ ginPlaceToPage(GinBtree btree, GinBtreeStack *stack,
}
else if (rc == GPTP_INSERT)
{
+ if (needwal)
+ CheckWALPermitted();
+
/* It will fit, perform the insertion */
START_CRIT_SECTION();
- if (RelationNeedsWAL(btree->index) && !btree->isBuild)
+ if (needwal)
{
XLogBeginInsert();
XLogRegisterBuffer(0, stack->buffer, REGBUF_STANDARD);
@@ -409,7 +415,7 @@ ginPlaceToPage(GinBtree btree, GinBtreeStack *stack,
MarkBufferDirty(childbuf);
}
- if (RelationNeedsWAL(btree->index) && !btree->isBuild)
+ if (needwal)
{
XLogRecPtr recptr;
ginxlogInsert xlrec;
@@ -547,6 +553,9 @@ ginPlaceToPage(GinBtree btree, GinBtreeStack *stack,
}
}
+ if (needwal)
+ CheckWALPermitted();
+
/*
* OK, we have the new contents of the left page in a temporary copy
* now (newlpage), and likewise for the new contents of the
@@ -587,7 +596,7 @@ ginPlaceToPage(GinBtree btree, GinBtreeStack *stack,
}
/* write WAL record */
- if (RelationNeedsWAL(btree->index) && !btree->isBuild)
+ if (needwal)
{
XLogRecPtr recptr;
diff --git a/src/backend/access/gin/gindatapage.c b/src/backend/access/gin/gindatapage.c
index 06c05865435..2b4812fb23a 100644
--- a/src/backend/access/gin/gindatapage.c
+++ b/src/backend/access/gin/gindatapage.c
@@ -16,6 +16,7 @@
#include "access/gin_private.h"
#include "access/ginxlog.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "lib/ilist.h"
#include "miscadmin.h"
@@ -811,6 +812,7 @@ ginVacuumPostingTreeLeaf(Relation indexrel, Buffer buffer, GinVacuumState *gvs)
if (removedsomething)
{
bool modified;
+ bool needwal;
/*
* Make sure we have a palloc'd copy of all segments, after the first
@@ -835,8 +837,12 @@ ginVacuumPostingTreeLeaf(Relation indexrel, Buffer buffer, GinVacuumState *gvs)
}
}
- if (RelationNeedsWAL(indexrel))
+ needwal = RelationNeedsWAL(indexrel);
+ if (needwal)
+ {
+ CheckWALPermitted();
computeLeafRecompressWALData(leaf);
+ }
/* Apply changes to page */
START_CRIT_SECTION();
@@ -845,7 +851,7 @@ ginVacuumPostingTreeLeaf(Relation indexrel, Buffer buffer, GinVacuumState *gvs)
MarkBufferDirty(buffer);
- if (RelationNeedsWAL(indexrel))
+ if (needwal)
{
XLogRecPtr recptr;
@@ -1777,6 +1783,7 @@ createPostingTree(Relation index, ItemPointerData *items, uint32 nitems,
int nrootitems;
int rootsize;
bool is_build = (buildStats != NULL);
+ bool needwal;
/* Construct the new root page in memory first. */
tmppage = (Page) palloc(BLCKSZ);
@@ -1825,12 +1832,17 @@ createPostingTree(Relation index, ItemPointerData *items, uint32 nitems,
*/
PredicateLockPageSplit(index, BufferGetBlockNumber(entrybuffer), blkno);
+ needwal = RelationNeedsWAL(index) && !is_build;
+
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
PageRestoreTempPage(tmppage, page);
MarkBufferDirty(buffer);
- if (RelationNeedsWAL(index) && !is_build)
+ if (needwal)
{
XLogRecPtr recptr;
ginxlogCreatePostingTree data;
diff --git a/src/backend/access/gin/ginfast.c b/src/backend/access/gin/ginfast.c
index e0d99409461..53c156018e7 100644
--- a/src/backend/access/gin/ginfast.c
+++ b/src/backend/access/gin/ginfast.c
@@ -20,6 +20,7 @@
#include "access/gin_private.h"
#include "access/ginxlog.h"
+#include "access/walprohibit.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "catalog/pg_am.h"
@@ -68,6 +69,8 @@ writeListPage(Relation index, Buffer buffer,
PGAlignedBlock workspace;
char *ptr;
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
GinInitBuffer(buffer, GIN_LIST);
@@ -548,6 +551,7 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber newHead,
Page metapage;
GinMetaPageData *metadata;
BlockNumber blknoToDelete;
+ bool needwal = RelationNeedsWAL(index);
metapage = BufferGetPage(metabuffer);
metadata = GinPageGetMeta(metapage);
@@ -586,8 +590,11 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber newHead,
* prepare the XLogInsert machinery for that before entering the
* critical section.
*/
- if (RelationNeedsWAL(index))
+ if (needwal)
+ {
+ CheckWALPermitted();
XLogEnsureRecordSpace(data.ndeleted, 0);
+ }
START_CRIT_SECTION();
@@ -625,7 +632,7 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber newHead,
MarkBufferDirty(buffers[i]);
}
- if (RelationNeedsWAL(index))
+ if (needwal)
{
XLogRecPtr recptr;
diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c
index 0e8672c9e90..79312e5d2d0 100644
--- a/src/backend/access/gin/gininsert.c
+++ b/src/backend/access/gin/gininsert.c
@@ -17,6 +17,7 @@
#include "access/gin_private.h"
#include "access/ginxlog.h"
#include "access/tableam.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "miscadmin.h"
@@ -447,6 +448,9 @@ ginbuildempty(Relation index)
ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
LockBuffer(RootBuffer, BUFFER_LOCK_EXCLUSIVE);
+ /* Building indexes will have an XID */
+ AssertWALPermittedHaveXID();
+
/* Initialize and xlog metabuffer and root buffer. */
START_CRIT_SECTION();
GinInitMetabuffer(MetaBuffer);
diff --git a/src/backend/access/gin/ginutil.c b/src/backend/access/gin/ginutil.c
index cdd626ff0a4..0940b20c718 100644
--- a/src/backend/access/gin/ginutil.c
+++ b/src/backend/access/gin/ginutil.c
@@ -17,6 +17,7 @@
#include "access/gin_private.h"
#include "access/ginxlog.h"
#include "access/reloptions.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
@@ -658,12 +659,18 @@ ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
Buffer metabuffer;
Page metapage;
GinMetaPageData *metadata;
+ bool needwal;
metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
LockBuffer(metabuffer, GIN_EXCLUSIVE);
metapage = BufferGetPage(metabuffer);
metadata = GinPageGetMeta(metapage);
+ needwal = RelationNeedsWAL(index) && !is_build;
+
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
metadata->nTotalPages = stats->nTotalPages;
@@ -683,7 +690,7 @@ ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
MarkBufferDirty(metabuffer);
- if (RelationNeedsWAL(index) && !is_build)
+ if (needwal)
{
XLogRecPtr recptr;
ginxlogUpdateMeta data;
diff --git a/src/backend/access/gin/ginvacuum.c b/src/backend/access/gin/ginvacuum.c
index a276eb020b5..049b5bde0a2 100644
--- a/src/backend/access/gin/ginvacuum.c
+++ b/src/backend/access/gin/ginvacuum.c
@@ -16,6 +16,7 @@
#include "access/gin_private.h"
#include "access/ginxlog.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
@@ -136,6 +137,7 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
Page page,
parentPage;
BlockNumber rightlink;
+ bool needwal;
/*
* This function MUST be called only if someone of parent pages hold
@@ -159,6 +161,10 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
*/
PredicateLockPageCombine(gvs->index, deleteBlkno, rightlink);
+ needwal = RelationNeedsWAL(gvs->index);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/* Unlink the page by changing left sibling's rightlink */
@@ -195,7 +201,7 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
MarkBufferDirty(lBuffer);
MarkBufferDirty(dBuffer);
- if (RelationNeedsWAL(gvs->index))
+ if (needwal)
{
XLogRecPtr recptr;
ginxlogDeletePage data;
@@ -651,6 +657,9 @@ ginbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
if (resPage)
{
+ if (RelationNeedsWAL(gvs.index))
+ CheckWALPermitted();
+
START_CRIT_SECTION();
PageRestoreTempPage(resPage, page);
MarkBufferDirty(buffer);
diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 0683f42c258..3bb20b787ca 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -16,6 +16,7 @@
#include "access/gist_private.h"
#include "access/gistscan.h"
+#include "access/walprohibit.h"
#include "catalog/pg_collation.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
@@ -135,6 +136,9 @@ gistbuildempty(Relation index)
buffer = ReadBufferExtended(index, INIT_FORKNUM, P_NEW, RBM_NORMAL, NULL);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+ /* Building indexes will have an XID */
+ AssertWALPermittedHaveXID();
+
/* Initialize and xlog buffer */
START_CRIT_SECTION();
GISTInitBuffer(buffer, F_LEAF);
@@ -235,6 +239,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
XLogRecPtr recptr;
int i;
bool is_split;
+ bool needwal = RelationNeedsWAL(rel);
/*
* Refuse to modify a page that's incompletely split. This should not
@@ -469,8 +474,11 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
* insertion for that. NB: The number of pages and data segments
* specified here must match the calculations in gistXLogSplit()!
*/
- if (!is_build && RelationNeedsWAL(rel))
+ if (!is_build && needwal)
+ {
XLogEnsureRecordSpace(npage, 1 + npage * 2);
+ CheckWALPermitted();
+ }
START_CRIT_SECTION();
@@ -504,7 +512,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
recptr = GistBuildLSN;
else
{
- if (RelationNeedsWAL(rel))
+ if (needwal)
recptr = gistXLogSplit(is_leaf,
dist, oldrlink, oldnsn, leftchildbuf,
markfollowright);
@@ -530,6 +538,9 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
}
else
{
+ if (!is_build && needwal)
+ CheckWALPermitted();
+
/*
* Enough space. We always get here if ntup==0.
*/
@@ -571,7 +582,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
recptr = GistBuildLSN;
else
{
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
OffsetNumber ndeloffs = 0,
deloffs[1];
@@ -1645,6 +1656,7 @@ gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
int ndeletable = 0;
OffsetNumber offnum,
maxoff;
+ bool needwal = RelationNeedsWAL(rel);
Assert(GistPageIsLeaf(page));
@@ -1667,11 +1679,14 @@ gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
{
TransactionId latestRemovedXid = InvalidTransactionId;
- if (XLogStandbyInfoActive() && RelationNeedsWAL(rel))
+ if (XLogStandbyInfoActive() && needwal)
latestRemovedXid =
index_compute_xid_horizon_for_tuples(rel, heapRel, buffer,
deletable, ndeletable);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
PageIndexMultiDelete(page, deletable, ndeletable);
@@ -1688,7 +1703,7 @@ gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
MarkBufferDirty(buffer);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
XLogRecPtr recptr;
diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c
index 0663193531a..7af25731d3f 100644
--- a/src/backend/access/gist/gistvacuum.c
+++ b/src/backend/access/gist/gistvacuum.c
@@ -17,6 +17,7 @@
#include "access/genam.h"
#include "access/gist_private.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "commands/vacuum.h"
#include "lib/integerset.h"
#include "miscadmin.h"
@@ -272,6 +273,7 @@ gistvacuumpage(GistVacState *vstate, BlockNumber blkno, BlockNumber orig_blkno)
Buffer buffer;
Page page;
BlockNumber recurse_to;
+ bool needwal = RelationNeedsWAL(rel);
restart:
recurse_to = InvalidBlockNumber;
@@ -353,6 +355,9 @@ restart:
*/
if (ntodelete > 0)
{
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
MarkBufferDirty(buffer);
@@ -360,7 +365,7 @@ restart:
PageIndexMultiDelete(page, todelete, ntodelete);
GistMarkTuplesDeleted(page);
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
XLogRecPtr recptr;
@@ -589,6 +594,7 @@ gistdeletepage(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
IndexTuple idxtuple;
XLogRecPtr recptr;
FullTransactionId txid;
+ bool needwal = RelationNeedsWAL(info->index);
/*
* Check that the leaf is still empty and deletable.
@@ -643,6 +649,9 @@ gistdeletepage(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
*/
txid = ReadNextFullTransactionId();
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/* mark the page as deleted */
@@ -655,7 +664,7 @@ gistdeletepage(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
MarkBufferDirty(parentBuffer);
PageIndexTupleDelete(parentPage, downlink);
- if (RelationNeedsWAL(info->index))
+ if (needwal)
recptr = gistXLogPageDelete(leafBuffer, txid, parentBuffer, downlink);
else
recptr = gistGetFakeLSN(info->index);
diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c
index 0752fb38a92..52b2b36e4a9 100644
--- a/src/backend/access/hash/hash.c
+++ b/src/backend/access/hash/hash.c
@@ -22,6 +22,7 @@
#include "access/hash_xlog.h"
#include "access/relscan.h"
#include "access/tableam.h"
+#include "access/walprohibit.h"
#include "catalog/index.h"
#include "commands/progress.h"
#include "commands/vacuum.h"
@@ -468,6 +469,7 @@ hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
Buffer metabuf = InvalidBuffer;
HashMetaPage metap;
HashMetaPage cachedmetap;
+ bool needwal;
tuples_removed = 0;
num_index_tuples = 0;
@@ -574,6 +576,10 @@ loop_top:
goto loop_top;
}
+ needwal = RelationNeedsWAL(rel);
+ if (needwal)
+ CheckWALPermitted();
+
/* Okay, we're really done. Update tuple count in metapage. */
START_CRIT_SECTION();
@@ -604,7 +610,7 @@ loop_top:
MarkBufferDirty(metabuf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_hash_update_meta_page xlrec;
XLogRecPtr recptr;
@@ -691,6 +697,7 @@ hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
Buffer buf;
Bucket new_bucket PG_USED_FOR_ASSERTS_ONLY = InvalidBucket;
bool bucket_dirty = false;
+ bool needwal = RelationNeedsWAL(rel);
blkno = bucket_blkno;
buf = bucket_buf;
@@ -789,6 +796,9 @@ hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
*/
if (ndeletable > 0)
{
+ if (needwal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -810,7 +820,7 @@ hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
MarkBufferDirty(buf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_hash_delete xlrec;
XLogRecPtr recptr;
@@ -884,6 +894,9 @@ hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
page = BufferGetPage(bucket_buf);
bucket_opaque = (HashPageOpaque) PageGetSpecialPointer(page);
+ if (needwal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -891,7 +904,7 @@ hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
MarkBufferDirty(bucket_buf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
XLogRecPtr recptr;
diff --git a/src/backend/access/hash/hashinsert.c b/src/backend/access/hash/hashinsert.c
index d254a00b6ac..20ff6d051bf 100644
--- a/src/backend/access/hash/hashinsert.c
+++ b/src/backend/access/hash/hashinsert.c
@@ -17,6 +17,7 @@
#include "access/hash.h"
#include "access/hash_xlog.h"
+#include "access/walprohibit.h"
#include "miscadmin.h"
#include "storage/buf_internals.h"
#include "storage/lwlock.h"
@@ -193,6 +194,8 @@ restart_insert:
*/
LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
+ AssertWALPermittedHaveXID();
+
/* Do the update. No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -360,6 +363,7 @@ _hash_vacuum_one_page(Relation rel, Relation hrel, Buffer metabuf, Buffer buf)
if (ndeletable > 0)
{
TransactionId latestRemovedXid;
+ bool needwal = RelationNeedsWAL(rel);
latestRemovedXid =
index_compute_xid_horizon_for_tuples(rel, hrel, buf,
@@ -370,6 +374,9 @@ _hash_vacuum_one_page(Relation rel, Relation hrel, Buffer metabuf, Buffer buf)
*/
LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
+ if (needwal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -393,7 +400,7 @@ _hash_vacuum_one_page(Relation rel, Relation hrel, Buffer metabuf, Buffer buf)
MarkBufferDirty(metabuf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_hash_vacuum_one_page xlrec;
XLogRecPtr recptr;
diff --git a/src/backend/access/hash/hashovfl.c b/src/backend/access/hash/hashovfl.c
index 1ff2e0c18ee..a6ac301ba0d 100644
--- a/src/backend/access/hash/hashovfl.c
+++ b/src/backend/access/hash/hashovfl.c
@@ -19,6 +19,7 @@
#include "access/hash.h"
#include "access/hash_xlog.h"
+#include "access/walprohibit.h"
#include "miscadmin.h"
#include "utils/rel.h"
@@ -312,6 +313,8 @@ _hash_addovflpage(Relation rel, Buffer metabuf, Buffer buf, bool retain_pin)
found:
+ AssertWALPermittedHaveXID();
+
/*
* Do the update. No ereport(ERROR) until changes are logged. We want to
* log the changes for bitmap page and overflow page together to avoid
@@ -510,6 +513,7 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
Buffer prevbuf = InvalidBuffer;
Buffer nextbuf = InvalidBuffer;
bool update_metap = false;
+ bool needwal;
/* Get information from the doomed page */
_hash_checkpage(rel, ovflbuf, LH_OVERFLOW_PAGE);
@@ -573,9 +577,14 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
/* Get write-lock on metapage to update firstfree */
LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
+ needwal = RelationNeedsWAL(rel);
+
/* This operation needs to log multiple tuples, prepare WAL for that */
- if (RelationNeedsWAL(rel))
+ if (needwal)
+ {
+ CheckWALPermitted();
XLogEnsureRecordSpace(HASH_XLOG_FREE_OVFL_BUFS, 4 + nitups);
+ }
START_CRIT_SECTION();
@@ -641,7 +650,7 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
}
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_hash_squeeze_page xlrec;
XLogRecPtr recptr;
@@ -922,14 +931,19 @@ readpage:
if (nitups > 0)
{
+ bool needwal = RelationNeedsWAL(rel);
+
Assert(nitups == ndeletable);
/*
* This operation needs to log multiple tuples, prepare
* WAL for that.
*/
- if (RelationNeedsWAL(rel))
+ if (needwal)
+ {
+ CheckWALPermitted();
XLogEnsureRecordSpace(0, 3 + nitups);
+ }
START_CRIT_SECTION();
@@ -947,7 +961,7 @@ readpage:
MarkBufferDirty(rbuf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
XLogRecPtr recptr;
xl_hash_move_page_contents xlrec;
diff --git a/src/backend/access/hash/hashpage.c b/src/backend/access/hash/hashpage.c
index 49a98677876..f161a9a49eb 100644
--- a/src/backend/access/hash/hashpage.c
+++ b/src/backend/access/hash/hashpage.c
@@ -30,6 +30,7 @@
#include "access/hash.h"
#include "access/hash_xlog.h"
+#include "access/walprohibit.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/lmgr.h"
@@ -816,6 +817,8 @@ restart_expand:
goto fail;
}
+ AssertWALPermittedHaveXID();
+
/*
* Since we are scribbling on the pages in the shared buffers, establish a
* critical section. Any failure in this next code leaves us with a big
@@ -1172,6 +1175,8 @@ _hash_splitbucket(Relation rel,
if (PageGetFreeSpaceForMultipleTuples(npage, nitups + 1) < (all_tups_size + itemsz))
{
+ AssertWALPermittedHaveXID();
+
/*
* Change the shared buffer state in critical section,
* otherwise any error could make it unrecoverable.
@@ -1223,6 +1228,8 @@ _hash_splitbucket(Relation rel,
/* Exit loop if no more overflow pages in old bucket */
if (!BlockNumberIsValid(oblkno))
{
+ AssertWALPermittedHaveXID();
+
/*
* Change the shared buffer state in critical section, otherwise
* any error could make it unrecoverable.
@@ -1269,6 +1276,8 @@ _hash_splitbucket(Relation rel,
npage = BufferGetPage(bucket_nbuf);
nopaque = (HashPageOpaque) PageGetSpecialPointer(npage);
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
oopaque->hasho_flag &= ~LH_BUCKET_BEING_SPLIT;
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index ba36da2b83c..c0b8ccd1755 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -47,6 +47,7 @@
#include "access/transam.h"
#include "access/valid.h"
#include "access/visibilitymap.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
@@ -2132,6 +2133,8 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
*/
CheckForSerializableConflictIn(relation, NULL, InvalidBlockNumber);
+ AssertWALPermittedHaveXID();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -2455,6 +2458,8 @@ heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
if (starting_with_empty_page && (options & HEAP_INSERT_FROZEN))
all_frozen_set = true;
+ AssertWALPermittedHaveXID();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -3015,6 +3020,8 @@ l1:
xid, LockTupleExclusive, true,
&new_xmax, &new_infomask, &new_infomask2);
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/*
@@ -3770,6 +3777,8 @@ l2:
Assert(HEAP_XMAX_IS_LOCKED_ONLY(infomask_lock_old_tuple));
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/* Clear obsolete visibility flags ... */
@@ -3954,6 +3963,8 @@ l2:
bms_overlap(modified_attrs, id_attrs),
&old_key_copied);
+ AssertWALPermittedHaveXID();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -4886,6 +4897,8 @@ failed:
GetCurrentTransactionId(), mode, false,
&xid, &new_infomask, &new_infomask2);
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/*
@@ -5676,6 +5689,8 @@ l4:
VISIBILITYMAP_ALL_FROZEN))
cleared_all_frozen = true;
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/* ... and set them */
@@ -5834,6 +5849,8 @@ heap_finish_speculative(Relation relation, ItemPointer tid)
StaticAssertStmt(MaxOffsetNumber < SpecTokenOffsetNumber,
"invalid speculative token constant");
+ AssertWALPermittedHaveXID();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -5942,6 +5959,8 @@ heap_abort_speculative(Relation relation, ItemPointer tid)
elog(ERROR, "attempted to kill a non-speculative tuple");
Assert(!HeapTupleHeaderIsHeapOnly(tp.t_data));
+ AssertWALPermittedHaveXID();
+
/*
* No need to check for serializable conflicts here. There is never a
* need for a combo CID, either. No need to extract replica identity, or
@@ -6062,6 +6081,7 @@ heap_inplace_update(Relation relation, HeapTuple tuple)
HeapTupleHeader htup;
uint32 oldlen;
uint32 newlen;
+ bool needwal;
/*
* For now, we don't allow parallel updates. Unlike a regular update,
@@ -6092,6 +6112,10 @@ heap_inplace_update(Relation relation, HeapTuple tuple)
if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
elog(ERROR, "wrong tuple length");
+ needwal = RelationNeedsWAL(relation);
+ if (needwal)
+ CheckWALPermitted();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -6102,7 +6126,7 @@ heap_inplace_update(Relation relation, HeapTuple tuple)
MarkBufferDirty(buffer);
/* XLOG stuff */
- if (RelationNeedsWAL(relation))
+ if (needwal)
{
xl_heap_inplace xlrec;
XLogRecPtr recptr;
diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index 0c8e49d3e6c..bd2cf50f20a 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -18,6 +18,7 @@
#include "access/heapam_xlog.h"
#include "access/htup_details.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "miscadmin.h"
@@ -94,11 +95,11 @@ heap_page_prune_opt(Relation relation, Buffer buffer)
Size minfree;
/*
- * We can't write WAL in recovery mode, so there's no point trying to
+ * We can't write WAL during read-only mode, so there's no point trying to
* clean the page. The primary will likely issue a cleaning WAL record soon
* anyway, so this is no particular loss.
*/
- if (RecoveryInProgress())
+ if (!XLogInsertAllowed())
return;
/*
@@ -228,6 +229,7 @@ heap_page_prune(Relation relation, Buffer buffer,
OffsetNumber offnum,
maxoff;
PruneState prstate;
+ bool needwal;
/*
* Our strategy is to scan the page and make lists of items to change,
@@ -282,6 +284,10 @@ heap_page_prune(Relation relation, Buffer buffer,
if (off_loc)
*off_loc = InvalidOffsetNumber;
+ needwal = RelationNeedsWAL(relation);
+ if (needwal)
+ CheckWALPermitted();
+
/* Any error while applying the changes is critical */
START_CRIT_SECTION();
@@ -315,7 +321,7 @@ heap_page_prune(Relation relation, Buffer buffer,
/*
* Emit a WAL XLOG_HEAP2_PRUNE record showing what we did
*/
- if (RelationNeedsWAL(relation))
+ if (needwal)
{
xl_heap_prune xlrec;
XLogRecPtr recptr;
diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 47ac6385d12..a4436c5ab4b 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -59,6 +59,7 @@
#include "access/parallel.h"
#include "access/transam.h"
#include "access/visibilitymap.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/index.h"
@@ -1301,6 +1302,11 @@ lazy_scan_heap(LVRelState *vacrel, VacuumParams *params, bool aggressive)
*/
if (!PageIsAllVisible(page))
{
+ bool needwal = RelationNeedsWAL(vacrel->rel);
+
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/* mark buffer dirty before writing a WAL record */
@@ -1316,8 +1322,7 @@ lazy_scan_heap(LVRelState *vacrel, VacuumParams *params, bool aggressive)
* page has been previously WAL-logged, and if not, do that
* now.
*/
- if (RelationNeedsWAL(vacrel->rel) &&
- PageGetLSN(page) == InvalidXLogRecPtr)
+ if (needwal && PageGetLSN(page) == InvalidXLogRecPtr)
log_newpage_buffer(buf, true);
PageSetAllVisible(page);
@@ -1928,8 +1933,13 @@ retry:
*/
if (nfrozen > 0)
{
+ bool needwal = RelationNeedsWAL(vacrel->rel);
+
Assert(prunestate->hastup);
+ if (needwal)
+ CheckWALPermitted();
+
/*
* At least one tuple with storage needs to be frozen -- execute that
* now.
@@ -1954,7 +1964,7 @@ retry:
}
/* Now WAL-log freezing if necessary */
- if (RelationNeedsWAL(vacrel->rel))
+ if (needwal)
{
XLogRecPtr recptr;
@@ -2387,6 +2397,7 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
TransactionId visibility_cutoff_xid;
bool all_frozen;
LVSavedErrInfo saved_err_info;
+ bool needwal = RelationNeedsWAL(vacrel->rel);
Assert(vacrel->nindexes == 0 || vacrel->do_index_vacuuming);
@@ -2397,6 +2408,9 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
VACUUM_ERRCB_PHASE_VACUUM_HEAP, blkno,
InvalidOffsetNumber);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
for (; tupindex < dead_tuples->num_tuples; tupindex++)
@@ -2427,7 +2441,7 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
MarkBufferDirty(buffer);
/* XLOG stuff */
- if (RelationNeedsWAL(vacrel->rel))
+ if (needwal)
{
xl_heap_vacuum xlrec;
XLogRecPtr recptr;
diff --git a/src/backend/access/heap/visibilitymap.c b/src/backend/access/heap/visibilitymap.c
index e198df65d82..05f7de15c76 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -88,6 +88,7 @@
#include "access/heapam_xlog.h"
#include "access/visibilitymap.h"
+#include "access/walprohibit.h"
#include "access/xlog.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
@@ -249,6 +250,7 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
Page page;
uint8 *map;
+ bool needwal = RelationNeedsWAL(rel);
#ifdef TRACE_VISIBILITYMAP
elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
@@ -270,6 +272,16 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
map = (uint8 *) PageGetContents(page);
LockBuffer(vmBuf, BUFFER_LOCK_EXCLUSIVE);
+ /*
+ * Can reach here from VACUUM or from startup process, so need not have an
+ * XID.
+ *
+ * Recovery in the startup process never have wal prohibit state, skip
+ * permission check if reach here in the startup process.
+ */
+ if (needwal)
+ InRecovery ? AssertWALPermitted() : CheckWALPermitted();
+
if (flags != (map[mapByte] >> mapOffset & VISIBILITYMAP_VALID_BITS))
{
START_CRIT_SECTION();
@@ -277,7 +289,7 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
map[mapByte] |= (flags << mapOffset);
MarkBufferDirty(vmBuf);
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
if (XLogRecPtrIsInvalid(recptr))
{
@@ -476,6 +488,7 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
Buffer mapBuffer;
Page page;
char *map;
+ bool needwal;
newnblocks = truncBlock + 1;
@@ -489,8 +502,13 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
page = BufferGetPage(mapBuffer);
map = PageGetContents(page);
+ needwal = (!InRecovery && RelationNeedsWAL(rel) && XLogHintBitIsNeeded());
+
LockBuffer(mapBuffer, BUFFER_LOCK_EXCLUSIVE);
+ if (needwal)
+ CheckWALPermitted();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -518,7 +536,7 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
* during recovery.
*/
MarkBufferDirty(mapBuffer);
- if (!InRecovery && RelationNeedsWAL(rel) && XLogHintBitIsNeeded())
+ if (needwal)
log_newpage_buffer(mapBuffer, false);
END_CRIT_SECTION();
diff --git a/src/backend/access/nbtree/nbtdedup.c b/src/backend/access/nbtree/nbtdedup.c
index 854e3b2cf9a..71f5ade8c28 100644
--- a/src/backend/access/nbtree/nbtdedup.c
+++ b/src/backend/access/nbtree/nbtdedup.c
@@ -16,6 +16,7 @@
#include "access/nbtree.h"
#include "access/nbtxlog.h"
+#include "access/walprohibit.h"
#include "miscadmin.h"
#include "utils/rel.h"
@@ -229,6 +230,8 @@ _bt_dedup_pass(Relation rel, Buffer buf, Relation heapRel, IndexTuple newitem,
nopaque->btpo_flags &= ~BTP_HAS_GARBAGE;
}
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
PageRestoreTempPage(newpage, page);
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 6ac205c98ee..d1a51864aae 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -18,6 +18,7 @@
#include "access/nbtree.h"
#include "access/nbtxlog.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -1249,6 +1250,8 @@ _bt_insertonpg(Relation rel,
}
}
+ AssertWALPermittedHaveXID();
+
/* Do the update. No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -1899,13 +1902,16 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf,
ropaque->btpo_flags |= BTP_SPLIT_END;
}
+ AssertWALPermittedHaveXID();
+
/*
* Right sibling is locked, new siblings are prepared, but original page
* is not updated yet.
*
* NO EREPORT(ERROR) till right sibling is updated. We can get away with
* not starting the critical section till here because we haven't been
- * scribbling on the original page yet; see comments above.
+ * scribbling on the original page yet; see the comments above for grabbing
+ * the right sibling.
*/
START_CRIT_SECTION();
@@ -2467,6 +2473,8 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
right_item = CopyIndexTuple(item);
BTreeTupleSetDownLink(right_item, rbkno);
+ AssertWALPermittedHaveXID();
+
/* NO EREPORT(ERROR) from here till newroot op is logged */
START_CRIT_SECTION();
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 706e16ae949..44523f3c26c 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -26,6 +26,7 @@
#include "access/nbtxlog.h"
#include "access/tableam.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "miscadmin.h"
@@ -236,6 +237,7 @@ _bt_set_cleanup_info(Relation rel, BlockNumber num_delpages)
Buffer metabuf;
Page metapg;
BTMetaPageData *metad;
+ bool needwal;
/*
* On-disk compatibility note: The btm_last_cleanup_num_delpages metapage
@@ -272,6 +274,10 @@ _bt_set_cleanup_info(Relation rel, BlockNumber num_delpages)
_bt_unlockbuf(rel, metabuf);
_bt_lockbuf(rel, metabuf, BT_WRITE);
+ needwal = RelationNeedsWAL(rel);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/* upgrade meta-page if needed */
@@ -284,7 +290,7 @@ _bt_set_cleanup_info(Relation rel, BlockNumber num_delpages)
MarkBufferDirty(metabuf);
/* write wal record if needed */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_btree_metadata md;
XLogRecPtr recptr;
@@ -403,6 +409,7 @@ _bt_getroot(Relation rel, int access)
if (metad->btm_root == P_NONE)
{
Page metapg;
+ bool needwal;
/* If access = BT_READ, caller doesn't want us to create root yet */
if (access == BT_READ)
@@ -448,6 +455,10 @@ _bt_getroot(Relation rel, int access)
/* Get raw page pointer for metapage */
metapg = BufferGetPage(metabuf);
+ needwal = RelationNeedsWAL(rel);
+ if (needwal)
+ CheckWALPermitted();
+
/* NO ELOG(ERROR) till meta is updated */
START_CRIT_SECTION();
@@ -466,7 +477,7 @@ _bt_getroot(Relation rel, int access)
MarkBufferDirty(metabuf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_btree_newroot xlrec;
XLogRecPtr recptr;
@@ -1184,6 +1195,9 @@ _bt_delitems_vacuum(Relation rel, Buffer buf,
updatedoffsets, &updatedbuflen,
needswal);
+ if (needswal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -1314,6 +1328,8 @@ _bt_delitems_delete(Relation rel, Buffer buf, TransactionId latestRemovedXid,
updatedoffsets, &updatedbuflen,
needswal);
+ AssertWALPermittedHaveXID();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -2098,6 +2114,7 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
OffsetNumber nextoffset;
IndexTuple itup;
IndexTupleData trunctuple;
+ bool needwal;
page = BufferGetPage(leafbuf);
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
@@ -2186,6 +2203,10 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
*/
PredicateLockPageCombine(rel, leafblkno, leafrightsib);
+ needwal = RelationNeedsWAL(rel);
+ if (needwal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -2237,7 +2258,7 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
MarkBufferDirty(leafbuf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_btree_mark_page_halfdead xlrec;
XLogRecPtr recptr;
@@ -2324,6 +2345,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, BlockNumber scanblkno,
uint32 targetlevel;
IndexTuple leafhikey;
BlockNumber leaftopparent;
+ bool needwal;
page = BufferGetPage(leafbuf);
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
@@ -2555,6 +2577,10 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, BlockNumber scanblkno,
* Here we begin doing the deletion.
*/
+ needwal = RelationNeedsWAL(rel);
+ if (needwal)
+ CheckWALPermitted();
+
/* No ereport(ERROR) until changes are logged */
START_CRIT_SECTION();
@@ -2631,7 +2657,7 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, BlockNumber scanblkno,
MarkBufferDirty(leafbuf);
/* XLOG stuff */
- if (RelationNeedsWAL(rel))
+ if (needwal)
{
xl_btree_unlink_page xlrec;
xl_btree_metadata xlmeta;
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index 4d380c99f06..df9506b7cd5 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -18,6 +18,7 @@
#include "access/genam.h"
#include "access/spgist_private.h"
#include "access/spgxlog.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -214,6 +215,8 @@ addLeafTuple(Relation index, SpGistState *state, SpGistLeafTuple leafTuple,
xlrec.offnumParent = InvalidOffsetNumber;
xlrec.nodeI = 0;
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
if (current->offnum == InvalidOffsetNumber ||
@@ -458,6 +461,8 @@ moveLeafs(Relation index, SpGistState *state,
leafdata = leafptr = palloc(size);
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/* copy all the old tuples to new page, unless they're dead */
@@ -1130,6 +1135,8 @@ doPickSplit(Relation index, SpGistState *state,
leafdata = leafptr = (char *) palloc(totalLeafSizes);
+ AssertWALPermittedHaveXID();
+
/* Here we begin making the changes to the target pages */
START_CRIT_SECTION();
@@ -1538,6 +1545,8 @@ spgAddNodeAction(Relation index, SpGistState *state,
if (PageGetExactFreeSpace(current->page) >=
newInnerTuple->size - innerTuple->size)
{
+ AssertWALPermittedHaveXID();
+
/*
* We can replace the inner tuple by new version in-place
*/
@@ -1624,6 +1633,8 @@ spgAddNodeAction(Relation index, SpGistState *state,
else
xlrec.parentBlk = 2;
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/* insert new ... */
@@ -1809,6 +1820,8 @@ spgSplitNodeAction(Relation index, SpGistState *state,
&xlrec.newPage);
}
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
/*
diff --git a/src/backend/access/spgist/spgvacuum.c b/src/backend/access/spgist/spgvacuum.c
index 76fb0374c42..b3f00f28b16 100644
--- a/src/backend/access/spgist/spgvacuum.c
+++ b/src/backend/access/spgist/spgvacuum.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "access/xloginsert.h"
#include "catalog/storage_xlog.h"
#include "commands/vacuum.h"
@@ -139,6 +140,7 @@ vacuumLeafPage(spgBulkDeleteState *bds, Relation index, Buffer buffer,
int nDeletable;
OffsetNumber i,
max = PageGetMaxOffsetNumber(page);
+ bool needwal;
memset(predecessor, 0, sizeof(predecessor));
memset(deletable, 0, sizeof(deletable));
@@ -323,6 +325,10 @@ vacuumLeafPage(spgBulkDeleteState *bds, Relation index, Buffer buffer,
if (nDeletable != xlrec.nDead + xlrec.nPlaceholder + xlrec.nMove)
elog(ERROR, "inconsistent counts of deletable tuples");
+ needwal = RelationNeedsWAL(index);
+ if (needwal)
+ CheckWALPermitted();
+
/* Do the updates */
START_CRIT_SECTION();
@@ -371,7 +377,7 @@ vacuumLeafPage(spgBulkDeleteState *bds, Relation index, Buffer buffer,
MarkBufferDirty(buffer);
- if (RelationNeedsWAL(index))
+ if (needwal)
{
XLogRecPtr recptr;
@@ -411,6 +417,7 @@ vacuumLeafRoot(spgBulkDeleteState *bds, Relation index, Buffer buffer)
OffsetNumber toDelete[MaxIndexTuplesPerPage];
OffsetNumber i,
max = PageGetMaxOffsetNumber(page);
+ bool needwal;
xlrec.nDelete = 0;
@@ -447,6 +454,10 @@ vacuumLeafRoot(spgBulkDeleteState *bds, Relation index, Buffer buffer)
if (xlrec.nDelete == 0)
return; /* nothing more to do */
+ needwal = RelationNeedsWAL(index);
+ if (needwal)
+ CheckWALPermitted();
+
/* Do the update */
START_CRIT_SECTION();
@@ -455,7 +466,7 @@ vacuumLeafRoot(spgBulkDeleteState *bds, Relation index, Buffer buffer)
MarkBufferDirty(buffer);
- if (RelationNeedsWAL(index))
+ if (needwal)
{
XLogRecPtr recptr;
@@ -502,6 +513,7 @@ vacuumRedirectAndPlaceholder(Relation index, Buffer buffer)
OffsetNumber itemnos[MaxIndexTuplesPerPage];
spgxlogVacuumRedirect xlrec;
GlobalVisState *vistest;
+ bool needwal;
xlrec.nToPlaceholder = 0;
xlrec.newestRedirectXid = InvalidTransactionId;
@@ -509,6 +521,10 @@ vacuumRedirectAndPlaceholder(Relation index, Buffer buffer)
/* XXX: providing heap relation would allow more pruning */
vistest = GlobalVisTestFor(NULL);
+ needwal = RelationNeedsWAL(index);
+ if (needwal)
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/*
@@ -584,7 +600,7 @@ vacuumRedirectAndPlaceholder(Relation index, Buffer buffer)
if (hasUpdate)
MarkBufferDirty(buffer);
- if (hasUpdate && RelationNeedsWAL(index))
+ if (hasUpdate && needwal)
{
XLogRecPtr recptr;
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index ec742f86b50..4d215b1c8dc 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -73,6 +73,7 @@
#include "access/transam.h"
#include "access/twophase.h"
#include "access/twophase_rmgr.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
@@ -1162,6 +1163,8 @@ GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
ExtendMultiXactMember(nextOffset, nmembers);
+ CheckWALPermitted();
+
/*
* Critical section from here until caller has written the data into the
* just-reserved SLRU space; we don't want to error out with a partly
@@ -2946,7 +2949,7 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
mxtruncinfo trunc;
MultiXactId earliest;
- Assert(!RecoveryInProgress());
+ Assert(XLogInsertAllowed());
Assert(MultiXactState->finishedStartup);
/*
diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 46f3d082492..eb9174cedf9 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -82,6 +82,7 @@
#include "access/transam.h"
#include "access/twophase.h"
#include "access/twophase_rmgr.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
@@ -1107,6 +1108,8 @@ EndPrepare(GlobalTransaction gxact)
*/
XLogEnsureRecordSpace(0, records.num_chunks);
+ AssertWALPermittedHaveXID();
+
START_CRIT_SECTION();
MyProc->delayChkpt = true;
@@ -2208,6 +2211,9 @@ RecordTransactionCommitPrepared(TransactionId xid,
replorigin = (replorigin_session_origin != InvalidRepOriginId &&
replorigin_session_origin != DoNotReplicateId);
+ /* COMMIT PREPARED need not have an XID */
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/* See notes in RecordTransactionCommit */
@@ -2306,6 +2312,9 @@ RecordTransactionAbortPrepared(TransactionId xid,
elog(PANIC, "cannot abort transaction %u, it was already committed",
xid);
+ /* ROLLBACK PREPARED need not have an XID */
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/*
diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c
index 142da4aaff3..ad421426955 100644
--- a/src/backend/access/transam/varsup.c
+++ b/src/backend/access/transam/varsup.c
@@ -17,6 +17,7 @@
#include "access/commit_ts.h"
#include "access/subtrans.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "commands/dbcommands.h"
@@ -75,6 +76,9 @@ GetNewTransactionId(bool isSubXact)
if (RecoveryInProgress())
elog(ERROR, "cannot assign TransactionIds during recovery");
+ /* do not assign transaction id in read-only mode */
+ CheckWALPermitted();
+
LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
full_xid = ShmemVariableCache->nextXid;
diff --git a/src/backend/access/transam/walprohibit.c b/src/backend/access/transam/walprohibit.c
index c727775b017..48a06709cb0 100644
--- a/src/backend/access/transam/walprohibit.c
+++ b/src/backend/access/transam/walprohibit.c
@@ -26,6 +26,16 @@
#include "utils/fmgroids.h"
#include "utils/fmgrprotos.h"
+/*
+ * Assert flag to enforce WAL insert permission check rule before starting a
+ * critical section for the WAL writes. For this, either of
+ * CheckWALPermitted(), AssertWALPermittedHaveXID(), or AssertWALPermitted()
+ * must be called before starting the critical section.
+ */
+#ifdef USE_ASSERT_CHECKING
+WALPermitCheckState walpermit_checked_state = WALPERMIT_UNCHECKED;
+#endif
+
/*
* Private state.
*/
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 6c609e0e4b4..b0363e82c77 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -26,6 +26,7 @@
#include "access/subtrans.h"
#include "access/transam.h"
#include "access/twophase.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
@@ -1317,6 +1318,8 @@ RecordTransactionCommit(void)
/* Tell bufmgr and smgr to prepare for commit */
BufmgrCommit();
+ AssertWALPermittedHaveXID();
+
/*
* Mark ourselves as within our "commit critical section". This
* forces any concurrent checkpoint to wait until we've updated
@@ -1677,6 +1680,9 @@ RecordTransactionAbort(bool isSubXact)
elog(PANIC, "cannot abort transaction %u, it was already committed",
xid);
+ /* We'll be reaching here with valid XID only. */
+ AssertWALPermittedHaveXID();
+
/* Fetch the data we need for the abort record */
nrels = smgrGetPendingDeletes(false, &rels);
nchildren = xactGetCommittedChildren(&children);
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 82ac0bc712a..171f43820fb 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -1037,7 +1037,7 @@ XLogInsertRecord(XLogRecData *rdata,
/* cross-check on whether we should be here or not */
if (!XLogInsertAllowed())
- elog(ERROR, "cannot make new WAL entries during recovery");
+ elog(ERROR, "cannot make new WAL entries now");
/*----------
*
@@ -2890,9 +2890,11 @@ XLogFlush(XLogRecPtr record)
* trying to flush the WAL, we should update minRecoveryPoint instead. We
* test XLogInsertAllowed(), not InRecovery, because we need checkpointer
* to act this way too, and because when it tries to write the
- * end-of-recovery checkpoint, it should indeed flush.
+ * end-of-recovery checkpoint, it should indeed flush. Also, WAL prohibit
+ * state should not restrict WAL flushing. Otherwise, the dirty buffer
+ * cannot be evicted until WAL has been flushed up to the buffer's LSN.
*/
- if (!XLogInsertAllowed())
+ if (!XLogInsertAllowed() && !IsWALProhibited())
{
UpdateMinRecoveryPoint(record, false);
return;
@@ -9326,6 +9328,9 @@ CreateCheckPoint(int flags)
if (!shutdown && XLogStandbyInfoActive())
LogStandbySnapshot();
+ /* Error out if wal writes are disabled. */
+ CheckWALPermitted();
+
START_CRIT_SECTION();
/*
@@ -9483,6 +9488,8 @@ CreateEndOfRecoveryRecord(void)
LocalSetXLogInsertAllowed();
+ AssertWALPermitted();
+
START_CRIT_SECTION();
XLogBeginInsert();
@@ -10135,7 +10142,7 @@ void
UpdateFullPageWrites(void)
{
XLogCtlInsert *Insert = &XLogCtl->Insert;
- bool recoveryInProgress;
+ bool WALInsertAllowed;
/*
* Do nothing if full_page_writes has not been changed.
@@ -10149,10 +10156,10 @@ UpdateFullPageWrites(void)
/*
* Perform this outside critical section so that the WAL insert
- * initialization done by RecoveryInProgress() doesn't trigger an
- * assertion failure.
+ * initialization done by XLogInsertAllowed() doesn't trigger an assertion
+ * failure.
*/
- recoveryInProgress = RecoveryInProgress();
+ WALInsertAllowed = XLogInsertAllowed();
START_CRIT_SECTION();
@@ -10174,8 +10181,11 @@ UpdateFullPageWrites(void)
* Write an XLOG_FPW_CHANGE record. This allows us to keep track of
* full_page_writes during archive recovery, if required.
*/
- if (XLogStandbyInfoActive() && !recoveryInProgress)
+ if (XLogStandbyInfoActive() && WALInsertAllowed)
{
+ /* Assured that WAL permission has been checked */
+ AssertWALPermitted();
+
XLogBeginInsert();
XLogRegisterData((char *) (&fullPageWrites), sizeof(bool));
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index 7052dc245ee..a925cc9d5cd 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -19,6 +19,7 @@
#include "postgres.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
@@ -126,9 +127,14 @@ XLogBeginInsert(void)
Assert(mainrdata_last == (XLogRecData *) &mainrdata_head);
Assert(mainrdata_len == 0);
+ /*
+ * WAL permission must have checked before entering the critical section.
+ * Otherwise, WAL prohibited error will force system panic.
+ */
+ Assert(walpermit_checked_state != WALPERMIT_UNCHECKED || !CritSectionCount);
+
/* cross-check on whether we should be here or not */
- if (!XLogInsertAllowed())
- elog(ERROR, "cannot make new WAL entries during recovery");
+ CheckWALPermitted();
if (begininsert_called)
elog(ERROR, "XLogBeginInsert was already called");
@@ -210,6 +216,9 @@ XLogResetInsertion(void)
mainrdata_last = (XLogRecData *) &mainrdata_head;
curinsert_flags = 0;
begininsert_called = false;
+
+ /* Reset walpermit_checked flag */
+ RESET_WALPERMIT_CHECKED_STATE();
}
/*
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 0415df9ccb7..ab108e621d4 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -20,6 +20,7 @@
#include "access/relation.h"
#include "access/table.h"
#include "access/transam.h"
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
@@ -378,8 +379,13 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
/* check the comment above nextval_internal()'s equivalent call. */
if (RelationNeedsWAL(rel))
+ {
GetTopTransactionId();
+ /* Cannot have valid XID without WAL permission */
+ AssertWALPermittedHaveXID();
+ }
+
START_CRIT_SECTION();
MarkBufferDirty(buf);
@@ -766,8 +772,13 @@ nextval_internal(Oid relid, bool check_permissions)
* (Have to do that here, so we're outside the critical section)
*/
if (logit && RelationNeedsWAL(seqrel))
+ {
GetTopTransactionId();
+ /* Cannot have valid XID without WAL permission */
+ AssertWALPermittedHaveXID();
+ }
+
/* ready to change the on-disk (or really, in-buffer) tuple */
START_CRIT_SECTION();
@@ -977,8 +988,13 @@ do_setval(Oid relid, int64 next, bool iscalled)
/* check the comment above nextval_internal()'s equivalent call. */
if (RelationNeedsWAL(seqrel))
+ {
GetTopTransactionId();
+ /* Cannot have valid XID without WAL permission */
+ AssertWALPermittedHaveXID();
+ }
+
/* ready to change the on-disk (or really, in-buffer) tuple */
START_CRIT_SECTION();
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 833c7f5139b..8ce2eefd52e 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -501,11 +501,14 @@ check_transaction_read_only(bool *newval, void **extra, GucSource source)
GUC_check_errmsg("transaction read-write mode must be set before any query");
return false;
}
- /* Can't go to r/w mode while recovery is still active */
- if (RecoveryInProgress())
+ /*
+ * Can't go to r/w mode while recovery is still active or while in WAL
+ * prohibit state
+ */
+ if (!XLogInsertAllowed())
{
GUC_check_errcode(ERRCODE_FEATURE_NOT_SUPPORTED);
- GUC_check_errmsg("cannot set transaction read-write mode during recovery");
+ GUC_check_errmsg("cannot set transaction read-write mode while system is read only");
return false;
}
/* Can't go to r/w mode while WAL is prohibited */
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 76affb7b549..ec05110b571 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -935,6 +935,10 @@ RequestCheckpoint(int flags)
int old_failed,
old_started;
+ /* The checkpoint is allowed in recovery but not in WAL prohibit state */
+ if (!RecoveryInProgress())
+ CheckWALPermitted();
+
/*
* If in a standalone backend, just do it ourselves.
*/
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 0c5b87864b9..8335382870f 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -3904,13 +3904,15 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
{
/*
* If we must not write WAL, due to a relfilenode-specific
- * condition or being in recovery, don't dirty the page. We can
- * set the hint, just not dirty the page as a result so the hint
- * is lost when we evict the page or shutdown.
+ * condition or in general, don't dirty the page. We can
+ * set the hint, but must not dirty the page as a result, lest
+ * we trigger WAL generation. Unless the page is dirtied again
+ * later, the hint will be lost when the page is evicted, or at
+ * shutdown.
*
* See src/backend/storage/page/README for longer discussion.
*/
- if (RecoveryInProgress() ||
+ if (!XLogInsertAllowed() ||
RelFileNodeSkippingWAL(bufHdr->tag.rnode))
return;
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index 8c12dda2380..41ba8c59a88 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -24,6 +24,7 @@
#include "postgres.h"
#include "access/htup_details.h"
+#include "access/walprohibit.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "storage/freespace.h"
@@ -285,12 +286,19 @@ FreeSpaceMapPrepareTruncateRel(Relation rel, BlockNumber nblocks)
*/
if (first_removed_slot > 0)
{
+ bool needwal;
+
buf = fsm_readbuf(rel, first_removed_address, false);
if (!BufferIsValid(buf))
return InvalidBlockNumber; /* nothing to do; the FSM was already
* smaller */
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
+ needwal = (!InRecovery && RelationNeedsWAL(rel) && XLogHintBitIsNeeded());
+
+ if (needwal)
+ CheckWALPermitted();
+
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();
@@ -305,7 +313,7 @@ FreeSpaceMapPrepareTruncateRel(Relation rel, BlockNumber nblocks)
* during recovery.
*/
MarkBufferDirty(buf);
- if (!InRecovery && RelationNeedsWAL(rel) && XLogHintBitIsNeeded())
+ if (needwal)
log_newpage_buffer(buf, false);
END_CRIT_SECTION();
diff --git a/src/backend/utils/cache/relmapper.c b/src/backend/utils/cache/relmapper.c
index 424624cf0da..d91e247f3e4 100644
--- a/src/backend/utils/cache/relmapper.c
+++ b/src/backend/utils/cache/relmapper.c
@@ -43,6 +43,7 @@
#include <sys/stat.h>
#include <unistd.h>
+#include "access/walprohibit.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
@@ -841,6 +842,8 @@ write_relmap_file(bool shared, RelMapFile *newmap,
xl_relmap_update xlrec;
XLogRecPtr lsn;
+ AssertWALPermittedHaveXID();
+
/* now errors are fatal ... */
START_CRIT_SECTION();
diff --git a/src/include/access/walprohibit.h b/src/include/access/walprohibit.h
index 2fad0629202..807fbd45273 100644
--- a/src/include/access/walprohibit.h
+++ b/src/include/access/walprohibit.h
@@ -55,4 +55,50 @@ GetWALProhibitState(uint32 wal_prohibit_counter)
return (WALProhibitState) (wal_prohibit_counter & 3);
}
+/* Never reaches when WAL is prohibited. */
+static inline void
+AssertWALPermitted(void)
+{
+ /*
+ * Recovery in the startup process never is in wal prohibited state.
+ */
+ Assert(InRecovery || XLogInsertAllowed());
+
+#ifdef USE_ASSERT_CHECKING
+ walpermit_checked_state = WALPERMIT_CHECKED;
+#endif
+}
+
+/*
+ * XID-bearing transactions are killed off by executing pg_prohibit_wal()
+ * function, so any part of the code that can only be reached with an XID
+ * assigned is never reached when WAL is prohibited.
+ */
+static inline void
+AssertWALPermittedHaveXID(void)
+{
+ /* Must be performing an INSERT, UPDATE or DELETE, so we'll have an XID */
+ Assert(FullTransactionIdIsValid(GetTopFullTransactionIdIfAny()));
+ AssertWALPermitted();
+}
+
+/*
+ * In opposite to the above assertion if a transaction doesn't have valid XID
+ * (e.g. VACUUM) then it won't be killed while changing the system state to WAL
+ * prohibited. Therefore, we need to explicitly error out before entering into
+ * the critical section.
+ */
+static inline void
+CheckWALPermitted(void)
+{
+ if (!XLogInsertAllowed())
+ ereport(ERROR,
+ (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
+ errmsg("system is now read only")));
+
+#ifdef USE_ASSERT_CHECKING
+ walpermit_checked_state = WALPERMIT_CHECKED;
+#endif
+}
+
#endif /* WALPROHIBIT_H */
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index 95202d37af5..f18232cbf53 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -97,12 +97,37 @@ extern PGDLLIMPORT volatile uint32 CritSectionCount;
/* in tcop/postgres.c */
extern void ProcessInterrupts(void);
+#ifdef USE_ASSERT_CHECKING
+typedef enum
+{
+ WALPERMIT_UNCHECKED,
+ WALPERMIT_CHECKED,
+ WALPERMIT_CHECKED_AND_USED
+} WALPermitCheckState;
+
+/* in access/walprohibit.c */
+extern WALPermitCheckState walpermit_checked_state;
+
+/*
+ * Reset walpermit_checked flag when no longer in the critical section.
+ * Otherwise, marked checked and used.
+ */
+#define RESET_WALPERMIT_CHECKED_STATE() \
+do { \
+ walpermit_checked_state = CritSectionCount ? \
+ WALPERMIT_CHECKED_AND_USED : WALPERMIT_UNCHECKED; \
+} while(0)
+#else
+#define RESET_WALPERMIT_CHECKED_STATE() ((void) 0)
+#endif
+
#ifndef WIN32
#define CHECK_FOR_INTERRUPTS() \
do { \
if (unlikely(InterruptPending)) \
ProcessInterrupts(); \
+ RESET_WALPERMIT_CHECKED_STATE(); \
} while(0)
#else /* WIN32 */
@@ -112,6 +137,7 @@ do { \
pgwin32_dispatch_queued_signals(); \
if (unlikely(InterruptPending)) \
ProcessInterrupts(); \
+ RESET_WALPERMIT_CHECKED_STATE(); \
} while(0)
#endif /* WIN32 */
@@ -138,6 +164,7 @@ do { \
do { \
Assert(CritSectionCount > 0); \
CritSectionCount--; \
+ RESET_WALPERMIT_CHECKED_STATE(); \
} while(0)
--
2.18.0