Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*-------------------------------------------------------------------------
 *
 * ipci.c
 *    POSTGRES inter-process communication initialization code.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    src/backend/storage/ipc/ipci.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
 
#include "miscadmin.h"
#include "pgstat.h"
/* [PG_CUSTOM: CFS] Compressed File Store shared memory and lifecycle. */
#ifdef USE_CFS
#include "storage/cfs/cfs_recovery.h"
#include "storage/cfs/cfs_shmem.h"
#endif
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/lock.h"
#include "storage/pg_shmem.h"
#include "storage/proc.h"
#include "storage/shmem_internal.h"
#include "storage/subsystems.h"
#include "utils/guc.h"
 
/* GUCs */
int         shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE;
 
shmem_startup_hook_type shmem_startup_hook = NULL;
 
static Size total_addin_request = 0;
 
/*
 * RequestAddinShmemSpace
 *      Request that extra shmem space be allocated for use by
 *      a loadable module.
 *
 * This may only be called via the shmem_request_hook of a library that is
 * loaded into the postmaster via shared_preload_libraries.  Calls from
 * elsewhere will fail.
 */
void
RequestAddinShmemSpace(Size size)
{
    if (!process_shmem_requests_in_progress)
        elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
    total_addin_request = add_size(total_addin_request, size);
}
 
/*
 * CalculateShmemSize
 *      Calculates the amount of shared memory needed.
 */
Size
CalculateShmemSize(void)
{
    Size        size;
 
    /*
     * Size of the Postgres shared-memory block is estimated via moderately-
     * accurate estimates for the big hogs, plus 100K for the stuff that's too
     * small to bother with estimating.
     *
     * We take some care to ensure that the total size request doesn't
     * overflow size_t.  If this gets through, we don't need to be so careful
     * during the actual allocation phase.
     */
    size = 100000;
    size = add_size(size, ShmemGetRequestedSize());
 
    /* [PG_CUSTOM: CFS] */
#ifdef USE_CFS
    size = add_size(size, CfsShmemSize());
#endif
 
    /* include additional requested shmem from preload libraries */
    size = add_size(size, total_addin_request);
 
    /* might as well round it off to a multiple of a typical page size */
    size = add_size(size, 8192 - (size % 8192));
 
    return size;
}
 
#ifdef EXEC_BACKEND
/*
 * AttachSharedMemoryStructs
 *      Initialize a postmaster child process's access to shared memory
 *      structures.
 *
 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
 * isn't needed.
 */
void
AttachSharedMemoryStructs(void)
{
    /* InitProcess must've been called already */
    Assert(MyProc != NULL);
    Assert(IsUnderPostmaster);
 
    /*
     * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
     * groups we calculated before setting the shmem up, so recalculate it.
     */
    InitializeFastPathLocks();
 
    /* Establish pointers to all shared memory areas in this backend */
    ShmemAttachRequested();
 
    /* [PG_CUSTOM: CFS] */
#ifdef USE_CFS
    CfsShmemInit();
    CfsSubsystemInit();
#endif
 
    /*
     * Now give loadable modules a chance to set up their shmem allocations
     */
    if (shmem_startup_hook)
        shmem_startup_hook();
}
#endif
 
/*
 * CreateSharedMemoryAndSemaphores
 *      Creates and initializes shared memory and semaphores.
 */
void
CreateSharedMemoryAndSemaphores(void)
{
    PGShmemHeader *shim;
    PGShmemHeader *seghdr;
    Size        size;
 
    Assert(!IsUnderPostmaster);
 
    /* Compute the size of the shared-memory block */
    size = CalculateShmemSize();
    elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
 
    /*
     * Create the shmem segment
     */
    seghdr = PGSharedMemoryCreate(size, &shim);
 
    /*
     * Make sure that huge pages are never reported as "unknown" while the
     * server is running.
     */
    Assert(strcmp("unknown",
                  GetConfigOption("huge_pages_status", false, false)) != 0);
 
    /*
     * Set up shared memory allocation mechanism
     */
    InitShmemAllocator(seghdr);
 
    /* Initialize all shmem areas */
    ShmemInitRequested();
 
    /* [PG_CUSTOM: CFS] */
#ifdef USE_CFS
    CfsShmemInit();
    CfsSubsystemInit();
#endif
 
    /* Initialize dynamic shared memory facilities. */
    dsm_postmaster_startup(shim);
 
    /*
     * Now give loadable modules a chance to set up their shmem allocations
     */
    if (shmem_startup_hook)
        shmem_startup_hook();
}
 
/*
 * Early initialization of various subsystems, giving them a chance to
 * register their shared memory needs before the shared memory segment is
 * allocated.
 */
void
RegisterBuiltinShmemCallbacks(void)
{
    /*
     * Call RegisterShmemCallbacks(...) on each subsystem listed in
     * subsystemlist.h
     */
#define PG_SHMEM_SUBSYSTEM(subsystem_callbacks) \
    RegisterShmemCallbacks(&(subsystem_callbacks));
 
#include "storage/subsystemlist.h"
 
#undef PG_SHMEM_SUBSYSTEM
}
 
/*
 * 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;
    Size        hp_size;
 
    /*
     * Calculate the shared memory size and round up to the nearest megabyte.
     */
    size_b = CalculateShmemSize();
    size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
    sprintf(buf, "%zu", size_mb);
    SetConfigOption("shared_memory_size", buf,
                    PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
 
    /*
     * Calculate the number of huge pages required.
     */
    GetHugePageSize(&hp_size, NULL);
    if (hp_size != 0)
    {
        Size        hp_required;
 
        hp_required = size_b / hp_size;
        if (size_b % hp_size != 0)
            hp_required = add_size(hp_required, 1);
        sprintf(buf, "%zu", hp_required);
        SetConfigOption("shared_memory_size_in_huge_pages", buf,
                        PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
    }
 
    sprintf(buf, "%d", ProcGlobalSemas());
    SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
}