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

开始

基本上:

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

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

[作者:技术者高健@博客园  mail: [email protected] ]

/*                    

 *     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 */

        ……            

    }                

}                    

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

 * 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();

}    

对于  InitCommunication ,是这样的:

/* --------------------------------            

 *        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);    

    }        

}            

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

[作者:技术者高健@博客园  mail: [email protected] ]

结束

你可能感兴趣的:(PostgreSQL)