开发者社区> 嗯哼9925> 正文

对PostgreSQL 的 background writer 的初步理解

简介:
+关注继续查看

代码缩略如下:

复制代码
/*                                            
 * Main entry point for bgwriter process                                            
 *                                            
 * This is invoked from AuxiliaryProcessMain, which has already created the                                            
 * basic execution environment, but not enabled signals yet.                                            
 */                                            
void                                            
BackgroundWriterMain(void)                                            
{                                            
    ……                                        
    /*                                        
     * Properly accept or ignore signals the postmaster might send us.                                        
     *                                        
     * bgwriter doesn't participate in ProcSignal signalling, but a SIGUSR1                                        
     * handler is still needed for latch wakeups.                                        
     */                                        
    pqsignal(SIGHUP, BgSigHupHandler);                    /* set flag to read config file */                    
    pqsignal(SIGINT, SIG_IGN);                                        
    pqsignal(SIGTERM, ReqShutdownHandler);                    /* shutdown */                    
    pqsignal(SIGQUIT, bg_quickdie);                    /* hard crash time */                    
    pqsignal(SIGALRM, SIG_IGN);                                        
    pqsignal(SIGPIPE, SIG_IGN);                                        
    pqsignal(SIGUSR1, bgwriter_sigusr1_handler);                                        
    pqsignal(SIGUSR2, SIG_IGN);                                        
                                            
    /*                                        
     * Reset some signals that are accepted by postmaster but not here                                        
     */                                        
    pqsignal(SIGCHLD, SIG_DFL);                                        
    pqsignal(SIGTTIN, SIG_DFL);                                        
    pqsignal(SIGTTOU, SIG_DFL);                                        
    pqsignal(SIGCONT, SIG_DFL);                                        
    pqsignal(SIGWINCH, SIG_DFL);                                        
                                            
    /* We allow SIGQUIT (quickdie) at all times */                                        
    sigdelset(&BlockSig, SIGQUIT);                                        
                                            
    ……                                        
    /*                                        
     * Loop forever                                        
     */                                        
    for (;;)                                        
    {                                        
        bool        can_hibernate;                            
        int        rc;                            
                                            
        /* Clear any already-pending wakeups */                                    
        ResetLatch(&MyProc->procLatch);                                    
                                            
        if (got_SIGHUP)                                    
        {                                    
            got_SIGHUP = false;                                
            ProcessConfigFile(PGC_SIGHUP);                                
        }                                    
        if (shutdown_requested)                                    
        {                                    
            /*                                
             * From here on, elog(ERROR) should end with exit(1), not send                                
             * control back to the sigsetjmp block above                                
             */                                
            ExitOnAnyError = true;                                
            /* Normal exit from the bgwriter is here */                                
            proc_exit(0);        /* done */                        
        }                                    
                                            
        /*                                    
         * Do one cycle of dirty-buffer writing.                                    
         */                                    
        can_hibernate = BgBufferSync();                                    
                                            
        /*                                    
         * Send off activity statistics to the stats collector                                    
         */                                    
        pgstat_send_bgwriter();                                    
                                            
        if (FirstCallSinceLastCheckpoint())                                    
        {                                    
            /*                                
             * After any checkpoint, close all smgr files.                    This is so we            
             * won't hang onto smgr references to deleted files indefinitely.                                
             */                                
            smgrcloseall();                                
        }                                    
                                            
        /*                                    
         * Sleep until we are signaled or BgWriterDelay has elapsed.                                    
         *                                    
         * Note: the feedback control loop in BgBufferSync() expects that we                                    
         * will call it every BgWriterDelay msec.  While it's not critical for                                    
         * correctness that that be exact, the feedback loop might misbehave                                    
         * if we stray too far from that.  Hence, avoid loading this process                                    
         * down with latch events that are likely to happen frequently during                                    
         * normal operation.                                    
         */                                    
        rc = WaitLatch(&MyProc->procLatch,                                    
                   WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,                            
                   BgWriterDelay /* ms */ );                            
                                            
        /*                                    
         * If no latch event and BgBufferSync says nothing's happening, extend                                    
         * the sleep in "hibernation" mode, where we sleep for much longer                                    
         * than bgwriter_delay says.  Fewer wakeups save electricity.  When a                                    
         * backend starts using buffers again, it will wake us up by setting                                    
         * our latch.  Because the extra sleep will persist only as long as no                                    
         * buffer allocations happen, this should not distort the behavior of                                    
         * BgBufferSync's control loop too badly; essentially, it will think                                    
         * that the system-wide idle interval didn't exist.                                    
         *                                    
         * There is a race condition here, in that a backend might allocate a                                    
         * buffer between the time BgBufferSync saw the alloc count as zero                                    
         * and the time we call StrategyNotifyBgWriter.  While it's not                                    
         * critical that we not hibernate anyway, we try to reduce the odds of                                    
         * that by only hibernating when BgBufferSync says nothing's happening                                    
         * for two consecutive cycles.    Also, we mitigate any possible                                
         * consequences of a missed wakeup by not hibernating forever.                                    
         */                                    
        if (rc == WL_TIMEOUT && can_hibernate && prev_hibernate)                                    
        {                                    
            /* Ask for notification at next buffer allocation */                                
            StrategyNotifyBgWriter(&MyProc->procLatch);                                
            /* Sleep ... */                                
            rc = WaitLatch(&MyProc->procLatch,                                
                           WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,                    
                           BgWriterDelay * HIBERNATE_FACTOR);                    
            /* Reset the notification request in case we timed out */                                
            StrategyNotifyBgWriter(NULL);                                
        }                                    
        ……                                    
    }                                        
}                                            
复制代码

我的理解:其中最为关键的就是这一段:

/* 
* Do one cycle of dirty-buffer writing.
*/
can_hibernate = BgBufferSync(); 

其位于 src\backend\storage\buffer 目录下的 bufmgr.c 中。








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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
postgresql :pg_read_binary_file插入文件
postgresql :pg_read_binary_file插入文件
88 0
RDS MySQL 8.0 SQL Outline 功能
## 背景 在生产环境,MySQL 数据库实例运行过程中,一些 SQL 语句会发生执行计划的变化,导致增加了数据库稳定性的风险, 这里边有几个因素和场景,比如:随着表数据量的变化,以及统计信息的自动收集,CBO optimizer 计算得到了一个cost 更低的 plan, 又或者 表结构发生了变化,增加和删减了某些索引,或者在实例升级迁移等过程中,MySQL 自身优化器行为和算法发生了
694 0
PostgreSQL pg_top pgcenter - 实时top类工具
标签 PostgreSQL , pg_top , pgcenter 背景 PostgreSQL 的统计信息、实时会话信息、操作系统状态信息等汇总,统计,展示。 https://www.postgresql.
932 0
+关注
嗯哼9925
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
PostgreSQL 9.6 New advances in Full Text Searc
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载