Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGNq6-00CTTN-W4 for pgsql-hackers@arkaria.postgresql.org; Sun, 09 Jun 2024 19:04:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sGNq3-005XhR-Oq for pgsql-hackers@arkaria.postgresql.org; Sun, 09 Jun 2024 19:04:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGNq3-005Xg4-7l for pgsql-hackers@lists.postgresql.org; Sun, 09 Jun 2024 19:04:24 +0000 Received: from mail-il1-x12c.google.com ([2607:f8b0:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGNq1-000Val-6F for pgsql-hackers@postgresql.org; Sun, 09 Jun 2024 19:04:22 +0000 Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-374a82e20deso15307455ab.1 for ; Sun, 09 Jun 2024 12:04:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717959860; x=1718564660; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=4sGrdyC6DA6z3eLInb4kPPz0pMQ8w5USkrnNTZ2r1Rg=; b=ONEZPjbkG+7UM4MKm/WPriTB/m49CRzZBj6DDyjSo3pa0WVFYNWTQd7tD2+lpi2M7R CyAY3R+CGqzDaSMaHsq5vVdLaF4P8VLORzQYxXsxcWVOhA1IwqGWoKcw6sY6duaerq6Y K0Mp5U+EVe9VHYys/kV+w9J3Z1grlJ3G8C6kD6f8h6qXGq6eNUBT8PFqRr7HKATNV94h J7Kd4C+Y0XJwb+jfcFemwEir1GrsFX9barxz1YIOH8S5Elh80rUAPrbpOpsxX5DioxVh +fmB8ctHdvORn/a6Ur9oCuZeuKhv+EBTxfScJEYWEgkbv7X99rCCSED68ouGeAEDG6jT +/Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717959860; x=1718564660; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=4sGrdyC6DA6z3eLInb4kPPz0pMQ8w5USkrnNTZ2r1Rg=; b=n5nvl9iy7qQoREgiOGez9KhCAOZBBb+5Ys1DtqORwqn5wiuq1B22uil3H5m857ykqb Hqh4jg4jmnPX9pWsLndZFD4C7t3g7e553Dg25XdA1RirbOtguZABZ0dhM+dLJ7mtx7ke ds8QI2fK/smXRlAt+hDaVrZLKqnNM1GkT5oskc7S5yiciFDnuaBYW6y1vOnygeWP2MDo PLVsV9/wEYK7nHZZIeyepj1qgZ9Ja/8MYR0uUKgba+6l84IuaVfdvKJoRX4tx8JVHcop JwWykEKwrVQkKUEQZozUsTHK/dRjONjYls7RD9QXI09LNpmnmslxZaQpE81tHW7xWSHj 0ejA== X-Forwarded-Encrypted: i=1; AJvYcCXN/s82X+ca+OZURbvf4ef+PSXywD+BiyQ0apCNrMGxbwohkX70N+U25QXbdwj2kQw/iTp4Af4luBqzQyE/o1crgAG3U/Xrb44U5/9D X-Gm-Message-State: AOJu0Yx1I7rwOhG+AhhX3A7x9lqsCxvJbHeiVGBvRcfG9MWFbL+6zl1X pzoApcd8mbg2dK5ROuS6CqKBE52ZNgTNDfuENRpamOxS7KXKcyet X-Google-Smtp-Source: AGHT+IGnPY5Coz5hVTLNypEUljOat+4R+J8HBnHOKzUmyXA140S0d6Ip/jIPeqMRbAAj5liP/TVnXQ== X-Received: by 2002:a05:6e02:dcc:b0:374:8172:c24a with SMTP id e9e14a558f8ab-374b97fc44amr46718005ab.11.1717959860251; Sun, 09 Jun 2024 12:04:20 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3752f19f01fsm16318975ab.87.2024.06.09.12.04.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jun 2024 12:04:19 -0700 (PDT) Date: Sun, 9 Jun 2024 14:04:17 -0500 From: Nathan Bossart To: Robert Haas Cc: "Imseih (AWS), Sami" , Tom Lane , Justin Pryzby , "pgsql-hackers@postgresql.org" Subject: Re: problems with "Shared Memory and Semaphores" section of docs Message-ID: References: <20240517174837.GB1918496@nathanxps13> <20240517192123.GA1933425@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qpSa1Hh12E00TVdZ" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --qpSa1Hh12E00TVdZ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Jun 06, 2024 at 02:51:42PM -0500, Nathan Bossart wrote: > On Thu, Jun 06, 2024 at 03:31:53PM -0400, Robert Haas wrote: >> I do think the name could use some more thought, though. >> semaphores_required would end up being the same kind of thing as >> shared_memory_size_in_huge_pages, but the names seem randomly >> different. If semaphores_required is right here, why isn't >> shared_memory_required used there? Seems more like we ought to call >> this semaphores or os_semaphores or num_semaphores or >> num_os_semaphores or something. > > I'm fine with any of your suggestions. If I _had_ to pick one, I'd > probably choose num_os_semaphores because it's the most descriptive. Here's a new version of the patch with the GUC renamed to num_os_semaphores. -- nathan --qpSa1Hh12E00TVdZ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-add-num_os_semaphores-GUC.patch" From 4321c19071eec065d8712ae0080d237d56a1478e Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 21 May 2024 14:02:22 -0500 Subject: [PATCH v3 1/1] add num_os_semaphores GUC --- doc/src/sgml/config.sgml | 14 +++++++++++ doc/src/sgml/runtime.sgml | 39 ++++++++++++++++------------- src/backend/storage/ipc/ipci.c | 6 ++++- src/backend/utils/misc/guc_tables.c | 12 +++++++++ 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 698169afdb..a8372eadf5 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -11215,6 +11215,20 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir' + + num_os_semaphores (integer) + + num_os_semaphores configuration parameter + + + + + Reports the number of semaphores that are needed for the server based + on the number of allowed connections, worker processes, etc. + + + + ssl_library (string) diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml index 2f7c618886..26f3cbe555 100644 --- a/doc/src/sgml/runtime.sgml +++ b/doc/src/sgml/runtime.sgml @@ -781,13 +781,13 @@ psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such SEMMNI Maximum number of semaphore identifiers (i.e., sets) - at least ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 7) / 16) plus room for other applications + at least ceil(num_os_semaphores / 16) plus room for other applications SEMMNS Maximum number of semaphores system-wide - ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 7) / 16) * 17 plus room for other applications + ceil(num_os_semaphores / 16) * 17 plus room for other applications @@ -836,30 +836,38 @@ psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such When using System V semaphores, - PostgreSQL uses one semaphore per allowed connection - (), allowed autovacuum worker process - (), allowed WAL sender process - (), and allowed background - process (), in sets of 16. + PostgreSQL uses one semaphore per allowed connection, + worker process, etc., in sets of 16. Each such set will also contain a 17th semaphore which contains a magic number, to detect collision with semaphore sets used by other applications. The maximum number of semaphores in the system is set by SEMMNS, which consequently must be at least - as high as max_connections plus - autovacuum_max_workers plus max_wal_senders, - plus max_worker_processes, plus one extra for each 16 - allowed connections plus workers (see the formula in plus one extra for + each set of 16 required semaphores (see the formula in ). The parameter SEMMNI determines the limit on the number of semaphore sets that can exist on the system at one time. Hence this parameter must be at - least ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 7) / 16). + least ceil(num_os_semaphores / 16). Lowering the number of allowed connections is a temporary workaround for failures, which are usually confusingly worded No space left on device, from the function semget. + + The number of semaphores required by PostgreSQL + is provided by the runtime-computed parameter + num_os_semaphores, which can be determined before + starting the server with a postgres command like: + +$ postgres -D $PGDATA -C num_os_semaphores + + The value of num_os_semaphores should be input into + the aforementioned formulas to determine appropriate values for + SEMMNI and SEMMNS. + + In some cases it might also be necessary to increase SEMMAP to be at least on the order of @@ -882,11 +890,8 @@ psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such When using POSIX semaphores, the number of semaphores needed is the - same as for System V, that is one semaphore per allowed connection - (), allowed autovacuum worker process - (), allowed WAL sender process - (), and allowed background - process (). + same as for System V, that is one semaphore per allowed connection, + worker process, etc. On the platforms where this option is preferred, there is no specific kernel limit on the number of POSIX semaphores. diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c index 521ed5418c..0314513aa6 100644 --- a/src/backend/storage/ipc/ipci.c +++ b/src/backend/storage/ipc/ipci.c @@ -372,11 +372,12 @@ InitializeShmemGUCs(void) Size size_b; Size size_mb; Size hp_size; + int num_semas; /* * Calculate the shared memory size and round up to the nearest megabyte. */ - size_b = CalculateShmemSize(NULL); + size_b = CalculateShmemSize(&num_semas); size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024); sprintf(buf, "%zu", size_mb); SetConfigOption("shared_memory_size", buf, @@ -395,4 +396,7 @@ InitializeShmemGUCs(void) SetConfigOption("shared_memory_size_in_huge_pages", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT); } + + sprintf(buf, "%d", num_semas); + SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT); } diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c index 46c258be28..80e77cbac9 100644 --- a/src/backend/utils/misc/guc_tables.c +++ b/src/backend/utils/misc/guc_tables.c @@ -599,6 +599,7 @@ static int segment_size; static int shared_memory_size_mb; static int shared_memory_size_in_huge_pages; static int wal_block_size; +static int num_os_semaphores; static bool data_checksums; static bool integer_datetimes; @@ -2291,6 +2292,17 @@ struct config_int ConfigureNamesInt[] = NULL, NULL, NULL }, + { + {"num_os_semaphores", PGC_INTERNAL, PRESET_OPTIONS, + gettext_noop("Shows the number of semaphores required for the server."), + NULL, + GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_RUNTIME_COMPUTED + }, + &num_os_semaphores, + 0, 0, INT_MAX, + NULL, NULL, NULL + }, + { {"commit_timestamp_buffers", PGC_POSTMASTER, RESOURCES_MEM, gettext_noop("Sets the size of the dedicated buffer pool used for the commit timestamp cache."), -- 2.39.3 (Apple Git-146) --qpSa1Hh12E00TVdZ--