postgresql replication slots 的一些个人理解

本文介绍了PostgreSQL中复制槽(replicationslots)的概念及其配置方法。复制槽从PostgreSQL 9.4版本开始引入,用于确保主节点在所有备机接收到WAL段前不移除它们,防止恢复冲突。文章详细解释了如何在CentOS 7.4环境下配置复制槽,包括创建、使用及删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

os: centos 7.4
postgresql: 9.6.9

replication slots 是从postgresql 9.4 引入的, 主要是提供了一种自动化的方法来确保主控机在所有的后备机收到 WAL 段 之前不会移除它们,并且主控机也不会移除可能导致恢复冲突的行,即使后备机断开也是如此。

在没有启用 replication slots 的环境中,如果碰到 ERROR: requested WAL segment xxxx has already been removed 的错误,解决办法是要么提前开启了归档,要么重做slave,另外还需要在master上设置 wal_keep_segments 为更大的值。

需要注意的是流复制并不会默认开启 replication slots, 需要手工配置。

创建复制槽

每个复制槽都有一个名字,名字可以包含小写字母、数字和下划线字符。

master设置几个参数

max_replication_slots = 10

hot_standby = on
wal_level = replica 或者 logical
hot_standby_feedback = on
max_wal_senders = 10

重启postgresql后,创建 replication slots

postgres=# SELECT * FROM pg_create_physical_replication_slot('pg96_102');
postgres=# SELECT * FROM pg_create_physical_replication_slot('pg96_103');

postgres=# select * from pg_replication_slots;
 slot_name | plugin | slot_type | datoid | database | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn 
-----------+--------+-----------+--------+----------+--------+------------+------+--------------+-------------+---------------------
 pg96_102  |        | physical  |        |          | t      |       1675 |      |              | 0/8000140   | 
 pg96_103  |        | physical  |        |          | t      |       1787 |      |              | 0/8000140   | 
(2 rows)

要配置slave使用这个槽,在后备机的recovery.conf中应该配置 primary_slot_name,如下:

$ vi $PGDATA/recovery.conf

primary_slot_name = 'pg96_101'
standby_mode = 'on'
recovery_target_timeline = 'latest'
primary_conninfo = 'user=replicator password=1qaz2wsx host=192.168.56.101 port=5432 application_name=pg96_103'
trigger_file = '/tmp/postgresql.trigger.5432'

查看复制情况

postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 1675
usesysid         | 16384
usename          | replicator
application_name | pg96_102
client_addr      | 192.168.56.102
client_hostname  | 
client_port      | 35474
backend_start    | 2018-07-25 17:43:44.652145+08
backend_xmin     | 
state            | streaming
sent_location    | 0/8000060
write_location   | 0/8000060
flush_location   | 0/8000060
replay_location  | 0/8000060
sync_priority    | 1
sync_state       | sync
-[ RECORD 2 ]----+------------------------------
pid              | 1787
usesysid         | 16384
usename          | replicator
application_name | pg96_103
client_addr      | 192.168.56.103
client_hostname  | 
client_port      | 49790
backend_start    | 2018-07-25 17:47:07.663301+08
backend_xmin     | 
state            | streaming
sent_location    | 0/8000060
write_location   | 0/8000060
flush_location   | 0/8000060
replay_location  | 0/8000060
sync_priority    | 1
sync_state       | potential

删除复制槽

slave在使用 primary_slot_name 参数时是无法删除 replication slots

postgres=# SELECT * FROM pg_drop_replication_slot('pg96_102');
postgres=# SELECT * FROM pg_drop_replication_slot('pg96_103');

参考:
http://postgres.cn/docs/10/warm-standby.html#STREAMING-REPLICATION-SLOTS
http://postgres.cn/docs/10/view-pg-replication-slots.html

### PostgreSQL 连接槽被占满问题解决方案 当遇到 `FATAL: remaining connection slots are reserved for non-replication superuser connections` 错误时,这通常表明当前数据库实例中的可用连接槽数量已经耗尽。以下是详细的分析和解决方法: #### 1. 原因分析 此错误的核心原因是 PostgreSQL 数据库的连接数达到了配置文件中定义的最大值 (`max_connections`) 或者剩余的连接槽位已被保留给超级用户的非复制连接[^1]。 - **最大连接数限制**:如果应用程序或其他客户端创建了过多的活动或闲置连接,则可能导致其他新连接无法建立。 - **Idle 链接占用资源**:长时间处于 idle 状态的连接会持续占用连接槽,即使它们并未执行任何操作[^2]。 #### 2. 查询当前连接情况 可以通过以下 SQL 查询来查看当前数据库的连接状态以及哪些用户正在使用这些连接: ```sql SELECT pid, usename, application_name, client_addr, state, backend_start, query_start, now() - query_start AS duration FROM pg_stat_activity; ``` 上述查询可以帮助识别是否存在大量 idle 的连接,并定位具体的用户或应用名称。 #### 3. 调整 PostgreSQL 参数 调整参数可以增加允许的最大连接数量或将更多连接分配给普通用户而非仅限于超级用户。 ##### 修改 `postgresql.conf` 编辑 PostgreSQL 配置文件 `/var/lib/postgresql/data/postgresql.conf` 并修改以下参数: ```plaintext max_connections = 2000 -- 提高总的连接上限 superuser_reserved_connections = 50 -- 减少为超级用户预留的连接数 reserved_user_name = 'your_admin_user' -- 如果有特定管理员账户可指定 ``` 完成更改后重启 PostgreSQL 服务使改动生效[^3]: ```bash sudo systemctl restart postgresql ``` #### 4. 清理 Idle 连接 对于那些不再使用的 idle 连接可以直接终止以释放其占据的连接槽。通过下面命令实现强制断开选定进程 ID (PID): ```sql SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state='idle'; ``` 注意谨慎操作以免影响正常运行的应用程序。 #### 5. 使用连接池优化管理 长期来看,采用连接池技术能够有效减少频繁打开关闭短时间存在的临时连接所带来的压力。常见的 PostgreSQL 连接池工具包括 PgBouncer 和 Citus Pooler 。设置合适的 pool_mode 可进一步提升性能并降低直接访问带来的风险。 --- ### 总结 综上所述,针对 `remaining connection slots are reserved for non-replication superuser connections` 报错,应先诊断现有连接状况,清理不必要的 idle 连接;其次考虑适当调增全局最大连接数目或是重新规划超管与常规用户的比例关系;最后引入专业的连接池机制从根本上解决问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值