From 3d6c8cf64b2600d5884d7ae75a302a066a6ac14a Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Fri, 4 Apr 2025 14:32:29 +0300 Subject: [PATCH 1/2] Move CheckPointBuffers to PreCheckPointGuts to save FSM/VM pages on normal shutdown --- src/backend/access/transam/xlog.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index ae7a0556ed5..1f68fc4990d 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -9961,7 +9961,10 @@ static void PreCheckPointGuts(int flags) { if (flags & CHECKPOINT_IS_SHUTDOWN) + { CheckPointReplicationState(); + CheckPointBuffers(flags); + } } /* @@ -9984,7 +9987,8 @@ CheckPointGuts(XLogRecPtr checkPointRedo, int flags) CheckPointSUBTRANS(); CheckPointMultiXact(); CheckPointPredicate(); - CheckPointBuffers(flags); + if (!(flags & CHECKPOINT_IS_SHUTDOWN)) + CheckPointBuffers(flags); /* Perform all queued up fsyncs */ TRACE_POSTGRESQL_BUFFER_CHECKPOINT_SYNC_START(); From a0391901a2af13aa029b905272a5b2024133c926 Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Fri, 4 Apr 2025 21:37:46 +0300 Subject: [PATCH 2/2] Add comment explaning why CheckPointBuffers has to be called in PreCheckPointGuts --- src/backend/access/transam/xlog.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 1f68fc4990d..54c3dab7ad9 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -9955,7 +9955,8 @@ CheckPointReplicationState(void) /* * NEON: we use logical records to persist information of about slots, origins, relation map... * If it is done inside shutdown checkpoint, then Postgres panics: "concurrent write-ahead log activity while database system is shutting down" - * So it before checkpoint REDO position is determined. + * So do it before checkpoint REDO position is determined. + * The same is true for CheckPointBuffers which wallog dirty FSM/VM pages. */ static void PreCheckPointGuts(int flags) @@ -9987,6 +9988,12 @@ CheckPointGuts(XLogRecPtr checkPointRedo, int flags) CheckPointSUBTRANS(); CheckPointMultiXact(); CheckPointPredicate(); + /* + * NEON: Checkpoint buffer will write dirty pages to the disk and Neon SMGR + * wallog FSM/VM pages to persist them at page server. + * Writing to the WAL during shutdown checkpoint cause Postgres panic. + * So do it before in PreCheckPointGuts. + */ if (!(flags & CHECKPOINT_IS_SHUTDOWN)) CheckPointBuffers(flags);