对PostgreSQL中后台进程的内存结构建立的初步跟踪

开始

基本上:

AuxiliaryProcessMain  --> BaseInit --> InitCommunication --> CreateSharedMemoryAndSemaphores

AuxiliaryProcessMain 是各个后台进程(bgwriter等)的调用起始点

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

对PostgreSQL中后台进程的内存结构建立的初步跟踪
/*                    
 *     AuxiliaryProcessMain                
 *                    
 *     The main entry point for auxiliary processes, such as the bgwriter,                
 *     walwriter, walreceiver, bootstrapper and the shared memory checker code.                
 *                    
 *     This code is here just because of historical reasons.                
 */                    
void                    
AuxiliaryProcessMain(int argc, char *argv[])                    
{                    
    ……                
    /*                
     * Fire up essential subsystems: error and memory management                
     *                
     * If we are running under the postmaster, this is done already.                
     */                
    if (!IsUnderPostmaster)                
        MemoryContextInit();            
                    
    ……                
    /*                
     * Identify myself via ps                
     */                
    if (IsUnderPostmaster)                
    {                
        const char *statmsg;            
                    
        switch (MyAuxProcType)            
        {            
            case StartupProcess:        
                statmsg = "startup process";    
                break;    
            case BgWriterProcess:        
                statmsg = "writer process";    
                break;    
            case CheckpointerProcess:        
                statmsg = "checkpointer process";    
                break;    
            case WalWriterProcess:        
                statmsg = "wal writer process";    
                break;    
            case WalReceiverProcess:        
                statmsg = "wal receiver process";    
                break;    
            default:        
                statmsg = "??? process";    
                break;    
        }            
        init_ps_display(statmsg, "", "", "");            
    }                
                    
    ……                
    BaseInit();                
                    
    /*                
     * When we are an auxiliary process, we aren't going to do the full                
     * InitPostgres pushups, but there are a couple of things that need to get                
     * lit up even in an auxiliary process.                
     */                
    if (IsUnderPostmaster)                
    {                
        /*            
         * Create a PGPROC so we can use LWLocks.  In the EXEC_BACKEND case,            
         * this was already done by SubPostmasterMain().            
         */            
        #ifndef EXEC_BACKEND            
            InitAuxiliaryProcess();        
        #endif            
        ……            
    }                
                    
    /*                
     * XLOG operations                
     */                
    SetProcessingMode(NormalProcessing);                
                    
    switch (MyAuxProcType)                
    {                
        ……            
        case BgWriterProcess:            
            /* don't set signals, bgwriter has its own agenda */        
            BackgroundWriterMain();        
            proc_exit(1);        /* should never return */
        ……            
    }                
}                    
对PostgreSQL中后台进程的内存结构建立的初步跟踪

而其中的 BaseInit 要完成如下几件事:

对PostgreSQL中后台进程的内存结构建立的初步跟踪
 * Early initialization of a backend (either standalone or under postmaster).    
 * This happens even before InitPostgres.    
 *    
 * This is separate from InitPostgres because it is also called by auxiliary    
 * processes, such as the background writer process, which may not call    
 * InitPostgres at all.    
 */    
void    
BaseInit(void)    
{    
    /*
     * Attach to shared memory and semaphores, and initialize our
     * input/output/debugging file descriptors.
     */
    InitCommunication();
    DebugFileOpen();
    
    /* Do local initialization of file, storage and buffer managers */
    InitFileAccess();
    smgrinit();
    InitBufferPoolAccess();
}    
对PostgreSQL中后台进程的内存结构建立的初步跟踪

对于  InitCommunication ,是这样的:

对PostgreSQL中后台进程的内存结构建立的初步跟踪
/* --------------------------------            
 *        InitCommunication    
 *            
 *        This routine initializes stuff needed for ipc, locking, etc.    
 *        it should be called something more informative.    
 * --------------------------------            
 */            
static void            
InitCommunication(void)            
{            
    /*        
     * initialize shared memory and semaphores appropriately.        
     */        
    if (!IsUnderPostmaster)        /* postmaster already did this */
    {        
        /*    
         * We're running a postgres bootstrap process or a standalone backend.    
         * Create private "shmem" and semaphores.    
         */    
        CreateSharedMemoryAndSemaphores(true, 0);    
    }        
}            
对PostgreSQL中后台进程的内存结构建立的初步跟踪

真正建立为每个后台进程建立内存结构的,就是这个  CreateSharedMemoryAndSemaphores






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/11/06/2756956.html,如需转载请自行联系原作者

上一篇:[cb]Unity 关卡编辑器 开发


下一篇:SpringMVC中原生态的Servlet属性