From 8d085c3c939d6f60b7c6821b550f989a3f8e5a51 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 9 Jul 2025 14:19:21 -0500 Subject: [PATCH v8 5/5] Add FLUSH_UNLOGGED option to CHECKPOINT command. This option, which is disabled by default, can be used to request the checkpoint also flush data files for unlogged relations. As with the MODE option, the server may consolidate the options for concurrently requested checkpoints. For example, if one session uses (FLUSH_UNLOGGED FALSE) and another uses (FLUSH_UNLOGGED TRUE), the server may perform one checkpoint with FLUSH_UNLOGGED enabled. Author: Christoph Berg Reviewed-by: Laurenz Albe Discussion: https://postgr.es/m/aDnaKTEf-0dLiEfz%40msg.df7cb.de --- doc/src/sgml/ref/checkpoint.sgml | 26 ++++++++++++++++++++++++++ src/backend/postmaster/checkpointer.c | 4 ++++ src/bin/psql/tab-complete.in.c | 2 +- src/test/regress/expected/stats.out | 2 +- src/test/regress/sql/stats.sql | 2 +- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/ref/checkpoint.sgml b/doc/src/sgml/ref/checkpoint.sgml index d66aa967afc..8054de78a9b 100644 --- a/doc/src/sgml/ref/checkpoint.sgml +++ b/doc/src/sgml/ref/checkpoint.sgml @@ -25,6 +25,7 @@ CHECKPOINT [ ( option [, ...] ) ] where option can be one of: + FLUSH_UNLOGGED [ boolean ] MODE { FAST | SPREAD } @@ -75,6 +76,17 @@ CHECKPOINT [ ( option [, ...] ) ] Parameters + + FLUSH_UNLOGGED + + + Normally, CHECKPOINT does not flush data files for + unlogged relations. This option, which is disabled by default, enables + flushing unlogged relations. + + + + MODE @@ -92,6 +104,20 @@ CHECKPOINT [ ( option [, ...] ) ] + + + boolean + + + Specifies whether the selected option should be turned on or off. + You can write TRUE, ON, or + 1 to enable the option, and FALSE, + OFF, or 0 to disable it. The + boolean value can also + be omitted, in which case TRUE is assumed. + + + diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c index 723322e3e03..a3e4e70ad66 100644 --- a/src/backend/postmaster/checkpointer.c +++ b/src/backend/postmaster/checkpointer.c @@ -989,6 +989,7 @@ void ExecCheckpoint(ParseState *pstate, CheckPointStmt *stmt) { bool fast = true; + bool unlogged = false; foreach_ptr(DefElem, opt, stmt->options) { @@ -1004,6 +1005,8 @@ ExecCheckpoint(ParseState *pstate, CheckPointStmt *stmt) errmsg("unrecognized MODE option \"%s\"", opt->defname), parser_errposition(pstate, opt->location))); } + else if (strcmp(opt->defname, "flush_unlogged") == 0) + unlogged = defGetBoolean(opt); else ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), @@ -1022,6 +1025,7 @@ ExecCheckpoint(ParseState *pstate, CheckPointStmt *stmt) RequestCheckpoint(CHECKPOINT_WAIT | (fast ? CHECKPOINT_FAST : 0) | + (unlogged ? CHECKPOINT_FLUSH_UNLOGGED : 0) | (RecoveryInProgress() ? 0 : CHECKPOINT_FORCE)); } diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index a7db04efd93..c7e6e8b16a5 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -3165,7 +3165,7 @@ match_previous_words(int pattern_id, * one word, so the above test is correct. */ if (ends_with(prev_wd, '(') || ends_with(prev_wd, ',')) - COMPLETE_WITH("MODE"); + COMPLETE_WITH("MODE, FLUSH_UNLOGGED"); else if (TailMatches("MODE")) COMPLETE_WITH("FAST", "SPREAD"); } diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out index 02cdc3cbcf6..6ebcc292834 100644 --- a/src/test/regress/expected/stats.out +++ b/src/test/regress/expected/stats.out @@ -930,7 +930,7 @@ DROP TABLE test_stats_temp; -- While at it, test checkpoint options. Note that we don't test MODE SPREAD -- because it would prolong the test. CHECKPOINT (MODE FAST); -CHECKPOINT; +CHECKPOINT (FLUSH_UNLOGGED); SELECT num_requested > :rqst_ckpts_before FROM pg_stat_checkpointer; ?column? ---------- diff --git a/src/test/regress/sql/stats.sql b/src/test/regress/sql/stats.sql index 5ed71d7054e..dab19918056 100644 --- a/src/test/regress/sql/stats.sql +++ b/src/test/regress/sql/stats.sql @@ -443,7 +443,7 @@ DROP TABLE test_stats_temp; -- While at it, test checkpoint options. Note that we don't test MODE SPREAD -- because it would prolong the test. CHECKPOINT (MODE FAST); -CHECKPOINT; +CHECKPOINT (FLUSH_UNLOGGED); SELECT num_requested > :rqst_ckpts_before FROM pg_stat_checkpointer; SELECT wal_bytes > :wal_bytes_before FROM pg_stat_wal; -- 2.39.5 (Apple Git-154)