Streaming replication slots in PostgreSQL 9.4

Streaming replication slots are a pending feature in PostgreSQL 9.4, as part of the logical changeset extraction feature.

What are they for, what do you need to know, what changes?

What are replication slots?

Streaming replication slots are a new facility introduced in PostgreSQL 9.4. They are a persistent record of the state of a replica that is kept on the master server even when the replica is offline and disconnected.

They aren’t used for physical replication by default, so you’ll only be dealing with them if you enable their use. This post explains what they do, why they’re useful for physical replication, and why they’re necessary for logical replication.

Why slots?

As part of the ongoing work Andres has been doing on log-streaming logical replication, changeset extraction, and bidirectional replication, there’s been the need to better control WAL retention.

As streaming replication currently works, the master doesn’t make any effort to retain extra WAL for a standby that’s fallen behind. If the standby gets too far behind, the master will delete WAL segments the standby still needs to replay, and the standby cannot recover. You get an error like:

ERROR:  requested WAL segment 00000001000000010000002D has already been removed

To prevent this, you are currently expected to configure continuous archiving and provide a restore_command to give the replica access to the archive, or be willing to re-create a standby that falls behind.

A less reliable alternative is to set wal_keep_segments to a “high enough” value to ensure that the replica never falls too far behind – but there’s no real way to guess what is high enough, and the higher the value is set the greater the risk the master will be able to run out of space in pg_xlog. So generally archiving is preferred.

The master could retain WAL for a replica while it’s connected, but as soon as the replica disconnects for any reason the master forgets all about it. As transient disconnects are common, this means there’s not much point in trying to retain WAL just for connected replicas.

We’ve gotten away with this so far because the option of falling back to continuous archiving has been available for when the master discards WAL the replicas still need. When combined with or scripts likeWAL-E, a shared network file system, or a shared host to scp archives to/from, it’s done the job pretty well. It’s a bit confusing for new users, but it works well.

With log streaming logical replication we can’t fall back on continuous archiving anymore, because the master must read WAL and decode it to send the results to the replicas; it can’t just send raw WAL records. Replicas cannot just read raw WAL from the master and extract what they require themselves because only the master has a record of transaction ID statuses, the local oids of objects, etc.

We could just teach the master to run restore_command (something people seem to assume happens anyway) – but that could get very inefficient when many replicas want the same WAL archive. We’d need a caching system and other complexity. It also doesn’t get rid of the need for the admin to manage retention for archived WAL.

Unlike with physical replication, the option of just re-seeding a replica that has fallen too far behind is not acceptable for logical replication. There may be other databases on the replica host that contain their own original data. In the bidirectional replication case, the replica database may also contain newdata not yet replicated to other nodes. So we can’t just write the replica off and make a new pg_basebackupfrom the master or re-rsync it like we do with physcial replication.

The introduction of slots is intended to deal with this issue by giving the master persistent knowledge of the state of replicas, even when they are disconnected. While it was added for logical replication, the concept is useful in physical replication as well, as it lets the master retain as much WAL as is required by all its standbys and no more. There’s no guesswork like there is with wal_keep_segments. There’s no need to set up a separate WAL archiving system and manage retention in it. So long as you have enough space in pg_xlog for the extra WAL, it just works. If a replica disconnects the master can keep WAL for it until it reconnects and replays the WAL.

The downside of this approach is that it adds another way for unbounded WAL growth to fill pg_xlog – when a replica cannot receive or replay WAL fast enough or remains offline for a long time, the master will keep on retaining WAL until pg_xlog fills up and it starts reporting errors to clients. This can already happen on a master with long checkpoint intervals under heavy load, but it’s much easer to trigger with slots as a replica that goes ofline indefinitely will cause pg_xlog to fill up.

While monitoring and adjustment of pg_xlog capacity is already necessary for a busy PostgreSQL server, it is much more so for a server that is using slots to manage WAL retention for replication.

Operational impact

By default, physical replication will not use slots. Nothing changes. You must use WAL archiving on top of streaming to ensure reliable replication.

You can enable the use of replication slots for physical replication with the primary-slotname parameter in recovery.conf. The physical replication slot must already have been created on the master with the pg_create_physical_replication_slot(...) function; see the docs.

When using slots to manage physical replication, WAL archiving is no longer required for replication (though you may wish to retain it for PITR anyway). You don’t need to deal with the difficulties of managing WAL retention amongst multiple replicas that may or may not be online, with replay positions that are not known to the master when offline. You don’t need to manage the space in the archive store.

Instead, you need to keep an eye on space in pg_xlog. WAL retention is managed automatically for you, but you must ensure there’s enough space available.

If a replica is retired from service or unrecoverably lost due to failure of the host, you must manually remove the slot from the master because the master won’t delete any xlog until you do. If you have a system where the master produces xlog rapidly, this may require a lot of pg_xlog space or fairly quick action. A monitoring system like Icinga or Zabbix is strongly recommended.

A replica that is unable to keep up the master must be fixed or retired to prevent the master from running out of xlog space.

With slots you no longer need to monitor the replicas to determine their state, as accurate and up to date information is always available in the pg_replication_slots view. So all you need to do is monitor the master’s pg_xlog space and the status of all replication slots.

Any wal_keep_segments parameter will be respected as a minimum for WAL retention.

So: using slots for physical replication is a trade-off. You don’t need archiving anymore, but you have to monitor the state of the system more closely to avoid disrupting the master.

Future work

To reduce the need for a large pg_xlog space allocation on what could be expensive high-performance storage, in future slots may be extended to support moving WAL to a separate location once it’s no longer required for the master’s own checkpointing.

参考:

http://blog.2ndquadrant.com/postgresql-9-4-slots/

时间: 2024-10-23 01:36:29

Streaming replication slots in PostgreSQL 9.4的相关文章

PostgreSQL数据库Streaming Replication流复制主备延迟测试

PostgreSQL数据库流复制主库和备库之间的延迟时间是多少,无论对HA还是负载均衡来说都应该做个评估.比如单纯的HA架构,当主库发生故障时,我们允许多少时间内的数据丢失.不废话,直接进入本次实验测试. 测试环境: 主库:内存:32G,CPU:8核,IP:192.168.122.101 备库:内存:32G,CPU:8核,IP:192.168.122.102 数据库配置:默认 测试准备: 在两台服务器上安装好PostgreSQL数据库,安装过程不清楚的可以参考文章<PostgreSQL数据库编译

postgresql 流复制(streaming replication)

基本环境说明: os:FreeBSD 9.3 postgresql version:9.3.6 master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 1.配置master端 # psql -U pgsql -d postgres -c "CREATE USER rep REPLICATION LOGIN ENCRYPTED PASSWORD 'password';" # cd /usr/local/pgsql # vim d

PostgreSQL Replication之扩展与BDR

在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项目. 本章将是关于如下这些主题: ?理解 BDR 复制概念 ?安装 BDR ?设置一个简单的集群 ?修改集群和故障转移 ?了解 BDR 的性能 在挖掘所有的技术细节之前,理解 BDR 方面的基本技术是非常重要的.           理解 BDR 复制概念 过去,在9.0被引进之前,人们不得不使用S

PostgreSQL Replication之第十四章 扩展与BDR

在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项目. 本章将是关于如下这些主题: •理解 BDR 复制概念 •安装 BDR •设置一个简单的集群 •修改集群和故障转移 •了解 BDR 的性能 在挖掘所有的技术细节之前,理解 BDR 方面的基本技术是非常重要的.           理解 BDR 复制概念 过去,在9.0被引进之前,人们不得不使用S

PostgreSQL Replication之第四章 设置异步复制(1)

执行完您的第一个即时恢复(PITR,Point-In-Time-Recovery),我们准备在一个真正的复制设置上工作.在本章,您将学会如何设置异步复制和流.我们的目标是确保您可以实现更高的高可用和更高的数据安全性. 在本章,我们将讨论以下主题: • 配置异步复制 • 理解流 • 合并流和归档 • 管理时间线 在本章的最后,您将很容易地在几分钟内设置流复制. 4.1 设置流复制 在前面章节中,我们已经从简单的16MB XLOG文件做了恢复.从逻辑上讲,重放进程一次只能重放16MB.这在您的复制设

ubuntu编译安装postgresql及主从配置

操作系统版本    Ubuntu 14.04 数据库软件版本     postgresql-9.4.7.tar.gz 安装方式    编译安装 软件安装目录    /usr/local/pgsql 数据存放目录    /service/postgresql/data 日志存放目录    /service/postgresql/archive 1.安装依赖包 sudo apt-get update sudo apt-get install  libreadline6-dev sudo apt-ge

Postgresql 配置文件详解

如果要查看配置文件中的一些选项,则可以登录psql后 使用 命令来查看: show  选项名; show all:  #查看所有数据库参数的值 主要选项: 选项 默认值 说明 是否优化 原因 max_connections 100 允许客户端的最大并发连接数目 否 因为在测试的过程中,100个连接已经足够 fsync on 强制把数据同步更新到磁盘 是 因为系统的IO压力很大,为了更好的测试其他配置的影响,把改参数改为off shared_buffers 24MB 决定有多少内存可以被Postg

PostgreSQL+pgpooll+Keepalived双机HA方案

(注:本文档中的所有操作在没有说明的情况下都应该使用postgres用户进行操作,postgres用户没有权限的操作方可采用root用户进行操作,可以在本方案中使用postgres用户及该用户的密码替换replcia及replica用户的密码) 方案逻辑图 192.168.100.3的pgpool故障图 192.168.100.4的pgpool故障图 192.168.100.3的Master故障图 一.环境信息 操作系统:CentOS6.6 数据库:PostgreSQL9.3.5 物理机两台:n

postgresql集群方案参考答案

PostgreSQL配置Streaming Replication集群 http://www.cnblogs.com/marsprj/archive/2013/03/04/2943373.html postgresql集群方案hot standby初级测试(三)——蛋疼测试——手动同步数据 http://blog.csdn.net/lengzijian/article/details/7736961