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 1s9UuC-002ju6-9X for pgsql-hackers@arkaria.postgresql.org; Tue, 21 May 2024 19:12:13 +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 1s9UuC-00C8T1-AN for pgsql-hackers@arkaria.postgresql.org; Tue, 21 May 2024 19:12:12 +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 1s9UuB-00C8St-T8 for pgsql-hackers@lists.postgresql.org; Tue, 21 May 2024 19:12:12 +0000 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9Uu8-001KtE-Hi for pgsql-hackers@postgresql.org; Tue, 21 May 2024 19:12:10 +0000 Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-36da84df7adso507075ab.1 for ; Tue, 21 May 2024 12:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716318728; x=1716923528; 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=LGOBS50GtPe2U5mjQptBnWj4tbY79oa5O0TDa+5WpMw=; b=LAMLSNgVCXLW1aO1PUK+01YocHjPYCh2+TxM5zZDQJ0y/JYd60DtFULOOY1xZZvPOi MSued1PvSXkqcFAtoyszh3lNUEiASZp7IUu9auKBmdyGfe+1vLrTTjzJoXg0jVUdKwHh kR/LcbAmS7uOu9gN0BgIRil4vdXG0QFDJa7TtVB+seoVDQehRTfX1zC/doUQNLELxsRW BeoDe/qlfIhMTD8yifdjgG8WqZil9PnQ3A7jZl89qPlsL0DWAv2V1g/tziyzobhgmmDg mKdIpqxx7RMiorOyBCCLb/g8CAWKJnrLUQKZ9hLn40xtknRQmgRaZOoPWpzf07pmdbwM BObg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716318728; x=1716923528; 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=LGOBS50GtPe2U5mjQptBnWj4tbY79oa5O0TDa+5WpMw=; b=pIt/hFQNRp11bWD+qRCQttZCRak3Ce1siBgCbp29065ZBLV2yajvVZL615GfL/3yUa PFc1bVv9QsCqYkm5+/IDBCyiOhAxaEUHETQ/iVQABLEM+CCxp/18+oi5WavDfISnzOIK XOibrYz4tOO7QZdP6xdgWo904XeK7T3j/338gBiZz/68ojMdhnCgN/JZxwih4zNiHK9f DE7y3MB8134aCsumRWdFwQFc8w5UDptoR/xn1nHRcgt6FFfYtWfxModiIECuQUbt3PMh OrnzijqQfDX3g7yucSuLHBivI9MJhaFIgDBM/lMsZPph75+z2jU062bM0sJ+ER1HRbJx HedA== X-Forwarded-Encrypted: i=1; AJvYcCUcRzib8Bef6qeQtXb0+4K9Zh2SDfdsQZx3/HIneoeiK2HmJCeZTrRsieWwDNa5/Mo3WeBUJXYPUIfZjielic7uWMuebcyB/5DGn6I3 X-Gm-Message-State: AOJu0Yxp/Rrt0LSPUbFzMRQOH4xGAUt2t6XUtGe5W81aMKPRT5tA8UrQ qnZS9Lu4XiPd9ILZBkliMZRSXQWEzz38y3hkD4FpD3/h1G+ozciN X-Google-Smtp-Source: AGHT+IGyFiYr/LO6HDOnvNEyfeOkFENIuJ3BaLwsYI+xz0mataFDHLTsOiuNwwTwfjUdE+K2VN81nA== X-Received: by 2002:a05:6e02:1c08:b0:36c:4ae0:53c5 with SMTP id e9e14a558f8ab-36dd0b7fae4mr71677885ab.12.1716318726930; Tue, 21 May 2024 12:12:06 -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-4895e006516sm6643752173.27.2024.05.21.12.12.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 12:12:06 -0700 (PDT) Date: Tue, 21 May 2024 14:12:04 -0500 From: Nathan Bossart To: Tom Lane Cc: "Imseih (AWS), Sami" , 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="gMGA3/ZDWD/mkbbv" Content-Disposition: inline In-Reply-To: <20240517192123.GA1933425@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --gMGA3/ZDWD/mkbbv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, May 17, 2024 at 02:21:23PM -0500, Nathan Bossart wrote: > On Fri, May 17, 2024 at 12:48:37PM -0500, Nathan Bossart wrote: >> Cool. I'll at least fix the back-branches as-is, but I'll see about >> revamping this stuff for v18. > > Attached is probably the absolute least we should do for the back-branches. Any concerns with doing something like this [0] for the back-branches? The constant would be 6 instead of 7 on v14 through v16. I wrote a quick sketch for what a runtime-computed GUC might look like for v18. We don't have agreement on this approach, but I figured I'd post something while we search for a better one. [0] https://postgr.es/m/attachment/160360/v1-0001-fix-kernel-resources-docs-on-back-branches.patch -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --gMGA3/ZDWD/mkbbv Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-add-semaphores_required-GUC.patch" From f5abd8150c317a0eaf4fb2aff79164ce929d2d96 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 21 May 2024 14:02:22 -0500 Subject: [PATCH v1 1/1] add semaphores_required GUC --- doc/src/sgml/config.sgml | 14 ++++++++++++++ doc/src/sgml/runtime.sgml | 28 +++++++++++++++++----------- src/backend/storage/ipc/ipci.c | 6 +++++- src/backend/utils/misc/guc_tables.c | 12 ++++++++++++ 4 files changed, 48 insertions(+), 12 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 6047b8171d..e5ebfb8a8b 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 + 5) / 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 + 5) / 16) * 17 plus room for other applications + ceil(semaphores_required / 16) * 17 plus room for other applications @@ -836,27 +836,33 @@ 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 - () 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 + 5) / 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. 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) --gMGA3/ZDWD/mkbbv--