PostgreSQL 10.0 preview 功能增强 - slave支持WAITLSN 'lsn', time;用于设置安全replay栅栏

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

标签

PostgreSQL , 10.0 , waitlsn , replay , slave , 流复制 , 等待恢复


背景

PostgreSQL 的流复制是非常灵活的,在主库端,可以将事务的级别设置为同步,异步,本地同步,远程同步刷盘,远程同步刷OS缓存,远程数据库apply等多种级别。

《PostgreSQL 9.6 同步多副本 与 remote_apply事务同步级别》

但是在备库,还没有一个可以设置安全栅栏的手段,比如说,我们要让备库apply到某个位点后,再执行某个QUERY。

10.0提供了这样的接口,来完成这个功能。

waitlsn 'lsn', time;语法

例子:

session a:   
连接到主节点,执行了某个QUERY,并提交,产生的LSN为lsn1位置。  

session b:  
连接到备节点,它如果要等待一致性的结果,需要等待lsn1在备节点已经apply,怎么做呢?  
waitlsn 'lsn1';即可  

详情

Hi hackers,  

Few days earlier I've finished my work on WAITLSN statement utility, so   
I’d like to share it.  


Introduction  
============  

Our clients who deal with 9.5 and use asynchronous master-slave   
replication, asked to make the wait-mechanism on the slave side to   
prevent the situation when slave handles query which needs data (LSN)   
that was received, flushed, but still not replayed.  


Problem description  
===================  

The implementation:  
Must handle the wait-mechanism using pg_sleep() in order not to load system  
Must avoid race conditions if different backend want to wait for   
different LSN  
Must not take snapshot of DB, to avoid troubles with sudden minXID change  
Must have optional timeout parameter if LSN traffic has stalled.  
Must release on postmaster’s death or interrupts.  


Implementation  
==============  

To avoid troubles with snapshots, WAITLSN was implemented as a utility   
statement, this allows us to circumvent the snapshot-taking mechanism.  
We tried different variants and the most effective way was to use Latches.  
To handle interprocess interaction all Latches are stored in shared   
memory and to cope with race conditions, each Latch is protected by a   
Spinlock.  
Timeout was made optional parameter, it is set in milliseconds.  


What works  
==========  

Actually, it works well even with significant timeout or wait period   
values, but of course there might be things I've overlooked.  

How to use it  
==========  

WAITLSN ‘LSN’ [, timeout in ms];  

#Wait until LSN 0/303EC60 will be replayed, or 10 second passed.  
WAITLSN ‘0/303EC60’, 10000;  

#Or same without timeout.  
WAITLSN ‘0/303EC60’;  

Notice: WAITLSN will release on PostmasterDeath or Interruption events   
if they come earlier then LSN or timeout.  

Testing the implementation  
======================  

The implementation was tested with testgres and unittest python modules.  

How to test this implementation:  
Start master server  
Make table test, insert tuple 1  
Make asynchronous slave replication (9.5 wal_level = standby, 9.6 or   
higher wal_level =  replica)  
Slave: START TRANSACTION ISOLATION LEVEL REPEATABLE READ ;  
        SELECT * FROM test;  
Master: delete tuple + make vacuum + get new LSN  
Slave: WAITLSN ‘newLSN’, 60000;  
        Waitlsn finished with FALSE “LSN doesn`t reached”  
Slave: COMMIT;  
        WAITLSN ‘newLSN’, 60000;  
        Waitlsn finished with success (without NOTICE message)  

The WAITLSN as expected wait LSN, and interrupts on PostmasterDeath,   
interrupts or timeout.  

Your feedback is welcome!  


---  
Ivan Kartyshov  
Postgres Professional: http://www.postgrespro.com  
The Russian Postgres Company  

这个patch的讨论,详见邮件组,本文末尾URL。

PostgreSQL社区的作风非常严谨,一个patch可能在邮件组中讨论几个月甚至几年,根据大家的意见反复的修正,patch合并到master已经非常成熟,所以PostgreSQL的稳定性也是远近闻名的。

参考

https://commitfest.postgresql.org/13/772/

https://www.postgresql.org/message-id/flat/0240c26c-9f84-30ea-fca9-93ab2df5f305@postgrespro.ru#0240c26c-9f84-30ea-fca9-93ab2df5f305@postgrespro.ru

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
1月前
|
关系型数据库 Serverless 分布式数据库
【公测】PolarDB PostgreSQL版Serverless功能免费使用​!
【公测】PolarDB PostgreSQL版Serverless功能免费使用​,公测于2024年3月28日开始,持续三个月,公测期间可以免费使用!
|
4月前
|
存储 关系型数据库 MySQL
PolarDB优势功能
PolarDB优势功能
|
2月前
|
关系型数据库 Serverless 分布式数据库
PolarDB PostgreSQL版Serverless功能上线公测啦,公测期间免费使用!
Serverless数据库能够使得数据库集群资源随客户业务负载动态弹性扩缩,将客户从复杂的业务资源评估和运维工作中解放出来。PolarDB PostgreSQL版 Serverless提供了CPU、内存、存储、网络资源的实时弹性能力,构建计算与存储分离架构下的 PolarDB PostgreSQL版产品新形态。
|
1天前
|
关系型数据库 MySQL 分布式数据库
PolarDB MySQL版集群版本支持库表恢复功能的版本要求是什么?
【5月更文挑战第13天】PolarDB MySQL版集群版本支持库表恢复功能的版本要求是什么?
8 0
|
15天前
|
SQL 运维 关系型数据库
PolarDB产品使用合集之PolarDB 2.3.0 版本的 CDC 功能支持 Polardb-X 到 Polardb-X 的数据同步吗
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
15天前
|
关系型数据库 MySQL 分布式数据库
PolarDB产品使用合集之PolarDB MySQL标准版中带有分区功能吗
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
15天前
|
运维 安全 关系型数据库
PolarDB产品使用合集之关于PolarDB DRDS的设置参数,主要有哪些
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
7月前
|
安全 关系型数据库 Go
远程连接PostgreSQL:配置指南与安全建议
远程连接PostgreSQL:配置指南与安全建议
350 0
|
3月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
47 7
|
3月前
|
关系型数据库 Linux Shell
Centos系统上安装PostgreSQL和常用PostgreSQL功能
Centos系统上安装PostgreSQL和常用PostgreSQL功能

相关产品

  • 云原生数据库 PolarDB