From a8fbe864c5192b4b5ca1b91af30dfd597c2bb5e6 Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Fri, 4 Apr 2025 14:32:16 +0300 Subject: [PATCH 1/2] Move CheckPointBuffers to PreCheckPointGuts to save FSM/VM pages on normal shutdown --- src/backend/access/transam/xlog.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index e99b520c337..4d0640f3a94 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7058,6 +7058,7 @@ PreCheckPointGuts(int flags) if (flags & CHECKPOINT_IS_SHUTDOWN) { CheckPointReplicationState(); + CheckPointBuffers(flags); /* * pgstat_write_statsfile will be called later by before_shmem_exit() hook, but by then it's too late * to write WAL records. In Neon, pgstat_write_statsfile() writes the pgstats file to the WAL, so we have @@ -7087,7 +7088,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 aeb292eeace9072e07071254b6ffc7a74007d4d2 Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Fri, 4 Apr 2025 21:36:17 +0300 Subject: [PATCH 2/2] Add comment explaning why CheckPointBuffers has to be called in PreCheckPointGuts --- src/backend/access/transam/xlog.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 4d0640f3a94..cf9ee839897 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7051,6 +7051,7 @@ 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 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) @@ -7059,10 +7060,11 @@ PreCheckPointGuts(int flags) { CheckPointReplicationState(); CheckPointBuffers(flags); + /* * pgstat_write_statsfile will be called later by before_shmem_exit() hook, but by then it's too late * to write WAL records. In Neon, pgstat_write_statsfile() writes the pgstats file to the WAL, so we have - * to call it earlier. (The call that happens later is useless, but it doesn't do any harm either) + * to call it earlier. (The call that happens later is useless, but it doesn't do any harm either) */ pgstat_write_statsfile(); } @@ -7088,6 +7090,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);