对IsUnderPostmaster变量初步学习

开始

在postmaster.c 中的  BackendStartup 中,有如下的代码:

其中定义了 IsUnderPostmaster=true。

而bgwriter 作为 postmaster 的子进程,它的 IsUnderPostmaster 也是为真。

 * BackendStartup -- start backend process
 *
 * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
 *
 * Note: if you change this code, also consider StartAutovacuumWorker.
 */
static int
BackendStartup(Port *port)
{
    Backend    *bn;                /* for backend cleanup */
    pid_t        pid;

    /*
     * Create backend data structure.  Better before the fork() so we can
     * handle failure cleanly.
     */
    bn = (Backend *) malloc(sizeof(Backend));
    if (!bn)
    {
        ereport(LOG,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of memory")));
        return STATUS_ERROR;
    }

    /*
     * Compute the cancel key that will be assigned to this backend. The
     * backend will have its own copy in the forked-off process' value of
     * MyCancelKey, so that it can transmit the key to the frontend.
     */
    MyCancelKey = PostmasterRandom();
    bn->cancel_key = MyCancelKey;

    /* Pass down canAcceptConnections state */
    port->canAcceptConnections = canAcceptConnections();
    bn->dead_end = (port->canAcceptConnections != CAC_OK &&
                    port->canAcceptConnections != CAC_WAITBACKUP);

    /*
     * Unless it's a dead_end child, assign it a child slot number
     */
    if (!bn->dead_end)
        bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
    else
        bn->child_slot = 0;

#ifdef EXEC_BACKEND
    pid = backend_forkexec(port);
#else                            /* !EXEC_BACKEND */
    pid = fork_process();
    if (pid == 0)                /* child */
    {
        free(bn);

        /*
         * Let's clean up ourselves as the postmaster child, and close the
         * postmaster's listen sockets.  (In EXEC_BACKEND case this is all
         * done in SubPostmasterMain.)
         */
        IsUnderPostmaster = true;        /* we are a postmaster subprocess now */

        MyProcPid = getpid();    /* reset MyProcPid */

        MyStartTime = time(NULL);

        /* We don't want the postmaster's proc_exit() handlers */
        on_exit_reset();

        /* Close the postmaster's sockets */
        ClosePostmasterPorts(false);

        /* Perform additional initialization and collect startup packet */
        BackendInitialize(port);

        /* And run the backend */
        proc_exit(BackendRun(port));
    }
#endif   /* EXEC_BACKEND */

    if (pid < 0)
    {
        /* in parent, fork failed */
        int            save_errno = errno;

        if (!bn->dead_end)
            (void) ReleasePostmasterChildSlot(bn->child_slot);
        free(bn);
        errno = save_errno;
        ereport(LOG,
                (errmsg("could not fork new process for connection: %m")));
        report_fork_failure_to_client(port, save_errno);
        return STATUS_ERROR;
    }

    /* in parent, successful fork */
    ereport(DEBUG2,
            (errmsg_internal("forked new backend, pid=%d socket=%d",
                             (int) pid, (int) port->sock)));

    /*
     * Everything's been successful, it's safe to add this backend to our list
     * of backends.
     */
    bn->pid = pid;
    bn->is_autovacuum = false;
    DLInitElem(&bn->elem, bn);
    DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
    if (!bn->dead_end)
        ShmemBackendArrayAdd(bn);
#endif

    return STATUS_OK;
}

 

结束

posted @ 2012-11-06 14:24  健哥的数据花园  阅读(638)  评论(0编辑  收藏  举报