8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.64 2004/02/10 01:55:25 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.65 2004/02/25 19:41:22 momjian Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
36
36
*Creates and initializes shared memory and semaphores.
37
37
*
38
38
* This is called by the postmaster or by a standalone backend.
39
- * It is NEVER called by a backend forked from the postmaster;
40
- * for such a backend, the shared memory is already ready-to-go.
39
+ * It is also called by a backend forked from the postmaster under
40
+ * the EXEC_BACKEND case
41
+ *
42
+ * In the non EXEC_BACKEND case, backends already have shared memory ready-to-go.
41
43
*
42
44
* If "makePrivate" is true then we only need private memory, not shared
43
45
* memory.This is true for a standalone backend, false for a postmaster.
@@ -47,53 +49,68 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
47
49
int maxBackends ,
48
50
int port )
49
51
{
50
- int size ;
51
- int numSemas ;
52
- PGShmemHeader * seghdr ;
53
-
54
- /*
55
- * Size of the Postgres shared-memory block is estimated via
56
- * moderately-accurate estimates for the big hogs, plus 100K for the
57
- * stuff that's too small to bother with estimating.
58
- */
59
- size = BufferShmemSize ();
60
- size += LockShmemSize (maxBackends );
61
- size += XLOGShmemSize ();
62
- size += CLOGShmemSize ();
63
- size += LWLockShmemSize ();
64
- size += SInvalShmemSize (maxBackends );
65
- size += FreeSpaceShmemSize ();
52
+ PGShmemHeader * seghdr = NULL ;
53
+ if (!IsUnderPostmaster )
54
+ {
55
+ int size ;
56
+ int numSemas ;
57
+
58
+ /*
59
+ * Size of the Postgres shared-memory block is estimated via
60
+ * moderately-accurate estimates for the big hogs, plus 100K for the
61
+ * stuff that's too small to bother with estimating.
62
+ */
63
+ size = BufferShmemSize ();
64
+ size += LockShmemSize (maxBackends );
65
+ size += XLOGShmemSize ();
66
+ size += CLOGShmemSize ();
67
+ size += LWLockShmemSize ();
68
+ size += SInvalShmemSize (maxBackends );
69
+ size += FreeSpaceShmemSize ();
66
70
#ifdef EXEC_BACKEND
67
- size += ShmemBackendArraySize ();
71
+ size += ShmemBackendArraySize ();
68
72
#endif
69
- size += 100000 ;
70
- /* might as well round it off to a multiple of a typical page size */
71
- size += 8192 - (size %8192 );
72
-
73
- elog (DEBUG3 ,"invoking IpcMemoryCreate(size=%d)" ,size );
73
+ size += 100000 ;
74
+ /* might as well round it off to a multiple of a typical page size */
75
+ size += 8192 - (size %8192 );
76
+
77
+ elog (DEBUG3 ,"invoking IpcMemoryCreate(size=%d)" ,size );
78
+
79
+ /*
80
+ * Create the shmem segment
81
+ */
82
+ seghdr = PGSharedMemoryCreate (size ,makePrivate ,port );
83
+
84
+ /*
85
+ * Create semaphores
86
+ */
87
+ numSemas = ProcGlobalSemas (maxBackends );
88
+ numSemas += SpinlockSemas ();
89
+ PGReserveSemaphores (numSemas ,port );
90
+ }
91
+ else
92
+ {
93
+ /*
94
+ * Attach to the shmem segment.
95
+ * (this should only ever be reached by EXEC_BACKEND code,
96
+ * and only then with makePrivate == false)
97
+ */
98
+ Assert (ExecBackend && !makePrivate );
99
+ seghdr = PGSharedMemoryCreate (-1 ,makePrivate ,0 );
100
+ }
74
101
75
- /*
76
- * Create the shmem segment
77
- */
78
- seghdr = PGSharedMemoryCreate (size ,makePrivate ,port );
79
-
80
- /*
81
- * Create semaphores
82
- */
83
- numSemas = ProcGlobalSemas (maxBackends );
84
- numSemas += SpinlockSemas ();
85
- PGReserveSemaphores (numSemas ,port );
86
102
87
103
/*
88
104
* Set up shared memory allocation mechanism
89
105
*/
90
- InitShmemAllocation (seghdr ,true );
106
+ InitShmemAllocation (seghdr ,! IsUnderPostmaster );
91
107
92
108
/*
93
109
* Now initialize LWLocks, which do shared memory allocation and are
94
110
* needed for InitShmemIndex.
95
111
*/
96
- CreateLWLocks ();
112
+ if (!IsUnderPostmaster )
113
+ CreateLWLocks ();
97
114
98
115
/*
99
116
* Set up shmem.c index hashtable
@@ -137,41 +154,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
137
154
/*
138
155
* Alloc the win32 shared backend array
139
156
*/
140
- ShmemBackendArrayAllocation ();
157
+ if (!IsUnderPostmaster )
158
+ ShmemBackendArrayAllocation ();
141
159
#endif
142
160
}
143
-
144
-
145
- #ifdef EXEC_BACKEND
146
- /*
147
- * AttachSharedMemoryAndSemaphores
148
- *Attaches to the existing shared resources.
149
- */
150
-
151
- /* FIXME: [fork/exec] This function is starting to look pretty much like
152
- CreateSharedMemoryAndSemaphores. Refactor? */
153
- void
154
- AttachSharedMemoryAndSemaphores (void )
155
- {
156
- PGShmemHeader * seghdr = PGSharedMemoryCreate (-1 ,false,-1 );
157
-
158
- InitShmemAllocation (seghdr , false);
159
-
160
- InitShmemIndex ();
161
-
162
- XLOGShmemInit ();
163
- CLOGShmemInit ();
164
- InitBufferPool ();
165
-
166
- InitLocks ();
167
- InitLockTable (MaxBackends );
168
-
169
- InitProcGlobal (MaxBackends );
170
-
171
- CreateSharedInvalidationState (MaxBackends );
172
-
173
- InitFreeSpaceMap ();
174
-
175
- PMSignalInit ();
176
- }
177
- #endif