LCOV - differential code coverage report
Current view: top level - src/backend/storage/ipc - ipci.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 806555e3000d0b0e0c536c1dc65548128d457d86 vs 1d325ad99cb2dec0e8b45ba36909ee0a497d2a57 Lines: 99.2 % 121 120 1 6 114 9
Current Date: 2025-12-17 08:58:58 +0900 Functions: 100.0 % 5 5 4 1 1
Baseline: lcov-20251217-005640-baseline Branches: 56.2 % 16 9 7 9
Baseline Date: 2025-12-16 12:57:12 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 8 8 6 2
(360..) days: 99.1 % 113 112 1 112
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 4 4 3 1
Branch coverage date bins:
(360..) days: 56.2 % 16 9 7 9

 Age         Owner                    Branch data    TLA  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
 7003 tgl@sss.pgh.pa.us          75                 :CBC          12 : RequestAddinShmemSpace(Size size)
                                 76                 :                : {
 1314 rhaas@postgresql.org       77         [ -  + ]:             12 :     if (!process_shmem_requests_in_progress)
 1314 rhaas@postgresql.org       78         [ #  # ]:UBC           0 :         elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
 7003 tgl@sss.pgh.pa.us          79                 :CBC          12 :     total_addin_request = add_size(total_addin_request, size);
                                 80                 :             12 : }
                                 81                 :                : 
                                 82                 :                : /*
                                 83                 :                :  * CalculateShmemSize
                                 84                 :                :  *      Calculates the amount of shared memory needed.
                                 85                 :                :  */
                                 86                 :                : Size
   41 heikki.linnakangas@i       87                 :GNC        1986 : 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                 :                :      */
 1563 michael@paquier.xyz       100                 :CBC        1986 :     size = 100000;
                                101                 :           1986 :     size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
                                102                 :                :                                              sizeof(ShmemIndexEnt)));
                                103                 :           1986 :     size = add_size(size, dsm_estimate_size());
  698 nathan@postgresql.or      104                 :           1986 :     size = add_size(size, DSMRegistryShmemSize());
  475 heikki.linnakangas@i      105                 :           1986 :     size = add_size(size, BufferManagerShmemSize());
                                106                 :           1986 :     size = add_size(size, LockManagerShmemSize());
 1563 michael@paquier.xyz       107                 :           1986 :     size = add_size(size, PredicateLockShmemSize());
                                108                 :           1986 :     size = add_size(size, ProcGlobalShmemSize());
 1350 tmunro@postgresql.or      109                 :           1986 :     size = add_size(size, XLogPrefetchShmemSize());
  740 heikki.linnakangas@i      110                 :           1986 :     size = add_size(size, VarsupShmemSize());
 1563 michael@paquier.xyz       111                 :           1986 :     size = add_size(size, XLOGShmemSize());
 1400 heikki.linnakangas@i      112                 :           1986 :     size = add_size(size, XLogRecoveryShmemSize());
 1563 michael@paquier.xyz       113                 :           1986 :     size = add_size(size, CLOGShmemSize());
                                114                 :           1986 :     size = add_size(size, CommitTsShmemSize());
                                115                 :           1986 :     size = add_size(size, SUBTRANSShmemSize());
                                116                 :           1986 :     size = add_size(size, TwoPhaseShmemSize());
                                117                 :           1986 :     size = add_size(size, BackgroundWorkerShmemSize());
                                118                 :           1986 :     size = add_size(size, MultiXactShmemSize());
                                119                 :           1986 :     size = add_size(size, LWLockShmemSize());
                                120                 :           1986 :     size = add_size(size, ProcArrayShmemSize());
                                121                 :           1986 :     size = add_size(size, BackendStatusShmemSize());
  475 heikki.linnakangas@i      122                 :           1986 :     size = add_size(size, SharedInvalShmemSize());
 1563 michael@paquier.xyz       123                 :           1986 :     size = add_size(size, PMSignalShmemSize());
                                124                 :           1986 :     size = add_size(size, ProcSignalShmemSize());
                                125                 :           1986 :     size = add_size(size, CheckpointerShmemSize());
                                126                 :           1986 :     size = add_size(size, AutoVacuumShmemSize());
                                127                 :           1986 :     size = add_size(size, ReplicationSlotsShmemSize());
                                128                 :           1986 :     size = add_size(size, ReplicationOriginShmemSize());
                                129                 :           1986 :     size = add_size(size, WalSndShmemSize());
                                130                 :           1986 :     size = add_size(size, WalRcvShmemSize());
  728 rhaas@postgresql.org      131                 :           1986 :     size = add_size(size, WalSummarizerShmemSize());
 1563 michael@paquier.xyz       132                 :           1986 :     size = add_size(size, PgArchShmemSize());
                                133                 :           1986 :     size = add_size(size, ApplyLauncherShmemSize());
                                134                 :           1986 :     size = add_size(size, BTreeShmemSize());
                                135                 :           1986 :     size = add_size(size, SyncScanShmemSize());
                                136                 :           1986 :     size = add_size(size, AsyncShmemSize());
 1351 andres@anarazel.de        137                 :           1986 :     size = add_size(size, StatsShmemSize());
  538 noah@leadboat.com         138                 :           1986 :     size = add_size(size, WaitEventCustomShmemSize());
  695 michael@paquier.xyz       139                 :           1986 :     size = add_size(size, InjectionPointShmemSize());
  672 akapila@postgresql.o      140                 :           1986 :     size = add_size(size, SlotSyncShmemSize());
  275 andres@anarazel.de        141                 :           1986 :     size = add_size(size, AioShmemSize());
   44 akorotkov@postgresql      142                 :GNC        1986 :     size = add_size(size, WaitLSNShmemSize());
                                143                 :                : 
                                144                 :                :     /* include additional requested shmem from preload libraries */
 1563 michael@paquier.xyz       145                 :CBC        1986 :     size = add_size(size, total_addin_request);
                                146                 :                : 
                                147                 :                :     /* might as well round it off to a multiple of a typical page size */
                                148                 :           1986 :     size = add_size(size, 8192 - (size % 8192));
                                149                 :                : 
                                150                 :           1986 :     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
  745 heikki.linnakangas@i      190                 :           1071 : CreateSharedMemoryAndSemaphores(void)
                                191                 :                : {
                                192                 :                :     PGShmemHeader *shim;
                                193                 :                :     PGShmemHeader *seghdr;
                                194                 :                :     Size        size;
                                195                 :                : 
                                196         [ -  + ]:           1071 :     Assert(!IsUnderPostmaster);
                                197                 :                : 
                                198                 :                :     /* Compute the size of the shared-memory block */
   41 heikki.linnakangas@i      199                 :GNC        1071 :     size = CalculateShmemSize();
  745 heikki.linnakangas@i      200         [ +  + ]:CBC        1071 :     elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
                                201                 :                : 
                                202                 :                :     /*
                                203                 :                :      * Create the shmem segment
                                204                 :                :      */
                                205                 :           1071 :     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         [ -  + ]:           1069 :     Assert(strcmp("unknown",
                                212                 :                :                   GetConfigOption("huge_pages_status", false, false)) != 0);
                                213                 :                : 
                                214                 :           1069 :     InitShmemAccess(seghdr);
                                215                 :                : 
                                216                 :                :     /*
                                217                 :                :      * Set up shared memory allocation mechanism
                                218                 :                :      */
                                219                 :           1069 :     InitShmemAllocation();
                                220                 :                : 
                                221                 :                :     /* Initialize subsystems */
                                222                 :           1069 :     CreateOrAttachShmemStructs();
                                223                 :                : 
                                224                 :                :     /* Initialize dynamic shared memory facilities. */
                                225                 :           1069 :     dsm_postmaster_startup(shim);
                                226                 :                : 
                                227                 :                :     /*
                                228                 :                :      * Now give loadable modules a chance to set up their shmem allocations
                                229                 :                :      */
                                230         [ +  + ]:           1069 :     if (shmem_startup_hook)
                                231                 :             12 :         shmem_startup_hook();
                                232                 :           1069 : }
                                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                 :           1069 : CreateOrAttachShmemStructs(void)
                                251                 :                : {
                                252                 :                :     /*
                                253                 :                :      * Now initialize LWLocks, which do shared memory allocation and are
                                254                 :                :      * needed for InitShmemIndex.
                                255                 :                :      */
 4342 rhaas@postgresql.org      256                 :           1069 :     CreateLWLocks();
                                257                 :                : 
                                258                 :                :     /*
                                259                 :                :      * Set up shmem.c index hashtable
                                260                 :                :      */
 8845 tgl@sss.pgh.pa.us         261                 :           1069 :     InitShmemIndex();
                                262                 :                : 
 1965 tmunro@postgresql.or      263                 :           1069 :     dsm_shmem_init();
  698 nathan@postgresql.or      264                 :           1069 :     DSMRegistryShmemInit();
                                265                 :                : 
                                266                 :                :     /*
                                267                 :                :      * Set up xlog, clog, and buffers
                                268                 :                :      */
  740 heikki.linnakangas@i      269                 :           1069 :     VarsupShmemInit();
 9150 tgl@sss.pgh.pa.us         270                 :           1069 :     XLOGShmemInit();
 1350 tmunro@postgresql.or      271                 :           1069 :     XLogPrefetchShmemInit();
 1400 heikki.linnakangas@i      272                 :           1069 :     XLogRecoveryShmemInit();
 8880 tgl@sss.pgh.pa.us         273                 :           1069 :     CLOGShmemInit();
 4032 alvherre@alvh.no-ip.      274                 :           1069 :     CommitTsShmemInit();
 7839 tgl@sss.pgh.pa.us         275                 :           1069 :     SUBTRANSShmemInit();
 7538                           276                 :           1069 :     MultiXactShmemInit();
  475 heikki.linnakangas@i      277                 :           1069 :     BufferManagerShmemInit();
                                278                 :                : 
                                279                 :                :     /*
                                280                 :                :      * Set up lock manager
                                281                 :                :      */
                                282                 :           1069 :     LockManagerShmemInit();
                                283                 :                : 
                                284                 :                :     /*
                                285                 :                :      * Set up predicate lock manager
                                286                 :                :      */
                                287                 :           1069 :     PredicateLockShmemInit();
                                288                 :                : 
                                289                 :                :     /*
                                290                 :                :      * Set up process table
                                291                 :                :      */
 7287 tgl@sss.pgh.pa.us         292         [ +  - ]:           1069 :     if (!IsUnderPostmaster)
                                293                 :           1069 :         InitProcGlobal();
  475 heikki.linnakangas@i      294                 :           1069 :     ProcArrayShmemInit();
                                295                 :           1069 :     BackendStatusShmemInit();
 5136 rhaas@postgresql.org      296                 :           1069 :     TwoPhaseShmemInit();
 4537                           297                 :           1069 :     BackgroundWorkerShmemInit();
                                298                 :                : 
                                299                 :                :     /*
                                300                 :                :      * Set up shared-inval messaging
                                301                 :                :      */
  475 heikki.linnakangas@i      302                 :           1069 :     SharedInvalShmemInit();
                                303                 :                : 
                                304                 :                :     /*
                                305                 :                :      * Set up interprocess signaling mechanisms
                                306                 :                :      */
 6070 tgl@sss.pgh.pa.us         307                 :           1069 :     PMSignalShmemInit();
 5983                           308                 :           1069 :     ProcSignalShmemInit();
 4970 simon@2ndQuadrant.co      309                 :           1069 :     CheckpointerShmemInit();
 6880 alvherre@alvh.no-ip.      310                 :           1069 :     AutoVacuumShmemInit();
 4338 rhaas@postgresql.org      311                 :           1069 :     ReplicationSlotsShmemInit();
 3885 andres@anarazel.de        312                 :           1069 :     ReplicationOriginShmemInit();
 5815 heikki.linnakangas@i      313                 :           1069 :     WalSndShmemInit();
                                314                 :           1069 :     WalRcvShmemInit();
  728 rhaas@postgresql.org      315                 :           1069 :     WalSummarizerShmemInit();
 1738 fujii@postgresql.org      316                 :           1069 :     PgArchShmemInit();
 3254 peter_e@gmx.net           317                 :           1069 :     ApplyLauncherShmemInit();
  672 akapila@postgresql.o      318                 :           1069 :     SlotSyncShmemInit();
                                319                 :                : 
                                320                 :                :     /*
                                321                 :                :      * Set up other modules that need some shared memory space
                                322                 :                :      */
 7163 tgl@sss.pgh.pa.us         323                 :           1069 :     BTreeShmemInit();
 6767                           324                 :           1069 :     SyncScanShmemInit();
 5783                           325                 :           1069 :     AsyncShmemInit();
 1351 andres@anarazel.de        326                 :           1069 :     StatsShmemInit();
  538 noah@leadboat.com         327                 :           1069 :     WaitEventCustomShmemInit();
  695 michael@paquier.xyz       328                 :           1069 :     InjectionPointShmemInit();
  275 andres@anarazel.de        329                 :           1069 :     AioShmemInit();
   44 akorotkov@postgresql      330                 :GNC        1069 :     WaitLSNShmemInit();
10753 scrappy@hub.org           331                 :CBC        1069 : }
                                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
 1561 michael@paquier.xyz       340                 :            915 : 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                 :                :      */
   41 heikki.linnakangas@i      350                 :GNC         915 :     size_b = CalculateShmemSize();
 1561 michael@paquier.xyz       351                 :CBC         915 :     size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
                                352                 :            915 :     sprintf(buf, "%zu", size_mb);
 1288 tgl@sss.pgh.pa.us         353                 :            915 :     SetConfigOption("shared_memory_size", buf,
                                354                 :                :                     PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
                                355                 :                : 
                                356                 :                :     /*
                                357                 :                :      * Calculate the number of huge pages required.
                                358                 :                :      */
 1548 michael@paquier.xyz       359                 :            915 :     GetHugePageSize(&hp_size, NULL);
                                360         [ +  - ]:            915 :     if (hp_size != 0)
                                361                 :                :     {
                                362                 :                :         Size        hp_required;
                                363                 :                : 
                                364                 :            915 :         hp_required = add_size(size_b / hp_size, 1);
                                365                 :            915 :         sprintf(buf, "%zu", hp_required);
 1288 tgl@sss.pgh.pa.us         366                 :            915 :         SetConfigOption("shared_memory_size_in_huge_pages", buf,
                                367                 :                :                         PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
                                368                 :                :     }
                                369                 :                : 
   41 heikki.linnakangas@i      370                 :GNC         915 :     sprintf(buf, "%d", ProcGlobalSemas());
  509 nathan@postgresql.or      371                 :CBC         915 :     SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
 1561 michael@paquier.xyz       372                 :            915 : }
        

Generated by: LCOV version 2.4-beta