PostgreSQL 恢复模式错误日志增强 - 提供正在恢复的WAL(XLOG)文件位置-阿里云开发者社区

开发者社区> 阿里云数据库> 正文

PostgreSQL 恢复模式错误日志增强 - 提供正在恢复的WAL(XLOG)文件位置

简介: 标签 PostgreSQL , the database system is starting up , the database system is in recovery mode 背景 当数据库异常停库,再次启动时。

标签

PostgreSQL , the database system is starting up , the database system is in recovery mode


背景

当数据库异常停库,再次启动时。又或者由于进程CRASH后自动重启时。需要进入恢复模式,恢复完成后,数据库才能正常交互。

在启动过程中,正在恢复时,如果此时连接数据库,会报错如下:

the database system is starting up  

用户只看这个信息,并不知道数据库还要启动多久,现在已经恢复到什么状态了?

内核层面可以稍作改进,报错时,同时输出正在恢复的WAL位点,以及距离最后一个文件的WAL位点差多少MB没有恢复。这样用户大概就还知道还需要多久。

src/backend/postmaster/postmaster.c

/*  
 * Read a client's startup packet and do something according to it.  
 *  
 * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and  
 * not return at all.  
 *  
 * (Note that ereport(FATAL) stuff is sent to the client, so only use it  
 * if that's what you want.  Return STATUS_ERROR if you don't want to  
 * send anything to the client, which would typically be appropriate  
 * if we detect a communications failure.)  
 */  
static int  
ProcessStartupPacket(Port *port, bool SSLdone)  
{  
  
.......  
  
        /*  
         * If we're going to reject the connection due to database state, say so  
         * now instead of wasting cycles on an authentication exchange. (This also  
         * allows a pg_ping utility to be written.)  
         */  
        switch (port->canAcceptConnections)  
        {  
                case CAC_STARTUP:  
                        ereport(FATAL,  
                                        (errcode(ERRCODE_CANNOT_CONNECT_NOW),  
                                         errmsg("the database system is starting up")));  
                        break;  
                case CAC_SHUTDOWN:  
                        ereport(FATAL,  
                                        (errcode(ERRCODE_CANNOT_CONNECT_NOW),  
                                         errmsg("the database system is shutting down")));  
                        break;  
                case CAC_RECOVERY:  
                        ereport(FATAL,  
                                        (errcode(ERRCODE_CANNOT_CONNECT_NOW),  
                                         errmsg("the database system is in recovery mode")));  
                        break;  
                case CAC_TOOMANY:  
                        ereport(FATAL,  
                                        (errcode(ERRCODE_TOO_MANY_CONNECTIONS),  
                                         errmsg("sorry, too many clients already")));  
                        break;  
                case CAC_WAITBACKUP:  
                        /* OK for now, will check in InitPostgres */  
                        break;  
                case CAC_OK:  
                        break;  
        }  
  
        return STATUS_OK;  
}  

src/backend/access/transam/xlog.c

/*  
 * This must be called ONCE during postmaster or standalone-backend startup  
 */  
void  
StartupXLOG(void)  
{  

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

分享:
阿里云数据库
使用钉钉扫一扫加入圈子
+ 订阅

帮用户承担一切数据库风险,给您何止是安心!

官方博客
链接