From e35b4c5425cbe1120d3c38619be4f1523ae1f2c9 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 6 Jan 2025 17:14:54 -0600 Subject: [PATCH v1 1/1] Lower default value of autovacuum_worker_slots in initdb as needed. --- doc/src/sgml/config.sgml | 5 +++-- src/bin/initdb/initdb.c | 41 +++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 740ff5d5044..8683f0bdf53 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -8639,8 +8639,9 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv; Specifies the number of backend slots to reserve for autovacuum worker - processes. The default is 16. This parameter can only be set at server - start. + processes. The default is typically 16 slots, but might be less if + your kernel settings will not support it (as determined during initdb). + This parameter can only be set at server start. When changing this value, consider also adjusting diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 4e4b7ede190..1214f1b492a 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -197,6 +197,7 @@ static char *pgdata_native; /* defaults */ static int n_connections = 10; static int n_buffers = 50; +static int n_av_slots = 16; static const char *dynamic_shared_memory_type = NULL; static const char *default_timezone = NULL; @@ -273,7 +274,8 @@ static void check_input(char *path); static void write_version_file(const char *extrapath); static void set_null_conf(void); static void test_config_settings(void); -static bool test_specific_config_settings(int test_conns, int test_buffs); +static bool test_specific_config_settings(int test_conns, int test_buffs, + int test_av_slots); static void setup_config(void); static void bootstrap_template1(void); static void setup_auth(FILE *cmdfd); @@ -1118,6 +1120,13 @@ test_config_settings(void) */ #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10) + /* + * This macro defines the minimum default autovacuum_worker_slots we are + * willing to consider. It should be kept >= the default for + * autovacuum_max_workers. + */ +#define MIN_AV_WORKER_SLOTS (3) + static const int trial_conns[] = { 100, 50, 40, 30, 25 }; @@ -1155,7 +1164,9 @@ test_config_settings(void) test_conns = trial_conns[i]; test_buffs = MIN_BUFS_FOR_CONNS(test_conns); - if (test_specific_config_settings(test_conns, test_buffs)) + if (test_specific_config_settings(test_conns, + test_buffs, + MIN_AV_WORKER_SLOTS)) { ok_buffers = test_buffs; break; @@ -1180,7 +1191,9 @@ test_config_settings(void) break; } - if (test_specific_config_settings(n_connections, test_buffs)) + if (test_specific_config_settings(n_connections, + test_buffs, + MIN_AV_WORKER_SLOTS)) break; } n_buffers = test_buffs; @@ -1190,6 +1203,19 @@ test_config_settings(void) else printf("%dkB\n", n_buffers * (BLCKSZ / 1024)); + printf(_("selecting default \"autovacuum_worker_slots\" ... ")); + fflush(stdout); + + for (; n_av_slots > MIN_AV_WORKER_SLOTS; n_av_slots--) + { + if (test_specific_config_settings(n_connections, + n_buffers, + n_av_slots)) + break; + } + + printf("%d\n", n_av_slots); + printf(_("selecting default time zone ... ")); fflush(stdout); default_timezone = select_default_timezone(share_path); @@ -1200,7 +1226,7 @@ test_config_settings(void) * Test a specific combination of configuration settings. */ static bool -test_specific_config_settings(int test_conns, int test_buffs) +test_specific_config_settings(int test_conns, int test_buffs, int test_av_slots) { PQExpBufferData cmd; _stringlist *gnames, @@ -1214,9 +1240,10 @@ test_specific_config_settings(int test_conns, int test_buffs) "\"%s\" --check %s %s " "-c max_connections=%d " "-c shared_buffers=%d " + "-c autovacuum_worker_slots=%d " "-c dynamic_shared_memory_type=%s", backend_exec, boot_options, extra_options, - test_conns, test_buffs, + test_conns, test_buffs, test_av_slots, dynamic_shared_memory_type); /* Add any user-given setting overrides */ @@ -1289,6 +1316,10 @@ setup_config(void) conflines = replace_guc_value(conflines, "shared_buffers", repltok, false); + snprintf(repltok, sizeof(repltok), "%d", n_av_slots); + conflines = replace_guc_value(conflines, "autovacuum_worker_slots", + repltok, false); + conflines = replace_guc_value(conflines, "lc_messages", lc_messages, false); -- 2.39.5 (Apple Git-154)