暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

digoal 2018-06-14
974

作者

digoal

日期

2018-06-14

标签

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.
```

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论