Introduce GUC shared_memory_size
authorMichael Paquier <michael@paquier.xyz>
Wed, 8 Sep 2021 03:02:30 +0000 (12:02 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 8 Sep 2021 03:02:30 +0000 (12:02 +0900)
This runtime-computed GUC shows the size of the server's main shared
memory area, taking into account the amount of shared memory allocated
by extensions as this is calculated after processing
shared_preload_libraries.

Author: Nathan Bossart
Discussion: https://postgr.es/m/F2772387-CE0F-46BF-B5F1-CC55516EB885@amazon.com

doc/src/sgml/config.sgml
src/backend/postmaster/postmaster.c
src/backend/storage/ipc/ipci.c
src/backend/utils/misc/guc.c
src/include/storage/ipc.h

index 2c31c35a6b11d1cb9ef53196fcf6bba85f3d402e..ef0e2a77462c83f0c0b5babd3eed10e5fb871b4b 100644 (file)
@@ -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>
index 63043ed8d1f2b99b2ea12e196aef58bab1ae9ea8..b2fe420c3cf40e476a69482af417c30a897ea66a 100644 (file)
@@ -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.
     */
index 64bc16fa8480dd8c59efc7df24012c79c8a1af3c..1a408ad77e5cdafb5e89fea0fec9cc54ac87907c 100644 (file)
@@ -313,3 +313,25 @@ 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", size_mb);
+   SetConfigOption("shared_memory_size", buf, PGC_INTERNAL, PGC_S_OVERRIDE);
+}
index 467b0fd6fe79a6847bc5a5ba2219bdffce359fa6..c339acf067016f966f7330c98ec8f9d2b0489e7f 100644 (file)
@@ -664,6 +664,7 @@ static int  max_index_keys;
 static int max_identifier_length;
 static int block_size;
 static int segment_size;
+static int shared_memory_size_mb;
 static int wal_block_size;
 static bool data_checksums;
 static bool integer_datetimes;
@@ -2337,6 +2338,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
+       },
+       &shared_memory_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."),
index 80e191d407db97b6f26a3a363b519cf5c1092551..7a1ebc8559cd635f7375615cbdb1098c69a0c213 100644 (file)
@@ -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 */