Index: src/backend/access/transam/xlog.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/access/transam/xlog.c,v retrieving revision 1.126 diff -w -c -r1.126 xlog.c *** src/backend/access/transam/xlog.c 29 Nov 2003 19:51:40 -0000 1.126 --- src/backend/access/transam/xlog.c 10 Dec 2003 09:30:46 -0000 *************** *** 166,172 **** * to update from XLogCtl->Insert.RedoRecPtr if we hold the info_lck; * see GetRedoRecPtr. */ ! static XLogRecPtr RedoRecPtr; /*---------- * Shared-memory data structures for XLOG control --- 166,172 ---- * to update from XLogCtl->Insert.RedoRecPtr if we hold the info_lck; * see GetRedoRecPtr. */ ! XLogRecPtr RedoRecPtr; /*---------- * Shared-memory data structures for XLOG control *************** *** 231,241 **** XLogRecPtr Flush; /* last byte + 1 to flush */ } XLogwrtRqst; ! typedef struct XLogwrtResult ! { ! XLogRecPtr Write; /* last byte + 1 written out */ ! XLogRecPtr Flush; /* last byte + 1 flushed */ ! } XLogwrtResult; /* * Shared state data for XLogInsert. --- 231,237 ---- XLogRecPtr Flush; /* last byte + 1 to flush */ } XLogwrtRqst; ! /* FIXME: [fork/exec] XLogwrtResult struct moved to xlogdefs.h */ /* * Shared state data for XLogInsert. *************** *** 404,410 **** * Private, possibly out-of-date copy of shared LogwrtResult. * See discussion above. */ ! static XLogwrtResult LogwrtResult = {{0, 0}, {0, 0}}; /* * openLogFile is -1 or a kernel FD for an open log file segment. --- 400,406 ---- * Private, possibly out-of-date copy of shared LogwrtResult. * See discussion above. */ ! XLogwrtResult LogwrtResult = {{0, 0}, {0, 0}}; /* * openLogFile is -1 or a kernel FD for an open log file segment. *************** *** 2397,2403 **** void XLOGShmemInit(void) { ! bool found; /* this must agree with space requested by XLOGShmemSize() */ if (XLOGbuffers < MinXLOGbuffers) --- 2393,2399 ---- void XLOGShmemInit(void) { ! bool foundXLog, foundCFile; /* this must agree with space requested by XLOGShmemSize() */ if (XLOGbuffers < MinXLOGbuffers) *************** *** 2408,2418 **** MAXALIGN(sizeof(XLogCtlData) + sizeof(XLogRecPtr) * XLOGbuffers) + BLCKSZ * XLOGbuffers, ! &found); ! Assert(!found); ControlFile = (ControlFileData *) ! ShmemInitStruct("Control File", sizeof(ControlFileData), &found); ! Assert(!found); memset(XLogCtl, 0, sizeof(XLogCtlData)); --- 2404,2420 ---- MAXALIGN(sizeof(XLogCtlData) + sizeof(XLogRecPtr) * XLOGbuffers) + BLCKSZ * XLOGbuffers, ! &foundXLog); ControlFile = (ControlFileData *) ! ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile); ! ! if (foundXLog || foundCFile) ! { ! /* both should be present or neither */ ! Assert(foundXLog && foundCFile); ! return; ! } ! memset(XLogCtl, 0, sizeof(XLogCtlData)); Index: src/backend/postmaster/pgstat.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/pgstat.c,v retrieving revision 1.48 diff -w -c -r1.48 pgstat.c *** src/backend/postmaster/pgstat.c 29 Nov 2003 19:51:55 -0000 1.48 --- src/backend/postmaster/pgstat.c 10 Dec 2003 09:30:48 -0000 *************** *** 71,77 **** * Local data * ---------- */ ! static int pgStatSock = -1; static int pgStatPipe[2]; static struct sockaddr_storage pgStatAddr; static int pgStatPmPipe[2] = {-1, -1}; --- 71,77 ---- * Local data * ---------- */ ! int pgStatSock = -1; static int pgStatPipe[2]; static struct sockaddr_storage pgStatAddr; static int pgStatPmPipe[2] = {-1, -1}; Index: src/backend/postmaster/postmaster.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v retrieving revision 1.351 diff -w -c -r1.351 postmaster.c *** src/backend/postmaster/postmaster.c 1 Dec 2003 22:15:37 -0000 1.351 --- src/backend/postmaster/postmaster.c 10 Dec 2003 09:30:51 -0000 *************** *** 127,132 **** --- 127,133 ---- AuthBlockSig; #endif + /* * List of active backends (or child processes anyway; we don't actually * know whether a given child has become a backend or is still in the *************** *** 303,308 **** --- 304,311 ---- #define StartBackgroundWriter() SSDataBase(BS_XLOG_BGWRITER) #define ShutdownDataBase() SSDataBase(BS_XLOG_SHUTDOWN) + static enum CAC_state PMcanAcceptConnections = CAC_STARTUP; + static void checkDataDir(const char *checkdir) *************** *** 1244,1250 **** --- 1247,1257 ---- if (proto == CANCEL_REQUEST_CODE) { + #ifdef EXEC_BACKEND + abort(); /* Whoops. Not handled... yet FIXME */ + #else processCancelRequest(port, buf); + #endif return 127; /* XXX */ } *************** *** 1287,1293 **** FrontendProtocol = proto; /* Check we can handle the protocol the frontend is using. */ - if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) || PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) || (PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) && --- 1294,1299 ---- *************** *** 1429,1442 **** * Done putting stuff in TopMemoryContext. */ MemoryContextSwitchTo(oldcontext); - /* * If we're going to reject the connection due to database state, say * so now instead of wasting cycles on an authentication exchange. * (This also allows a pg_ping utility to be written.) */ ! cac = canAcceptConnections(); ! switch (cac) { case CAC_STARTUP: --- 1435,1446 ---- * Done putting stuff in TopMemoryContext. */ MemoryContextSwitchTo(oldcontext); /* * If we're going to reject the connection due to database state, say * so now instead of wasting cycles on an authentication exchange. * (This also allows a pg_ping utility to be written.) */ ! cac = PMcanAcceptConnections; switch (cac) { case CAC_STARTUP: *************** *** 2341,2380 **** } } /* ! * BackendFork -- perform authentication, and if successful, set up the ! * backend's argument list and invoke backend main(). ! * ! * This used to perform an execv() but we no longer exec the backend; ! * it's the same executable as the postmaster. * * returns: * Shouldn't return at all. * If PostgresMain() fails, return status. */ ! static int ! BackendFork(Port *port) { - char **av; - int maxac; - int ac; - char debugbuf[32]; - char protobuf[32]; - - #ifdef EXEC_BACKEND - char pbuf[NAMEDATALEN + 256]; - #endif - int i; int status; struct timeval now; struct timezone tz; char remote_host[NI_MAXHOST]; char remote_port[NI_MAXSERV]; - /* - * Let's clean up ourselves as the postmaster child - */ - IsUnderPostmaster = true; /* we are a postmaster subprocess now */ ClientAuthInProgress = true; /* limit visibility of log messages */ --- 2345,2368 ---- } } + /* ! * BackendInit/Fork -- perform authentication [BackendInit], and if successful, ! * set up the backend's argument list [BackendFork] and invoke ! * backend main() [or exec in EXEC_BACKEND case] * * returns: * Shouldn't return at all. * If PostgresMain() fails, return status. */ ! bool BackendInit(Port *port) { int status; struct timeval now; struct timezone tz; char remote_host[NI_MAXHOST]; char remote_port[NI_MAXSERV]; IsUnderPostmaster = true; /* we are a postmaster subprocess now */ ClientAuthInProgress = true; /* limit visibility of log messages */ *************** *** 2386,2394 **** * Signal handlers setting is moved to tcop/postgres... */ - /* Close the postmaster's other sockets */ - ClosePostmasterPorts(true); - /* Save port etc. for ps status */ MyProcPort = port; --- 2374,2379 ---- *************** *** 2444,2458 **** StrNCpy(remote_host, tmphost, sizeof(remote_host)); } ! /* ! * PreAuthDelay is a debugging aid for investigating problems in the ! * authentication cycle: it can be set in postgresql.conf to allow ! * time to attach to the newly-forked backend with a debugger. (See ! * also the -W backend switch, which we allow clients to pass through ! * PGOPTIONS, but it is not honored until after authentication.) ! */ ! if (PreAuthDelay > 0) ! sleep(PreAuthDelay); /* * Ready to begin client interaction. We will give up and exit(0) --- 2429,2435 ---- StrNCpy(remote_host, tmphost, sizeof(remote_host)); } ! /* FIXME: [fork/exec] previously had PreAuthDelay here */ /* * Ready to begin client interaction. We will give up and exit(0) *************** *** 2469,2475 **** status = ProcessStartupPacket(port, false); if (status != STATUS_OK) ! return 0; /* cancel request processed, or error */ /* * Now that we have the user and database name, we can set the process --- 2446,2452 ---- status = ProcessStartupPacket(port, false); if (status != STATUS_OK) ! return false; /* cancel request processed, or error */ /* * Now that we have the user and database name, we can set the process *************** *** 2506,2511 **** --- 2483,2533 ---- gettimeofday(&now, &tz); srandom((unsigned int) now.tv_usec); + #ifdef EXEC_BACKEND + ClientAuthInProgress = false; /* client_min_messages is active + * now */ + #endif + return true; + } + + + static int + BackendFork(Port *port) + { + char **av; + int maxac; + int ac; + char debugbuf[32]; + #ifndef BACKEND + char protobuf[32]; + #endif + int i; + char tmpExtraOptions[MAXPGPATH]; + + /* + * Let's clean up ourselves as the postmaster child + */ + /* Close the postmaster's other sockets */ + ClosePostmasterPorts(true); + + /* + * PreAuthDelay is a debugging aid for investigating problems in the + * authentication cycle: it can be set in postgresql.conf to allow + * time to attach to the newly-forked backend with a debugger. (See + * also the -W backend switch, which we allow clients to pass through + * PGOPTIONS, but it is not honored until after authentication.) + */ + if (PreAuthDelay > 0) + sleep(PreAuthDelay); + + /* FIXME: [fork/exec] Might be nice to put this in the port struct? */ + PMcanAcceptConnections = canAcceptConnections(); + + #ifndef EXEC_BACKEND + if (!BackendInit(port)) + return -1; + #endif + /* ---------------- * Now, build the argv vector that will be given to PostgresMain. * *************** *** 2540,2569 **** /* * Pass any backend switches specified with -o in the postmaster's own ! * command line. We assume these are secure. (It's OK to mangle ! * ExtraOptions since we are now in the child process; this won't ! * change the postmaster's copy.) */ ! split_opts(av, &ac, ExtraOptions); /* Tell the backend what protocol the frontend is using. */ snprintf(protobuf, sizeof(protobuf), "-v%u", port->proto); av[ac++] = protobuf; /* * Tell the backend it is being called from the postmaster, and which * database to use. -p marks the end of secure switches. */ - av[ac++] = "-p"; #ifdef EXEC_BACKEND ! Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL); ! /* database name at the end because it might contain commas */ ! snprintf(pbuf, sizeof(pbuf), "%d,%d,%lu,%p,%s", ! port->sock, canAcceptConnections(), ! UsedShmemSegID, UsedShmemSegAddr, ! port->database_name); ! av[ac++] = pbuf; #else av[ac++] = port->database_name; #endif --- 2562,2593 ---- /* * Pass any backend switches specified with -o in the postmaster's own ! * command line. We assume these are secure. ! * [FIXME: [fork/exec] changed to make a copy, to protect against future fork/exec changes] */ ! strcpy(tmpExtraOptions,ExtraOptions); ! split_opts(av, &ac, tmpExtraOptions); + #ifndef EXEC_BACKEND /* Tell the backend what protocol the frontend is using. */ snprintf(protobuf, sizeof(protobuf), "-v%u", port->proto); av[ac++] = protobuf; + #endif /* * Tell the backend it is being called from the postmaster, and which * database to use. -p marks the end of secure switches. */ #ifdef EXEC_BACKEND ! write_backend_variables(getpid(),port); ! ! /* pass data dir before end of secure switches (-p) */ ! av[ac++] = "-D"; ! av[ac++] = DataDir; ! av[ac++] = "-p"; ! av[ac++] = "FORK_EXEC"; /* FIXME: [fork/exec] This is totally bogus. We need to pass an arg to -p, but we'll get the db name out by ProcessStartupPacket in the exec'd process */ #else + av[ac++] = "-p"; av[ac++] = port->database_name; #endif *************** *** 2571,2576 **** --- 2595,2604 ---- * Pass the (insecure) option switches from the connection request. * (It's OK to mangle port->cmdline_options now.) */ + /* FIXME: [fork/exec] Hmmm.. we won't see these until after we BackendInit. + * Should we add code to BackendInit to add these (somehow!) into + * the PostgresMain argument list in the EXEC_BACKEND case? + */ if (port->cmdline_options) split_opts(av, &ac, port->cmdline_options); *************** *** 2594,2610 **** */ ereport(DEBUG3, (errmsg_internal("%s child[%d]: starting with (", ! progname, MyProcPid))); for (i = 0; i < ac; ++i) ereport(DEBUG3, (errmsg_internal("\t%s", av[i]))); ereport(DEBUG3, (errmsg_internal(")"))); ClientAuthInProgress = false; /* client_min_messages is active * now */ return (PostgresMain(ac, av, port->user_name)); } /* --- 2622,2642 ---- */ ereport(DEBUG3, (errmsg_internal("%s child[%d]: starting with (", ! progname, getpid()))); for (i = 0; i < ac; ++i) ereport(DEBUG3, (errmsg_internal("\t%s", av[i]))); ereport(DEBUG3, (errmsg_internal(")"))); + #ifdef EXEC_BACKEND + return execv(pg_pathname,av); + #else ClientAuthInProgress = false; /* client_min_messages is active * now */ return (PostgresMain(ac, av, port->user_name)); + #endif } /* *************** *** 3051,3053 **** --- 3083,3204 ---- va_end(ap); fprintf(stderr, "\n"); } + + + #if EXEC_BACKEND + + /* FIXME: [fork/exec] The following need to be available to the read/write_backend_variables functions... */ + typedef struct LWLock LWLock; + extern LWLock *LWLockArray; + extern slock_t *ProcStructLock; + extern XLogRecPtr RedoRecPtr; + extern XLogwrtResult LogwrtResult; + extern int pgStatSock; + + #define write_var(var,fp) fwrite((void*)&(var),sizeof(var),1,fp) + #define read_var(var,fp) fread((void*)&(var),sizeof(var),1,fp); + + /* FIXME: [fork/exec] Where's a good place for these functions? */ + /* FIXME: [fork/exec] Refactor these two functions into one? */ + + void + write_backend_variables(pid_t pid, Port *port) + { + char filename[MAXPGPATH]; + FILE *fp; + + Assert(DataDir); + sprintf(filename, "%s/backend_vars.%d", DataDir, pid); /* FIXME: [fork/exec] Is this really an appropriate directory? */ + + /* Open file */ + fp = AllocateFile(filename, "wb"); + if (!fp) + { + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not write to file \"%s\": %m", filename))); + return; + } + + /* Write vars */ + write_var(port->sock,fp); + write_var(port->proto,fp); + write_var(port->laddr,fp); + write_var(port->raddr,fp); + + write_var(PMcanAcceptConnections,fp); + + write_var(UsedShmemSegID,fp); + write_var(UsedShmemSegAddr,fp); + + write_var(ShmemSegHdr,fp); + write_var(ShmemLock,fp); + write_var(ShmemIndexLock,fp); + write_var(ShmemVariableCache,fp); + write_var(ShmemIndexAlloc,fp); + + write_var(LWLockArray,fp); + + write_var(ProcStructLock,fp); + + write_var(RedoRecPtr,fp); + write_var(LogwrtResult,fp); + + write_var(pgStatSock,fp); + + /* Release file */ + FreeFile(fp); + } + + void + read_backend_variables(pid_t pid, Port *port) + { + char filename[MAXPGPATH]; + FILE *fp; + + Assert(DataDir); + Assert(pid == getpid()); + sprintf(filename, "%s/backend_vars.%d", DataDir, pid); + + /* Open file */ + fp = AllocateFile(filename, "rb"); + if (!fp) + { + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not write to file \"%s\": %m", filename))); + return; + } + + /* Read vars */ + read_var(port->sock,fp); + read_var(port->proto,fp); + read_var(port->laddr,fp); + read_var(port->raddr,fp); + + read_var(PMcanAcceptConnections,fp); + + read_var(UsedShmemSegID,fp); + read_var(UsedShmemSegAddr,fp); + + read_var(ShmemSegHdr,fp); + read_var(ShmemLock,fp); + read_var(ShmemIndexLock,fp); + read_var(ShmemVariableCache,fp); + read_var(ShmemIndexAlloc,fp); + + read_var(LWLockArray,fp); + + read_var(ProcStructLock,fp); + + read_var(RedoRecPtr,fp); + read_var(LogwrtResult,fp); + + read_var(pgStatSock,fp); + + /* Release file */ + FreeFile(fp); + unlink(filename); + } + + #endif Index: src/backend/storage/buffer/buf_init.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/buffer/buf_init.c,v retrieving revision 1.58 diff -w -c -r1.58 buf_init.c *** src/backend/storage/buffer/buf_init.c 29 Nov 2003 19:51:56 -0000 1.58 --- src/backend/storage/buffer/buf_init.c 10 Dec 2003 09:30:51 -0000 *************** *** 136,142 **** * anyone else attached to the shmem at this point, we've got * problems. */ ! LWLockAcquire(BufMgrLock, LW_EXCLUSIVE); #ifdef BMTRACE CurTraceBuf = (long *) ShmemInitStruct("Buffer trace", --- 136,143 ---- * anyone else attached to the shmem at this point, we've got * problems. */ ! /* FIXME: [fork/exec] Can we remove this? */ ! /* LWLockAcquire(BufMgrLock, LW_EXCLUSIVE); */ #ifdef BMTRACE CurTraceBuf = (long *) ShmemInitStruct("Buffer trace", *************** *** 198,204 **** /* Init other shared buffer-management stuff */ StrategyInitialize(!foundDescs); ! LWLockRelease(BufMgrLock); } /* --- 199,206 ---- /* Init other shared buffer-management stuff */ StrategyInitialize(!foundDescs); ! /* FIXME: [fork/exec] Can we remove this? */ ! /* LWLockRelease(BufMgrLock); */ } /* Index: src/backend/storage/freespace/freespace.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/freespace/freespace.c,v retrieving revision 1.26 diff -w -c -r1.26 freespace.c *** src/backend/storage/freespace/freespace.c 29 Nov 2003 19:51:56 -0000 1.26 --- src/backend/storage/freespace/freespace.c 10 Dec 2003 09:30:52 -0000 *************** *** 180,186 **** /* Header for whole map */ struct FSMHeader { ! HTAB *relHash; /* hashtable of FSMRelation entries */ FSMRelation *usageList; /* FSMRelations in usage-recency order */ FSMRelation *usageListTail; /* tail of usage-recency list */ FSMRelation *firstRel; /* FSMRelations in arena storage order */ --- 180,187 ---- /* Header for whole map */ struct FSMHeader { ! /* FIXME: [fork/exec] Pointer to invalid memory location for backends in fork/exec case */ ! /* HTAB *relHash; */ /* hashtable of FSMRelation entries */ FSMRelation *usageList; /* FSMRelations in usage-recency order */ FSMRelation *usageListTail; /* tail of usage-recency list */ FSMRelation *firstRel; /* FSMRelations in arena storage order */ *************** *** 218,223 **** --- 219,225 ---- int MaxFSMPages; static FSMHeader *FreeSpaceMap; /* points to FSMHeader in shared memory */ + static HTAB *FreeSpaceMapRelHash; /* points to FSMHeader->relHash in shared memory */ static FSMRelation *lookup_fsm_rel(RelFileNode *rel); *************** *** 265,277 **** { HASHCTL info; int nchunks; /* Create table header */ ! FreeSpaceMap = (FSMHeader *) ShmemAlloc(sizeof(FSMHeader)); if (FreeSpaceMap == NULL) ereport(FATAL, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("insufficient shared memory for free space map"))); MemSet(FreeSpaceMap, 0, sizeof(FSMHeader)); /* Create hashtable for FSMRelations */ --- 267,281 ---- { HASHCTL info; int nchunks; + bool found; /* Create table header */ ! FreeSpaceMap = (FSMHeader *) ShmemInitStruct("Free Space Map Header",sizeof(FSMHeader),&found); if (FreeSpaceMap == NULL) ereport(FATAL, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("insufficient shared memory for free space map"))); + if (!found) MemSet(FreeSpaceMap, 0, sizeof(FSMHeader)); /* Create hashtable for FSMRelations */ *************** *** 279,295 **** info.entrysize = sizeof(FSMRelation); info.hash = tag_hash; ! FreeSpaceMap->relHash = ShmemInitHash("Free Space Map Hash", MaxFSMRelations / 10, MaxFSMRelations, &info, (HASH_ELEM | HASH_FUNCTION)); ! if (!FreeSpaceMap->relHash) ereport(FATAL, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("insufficient shared memory for free space map"))); /* Allocate page-storage arena */ nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1; /* This check ensures spareChunks will be greater than zero */ --- 283,303 ---- info.entrysize = sizeof(FSMRelation); info.hash = tag_hash; ! FreeSpaceMapRelHash = ShmemInitHash("Free Space Map Hash", MaxFSMRelations / 10, MaxFSMRelations, &info, (HASH_ELEM | HASH_FUNCTION)); ! if (!FreeSpaceMapRelHash) ereport(FATAL, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("insufficient shared memory for free space map"))); + if (found) + return; + + /* Allocate page-storage arena */ nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1; /* This check ensures spareChunks will be greater than zero */ *************** *** 974,980 **** { FSMRelation *fsmrel; ! fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash, (void *) rel, HASH_FIND, NULL); --- 982,988 ---- { FSMRelation *fsmrel; ! fsmrel = (FSMRelation *) hash_search(FreeSpaceMapRelHash, (void *) rel, HASH_FIND, NULL); *************** *** 995,1001 **** FSMRelation *fsmrel; bool found; ! fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash, (void *) rel, HASH_ENTER, &found); --- 1003,1009 ---- FSMRelation *fsmrel; bool found; ! fsmrel = (FSMRelation *) hash_search(FreeSpaceMapRelHash, (void *) rel, HASH_ENTER, &found); *************** *** 1050,1056 **** unlink_fsm_rel_usage(fsmrel); unlink_fsm_rel_storage(fsmrel); FreeSpaceMap->numRels--; ! result = (FSMRelation *) hash_search(FreeSpaceMap->relHash, (void *) &(fsmrel->key), HASH_REMOVE, NULL); --- 1058,1064 ---- unlink_fsm_rel_usage(fsmrel); unlink_fsm_rel_storage(fsmrel); FreeSpaceMap->numRels--; ! result = (FSMRelation *) hash_search(FreeSpaceMapRelHash, (void *) &(fsmrel->key), HASH_REMOVE, NULL); Index: src/backend/storage/ipc/ipci.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/ipci.c,v retrieving revision 1.59 diff -w -c -r1.59 ipci.c *** src/backend/storage/ipc/ipci.c 1 Dec 2003 21:59:25 -0000 1.59 --- src/backend/storage/ipc/ipci.c 10 Dec 2003 09:30:52 -0000 *************** *** 87,93 **** /* * Set up shared memory allocation mechanism */ ! InitShmemAllocation(seghdr); /* * Now initialize LWLocks, which do shared memory allocation and are --- 87,93 ---- /* * Set up shared memory allocation mechanism */ ! InitShmemAllocation(seghdr, true); /* * Now initialize LWLocks, which do shared memory allocation and are *************** *** 139,146 **** --- 139,167 ---- * AttachSharedMemoryAndSemaphores * Attaches to the existing shared resources. */ + + /* FIXME: [fork/exec] This function is starting to look pretty much like + CreateSharedMemoryAndSemaphores... refactor? */ void AttachSharedMemoryAndSemaphores(void) { + InitShmemAllocation(ShmemSegHdr, false); + + + InitShmemIndex(); + + XLOGShmemInit(); CLOGShmemInit(); + InitBufferPool(); + + InitLocks(); + InitLockTable(MaxBackends); + + InitProcGlobal(MaxBackends); + + CreateSharedInvalidationState(MaxBackends); + + InitFreeSpaceMap(); + + PMSignalInit(); } Index: src/backend/storage/ipc/pmsignal.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/pmsignal.c,v retrieving revision 1.6 diff -w -c -r1.6 pmsignal.c *** src/backend/storage/ipc/pmsignal.c 29 Nov 2003 19:51:56 -0000 1.6 --- src/backend/storage/ipc/pmsignal.c 10 Dec 2003 09:30:52 -0000 *************** *** 44,51 **** void PMSignalInit(void) { PMSignalFlags = (sig_atomic_t *) ! ShmemAlloc(NUM_PMSIGNALS * sizeof(sig_atomic_t)); MemSet(PMSignalFlags, 0, NUM_PMSIGNALS * sizeof(sig_atomic_t)); } --- 44,55 ---- void PMSignalInit(void) { + bool found; PMSignalFlags = (sig_atomic_t *) ! ShmemInitStruct("PMSignalFlags",NUM_PMSIGNALS * sizeof(sig_atomic_t),&found); ! ! if (found) ! return; MemSet(PMSignalFlags, 0, NUM_PMSIGNALS * sizeof(sig_atomic_t)); } Index: src/backend/storage/ipc/shmem.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/shmem.c,v retrieving revision 1.74 diff -w -c -r1.74 shmem.c *** src/backend/storage/ipc/shmem.c 29 Nov 2003 19:51:56 -0000 1.74 --- src/backend/storage/ipc/shmem.c 10 Dec 2003 09:30:53 -0000 *************** *** 68,80 **** /* shared memory global variables */ ! static PGShmemHeader *ShmemSegHdr; /* shared mem segment header */ SHMEM_OFFSET ShmemBase; /* start address of shared memory */ static SHMEM_OFFSET ShmemEnd; /* end+1 address of shared memory */ ! static slock_t *ShmemLock; /* spinlock for shared memory allocation */ static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */ --- 68,84 ---- /* shared memory global variables */ ! PGShmemHeader *ShmemSegHdr; /* shared mem segment header */ SHMEM_OFFSET ShmemBase; /* start address of shared memory */ static SHMEM_OFFSET ShmemEnd; /* end+1 address of shared memory */ ! slock_t *ShmemLock; /* spinlock for shared memory allocation */ ! ! slock_t *ShmemIndexLock; /* spinlock for ShmemIndex */ ! ! void *ShmemIndexAlloc = NULL; /* Memory actually allocated for ShmemIndex */ static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */ *************** *** 88,94 **** * but we use void to avoid having to include ipc.h in shmem.h. */ void ! InitShmemAllocation(void *seghdr) { PGShmemHeader *shmhdr = (PGShmemHeader *) seghdr; --- 92,98 ---- * but we use void to avoid having to include ipc.h in shmem.h. */ void ! InitShmemAllocation(void *seghdr, bool init) { PGShmemHeader *shmhdr = (PGShmemHeader *) seghdr; *************** *** 97,116 **** ShmemBase = (SHMEM_OFFSET) shmhdr; ShmemEnd = ShmemBase + shmhdr->totalsize; /* ! * Initialize the spinlock used by ShmemAlloc. We have to do the ! * space allocation the hard way, since ShmemAlloc can't be called ! * yet. */ ShmemLock = (slock_t *) (((char *) shmhdr) + shmhdr->freeoffset); shmhdr->freeoffset += MAXALIGN(sizeof(slock_t)); Assert(shmhdr->freeoffset <= shmhdr->totalsize); SpinLockInit(ShmemLock); /* ShmemIndex can't be set up yet (need LWLocks first) */ ShmemIndex = (HTAB *) NULL; /* * Initialize ShmemVariableCache for transaction manager. */ --- 101,129 ---- ShmemBase = (SHMEM_OFFSET) shmhdr; ShmemEnd = ShmemBase + shmhdr->totalsize; + if (init) + { /* ! * Initialize the spinlocks used by ShmemAlloc/ShmemInitStruct. We ! * have to do the space allocation the hard way, since ShmemAlloc ! * can't be called yet. */ ShmemLock = (slock_t *) (((char *) shmhdr) + shmhdr->freeoffset); shmhdr->freeoffset += MAXALIGN(sizeof(slock_t)); Assert(shmhdr->freeoffset <= shmhdr->totalsize); + ShmemIndexLock = (slock_t *) (((char *) shmhdr) + shmhdr->freeoffset); + shmhdr->freeoffset += MAXALIGN(sizeof(slock_t)); + Assert(shmhdr->freeoffset <= shmhdr->totalsize); + SpinLockInit(ShmemLock); + SpinLockInit(ShmemIndexLock); + /* ShmemIndex can't be set up yet (need LWLocks first) */ ShmemIndex = (HTAB *) NULL; + /* * Initialize ShmemVariableCache for transaction manager. */ *************** *** 118,123 **** --- 131,137 ---- ShmemAlloc(sizeof(*ShmemVariableCache)); memset(ShmemVariableCache, 0, sizeof(*ShmemVariableCache)); } + } /* * ShmemAlloc -- allocate max-aligned chunk from shared memory *************** *** 218,223 **** --- 232,239 ---- /* * Now, create an entry in the hashtable for the index itself. */ + if (!IsUnderPostmaster) + { MemSet(item.key, 0, SHMEM_INDEX_KEYSIZE); strncpy(item.key, "ShmemIndex", SHMEM_INDEX_KEYSIZE); *************** *** 234,242 **** result->size = SHMEM_INDEX_SIZE; ShmemBootstrap = false; /* now release the lock acquired in ShmemInitStruct */ ! LWLockRelease(ShmemIndexLock); } /* --- 250,259 ---- result->size = SHMEM_INDEX_SIZE; ShmemBootstrap = false; + } /* now release the lock acquired in ShmemInitStruct */ ! SpinLockRelease(ShmemIndexLock); } /* *************** *** 320,340 **** strncpy(item.key, name, SHMEM_INDEX_KEYSIZE); item.location = BAD_LOCATION; ! LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE); if (!ShmemIndex) { /* * If the shmem index doesn't exist, we are bootstrapping: we must * be trying to init the shmem index itself. * ! * Notice that the ShmemIndexLock is held until the shmem index has * been completely initialized. */ Assert(strcmp(name, "ShmemIndex") == 0); Assert(ShmemBootstrap); *foundPtr = FALSE; ! return ShmemAlloc(size); } /* look it up in the shmem index */ --- 337,369 ---- strncpy(item.key, name, SHMEM_INDEX_KEYSIZE); item.location = BAD_LOCATION; ! SpinLockAcquire(ShmemIndexLock); if (!ShmemIndex) { + if (IsUnderPostmaster) + { + /* Must be initializing a (non-standalone) backend */ + Assert(strcmp(name, "ShmemIndex") == 0); + Assert(ShmemBootstrap); + Assert(ShmemIndexAlloc); + *foundPtr = TRUE; + } + else + { /* * If the shmem index doesn't exist, we are bootstrapping: we must * be trying to init the shmem index itself. * ! * Notice that the ShmemLock is held until the shmem index has * been completely initialized. */ Assert(strcmp(name, "ShmemIndex") == 0); Assert(ShmemBootstrap); *foundPtr = FALSE; ! ShmemIndexAlloc = ShmemAlloc(size); ! } ! return ShmemIndexAlloc; } /* look it up in the shmem index */ *************** *** 343,349 **** if (!result) { ! LWLockRelease(ShmemIndexLock); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of shared memory"))); --- 372,378 ---- if (!result) { ! SpinLockRelease(ShmemIndexLock); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of shared memory"))); *************** *** 359,365 **** */ if (result->size != size) { ! LWLockRelease(ShmemIndexLock); elog(WARNING, "ShmemIndex entry size is wrong"); /* let caller print its message too */ --- 388,394 ---- */ if (result->size != size) { ! SpinLockRelease(ShmemIndexLock); elog(WARNING, "ShmemIndex entry size is wrong"); /* let caller print its message too */ *************** *** 376,382 **** /* out of memory */ Assert(ShmemIndex); hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL); ! LWLockRelease(ShmemIndexLock); ereport(WARNING, (errcode(ERRCODE_OUT_OF_MEMORY), --- 405,411 ---- /* out of memory */ Assert(ShmemIndex); hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL); ! SpinLockRelease(ShmemIndexLock); ereport(WARNING, (errcode(ERRCODE_OUT_OF_MEMORY), *************** *** 389,394 **** } Assert(ShmemIsValid((unsigned long) structPtr)); ! LWLockRelease(ShmemIndexLock); return structPtr; } --- 418,423 ---- } Assert(ShmemIsValid((unsigned long) structPtr)); ! SpinLockRelease(ShmemIndexLock); return structPtr; } Index: src/backend/storage/ipc/sinvaladt.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/sinvaladt.c,v retrieving revision 1.53 diff -w -c -r1.53 sinvaladt.c *** src/backend/storage/ipc/sinvaladt.c 29 Nov 2003 19:51:56 -0000 1.53 --- src/backend/storage/ipc/sinvaladt.c 10 Dec 2003 09:30:53 -0000 *************** *** 50,59 **** int segSize; SISeg *segP; int i; /* Allocate space in shared memory */ segSize = SInvalShmemSize(maxBackends); ! shmInvalBuffer = segP = (SISeg *) ShmemAlloc(segSize); /* Clear message counters, save size of procState array */ segP->minMsgNum = 0; --- 50,62 ---- int segSize; SISeg *segP; int i; + bool found; /* Allocate space in shared memory */ segSize = SInvalShmemSize(maxBackends); ! shmInvalBuffer = segP = (SISeg *) ShmemInitStruct("shmInvalBuffer",segSize,&found); ! if (found) ! return; /* Clear message counters, save size of procState array */ segP->minMsgNum = 0; Index: src/backend/storage/lmgr/lock.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/lmgr/lock.c,v retrieving revision 1.130 diff -w -c -r1.130 lock.c *** src/backend/storage/lmgr/lock.c 1 Dec 2003 21:59:25 -0000 1.130 --- src/backend/storage/lmgr/lock.c 10 Dec 2003 09:30:54 -0000 *************** *** 153,161 **** * map from lock method id to the lock table structure */ static LockMethod LockMethods[MAX_LOCK_METHODS]; - static int NumLockMethods; /* * InitLocks -- Init the lock module. Create a private data * structure for constructing conflict masks. --- 153,166 ---- * map from lock method id to the lock table structure */ static LockMethod LockMethods[MAX_LOCK_METHODS]; static int NumLockMethods; + /* FIXME: [fork/exec] Create a struct with two HTAB pointers? Other ideas? */ + static HTAB* LockMethodLockHash[MAX_LOCK_METHODS]; + static HTAB* LockMethodProcLockHash[MAX_LOCK_METHODS]; + + + /* * InitLocks -- Init the lock module. Create a private data * structure for constructing conflict masks. *************** *** 245,251 **** /* * Lock the LWLock for the table (probably not necessary here) */ ! LWLockAcquire(LockMgrLock, LW_EXCLUSIVE); /* * no zero-th table --- 250,257 ---- /* * Lock the LWLock for the table (probably not necessary here) */ ! /* FIXME: [fork/exec] Can we remove this? */ ! /* LWLockAcquire(LockMgrLock, LW_EXCLUSIVE); */ /* * no zero-th table *************** *** 279,293 **** hash_flags = (HASH_ELEM | HASH_FUNCTION); sprintf(shmemName, "%s (lock hash)", tabName); ! newLockMethod->lockHash = ShmemInitHash(shmemName, init_table_size, max_table_size, &info, hash_flags); ! if (!newLockMethod->lockHash) elog(FATAL, "could not initialize lock table \"%s\"", tabName); ! Assert(newLockMethod->lockHash->hash == tag_hash); /* * allocate a hash table for PROCLOCK structs. This is used to store --- 285,299 ---- hash_flags = (HASH_ELEM | HASH_FUNCTION); sprintf(shmemName, "%s (lock hash)", tabName); ! LockMethodLockHash[NumLockMethods-1] = ShmemInitHash(shmemName, init_table_size, max_table_size, &info, hash_flags); ! if (!LockMethodLockHash[NumLockMethods-1]) elog(FATAL, "could not initialize lock table \"%s\"", tabName); ! Assert(LockMethodLockHash[NumLockMethods-1]->hash == tag_hash); /* * allocate a hash table for PROCLOCK structs. This is used to store *************** *** 299,317 **** hash_flags = (HASH_ELEM | HASH_FUNCTION); sprintf(shmemName, "%s (proclock hash)", tabName); ! newLockMethod->proclockHash = ShmemInitHash(shmemName, init_table_size, max_table_size, &info, hash_flags); ! if (!newLockMethod->proclockHash) elog(FATAL, "could not initialize lock table \"%s\"", tabName); /* init data structures */ LockMethodInit(newLockMethod, conflictsP, numModes); ! LWLockRelease(LockMgrLock); pfree(shmemName); --- 305,324 ---- hash_flags = (HASH_ELEM | HASH_FUNCTION); sprintf(shmemName, "%s (proclock hash)", tabName); ! LockMethodProcLockHash[NumLockMethods-1] = ShmemInitHash(shmemName, init_table_size, max_table_size, &info, hash_flags); ! if (!LockMethodProcLockHash[NumLockMethods-1]) elog(FATAL, "could not initialize lock table \"%s\"", tabName); /* init data structures */ LockMethodInit(newLockMethod, conflictsP, numModes); ! /* FIXME: [fork/exec] Can we remove this? */ ! /* LWLockRelease(LockMgrLock); */ pfree(shmemName); *************** *** 449,456 **** /* * Find or create a lock with this tag */ ! Assert(lockMethodTable->lockHash->hash == tag_hash); ! lock = (LOCK *) hash_search(lockMethodTable->lockHash, (void *) locktag, HASH_ENTER, &found); if (!lock) --- 456,463 ---- /* * Find or create a lock with this tag */ ! Assert(LockMethodLockHash[lockmethodid]->hash == tag_hash); ! lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid], (void *) locktag, HASH_ENTER, &found); if (!lock) *************** *** 497,503 **** /* * Find or create a proclock entry with this tag */ ! proclockTable = lockMethodTable->proclockHash; proclock = (PROCLOCK *) hash_search(proclockTable, (void *) &proclocktag, HASH_ENTER, &found); --- 504,510 ---- /* * Find or create a proclock entry with this tag */ ! proclockTable = LockMethodProcLockHash[lockmethodid]; proclock = (PROCLOCK *) hash_search(proclockTable, (void *) &proclocktag, HASH_ENTER, &found); *************** *** 988,995 **** /* * Find a lock with this tag */ ! Assert(lockMethodTable->lockHash->hash == tag_hash); ! lock = (LOCK *) hash_search(lockMethodTable->lockHash, (void *) locktag, HASH_FIND, NULL); --- 995,1002 ---- /* * Find a lock with this tag */ ! Assert(LockMethodLockHash[lockmethodid]->hash == tag_hash); ! lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid], (void *) locktag, HASH_FIND, NULL); *************** *** 1014,1020 **** proclocktag.proc = MAKE_OFFSET(MyProc); TransactionIdStore(xid, &proclocktag.xid); ! proclockTable = lockMethodTable->proclockHash; proclock = (PROCLOCK *) hash_search(proclockTable, (void *) &proclocktag, HASH_FIND_SAVE, NULL); --- 1021,1027 ---- proclocktag.proc = MAKE_OFFSET(MyProc); TransactionIdStore(xid, &proclocktag.xid); ! proclockTable = LockMethodProcLockHash[lockmethodid]; proclock = (PROCLOCK *) hash_search(proclockTable, (void *) &proclocktag, HASH_FIND_SAVE, NULL); *************** *** 1086,1093 **** * if there's no one waiting in the queue, we just released the * last lock on this object. Delete it from the lock table. */ ! Assert(lockMethodTable->lockHash->hash == tag_hash); ! lock = (LOCK *) hash_search(lockMethodTable->lockHash, (void *) &(lock->tag), HASH_REMOVE, NULL); --- 1093,1100 ---- * if there's no one waiting in the queue, we just released the * last lock on this object. Delete it from the lock table. */ ! Assert(LockMethodLockHash[lockmethodid]->hash == tag_hash); ! lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid], (void *) &(lock->tag), HASH_REMOVE, NULL); *************** *** 1269,1275 **** /* * remove the proclock entry from the hashtable */ ! proclock = (PROCLOCK *) hash_search(lockMethodTable->proclockHash, (void *) proclock, HASH_REMOVE, NULL); --- 1276,1282 ---- /* * remove the proclock entry from the hashtable */ ! proclock = (PROCLOCK *) hash_search(LockMethodProcLockHash[lockmethodid], (void *) proclock, HASH_REMOVE, NULL); *************** *** 1287,1294 **** * lock object. */ LOCK_PRINT("LockReleaseAll: deleting", lock, 0); ! Assert(lockMethodTable->lockHash->hash == tag_hash); ! lock = (LOCK *) hash_search(lockMethodTable->lockHash, (void *) &(lock->tag), HASH_REMOVE, NULL); if (!lock) --- 1294,1301 ---- * lock object. */ LOCK_PRINT("LockReleaseAll: deleting", lock, 0); ! Assert(LockMethodLockHash[lockmethodid]->hash == tag_hash); ! lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid], (void *) &(lock->tag), HASH_REMOVE, NULL); if (!lock) *************** *** 1367,1373 **** LWLockAcquire(LockMgrLock, LW_EXCLUSIVE); ! proclockTable = LockMethods[DEFAULT_LOCKMETHOD]->proclockHash; data->nelements = i = proclockTable->hctl->nentries; --- 1374,1380 ---- LWLockAcquire(LockMgrLock, LW_EXCLUSIVE); ! proclockTable = LockMethodProcLockHash[DEFAULT_LOCKMETHOD]; data->nelements = i = proclockTable->hctl->nentries; *************** *** 1480,1486 **** if (!lockMethodTable) return; ! proclockTable = lockMethodTable->proclockHash; if (proc->waitLock) LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0); --- 1487,1493 ---- if (!lockMethodTable) return; ! proclockTable = LockMethodProcLockHash[lockmethodid]; if (proc->waitLock) LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0); Index: src/backend/storage/lmgr/lwlock.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/lmgr/lwlock.c,v retrieving revision 1.18 diff -w -c -r1.18 lwlock.c *** src/backend/storage/lmgr/lwlock.c 29 Nov 2003 19:51:57 -0000 1.18 --- src/backend/storage/lmgr/lwlock.c 10 Dec 2003 09:30:54 -0000 *************** *** 43,49 **** * the pointer by fork from the postmaster. LWLockIds are indexes into * the array. */ ! static LWLock *LWLockArray = NULL; /* shared counter for dynamic allocation of LWLockIds */ static int *LWLockCounter; --- 43,49 ---- * the pointer by fork from the postmaster. LWLockIds are indexes into * the array. */ ! LWLock *LWLockArray = NULL; /* shared counter for dynamic allocation of LWLockIds */ static int *LWLockCounter; Index: src/backend/storage/lmgr/proc.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/lmgr/proc.c,v retrieving revision 1.139 diff -w -c -r1.139 proc.c *** src/backend/storage/lmgr/proc.c 1 Dec 2003 21:59:25 -0000 1.139 --- src/backend/storage/lmgr/proc.c 10 Dec 2003 09:30:55 -0000 *************** *** 66,72 **** * relatively infrequently (only at backend startup or shutdown) and not for * very long, so a spinlock is okay. */ ! static slock_t *ProcStructLock = NULL; static PROC_HDR *ProcGlobal = NULL; --- 66,72 ---- * relatively infrequently (only at backend startup or shutdown) and not for * very long, so a spinlock is okay. */ ! slock_t *ProcStructLock = NULL; static PROC_HDR *ProcGlobal = NULL; *************** *** 247,252 **** --- 247,253 ---- MyProc->waitLock = NULL; MyProc->waitHolder = NULL; SHMQueueInit(&(MyProc->procHolders)); + /* * Arrange to clean up at backend exit. Index: src/backend/tcop/postgres.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/tcop/postgres.c,v retrieving revision 1.379 diff -w -c -r1.379 postgres.c *** src/backend/tcop/postgres.c 1 Dec 2003 22:15:37 -0000 1.379 --- src/backend/tcop/postgres.c 10 Dec 2003 09:30:58 -0000 *************** *** 68,73 **** --- 68,77 ---- extern int optind; extern char *optarg; + /* FIXME: [fork/exec] */ + extern int BackendInit(Port*); + extern void read_backend_variables(pid_t pid, Port *port); + /* ---------------- * global variables *************** *** 2052,2058 **** * initialize globals (already done if under postmaster, but not if * standalone; cheap enough to do over) */ - MyProcPid = getpid(); /* --- 2056,2061 ---- *************** *** 2060,2066 **** * * If we are running under the postmaster, this is done already. */ ! if (!IsUnderPostmaster) MemoryContextInit(); set_ps_display("startup"); --- 2063,2069 ---- * * If we are running under the postmaster, this is done already. */ ! if (!IsUnderPostmaster /* when exec || ExecBackend */) MemoryContextInit(); set_ps_display("startup"); *************** *** 2133,2139 **** case 'D': /* PGDATA directory */ if (secure) potential_DataDir = optarg; - break; case 'd': /* debug level */ { --- 2136,2141 ---- *************** *** 2268,2298 **** break; case 'p': - /* * p - special flag passed if backend was forked by a * postmaster. */ if (secure) { ! #ifdef EXEC_BACKEND ! char *p; ! int i; ! int PMcanAcceptConnections; /* will eventually be ! * global or static, ! * when fork */ ! ! sscanf(optarg, "%d,%d,%lu,%p,", ! &MyProcPort->sock, &PMcanAcceptConnections, ! &UsedShmemSegID, &UsedShmemSegAddr); ! /* Grab dbname as last param */ ! for (i = 0, p = optarg - 1; i < 4 && p; i++) ! p = strchr(p + 1, ','); ! if (i == 4 && p) ! dbname = strdup(p + 1); ! #else dbname = strdup(optarg); #endif secure = false; /* subsequent switches are NOT * secure */ ctx = PGC_BACKEND; --- 2270,2310 ---- break; case 'p': /* * p - special flag passed if backend was forked by a * postmaster. */ if (secure) { ! #ifndef EXEC_BACKEND dbname = strdup(optarg); + + #else + Port *port =(Port*)malloc(sizeof(Port)); + if (port == NULL) + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("insufficient memory to allocate port"))); + + Assert(potential_DataDir); + SetDataDir(potential_DataDir); + + read_nondefault_variables(); + read_backend_variables(getpid(),port); + + /* FIXME: [fork/exec] Ugh */ + load_hba(); + load_ident(); + load_user(); + load_group(); + + + if (!BackendInit(port)) + return -1; + + dbname = port->database_name; #endif + secure = false; /* subsequent switches are NOT * secure */ ctx = PGC_BACKEND; *************** *** 2494,2506 **** Assert(DataDir); /* Acquire configuration parameters */ ! if (IsUnderPostmaster) ! { ! #ifdef EXEC_BACKEND ! read_nondefault_variables(); ! #endif ! } ! else ProcessConfigFile(PGC_POSTMASTER); /* --- 2506,2512 ---- Assert(DataDir); /* Acquire configuration parameters */ ! if (!IsUnderPostmaster) ProcessConfigFile(PGC_POSTMASTER); /* *************** *** 2517,2523 **** * course, this isn't an issue for signals that are locally generated, * such as SIGALRM and SIGPIPE.) */ - pqsignal(SIGHUP, SigHupHandler); /* set flag to read config file */ pqsignal(SIGINT, StatementCancelHandler); /* cancel current query */ pqsignal(SIGTERM, die); /* cancel current query and exit */ --- 2523,2528 ---- *************** *** 2565,2574 **** errmsg("invalid command-line arguments for server process"), errhint("Try \"%s --help\" for more information.", argv[0]))); } ! BaseInit(); ! #ifdef EXECBACKEND AttachSharedMemoryAndSemaphores(); #endif } else { --- 2570,2583 ---- errmsg("invalid command-line arguments for server process"), errhint("Try \"%s --help\" for more information.", argv[0]))); } ! #ifdef EXEC_BACKEND ! PGSharedMemoryCreate(-1,false,-1); ! AttachSharedMemoryAndSemaphores(); #endif + XLOGPathInit(); + + BaseInit(); } else { *************** *** 2686,2692 **** * If an exception is encountered, processing resumes here so we abort * the current transaction and start a new one. */ - if (sigsetjmp(Warn_restart, 1) != 0) { /* --- 2695,2700 ---- *************** *** 2761,2767 **** /* * Non-error queries loop here. */ - for (;;) { /* --- 2769,2774 ---- *************** *** 2845,2851 **** --- 2852,2862 ---- if (got_SIGHUP) { got_SIGHUP = false; + #ifdef EXEC_BACKEND + read_nondefault_variables(); + #else ProcessConfigFile(PGC_SIGHUP); + #endif } /* *************** *** 2860,2871 **** case 'Q': /* simple query */ { const char *query_string; - query_string = pq_getmsgstring(&input_message); pq_getmsgend(&input_message); - exec_simple_query(query_string); - send_rfq = true; } break; --- 2871,2879 ---- *************** *** 3199,3202 **** pfree(str.data); } - --- 3207,3209 ---- Index: src/include/access/xlogdefs.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/access/xlogdefs.h,v retrieving revision 1.10 diff -w -c -r1.10 xlogdefs.h *** src/include/access/xlogdefs.h 29 Nov 2003 22:40:55 -0000 1.10 --- src/include/access/xlogdefs.h 10 Dec 2003 09:31:00 -0000 *************** *** 33,38 **** --- 33,47 ---- uint32 xrecoff; /* byte offset of location in log file */ } XLogRecPtr; + + /* FIXME: [fork/exec] following struct moved from xlog.c */ + typedef struct XLogwrtResult + { + XLogRecPtr Write; /* last byte + 1 written out */ + XLogRecPtr Flush; /* last byte + 1 flushed */ + } XLogwrtResult; + + /* * Macros for comparing XLogRecPtrs * Index: src/include/storage/lock.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/storage/lock.h,v retrieving revision 1.75 diff -w -c -r1.75 lock.h *** src/include/storage/lock.h 1 Dec 2003 21:59:25 -0000 1.75 --- src/include/storage/lock.h 10 Dec 2003 09:31:01 -0000 *************** *** 86,93 **** */ typedef struct LockMethodData { ! HTAB *lockHash; ! HTAB *proclockHash; LOCKMETHODID lockmethodid; int numLockModes; LOCKMASK conflictTab[MAX_LOCKMODES]; --- 86,94 ---- */ typedef struct LockMethodData { ! /* FIXME: [fork/exec] Pointers to invalid memory location for backends in fork/exec case */ ! /* HTAB *lockHash; */ ! /*HTAB *proclockHash; */ LOCKMETHODID lockmethodid; int numLockModes; LOCKMASK conflictTab[MAX_LOCKMODES]; Index: src/include/storage/lwlock.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/storage/lwlock.h,v retrieving revision 1.9 diff -w -c -r1.9 lwlock.h *** src/include/storage/lwlock.h 29 Nov 2003 22:41:13 -0000 1.9 --- src/include/storage/lwlock.h 10 Dec 2003 09:31:01 -0000 *************** *** 29,35 **** LockMgrLock, OidGenLock, XidGenLock, - ShmemIndexLock, SInvalLock, FreeSpaceLock, MMCacheLock, --- 29,34 ---- Index: src/include/storage/shmem.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/storage/shmem.h,v retrieving revision 1.40 diff -w -c -r1.40 shmem.h *** src/include/storage/shmem.h 29 Nov 2003 22:41:13 -0000 1.40 --- src/include/storage/shmem.h 10 Dec 2003 09:31:01 -0000 *************** *** 30,35 **** --- 30,43 ---- #define INVALID_OFFSET (-1) #define BAD_LOCATION (-1) + /* FIXME: [fork/exec] Other externs */ + #include "pg_shmem.h" + extern PGShmemHeader *ShmemSegHdr; + extern slock_t *ShmemLock; + extern slock_t *ShmemIndexLock; + extern HTAB *ShmemIndex; + extern void *ShmemIndexAlloc; + /* * Start of the primary shared memory region, in this process' address space. * The macros in this header file can only cope with offsets into this *************** *** 37,43 **** */ extern DLLIMPORT SHMEM_OFFSET ShmemBase; - /* coerce an offset into a pointer in this process's address space */ #define MAKE_PTR(xx_offs)\ (ShmemBase+((unsigned long)(xx_offs))) --- 45,50 ---- *************** *** 61,67 **** } SHM_QUEUE; /* shmem.c */ ! extern void InitShmemAllocation(void *seghdr); extern void *ShmemAlloc(Size size); extern bool ShmemIsValid(unsigned long addr); extern void InitShmemIndex(void); --- 68,74 ---- } SHM_QUEUE; /* shmem.c */ ! extern void InitShmemAllocation(void *seghdr, bool init); extern void *ShmemAlloc(Size size); extern bool ShmemIsValid(unsigned long addr); extern void InitShmemIndex(void);