PostgreSQL Streaming Replication COMMAND used in psql

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:
CF里面在讨论是否要添加一个查看数据库system id的函数, pg_system_identifier();
这个函数的用途和pg_controldata输出的Database system identifier值其实是一个效果.
只是它的目的是可以用SQL来得到这个值.
看到后面发现一个很有趣的东西,  Fujii Masao回复的如下 :
BTW, you can see the system identifier by executing IDENTIFY_SYSTEM
command in replication connection as follows:

1. Change the server settings so that the server can accept the
   replication connection
2. Connect to the server in replication mode
3. Execute IDENTIFY_SYSTEM command in replication connection

$ psql "replication=1"
=# IDENTIFY_SYSTEM;
      systemid       | timeline |  xlogpos
---------------------+----------+-----------
 5914930202950905854 |        1 | 0/183F720
(1 row)

This is not good way for a user, though ;P

> I don't know if that's justification enough, which is
> why I didn't add it to the commitfest yet.

You can add the patch to CF, and then hear the opinions from other people
during CF.

Regards,

-- 
Fujii Masao

原来还可以这么玩, 于是乎找了一个测试库试一试.
172.16.3.33 主库 (host replication postgres 172.16.3.0/24 md5)
172.16.3.39 备库
在172.16.3.39上以standby角色去连接172.16.3.33的主库.
pg94@db-172-16-3-39-> psql "replication=1" -h 172.16.3.33 -U postgres
Password for user postgres: 
psql (9.4devel)
Type "help" for help.
digoal=# 
digoal=# IDENTIFY_SYSTEM;
      systemid       | timeline |  xlogpos   
---------------------+----------+------------
 5912195073286594075 |        1 | 6/80000668
(1 row)

得到的值和 pg_controldata一致.
pg94@db-172-16-3-33-> pg_controldata |grep identifier
Database system identifier:           5912195073286594075
Maximum length of identifiers:        64

除了使用 IDENTIFY_SYSTEM, replication protocol还支持其他的命令.
详见 : 
http://www.postgresql.org/docs/devel/static/protocol-replication.html
IDENTIFY_SYSTEM
TIMELINE_HISTORY tli
START_REPLICATION XXX/XXX TIMELINE tli
BASE_BACKUP [LABEL 'label'] [PROGRESS] [FAST] [WAL] [NOWAIT]

包括pg_basebackup , 也是使用流复制协议进行数据复制的.
另外几个命令也可以在psql命令行中使用, 例如 : 
在主节点pg_xlog中创建一个history文件.
[root@db-172-16-3-33 pg_basebackup]# su - pg94
pg94@db-172-16-3-33-> cd $PGDATA
pg94@db-172-16-3-33-> vi pg_xlog/00000002.history 
test line 1
test line 2
使用流复制命令接收history文件内容.
digoal=# TIMELINE_HISTORY 2;
     filename     |   content   
------------------+-------------
 00000002.history | test line 1+
                  | test line 2+
                  | 
(1 row)

其他命令 : 
digoal=# select * from pg_current_xlog_location();
 pg_current_xlog_location 
--------------------------
 6/800007A8
(1 row)


digoal=# START_REPLICATION 6/80000700 TIMELINE 1;
unexpected PQresultStatus: 8
digoal=# START_REPLICATION 6/80000700 TIMELINE 1;
PQexec not allowed during COPY BOTH

BASE_BACKUP 是做基础备份的, 数据比较庞大.

[参考]
1. src/backend/replication/repl_gram.y
4. src/interfaces/libpq/libpq-int.h
/*
 * PGconn stores all the state data associated with a single connection
 * to a backend.
 */
struct pg_conn
{
        /* Saved values of connection options */
        char       *pghost;                     /* the machine on which the server is running */
        char       *pghostaddr;         /* the numeric IP address of the machine on
                                                                 * which the server is running.  Takes
                                                                 * precedence over above. */
        char       *pgport;                     /* the server's communication port */
        char       *pgunixsocket;       /* the Unix-domain socket that the server is
                                                                 * listening on; if NULL, uses a default
                                                                 * constructed from pgport */
        char       *pgtty;                      /* tty on which the backend messages is
                                                                 * displayed (OBSOLETE, NOT USED) */
        char       *connect_timeout;    /* connection timeout (numeric string) */
        char       *client_encoding_initial;            /* encoding to use */
        char       *pgoptions;          /* options to start the backend with */
        char       *appname;            /* application name */
        char       *fbappname;          /* fallback application name */
        char       *dbName;                     /* database name */
        char       *replication;        /* connect as the replication standby? */
replication指定是否以standby连接到主库.
... 其他略.

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3月前
|
关系型数据库 分布式数据库 数据库
PolarDB for PostgreSQL报错问题之psql连接数据库报错如何解决
PolarDB for PostgreSQL是基于PostgreSQL开发的一款云原生关系型数据库服务,它提供了高性能、高可用性和弹性扩展的特性;本合集将围绕PolarDB(pg)的部署、管理和优化提供指导,以及常见问题的排查和解决办法。
|
3月前
|
关系型数据库 Linux 数据安全/隐私保护
PostgreSQL【部署 02】在线安装PostgreSQL(Some psql features might not work 问题处理+角色密码设置+配置远程访问)
PostgreSQL【部署 02】在线安装PostgreSQL(Some psql features might not work 问题处理+角色密码设置+配置远程访问)
60 0
PostgreSQL【部署 02】在线安装PostgreSQL(Some psql features might not work 问题处理+角色密码设置+配置远程访问)
|
11月前
|
前端开发 关系型数据库 数据库
使用psql操作PostgreSQL数据库
使用psql操作PostgreSQL数据库
83 0
|
SQL 存储 Oracle
【postgreSQL】psql工具特有的快捷命令2
【postgreSQL】psql工具特有的快捷命令2
141 0
|
关系型数据库 PostgreSQL
使用psql连接 postgresql失败
使用psql链接postgresql服务失败,telnet这个地址和端口有没有反映。
140 0
|
SQL 存储 移动开发
PostgreSQL psql的使用,SQL语法,数据类型,递归SQL用法(四)|学习笔记
快速学习3 PostgreSQL psql的使用,SQL语法,数据类型,递归SQL用法(四)
433 0
 PostgreSQL psql的使用,SQL语法,数据类型,递归SQL用法(四)|学习笔记
|
SQL 关系型数据库 数据库
3 PostgreSQL psql的使用,SQL语法,数据类型,递归SQL用法(三)|学习笔记
快速学习3 PostgreSQL psql的使用,SQL语法,数据类型,递归SQL用法(三)
335 0
|
关系型数据库 C语言 PostgreSQL
PostgreSQL 开启with-llvm(JIT)后,新增插件异常(clang: Command not found)处理
标签 PostgreSQL , llvm , clang , jit 背景 PostgreSQL 11版本开始引入了对JIT的支持,在OLAP类型的SQL有比较大的性能提升。 如果你使用的是YUM安装的PG,clang可能没有加入,在后期编译其他插件时可能遇到类似的报错: 比如pg_hint_plan插件 git clone https://github.
2926 0
|
SQL 关系型数据库 数据库
PostgreSQL psql 绘制饼图
标签 PostgreSQL , SQL , PLPGSQL , 绘制饼图 背景 图像相比文字是更容易被理解的东西,在BI可视化领域,经常会使用图像来代替数值,展示一些信息,例如柱状图、饼图、线图等。
871 0