1010 *
1111 *
1212 * IDENTIFICATION
13- * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.61 1997/11/10 05:10:21 momjian Exp $
13+ * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.62 1997/11/17 03:47:28 scrappy Exp $
1414 *
1515 * NOTES
1616 *
@@ -123,6 +123,32 @@ static Dllist *PortList;
123123
124124static short PostPortName = -1 ;
125125static short ActiveBackends = FALSE;
126+ /* This is a boolean indicating that there is at least one backend
127+ that is accessing the current shared memory and semaphores.
128+ Between the time that we start up, or throw away shared memory
129+ segments and start over, and the time we generate the next
130+ backend (because we received a connection request), it is false.
131+ Other times, it is true.
132+ */
133+ static short shmem_seq = 0 ;
134+ /* This is a sequence number that indicates how many times we've had
135+ to throw away the shared memory and start over because we doubted
136+ its integrity. It starts off at zero and is incremented every
137+ time we start over. We use this to ensure that we use a new
138+ IPC shared memory key for the new shared memory segment in case
139+ the old segment isn't entirely gone yet.
140+
141+ The sequence actually cycles back to 0 after 9, so pathologically
142+ there could be an IPC failure if 10 sets of backends are all stuck
143+ and won't release IPC resources.
144+ */
145+
146+ static IpcMemoryKey ipc_key ;
147+ /* This is the base IPC shared memory key. Other keys are generated by
148+ adding to this.
149+ */
150+
151+
126152static int NextBackendId = MAXINT ;/* XXX why? */
127153static char * progname = (char * )NULL ;
128154
@@ -904,11 +930,11 @@ ConnCreate(int serverFd, int *newFdP)
904930static void
905931reset_shared (short port )
906932{
907- IPCKey key ;
908-
909- key = SystemPortAddressCreateIPCKey (( SystemPortAddress ) port ) ;
910- CreateSharedMemoryAndSemaphores ( key ) ;
911- ActiveBackends = FALSE ;
933+ ipc_key = port * 1000 + shmem_seq * 100 ;
934+ CreateSharedMemoryAndSemaphores ( ipc_key );
935+ ActiveBackends = FALSE ;
936+ shmem_seq += 1 ;
937+ if ( shmem_seq >= 10 ) shmem_seq -= 10 ;
912938}
913939
914940/*
@@ -1079,9 +1105,10 @@ BackendStartup(StartupInfo *packet,/* client's startup packet */
10791105Backend * bn ;/* for backend cleanup */
10801106int pid ,
10811107i ;
1082- static char envEntry [4 ][2 * ARGV_SIZE ];
1108+ #define NR_ENVIRONMENT_VBL 5
1109+ static char envEntry [NR_ENVIRONMENT_VBL ][2 * ARGV_SIZE ];
10831110
1084- for (i = 0 ;i < 4 ;++ i )
1111+ for (i = 0 ;i < NR_ENVIRONMENT_VBL ;++ i )
10851112{
10861113MemSet (envEntry [i ],0 ,2 * ARGV_SIZE );
10871114}
@@ -1101,6 +1128,9 @@ BackendStartup(StartupInfo *packet,/* client's startup packet */
11011128sprintf (envEntry [3 ],"PGDATA=%s" ,DataDir );
11021129putenv (envEntry [3 ]);
11031130}
1131+ sprintf (envEntry [4 ],"IPC_KEY=%d" ,ipc_key );
1132+ putenv (envEntry [4 ]);
1133+
11041134if (DebugLvl > 2 )
11051135{
11061136char * * p ;