kill bgwriter 的小实验

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介:

如果 我直接 kill 掉 bgwriter 的进程,会发生什么呢?

复制代码
[root@localhost postgresql-9.2.0]# ps -ef|grep post
root      2928  2897  0 10:34 pts/1    00:00:00 su - postgres
postgres  2929  2928  0 10:34 pts/1    00:00:00 -bash
postgres  3101  2929  0 11:09 pts/1    00:00:00 ./postgres -D /usr/local/pgsql/data
postgres  3103  3101  0 11:09 ?        00:00:00 postgres: checkpointer process     
postgres  3104  3101  0 11:09 ?        00:00:00 postgres: writer process           
postgres  3105  3101  0 11:09 ?        00:00:00 postgres: wal writer process       
postgres  3106  3101  0 11:09 ?        00:00:00 postgres: autovacuum launcher process   
postgres  3107  3101  0 11:09 ?        00:00:00 postgres: stats collector process   
root      3109  2977  0 11:10 pts/2    00:00:00 grep post
[root@localhost postgresql-9.2.0]# kill 3104
[root@localhost postgresql-9.2.0]# ps -ef|grep post
root      2928  2897  0 10:34 pts/1    00:00:00 su - postgres
postgres  2929  2928  0 10:34 pts/1    00:00:00 -bash
postgres  3101  2929  0 11:09 pts/1    00:00:00 ./postgres -D /usr/local/pgsql/data
postgres  3103  3101  0 11:09 ?        00:00:00 postgres: checkpointer process     
postgres  3105  3101  0 11:09 ?        00:00:00 postgres: wal writer process       
postgres  3106  3101  0 11:09 ?        00:00:00 postgres: autovacuum launcher process   
postgres  3107  3101  0 11:09 ?        00:00:00 postgres: stats collector process   
postgres  3110  3101  0 11:10 ?        00:00:00 postgres: writer process           
root      3112  2977  0 11:10 pts/2    00:00:00 grep post
[root@localhost postgresql-9.2.0]# kill 3110
[root@localhost postgresql-9.2.0]# ps -ef|grep post
root      2928  2897  0 10:34 pts/1    00:00:00 su - postgres
postgres  2929  2928  0 10:34 pts/1    00:00:00 -bash
postgres  3101  2929  0 11:09 pts/1    00:00:00 ./postgres -D /usr/local/pgsql/data
postgres  3103  3101  0 11:09 ?        00:00:00 postgres: checkpointer process     
postgres  3105  3101  0 11:09 ?        00:00:00 postgres: wal writer process       
postgres  3106  3101  0 11:09 ?        00:00:00 postgres: autovacuum launcher process   
postgres  3107  3101  0 11:09 ?        00:00:00 postgres: stats collector process   
postgres  3114  3101  0 11:10 ?        00:00:00 postgres: writer process           
root      3116  2977  0 11:10 pts/2    00:00:00 grep post
[root@localhost postgresql-9.2.0]# 
复制代码

我删除了几次 bgwriter 的进程,都再次生成了。

那么其原因是什么呢?

这和 postmaster.c 的监控有关。来看代码吧:为简化起见,吧postmaster 与 postgres 当成一个东西。

postmaster 生成了各个子进程以后,会在一旁进行监控:

复制代码
/*                                    
 * Reaper -- signal handler to cleanup after a child process dies.                                    
 */                                    
static void                                    
reaper(SIGNAL_ARGS)                                    
{                                    
    int            save_errno = errno;                    
    int            pid;        /* process id of dead child process */            
    int            exitstatus;        /* its exit status */            
                                    
    /* These macros hide platform variations in getting child status */                                
#ifdef HAVE_WAITPID                                    
    int            status;            /* child exit status */        
                                    
#define LOOPTEST()            ((pid = waitpid(-1, &status, WNOHANG)) > 0)                        
#define LOOPHEADER()            (exitstatus = status)                        
#else                            /* !HAVE_WAITPID */        
#ifndef WIN32                                    
    union wait    status;            /* child exit status */                
                                    
#define LOOPTEST()        ((pid = wait3(&status, WNOHANG, NULL)) > 0)                            
#define LOOPHEADER()    (exitstatus = status.w_status)                                
#else                            /* WIN32 */        
#define LOOPTEST()        ((pid = win32_waitpid(&exitstatus)) > 0)                            
#define LOOPHEADER()                                    
#endif   /* WIN32 */                                    
#endif   /* HAVE_WAITPID */                                    
                                    
    PG_SETMASK(&BlockSig);                                
                                    
    ereport(DEBUG4,                                
            (errmsg_internal("reaping dead processes")));                        
                                    
    while (LOOPTEST())                                
    {                                
        LOOPHEADER();                            
                                    
        ……                            
                                    
        /*                            
         * Was it the bgwriter?  Normal exit can be ignored; we'll start a new                            
         * one at the next iteration of the postmaster's main loop, if                            
         * necessary.  Any other exit condition is treated as a crash.                            
         */                            
        if (pid == BgWriterPID)                            
        {                            
            BgWriterPID = 0;                        
            if (!EXIT_STATUS_0(exitstatus))                        
                HandleChildCrash(pid, exitstatus,                    
                         _("background writer process"));            
            continue;                        
        }                            
                                    
        ……                            
    }                                
                                    
……                                    
}                                    
复制代码

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

由于我所使用的是 linux 平台,

[root@localhost postgresql-9.2.0]# find ./ -name "*.h"|xargs grep "HAVE_WAITPID"

./src/include/pg_config.h:#define HAVE_WAITPID 1
[root@localhost postgresql-9.2.0]# 
所以,循环程序可以认为是:

复制代码
while (((pid = waitpid(-1, &status, WNOHANG)) > 0))                                
{                                
    exitstatus = status;                            
                                
    ……                            
                                
    /*                            
     * Was it the bgwriter?  Normal exit can be ignored; we'll start a new                            
     * one at the next iteration of the postmaster's main loop, if                            
     * necessary.  Any other exit condition is treated as a crash.                            
     */                            
    if (pid == BgWriterPID)                            
    {                            
        BgWriterPID = 0;                        
        if (!(exitstatus==0))                        
            HandleChildCrash(pid, exitstatus,                    
                     _("background writer process"));            
        continue;                        
    }                            
                                
    ……                            
}                                
复制代码

waitpid 用于监控子进程的结束。

其参数:

pid=-1 就是 等待任何子进程,相当于 wait()。 

WNOHANG 就是  若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若结束,则返回该子进程的ID

而 HandleChildCrash 会完成重新建立子进程的工作。







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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
7天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
6天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
315 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
18天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1330 8
|
5天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
17天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1409 87
|
6天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
312 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
5天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
6天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
252 82
2025年阿里云域名备案流程(新手图文详细流程)