Skip to content

Commit 0dcaea5

Browse files
Introduce num_os_semaphores GUC.
The documentation for System V IPC parameters provides complicated formulas to determine the appropriate values for SEMMNI and SEMMNS. Furthermore, these formulas have often been wrong because folks forget to update them (e.g., when adding a new auxiliary process). This commit introduces a new runtime-computed GUC named num_os_semaphores that reports the number of semaphores needed for the configured number of allowed connections, worker processes, etc. This new GUC allows us to simplify the formulas in the documentation, and it should help prevent future inaccuracies. Like the other runtime-computed GUCs, users can view it with "postgres -C" before starting the server, which is useful for preconfiguring the necessary operating system resources. Reviewed-by: Tom Lane, Sami Imseih, Andres Freund, Robert Haas Discussion: https://postgr.es/m/20240517164452.GA1914161%40nathanxps13
1 parent 8a53539 commit 0dcaea5

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

doc/src/sgml/config.sgml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11145,6 +11145,24 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
1114511145
</listitem>
1114611146
</varlistentry>
1114711147

11148+
<varlistentry id="guc-num-os-semaphores" xreflabel="num_os_semaphores">
11149+
<term><varname>num_os_semaphores</varname> (<type>integer</type>)
11150+
<indexterm>
11151+
<primary><varname>num_os_semaphores</varname> configuration parameter</primary>
11152+
</indexterm>
11153+
</term>
11154+
<listitem>
11155+
<para>
11156+
Reports the number of semaphores that are needed for the server based
11157+
on the configured number of allowed connections
11158+
(<xref linkend="guc-max-connections"/>), allowed autovacuum worker
11159+
processes (<xref linkend="guc-autovacuum-max-workers"/>), allowed WAL
11160+
sender processes (<xref linkend="guc-max-wal-senders"/>), allowed
11161+
background processes (<xref linkend="guc-max-worker-processes"/>), etc.
11162+
</para>
11163+
</listitem>
11164+
</varlistentry>
11165+
1114811166
<varlistentry id="guc-segment-size" xreflabel="segment_size">
1114911167
<term><varname>segment_size</varname> (<type>integer</type>)
1115011168
<indexterm>

doc/src/sgml/runtime.sgml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,13 @@ psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such
781781
<row>
782782
<entry><varname>SEMMNI</varname></entry>
783783
<entry>Maximum number of semaphore identifiers (i.e., sets)</entry>
784-
<entry>at least <literal>ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 7) / 16)</literal> plus room for other applications</entry>
784+
<entry>at least <literal>ceil(num_os_semaphores / 16)</literal> plus room for other applications</entry>
785785
</row>
786786

787787
<row>
788788
<entry><varname>SEMMNS</varname></entry>
789789
<entry>Maximum number of semaphores system-wide</entry>
790-
<entry><literal>ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 7) / 16) * 17</literal> plus room for other applications</entry>
790+
<entry><literal>ceil(num_os_semaphores / 16) * 17</literal> plus room for other applications</entry>
791791
</row>
792792

793793
<row>
@@ -839,21 +839,28 @@ psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such
839839
<productname>PostgreSQL</productname> uses one semaphore per allowed connection
840840
(<xref linkend="guc-max-connections"/>), allowed autovacuum worker process
841841
(<xref linkend="guc-autovacuum-max-workers"/>), allowed WAL sender process
842-
(<xref linkend="guc-max-wal-senders"/>), and allowed background
843-
process (<xref linkend="guc-max-worker-processes"/>), in sets of 16.
844-
Each such set will
842+
(<xref linkend="guc-max-wal-senders"/>), allowed background
843+
process (<xref linkend="guc-max-worker-processes"/>), etc., in sets of 16.
844+
The runtime-computed parameter <xref linkend="guc-num-os-semaphores"/>
845+
reports the number of semaphores required. This parameter can be viewed
846+
before starting the server with a <command>postgres</command> command like:
847+
<programlisting>
848+
$ <userinput>postgres -D $PGDATA -C num_os_semaphores</userinput>
849+
</programlisting>
850+
</para>
851+
852+
<para>
853+
Each set of 16 semaphores will
845854
also contain a 17th semaphore which contains a <quote>magic
846855
number</quote>, to detect collision with semaphore sets used by
847856
other applications. The maximum number of semaphores in the system
848857
is set by <varname>SEMMNS</varname>, which consequently must be at least
849-
as high as <varname>max_connections</varname> plus
850-
<varname>autovacuum_max_workers</varname> plus <varname>max_wal_senders</varname>,
851-
plus <varname>max_worker_processes</varname>, plus one extra for each 16
852-
allowed connections plus workers (see the formula in <xref
858+
as high as <literal>num_os_semaphores</literal> plus one extra for
859+
each set of 16 required semaphores (see the formula in <xref
853860
linkend="sysvipc-parameters"/>). The parameter <varname>SEMMNI</varname>
854861
determines the limit on the number of semaphore sets that can
855862
exist on the system at one time. Hence this parameter must be at
856-
least <literal>ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 7) / 16)</literal>.
863+
least <literal>ceil(num_os_semaphores / 16)</literal>.
857864
Lowering the number
858865
of allowed connections is a temporary workaround for failures,
859866
which are usually confusingly worded <quote>No space
@@ -885,8 +892,8 @@ psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such
885892
same as for System V, that is one semaphore per allowed connection
886893
(<xref linkend="guc-max-connections"/>), allowed autovacuum worker process
887894
(<xref linkend="guc-autovacuum-max-workers"/>), allowed WAL sender process
888-
(<xref linkend="guc-max-wal-senders"/>), and allowed background
889-
process (<xref linkend="guc-max-worker-processes"/>).
895+
(<xref linkend="guc-max-wal-senders"/>), allowed background
896+
process (<xref linkend="guc-max-worker-processes"/>), etc.
890897
On the platforms where this option is preferred, there is no specific
891898
kernel limit on the number of POSIX semaphores.
892899
</para>

src/backend/storage/ipc/ipci.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,12 @@ InitializeShmemGUCs(void)
372372
Size size_b;
373373
Size size_mb;
374374
Size hp_size;
375+
int num_semas;
375376

376377
/*
377378
* Calculate the shared memory size and round up to the nearest megabyte.
378379
*/
379-
size_b = CalculateShmemSize(NULL);
380+
size_b = CalculateShmemSize(&num_semas);
380381
size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
381382
sprintf(buf, "%zu", size_mb);
382383
SetConfigOption("shared_memory_size", buf,
@@ -395,4 +396,7 @@ InitializeShmemGUCs(void)
395396
SetConfigOption("shared_memory_size_in_huge_pages", buf,
396397
PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
397398
}
399+
400+
sprintf(buf, "%d", num_semas);
401+
SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
398402
}

src/backend/utils/misc/guc_tables.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ static int segment_size;
591591
static int shared_memory_size_mb;
592592
static int shared_memory_size_in_huge_pages;
593593
static int wal_block_size;
594+
static int num_os_semaphores;
594595
static bool data_checksums;
595596
static bool integer_datetimes;
596597

@@ -2283,6 +2284,17 @@ struct config_int ConfigureNamesInt[] =
22832284
NULL, NULL, NULL
22842285
},
22852286

2287+
{
2288+
{"num_os_semaphores", PGC_INTERNAL, PRESET_OPTIONS,
2289+
gettext_noop("Shows the number of semaphores required for the server."),
2290+
NULL,
2291+
GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_RUNTIME_COMPUTED
2292+
},
2293+
&num_os_semaphores,
2294+
0, 0, INT_MAX,
2295+
NULL, NULL, NULL
2296+
},
2297+
22862298
{
22872299
{"commit_timestamp_buffers", PGC_POSTMASTER, RESOURCES_MEM,
22882300
gettext_noop("Sets the size of the dedicated buffer pool used for the commit timestamp cache."),

0 commit comments

Comments
 (0)