v6-0001-Introduce-shared_memory_size-GUC.patch

application/octet-stream

Filename: v6-0001-Introduce-shared_memory_size-GUC.patch
Type: application/octet-stream
Part: 2
Message: Re: Estimating HugePages Requirements?

Patch

Format: format-patch
Series: patch v6-0001
Subject: Introduce shared_memory_size GUC.
File+
doc/src/sgml/config.sgml 14 0
src/backend/postmaster/postmaster.c 7 0
src/backend/storage/ipc/ipci.c 24 0
src/backend/utils/misc/guc.c 13 0
src/include/storage/ipc.h 1 0
From fedabb6b8fec47ba783812b3408f3e6eeb122f74 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Mon, 6 Sep 2021 18:00:02 +0000
Subject: [PATCH v6 1/3] Introduce shared_memory_size GUC.

This runtime-computed GUC shows the size of the server's main
shared memory area.  While this is intended to be useful for
calculating the number of huge pages required prior to startup, it
cannot be viewed via 'postgres -C' yet.  This may be addressed in a
future change.
---
 doc/src/sgml/config.sgml            | 14 ++++++++++++++
 src/backend/postmaster/postmaster.c |  7 +++++++
 src/backend/storage/ipc/ipci.c      | 24 ++++++++++++++++++++++++
 src/backend/utils/misc/guc.c        | 13 +++++++++++++
 src/include/storage/ipc.h           |  1 +
 5 files changed, 59 insertions(+)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 2c31c35a6b..ef0e2a7746 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -10275,6 +10275,20 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-shared-memory-size" xreflabel="shared_memory_size">
+      <term><varname>shared_memory_size</varname> (<type>integer</type>)
+      <indexterm>
+       <primary><varname>shared_memory_size</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Reports the size of the main shared memory area, rounded up to the
+        nearest megabyte.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-ssl-library" xreflabel="ssl_library">
       <term><varname>ssl_library</varname> (<type>string</type>)
       <indexterm>
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 9c2c98614a..963c03fa93 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -1026,6 +1026,13 @@ PostmasterMain(int argc, char *argv[])
 	 */
 	InitializeMaxBackends();
 
+	/*
+	 * Now that loadable modules have had their chance to request additional
+	 * shared memory, determine the value of any runtime-computed GUCs that
+	 * depend on the amount of shared memory required.
+	 */
+	InitializeShmemGUCs();
+
 	/*
 	 * Set up shared memory and semaphores.
 	 */
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 64bc16fa84..614b8e92c4 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -313,3 +313,27 @@ CreateSharedMemoryAndSemaphores(void)
 	if (shmem_startup_hook)
 		shmem_startup_hook();
 }
+
+/*
+ * InitializeShmemGUCs
+ *
+ * This function initializes runtime-computed GUCs related to the amount of
+ * shared memory required for the current configuration.
+ */
+void
+InitializeShmemGUCs(void)
+{
+	char	buf[64];
+	Size	size_b;
+	Size	size_mb;
+
+	/*
+	 * Calculate the shared memory size and round up to the nearest
+	 * megabyte.
+	 */
+	size_b = CalculateShmemSize(NULL);
+	size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
+
+	sprintf(buf, "%lu MB", size_mb);
+	SetConfigOption("shared_memory_size", buf, PGC_INTERNAL, PGC_S_OVERRIDE);
+}
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 467b0fd6fe..a11387c5ce 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -620,6 +620,8 @@ char	   *pgstat_temp_directory;
 
 char	   *application_name;
 
+int			shmem_size_mb;
+
 int			tcp_keepalives_idle;
 int			tcp_keepalives_interval;
 int			tcp_keepalives_count;
@@ -2337,6 +2339,17 @@ static struct config_int ConfigureNamesInt[] =
 		NULL, NULL, NULL
 	},
 
+	{
+		{"shared_memory_size", PGC_INTERNAL, RESOURCES_MEM,
+			gettext_noop("Shows the size of the server's main shared memory area (rounded up to the nearest MB)."),
+			NULL,
+			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_UNIT_MB
+		},
+		&shmem_size_mb,
+		0, 0, INT_MAX,
+		NULL, NULL, NULL
+	},
+
 	{
 		{"temp_buffers", PGC_USERSET, RESOURCES_MEM,
 			gettext_noop("Sets the maximum number of temporary buffers used by each session."),
diff --git a/src/include/storage/ipc.h b/src/include/storage/ipc.h
index 80e191d407..7a1ebc8559 100644
--- a/src/include/storage/ipc.h
+++ b/src/include/storage/ipc.h
@@ -79,5 +79,6 @@ extern PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook;
 
 extern Size CalculateShmemSize(int *num_semaphores);
 extern void CreateSharedMemoryAndSemaphores(void);
+extern void InitializeShmemGUCs(void);
 
 #endif							/* IPC_H */
-- 
2.16.6