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 1sFIgJ-000ySV-Gi for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Jun 2024 19:21:52 +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 1sFIgI-003EgQ-2L for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Jun 2024 19:21:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sFIgH-003EgH-Nl for pgsql-hackers@lists.postgresql.org; Thu, 06 Jun 2024 19:21:50 +0000 Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sFIgB-000LAQ-NQ for pgsql-hackers@postgresql.org; Thu, 06 Jun 2024 19:21:50 +0000 Received: by mail-io1-xd34.google.com with SMTP id ca18e2360f4ac-7eb4db9ca53so46692139f.1 for ; Thu, 06 Jun 2024 12:21:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717701702; x=1718306502; 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=GEMyE3lNnt6rNVrUWBiklN101RDDVTSaAJ7mte4viQQ=; b=FvYxok9abMekWQf3BWMJsJX7dlaKtNq+OlPdoLkIEefbIMqQH94PMLOfsVHOVcZRdd QLnDBFvrwQOishwBc+lqGb1XoGxNFknr8OjOf7OvT0ibhSBddIGY6Y4pJjV9z/uuwyFu iqJ5JEWnr40QYdrER00BzjZqQvXPJlWzZx71OPHquNbcvZOFJgs21sENZZG5eanEMYzn 90BJ+CKJWQUFt1z6dPS8TTrFzUV2QQOgo8+gwzGaUTie+lb83+vpW771S1AlDDDkU4pj 1T62Tx5xkwfu3EqJfeY7pxwL1HQzEa+BaZOPyoxj8aga8QO/fPfhsXKZ1cA7fVc4F9Vf /hqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717701702; x=1718306502; 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=GEMyE3lNnt6rNVrUWBiklN101RDDVTSaAJ7mte4viQQ=; b=ISgA8AEf0cJzwENatxNmFbbGV3aYy8YyTr90d/xcLt50ycP9+n+SzyjgebVyNaE1k/ wA2ccL01ZqlJ/cJIAKuCsFdlBcXewS0N3goM9fpB2AF4AoQcVWVG3kAL22Zrg7P0dE05 +ZS2yn1v8NuYk37pT/ZB2kZ+E/dfsSBbMRI3YmmLSwiOab1V7ZOXlXsLbeZG9j20P6h9 rmEAt1d3nzM0vYKXhGDkrNSv5rBR7awu7l9q/HlMPl5aqadjnKIbUyNmpv2Bp57K4srP a9JC2RMlYT8FMLE/vmV9n8+Zki9lpd/hAAl6XchkaCMtaotei9aY+BRkDNAqAeMJQ/B0 z3tA== X-Forwarded-Encrypted: i=1; AJvYcCXc9WQsd/lRY6bHRh25b0icYtnYDsD3uENiAuv2bNrdYqDyeodAf2l6OErFkSjfLzIIURH8IU741D5KjnhtjnIe+W28XCpS3i1RmgxT X-Gm-Message-State: AOJu0Yy3fHT1mGVYFDIorkvlyYutkgYcyDHCqBm5dHH2uCsWb9h2KMNs 1Vd/fMG3uHd6WC6sM0S+y1UCUXQz5njQcuM7luGu9/lS4HslgTWJ X-Google-Smtp-Source: AGHT+IEoy0nqHUAXkcZHOa002gSKCfH0vcRczPYrgyknh0gQgDWMw07NYZASYjJ8TSIzLJ9q9uuCaQ== X-Received: by 2002:a05:6602:340d:b0:7ea:fadb:1cef with SMTP id ca18e2360f4ac-7eb572214f9mr72757339f.11.1717701701563; Thu, 06 Jun 2024 12:21:41 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4b7a24e33f2sm462390173.177.2024.06.06.12.21.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 12:21:41 -0700 (PDT) Date: Thu, 6 Jun 2024 14:21:38 -0500 From: Nathan Bossart To: "Imseih (AWS), Sami" Cc: Tom Lane , Justin Pryzby , "pgsql-hackers@postgresql.org" Subject: Re: problems with "Shared Memory and Semaphores" section of docs Message-ID: References: <20240517164452.GA1914161@nathanxps13> <2463906.1715965795@sss.pgh.pa.us> <20240517174837.GB1918496@nathanxps13> <20240517192123.GA1933425@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Alc5qowWceUm5XGB" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Alc5qowWceUm5XGB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here is a rebased version of the patch for v18 that adds a runtime-computed GUC. As I noted earlier, there still isn't a consensus on this approach. -- nathan --Alc5qowWceUm5XGB Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-add-semaphores_required-GUC.patch" From 74f638f7df9c51f5ab47b282bb7107c4ba6cb5b6 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 21 May 2024 14:02:22 -0500 Subject: [PATCH v2 1/1] add semaphores_required 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..f6afc941df 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' + + semaphores_required (integer) + + semaphores_required 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..c387f302d7 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(semaphores_required / 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(semaphores_required / 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(semaphores_required / 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 + semaphores_required, which can be determined before + starting the server with a postgres command like: + +$ postgres -D $PGDATA -C semaphores_required + + The value of semaphores_required 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..3e030accac 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("semaphores_required", 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..86157394d8 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 semaphores_required; static bool data_checksums; static bool integer_datetimes; @@ -2291,6 +2292,17 @@ struct config_int ConfigureNamesInt[] = NULL, NULL, NULL }, + { + {"semaphores_required", 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 + }, + &semaphores_required, + 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) --Alc5qowWceUm5XGB--