LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - ipci.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 118 119 99.2 %
Date: 2025-11-28 14:17:41 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * ipci.c
       4             :  *    POSTGRES inter-process communication initialization code.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/storage/ipc/ipci.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/clog.h"
      18             : #include "access/commit_ts.h"
      19             : #include "access/multixact.h"
      20             : #include "access/nbtree.h"
      21             : #include "access/subtrans.h"
      22             : #include "access/syncscan.h"
      23             : #include "access/transam.h"
      24             : #include "access/twophase.h"
      25             : #include "access/xlogprefetcher.h"
      26             : #include "access/xlogrecovery.h"
      27             : #include "access/xlogwait.h"
      28             : #include "commands/async.h"
      29             : #include "miscadmin.h"
      30             : #include "pgstat.h"
      31             : #include "postmaster/autovacuum.h"
      32             : #include "postmaster/bgworker_internals.h"
      33             : #include "postmaster/bgwriter.h"
      34             : #include "postmaster/walsummarizer.h"
      35             : #include "replication/logicallauncher.h"
      36             : #include "replication/origin.h"
      37             : #include "replication/slot.h"
      38             : #include "replication/slotsync.h"
      39             : #include "replication/walreceiver.h"
      40             : #include "replication/walsender.h"
      41             : #include "storage/aio_subsys.h"
      42             : #include "storage/bufmgr.h"
      43             : #include "storage/dsm.h"
      44             : #include "storage/dsm_registry.h"
      45             : #include "storage/ipc.h"
      46             : #include "storage/pg_shmem.h"
      47             : #include "storage/pmsignal.h"
      48             : #include "storage/predicate.h"
      49             : #include "storage/proc.h"
      50             : #include "storage/procarray.h"
      51             : #include "storage/procsignal.h"
      52             : #include "storage/sinvaladt.h"
      53             : #include "utils/guc.h"
      54             : #include "utils/injection_point.h"
      55             : 
      56             : /* GUCs */
      57             : int         shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE;
      58             : 
      59             : shmem_startup_hook_type shmem_startup_hook = NULL;
      60             : 
      61             : static Size total_addin_request = 0;
      62             : 
      63             : static void CreateOrAttachShmemStructs(void);
      64             : 
      65             : /*
      66             :  * RequestAddinShmemSpace
      67             :  *      Request that extra shmem space be allocated for use by
      68             :  *      a loadable module.
      69             :  *
      70             :  * This may only be called via the shmem_request_hook of a library that is
      71             :  * loaded into the postmaster via shared_preload_libraries.  Calls from
      72             :  * elsewhere will fail.
      73             :  */
      74             : void
      75          32 : RequestAddinShmemSpace(Size size)
      76             : {
      77          32 :     if (!process_shmem_requests_in_progress)
      78           0 :         elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
      79          32 :     total_addin_request = add_size(total_addin_request, size);
      80          32 : }
      81             : 
      82             : /*
      83             :  * CalculateShmemSize
      84             :  *      Calculates the amount of shared memory needed.
      85             :  */
      86             : Size
      87        4108 : CalculateShmemSize(void)
      88             : {
      89             :     Size        size;
      90             : 
      91             :     /*
      92             :      * Size of the Postgres shared-memory block is estimated via moderately-
      93             :      * accurate estimates for the big hogs, plus 100K for the stuff that's too
      94             :      * small to bother with estimating.
      95             :      *
      96             :      * We take some care to ensure that the total size request doesn't
      97             :      * overflow size_t.  If this gets through, we don't need to be so careful
      98             :      * during the actual allocation phase.
      99             :      */
     100        4108 :     size = 100000;
     101        4108 :     size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
     102             :                                              sizeof(ShmemIndexEnt)));
     103        4108 :     size = add_size(size, dsm_estimate_size());
     104        4108 :     size = add_size(size, DSMRegistryShmemSize());
     105        4108 :     size = add_size(size, BufferManagerShmemSize());
     106        4108 :     size = add_size(size, LockManagerShmemSize());
     107        4108 :     size = add_size(size, PredicateLockShmemSize());
     108        4108 :     size = add_size(size, ProcGlobalShmemSize());
     109        4108 :     size = add_size(size, XLogPrefetchShmemSize());
     110        4108 :     size = add_size(size, VarsupShmemSize());
     111        4108 :     size = add_size(size, XLOGShmemSize());
     112        4108 :     size = add_size(size, XLogRecoveryShmemSize());
     113        4108 :     size = add_size(size, CLOGShmemSize());
     114        4108 :     size = add_size(size, CommitTsShmemSize());
     115        4108 :     size = add_size(size, SUBTRANSShmemSize());
     116        4108 :     size = add_size(size, TwoPhaseShmemSize());
     117        4108 :     size = add_size(size, BackgroundWorkerShmemSize());
     118        4108 :     size = add_size(size, MultiXactShmemSize());
     119        4108 :     size = add_size(size, LWLockShmemSize());
     120        4108 :     size = add_size(size, ProcArrayShmemSize());
     121        4108 :     size = add_size(size, BackendStatusShmemSize());
     122        4108 :     size = add_size(size, SharedInvalShmemSize());
     123        4108 :     size = add_size(size, PMSignalShmemSize());
     124        4108 :     size = add_size(size, ProcSignalShmemSize());
     125        4108 :     size = add_size(size, CheckpointerShmemSize());
     126        4108 :     size = add_size(size, AutoVacuumShmemSize());
     127        4108 :     size = add_size(size, ReplicationSlotsShmemSize());
     128        4108 :     size = add_size(size, ReplicationOriginShmemSize());
     129        4108 :     size = add_size(size, WalSndShmemSize());
     130        4108 :     size = add_size(size, WalRcvShmemSize());
     131        4108 :     size = add_size(size, WalSummarizerShmemSize());
     132        4108 :     size = add_size(size, PgArchShmemSize());
     133        4108 :     size = add_size(size, ApplyLauncherShmemSize());
     134        4108 :     size = add_size(size, BTreeShmemSize());
     135        4108 :     size = add_size(size, SyncScanShmemSize());
     136        4108 :     size = add_size(size, AsyncShmemSize());
     137        4108 :     size = add_size(size, StatsShmemSize());
     138        4108 :     size = add_size(size, WaitEventCustomShmemSize());
     139        4108 :     size = add_size(size, InjectionPointShmemSize());
     140        4108 :     size = add_size(size, SlotSyncShmemSize());
     141        4108 :     size = add_size(size, AioShmemSize());
     142        4108 :     size = add_size(size, WaitLSNShmemSize());
     143             : 
     144             :     /* include additional requested shmem from preload libraries */
     145        4108 :     size = add_size(size, total_addin_request);
     146             : 
     147             :     /* might as well round it off to a multiple of a typical page size */
     148        4108 :     size = add_size(size, 8192 - (size % 8192));
     149             : 
     150        4108 :     return size;
     151             : }
     152             : 
     153             : #ifdef EXEC_BACKEND
     154             : /*
     155             :  * AttachSharedMemoryStructs
     156             :  *      Initialize a postmaster child process's access to shared memory
     157             :  *      structures.
     158             :  *
     159             :  * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
     160             :  * isn't needed.
     161             :  */
     162             : void
     163             : AttachSharedMemoryStructs(void)
     164             : {
     165             :     /* InitProcess must've been called already */
     166             :     Assert(MyProc != NULL);
     167             :     Assert(IsUnderPostmaster);
     168             : 
     169             :     /*
     170             :      * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
     171             :      * groups we calculated before setting the shmem up, so recalculate it.
     172             :      */
     173             :     InitializeFastPathLocks();
     174             : 
     175             :     CreateOrAttachShmemStructs();
     176             : 
     177             :     /*
     178             :      * Now give loadable modules a chance to set up their shmem allocations
     179             :      */
     180             :     if (shmem_startup_hook)
     181             :         shmem_startup_hook();
     182             : }
     183             : #endif
     184             : 
     185             : /*
     186             :  * CreateSharedMemoryAndSemaphores
     187             :  *      Creates and initializes shared memory and semaphores.
     188             :  */
     189             : void
     190        2208 : CreateSharedMemoryAndSemaphores(void)
     191             : {
     192             :     PGShmemHeader *shim;
     193             :     PGShmemHeader *seghdr;
     194             :     Size        size;
     195             : 
     196             :     Assert(!IsUnderPostmaster);
     197             : 
     198             :     /* Compute the size of the shared-memory block */
     199        2208 :     size = CalculateShmemSize();
     200        2208 :     elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
     201             : 
     202             :     /*
     203             :      * Create the shmem segment
     204             :      */
     205        2208 :     seghdr = PGSharedMemoryCreate(size, &shim);
     206             : 
     207             :     /*
     208             :      * Make sure that huge pages are never reported as "unknown" while the
     209             :      * server is running.
     210             :      */
     211             :     Assert(strcmp("unknown",
     212             :                   GetConfigOption("huge_pages_status", false, false)) != 0);
     213             : 
     214        2204 :     InitShmemAccess(seghdr);
     215             : 
     216             :     /*
     217             :      * Set up shared memory allocation mechanism
     218             :      */
     219        2204 :     InitShmemAllocation();
     220             : 
     221             :     /* Initialize subsystems */
     222        2204 :     CreateOrAttachShmemStructs();
     223             : 
     224             :     /* Initialize dynamic shared memory facilities. */
     225        2204 :     dsm_postmaster_startup(shim);
     226             : 
     227             :     /*
     228             :      * Now give loadable modules a chance to set up their shmem allocations
     229             :      */
     230        2204 :     if (shmem_startup_hook)
     231          30 :         shmem_startup_hook();
     232        2204 : }
     233             : 
     234             : /*
     235             :  * Initialize various subsystems, setting up their data structures in
     236             :  * shared memory.
     237             :  *
     238             :  * This is called by the postmaster or by a standalone backend.
     239             :  * It is also called by a backend forked from the postmaster in the
     240             :  * EXEC_BACKEND case.  In the latter case, the shared memory segment
     241             :  * already exists and has been physically attached to, but we have to
     242             :  * initialize pointers in local memory that reference the shared structures,
     243             :  * because we didn't inherit the correct pointer values from the postmaster
     244             :  * as we do in the fork() scenario.  The easiest way to do that is to run
     245             :  * through the same code as before.  (Note that the called routines mostly
     246             :  * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
     247             :  * This is a bit code-wasteful and could be cleaned up.)
     248             :  */
     249             : static void
     250        2204 : CreateOrAttachShmemStructs(void)
     251             : {
     252             :     /*
     253             :      * Now initialize LWLocks, which do shared memory allocation and are
     254             :      * needed for InitShmemIndex.
     255             :      */
     256        2204 :     CreateLWLocks();
     257             : 
     258             :     /*
     259             :      * Set up shmem.c index hashtable
     260             :      */
     261        2204 :     InitShmemIndex();
     262             : 
     263        2204 :     dsm_shmem_init();
     264        2204 :     DSMRegistryShmemInit();
     265             : 
     266             :     /*
     267             :      * Set up xlog, clog, and buffers
     268             :      */
     269        2204 :     VarsupShmemInit();
     270        2204 :     XLOGShmemInit();
     271        2204 :     XLogPrefetchShmemInit();
     272        2204 :     XLogRecoveryShmemInit();
     273        2204 :     CLOGShmemInit();
     274        2204 :     CommitTsShmemInit();
     275        2204 :     SUBTRANSShmemInit();
     276        2204 :     MultiXactShmemInit();
     277        2204 :     BufferManagerShmemInit();
     278             : 
     279             :     /*
     280             :      * Set up lock manager
     281             :      */
     282        2204 :     LockManagerShmemInit();
     283             : 
     284             :     /*
     285             :      * Set up predicate lock manager
     286             :      */
     287        2204 :     PredicateLockShmemInit();
     288             : 
     289             :     /*
     290             :      * Set up process table
     291             :      */
     292        2204 :     if (!IsUnderPostmaster)
     293        2204 :         InitProcGlobal();
     294        2204 :     ProcArrayShmemInit();
     295        2204 :     BackendStatusShmemInit();
     296        2204 :     TwoPhaseShmemInit();
     297        2204 :     BackgroundWorkerShmemInit();
     298             : 
     299             :     /*
     300             :      * Set up shared-inval messaging
     301             :      */
     302        2204 :     SharedInvalShmemInit();
     303             : 
     304             :     /*
     305             :      * Set up interprocess signaling mechanisms
     306             :      */
     307        2204 :     PMSignalShmemInit();
     308        2204 :     ProcSignalShmemInit();
     309        2204 :     CheckpointerShmemInit();
     310        2204 :     AutoVacuumShmemInit();
     311        2204 :     ReplicationSlotsShmemInit();
     312        2204 :     ReplicationOriginShmemInit();
     313        2204 :     WalSndShmemInit();
     314        2204 :     WalRcvShmemInit();
     315        2204 :     WalSummarizerShmemInit();
     316        2204 :     PgArchShmemInit();
     317        2204 :     ApplyLauncherShmemInit();
     318        2204 :     SlotSyncShmemInit();
     319             : 
     320             :     /*
     321             :      * Set up other modules that need some shared memory space
     322             :      */
     323        2204 :     BTreeShmemInit();
     324        2204 :     SyncScanShmemInit();
     325        2204 :     AsyncShmemInit();
     326        2204 :     StatsShmemInit();
     327        2204 :     WaitEventCustomShmemInit();
     328        2204 :     InjectionPointShmemInit();
     329        2204 :     AioShmemInit();
     330        2204 :     WaitLSNShmemInit();
     331        2204 : }
     332             : 
     333             : /*
     334             :  * InitializeShmemGUCs
     335             :  *
     336             :  * This function initializes runtime-computed GUCs related to the amount of
     337             :  * shared memory required for the current configuration.
     338             :  */
     339             : void
     340        1900 : InitializeShmemGUCs(void)
     341             : {
     342             :     char        buf[64];
     343             :     Size        size_b;
     344             :     Size        size_mb;
     345             :     Size        hp_size;
     346             : 
     347             :     /*
     348             :      * Calculate the shared memory size and round up to the nearest megabyte.
     349             :      */
     350        1900 :     size_b = CalculateShmemSize();
     351        1900 :     size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
     352        1900 :     sprintf(buf, "%zu", size_mb);
     353        1900 :     SetConfigOption("shared_memory_size", buf,
     354             :                     PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
     355             : 
     356             :     /*
     357             :      * Calculate the number of huge pages required.
     358             :      */
     359        1900 :     GetHugePageSize(&hp_size, NULL);
     360        1900 :     if (hp_size != 0)
     361             :     {
     362             :         Size        hp_required;
     363             : 
     364        1900 :         hp_required = add_size(size_b / hp_size, 1);
     365        1900 :         sprintf(buf, "%zu", hp_required);
     366        1900 :         SetConfigOption("shared_memory_size_in_huge_pages", buf,
     367             :                         PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
     368             :     }
     369             : 
     370        1900 :     sprintf(buf, "%d", ProcGlobalSemas());
     371        1900 :     SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
     372        1900 : }

Generated by: LCOV version 1.16