PostgreSQL 流复制协议交互式调试 - replication=true-阿里云开发者社区

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

PostgreSQL 流复制协议交互式调试 - replication=true

简介:

标签

PostgreSQL , replication , 流复制 , 调试 , 协议


背景

当客户端连接到PG服务端时,startup包里面可以带入一些信息。

src/interfaces/libpq/fe-connect.c

当postmaster解析startup包时,如果解析到replication的参数信息,则会启动wal sender进程与客户端交互,交互采用stream replication protocol.

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)  
{  
............  
  
  
                        else if (strcmp(nameptr, "replication") == 0)  
                        {  
                                /*  
                                 * Due to backward compatibility concerns the replication  
                                 * parameter is a hybrid beast which allows the value to be  
                                 * either boolean or the string 'database'. The latter  
                                 * connects to a specific database which is e.g. required for  
                                 * logical decoding while.  
                                 */  
                                if (strcmp(valptr, "database") == 0)  
                                {  
                                        am_walsender = true;  
                                        am_db_walsender = true;  
                                }  
                                else if (!parse_bool(valptr, &am_walsender))  
                                        ereport(FATAL,  
                                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),  
                                                         errmsg("invalid value for parameter \"%s\": \"%s\"",  
                                                                        "replication",  
                                                                        valptr),  
                                                         errhint("Valid values are: \"false\", 0, \"true\", 1, \"database\".")));  
                        }  
  
  
  
        /*  
         * Normal walsender backends, e.g. for streaming replication, are not  
         * connected to a particular database. But walsenders used for logical  
         * replication need to connect to a specific database. We allow streaming  
         * replication commands to be issued even if connected to a database as it  
         * can make sense to first make a basebackup and then stream changes  
         * starting from that.  
         */  
        if (am_walsender && !am_db_walsender)  
                port->database_name[0] = '\0';  

使用replication参数连接数据库的例子

参数允许设置为false,true,0,1,database

如果设置为database,可以使用logical replication命令

digoal@iZbp13nu0s9j3x3op4zpd4Z-> psql "replication=2" postgres  
psql: FATAL:  invalid value for parameter "replication": "2"  
HINT:  Valid values are: "false", 0, "true", 1, "database".  

当建立流复制连接后,就只能使用协议识别的命令,详见:

https://www.postgresql.org/docs/10/static/protocol-replication.html

https://www.postgresql.org/docs/10/static/protocol-logical-replication.html

digoal@iZbp13nu0s9j3x3op4zpd4Z-> psql "replication=1" postgres  
psql (10.4)  
Type "help" for help.  
  
postgres=# select 1;  
ERROR:  cannot execute SQL commands in WAL sender for physical replication  
postgres=# \set VERBOSITY verbose  
postgres=# select 1;  
ERROR:  XX000: cannot execute SQL commands in WAL sender for physical replication  
LOCATION:  exec_replication_command, walsender.c:1560  

注意流复制协议命令区分大小写

postgres=# IDENTIFY_SYSTEM  
postgres-# ;  
      systemid       | timeline |   xlogpos   | dbname   
---------------------+----------+-------------+--------  
 6561932752697330615 |        1 | 2B/FE093610 |   
(1 row)  
  
postgres=# show block_size;  
ERROR:  XX000: cannot execute SQL commands in WAL sender for physical replication  
LOCATION:  exec_replication_command, walsender.c:1560  
  
postgres=# SHOW block_size;  
 block_size   
------------  
 8192  
(1 row)  

使用流复制协议的一些客户端

1、接收物理WAL日志

https://www.postgresql.org/docs/10/static/app-pgreceivewal.html

2、接收上游decode好的逻辑日志

https://www.postgresql.org/docs/10/static/app-pgrecvlogical.html

3、在线备份

https://www.postgresql.org/docs/10/static/app-pgbasebackup.html

参考

https://www.postgresql.org/docs/10/static/protocol-replication.html

https://www.postgresql.org/docs/10/static/protocol-logical-replication.html

https://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters

replication = String  
  
Connection parameter passed in the startup message.   
  
This parameter accepts two values; "true" and database.   
  
Passing true tells the backend to go into walsender mode,   
wherein a small set of replication commands can be issued instead of SQL statements.   
  
Only the simple query protocol can be used in walsender mode.   
  
Passing "database" as the value instructs walsender to connect to the database specified in the dbname parameter,   
which will allow the connection to be used for logical replication from that database.  

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

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

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

官方博客
链接