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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

开始

基本上:

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

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

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

复制代码
/*                    
 *     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






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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
1月前
|
Unix Shell Linux
Linux 终端和进程的关系,以及在终端前后台切换进程
Linux 终端和进程的关系,以及在终端前后台切换进程
34 1
|
1月前
|
Linux Shell
Linux 进程的前台/后台切换
当你用shell启动一个程序时,往往他是在前台工作的。程序会一直占用终端命令行,例如你在前台解压的时候必须等着,期间干不了别的事(除非另开一个终端)。 例如经常用连接到远程服务器执行脚本的时候,如果本地网络中断后,这个时候前台进程就结束了,比较的懊恼,必须重新执行。
49 6
|
3月前
|
Linux
|
1月前
|
存储 Linux 程序员
【Linux C/C++ 堆内存分布】深入理解Linux进程的堆空间管理
【Linux C/C++ 堆内存分布】深入理解Linux进程的堆空间管理
76 0
|
1月前
|
前端开发 Android开发 iOS开发
应用研发平台EMAS使用 aliyun-react-native-push 库接入推送和辅助通道,推送都可以收到,但是在App切到后台或者杀掉进程之后就收不到推送了,是需要配置什么吗?
【2月更文挑战第31天】应用研发平台EMAS使用 aliyun-react-native-push 库接入推送和辅助通道,推送都可以收到,但是在App切到后台或者杀掉进程之后就收不到推送了,是需要配置什么吗?
32 2
|
1月前
|
消息中间件 Linux
Linux进程间通信(IPC)教程 Linux共享内存介绍:介绍POSIX共享内存的基本概念、用途和编程实践
Linux进程间通信(IPC)教程 Linux共享内存介绍:介绍POSIX共享内存的基本概念、用途和编程实践
25 2
|
1月前
|
存储 Java C++
JVM内存模型和结构详解(五大模型图解)
JVM内存模型和结构详解(五大模型图解)
|
1月前
|
Linux 数据安全/隐私保护
进程间通信之共享内存及其shm函数的使用【Linux】
进程间通信之共享内存及其shm函数的使用【Linux】
|
2月前
|
机器学习/深度学习 安全 API
如何在 Python 中启动后台进程?
如何在 Python 中启动后台进程?
68 1
|
2月前
|
Linux
百度搜索:蓝易云【深入解析Linux进程内存:VSS、RSS、PSS、USS及查看方式】
通过以上方法,你可以深入了解Linux进程的内存使用情况,包括VSS、RSS、PSS、USS等指标,帮助你进行性能优化和资源管理。
41 12