Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6d01541

Browse files
Lower default value of autovacuum_worker_slots in initdb as needed.
Commit c758119 increased the default number of semaphores required for autovacuum workers from 3 to 16. Unfortunately, some systems have very low default settings for SEMMNS, and this change moved the minimum required for Postgres well beyond that limit (see commit 38da053 for more details). With this commit, initdb will lower the default value for autovacuum_worker_slots as needed, just like it already does for parameters such as max_connections and shared_buffers. We test for (max_connections / 6) slots, which conveniently has the following properties: * For the initial max_connections default of 100, the default of autovacuum_worker_slots will be 16, which is its initial default value specified in the documentation and in guc_tables.c. * For the lowest possible max_connections default of 25, the default of autovacuum_worker_slots will be 4, which means we only need one additional semaphore for autovacuum workers (as compared to before commit c758119). This leaves some wiggle room for new auxiliary workers, etc. on systems with low SEMMNS, and it ensures that the default number of slots will be greater than or equal to the default value of autovacuum_max_workers (3). Reported-by: Tom Lane Suggested-by: Andres Freund Reviewed-by: Tom Lane Discussion: https://postgr.es/m/1346002.1736198977%40sss.pgh.pa.us
1 parent 0e5b144 commit 6d01541

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

doc/src/sgml/config.sgml

+3-2
Original file line numberDiff line numberDiff line change
@@ -8639,8 +8639,9 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
86398639
<listitem>
86408640
<para>
86418641
Specifies the number of backend slots to reserve for autovacuum worker
8642-
processes. The default is 16. This parameter can only be set at server
8643-
start.
8642+
processes. The default is typically 16 slots, but might be less if
8643+
your kernel settings will not support it (as determined during initdb).
8644+
This parameter can only be set at server start.
86448645
</para>
86458646
<para>
86468647
When changing this value, consider also adjusting

src/bin/initdb/initdb.c

+34-6
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ static char *pgdata_native;
196196

197197
/* defaults */
198198
static int n_connections = 10;
199+
static int n_av_slots = 16;
199200
static int n_buffers = 50;
200201
static const char *dynamic_shared_memory_type = NULL;
201202
static const char *default_timezone = NULL;
@@ -273,7 +274,8 @@ static void check_input(char *path);
273274
static void write_version_file(const char *extrapath);
274275
static void set_null_conf(void);
275276
static void test_config_settings(void);
276-
static bool test_specific_config_settings(int test_conns, int test_buffs);
277+
static bool test_specific_config_settings(int test_conns, int test_av_slots,
278+
int test_buffs);
277279
static void setup_config(void);
278280
static void bootstrap_template1(void);
279281
static void setup_auth(FILE *cmdfd);
@@ -1118,6 +1120,18 @@ test_config_settings(void)
11181120
*/
11191121
#define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
11201122

1123+
/*
1124+
* This macro defines the default value of autovacuum_worker_slots we want
1125+
* for a given max_connections value. Note that it has been carefully
1126+
* crafted to provide specific values for the associated values in
1127+
* trial_conns. We want it to return autovacuum_worker_slots's initial
1128+
* default value (16) for the maximum value in trial_conns (100), and we
1129+
* want it to return close to the minimum value we'd consider (3, which is
1130+
* the default of autovacuum_max_workers) for the minimum value in
1131+
* trial_conns (25).
1132+
*/
1133+
#define AV_SLOTS_FOR_CONNS(nconns) ((nconns) / 6)
1134+
11211135
static const int trial_conns[] = {
11221136
100, 50, 40, 30, 25
11231137
};
@@ -1145,17 +1159,19 @@ test_config_settings(void)
11451159

11461160
/*
11471161
* Probe for max_connections before shared_buffers, since it is subject to
1148-
* more constraints than shared_buffers.
1162+
* more constraints than shared_buffers. We also choose the default
1163+
* autovacuum_worker_slots here.
11491164
*/
11501165
printf(_("selecting default \"max_connections\" ... "));
11511166
fflush(stdout);
11521167

11531168
for (i = 0; i < connslen; i++)
11541169
{
11551170
test_conns = trial_conns[i];
1171+
n_av_slots = AV_SLOTS_FOR_CONNS(test_conns);
11561172
test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
11571173

1158-
if (test_specific_config_settings(test_conns, test_buffs))
1174+
if (test_specific_config_settings(test_conns, n_av_slots, test_buffs))
11591175
{
11601176
ok_buffers = test_buffs;
11611177
break;
@@ -1167,6 +1183,13 @@ test_config_settings(void)
11671183

11681184
printf("%d\n", n_connections);
11691185

1186+
/*
1187+
* We chose the default for autovacuum_worker_slots during the
1188+
* max_connections tests above, but we print a progress message anyway.
1189+
*/
1190+
printf(_("selecting default \"autovacuum_worker_slots\" ... %d\n"),
1191+
n_av_slots);
1192+
11701193
printf(_("selecting default \"shared_buffers\" ... "));
11711194
fflush(stdout);
11721195

@@ -1180,7 +1203,7 @@ test_config_settings(void)
11801203
break;
11811204
}
11821205

1183-
if (test_specific_config_settings(n_connections, test_buffs))
1206+
if (test_specific_config_settings(n_connections, n_av_slots, test_buffs))
11841207
break;
11851208
}
11861209
n_buffers = test_buffs;
@@ -1200,7 +1223,7 @@ test_config_settings(void)
12001223
* Test a specific combination of configuration settings.
12011224
*/
12021225
static bool
1203-
test_specific_config_settings(int test_conns, int test_buffs)
1226+
test_specific_config_settings(int test_conns, int test_av_slots, int test_buffs)
12041227
{
12051228
PQExpBufferData cmd;
12061229
_stringlist *gnames,
@@ -1213,10 +1236,11 @@ test_specific_config_settings(int test_conns, int test_buffs)
12131236
printfPQExpBuffer(&cmd,
12141237
"\"%s\" --check %s %s "
12151238
"-c max_connections=%d "
1239+
"-c autovacuum_worker_slots=%d "
12161240
"-c shared_buffers=%d "
12171241
"-c dynamic_shared_memory_type=%s",
12181242
backend_exec, boot_options, extra_options,
1219-
test_conns, test_buffs,
1243+
test_conns, test_av_slots, test_buffs,
12201244
dynamic_shared_memory_type);
12211245

12221246
/* Add any user-given setting overrides */
@@ -1280,6 +1304,10 @@ setup_config(void)
12801304
conflines = replace_guc_value(conflines, "max_connections",
12811305
repltok, false);
12821306

1307+
snprintf(repltok, sizeof(repltok), "%d", n_av_slots);
1308+
conflines = replace_guc_value(conflines, "autovacuum_worker_slots",
1309+
repltok, false);
1310+
12831311
if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
12841312
snprintf(repltok, sizeof(repltok), "%dMB",
12851313
(n_buffers * (BLCKSZ / 1024)) / 1024);

0 commit comments

Comments
 (0)