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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

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.  
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
弹性计算 网络协议 容灾
PostgreSQL 时间点恢复(PITR)在异步流复制主从模式下,如何避免主备切换后PITR恢复(备库、容灾节点、只读节点)走错时间线(timeline , history , partial , restore_command , recovery.conf)
标签 PostgreSQL , 恢复 , 时间点恢复 , PITR , restore_command , recovery.conf , partial , history , 任意时间点恢复 , timeline , 时间线 背景 政治正确非常重要,对于数据库来说亦如此,一个基于流复制的HA架构的集群,如果还有一堆只读节点,当HA集群发生了主备切换后,这些只读节点能否与新的主节点保持
1656 0
|
关系型数据库 流计算 PostgreSQL
关于PostgreSQL逻辑订阅中的复制状态
关于PostgreSQL逻辑订阅中的复制状态
2413 0
|
5月前
|
SQL 关系型数据库 分布式数据库
阿里云PolarDB是一款兼容MySQL、PostgreSQL和SQL Server等多种数据库协议的产品
阿里云PolarDB是一款兼容MySQL、PostgreSQL和SQL Server等多种数据库协议的产品
646 6
|
SQL 存储 关系型数据库
PostgreSQL 流复制搭建主从环境,同步和异步的解释,压力测试,主从角色切换|学习笔记
快速学习PostgreSQL 流复制搭建主从环境,同步和异步的解释,压力测试,主从角色切换
731 0
PostgreSQL 流复制搭建主从环境,同步和异步的解释,压力测试,主从角色切换|学习笔记
|
SQL 存储 关系型数据库
9 PostgreSQL 点对点多主表级复制-触发器篇|学习笔记
快速学习9 PostgreSQL 点对点多主表级复制-触发器篇
342 0
9 PostgreSQL 点对点多主表级复制-触发器篇|学习笔记
|
存储 SQL Oracle
10 PostgreSQL 表级复制-物化视图篇, 支持异地,异构如 Oracle 到 pg 的物化视图|学习笔记
快速学习10 PostgreSQL 表级复制-物化视图篇,支持异地,异构如 Oracle 到 pg 的物化视图
423 0
10 PostgreSQL 表级复制-物化视图篇, 支持异地,异构如 Oracle 到 pg 的物化视图|学习笔记
|
存储 SQL 缓存
PostgreSQL 复制原理及高可用集群(一)|学习笔记
快速学习 PostgreSQL 复制原理及高可用集群(一)
335 0
PostgreSQL 复制原理及高可用集群(一)|学习笔记
|
SQL 消息中间件 算法
14 PostgreSQL 表级复制-Londiste3哈希数据分区复制|学习笔记
快速学习14 PostgreSQL 表级复制-Londiste3哈希数据分区复制
127 0
14 PostgreSQL 表级复制-Londiste3哈希数据分区复制|学习笔记
|
SQL 消息中间件 存储
PostgreSQL 表级复制-Londiste3安装以及使用|学习笔记
快速学习 PostgreSQL 表级复制-Londiste3安装以及使用
303 0
PostgreSQL 表级复制-Londiste3安装以及使用|学习笔记
|
关系型数据库 PostgreSQL

相关产品

  • 云原生数据库 PolarDB