PostgreSQL attention : max_standby_archive_delay and max_standby_streaming_delay

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
简介:
今天群里的兄弟提到的一个疑问, 
max_standby_archive_delay = -1
max_standby_streaming_delay = -1
不知道有什么影响?
这两个参数控制了recovery时是否要kill掉hot_standby上与recovery冲突的sql.
如果配置为-1, 那么在hot_standby上可以无限时常的执行sql. 后果是standby只接收但是不recovery xlog. 造成standby xlog文件越来越多, 甚至撑爆磁盘分区. 还有就是造成了standby的延迟. 
所以在设置是需要小心.

为了便于理解, 下面将测试过程展示一下 : 
(primary and standby)安装PostgreSQL
tar -zxvf postgresql-1b1d3d9.tar.gz

cd postgresql-1b1d3d9
./configure --prefix=/home/pg94/pgsql9.4devel --with-pgport=2999 --with-perl --with-tcl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety --with-wal-blocksize=16 --enable-dtrace && gmake && gmake install
cd contrib/
gmake && gmake install

(primary)初始化数据库
su - pg94
initdb -D $PGDATA -E UTF8 --locale=C -W -U postgres


(primary)配置
pg94@db-172-16-3-33-> grep "^[a-z]" postgresql.conf
listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
port = 2999                             # (change requires restart)
max_connections = 100                   # (change requires restart)
superuser_reserved_connections = 13     # (change requires restart)
unix_socket_directories = '.'   # comma-separated list of directories
unix_socket_permissions = 0700          # begin with 0 to use octal notation
shared_buffers = 1024MB                 # min 128kB
maintenance_work_mem = 512MB            # min 1MB
shared_preload_libraries = 'pg_stat_statements'         # (change requires restart)
wal_level = hot_standby                 # minimal, archive, or hot_standby
synchronous_commit = off                # synchronization level;
wal_sync_method = fdatasync             # the default is the first option
wal_buffers = 16384kB                   # min 32kB, -1 sets based on shared_buffers
wal_writer_delay = 20ms         # 1-10000 milliseconds
checkpoint_segments = 32                # in logfile segments, min 1, 16MB each
max_wal_senders = 32            # max number of walsender processes
wal_keep_segments = 128                # in logfile segments, 16MB each; 0 disables
hot_standby = on                        # "on" allows queries during recovery
max_standby_archive_delay = -1  # max delay before canceling queries
max_standby_streaming_delay = -1        # max delay before canceling queries
wal_receiver_status_interval = 1s       # send replies at least this often
hot_standby_feedback = on               # send info from standby to prevent
random_page_cost = 1.0                  # same scale as above
effective_cache_size = 10240MB
log_destination = 'csvlog'              # Valid values are combinations of
logging_collector = on          # Enable capturing of stderr and csvlog
log_directory = 'pg_log'                # directory where log files are written,
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
log_file_mode = 0600                    # creation mode for log files,
log_truncate_on_rotation = on           # If on, an existing log file with the
log_rotation_age = 1d                   # Automatic rotation of logfiles will
log_rotation_size = 10MB                # Automatic rotation of logfiles will
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose           # terse, default, or verbose messages
log_lock_waits = on                     # log lock waits >= deadlock_timeout
log_statement = 'ddl'                   # none, ddl, mod, all
log_timezone = 'PRC'
track_activity_query_size = 2048        # (change requires restart)
autovacuum = on                 # Enable autovacuum subprocess?  'on'
log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'C'                       # locale for system error message
lc_monetary = 'C'                       # locale for monetary formatting
lc_numeric = 'C'                        # locale for number formatting
lc_time = 'C'                           # locale for time formatting
default_text_search_config = 'pg_catalog.english'
pg_stat_statements.max = 1000
pg_stat_statements.track = all


pg94@db-172-16-3-33-> grep "^[a-z]" pg_hba.conf 
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host all all 0.0.0.0/0 md5
host replication postgres 172.16.3.0/24 md5


pg94@db-172-16-3-33-> grep "^[a-z]" recovery.done 
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=172.16.3.39 port=2999 user=postgres keepalives_idle=60'                # e.g. 'host=localhost port=5432'


standby配置 : 
pg94@db-172-16-3-39-> vi ~/.pgpass 
172.16.3.33:2999:replication:postgres:postgres
pg94@db-172-16-3-39-> chmod 400 ~/.pgpass
-- 复制节点
pg94@db-172-16-3-39-> pg_basebackup -D $PGDATA -F p -P -v -U postgres -h 172.16.3.33
pg94@db-172-16-3-39-> cd $PGDATA
pg94@db-172-16-3-39-> mv recovery.done recovery.conf
pg94@db-172-16-3-39-> vi recovery.conf
primary_conninfo = 'host=172.16.3.33 port=2999 user=postgres keepalives_idle=60'


启动standby : 
pg94@db-172-16-3-39-> pg_ctl start


(primary)创建测试表 : 
pg94@db-172-16-3-33-> psql postgres postgres
psql (9.4devel)
Type "help" for help.
postgres=# create database digoal;
CREATE DATABASE
\c digoal 
digoal=# create table t1 (id int);
CREATE TABLE
digoal=# insert into t1 values (1);
INSERT 0 1


(standby)开启repeatable read查询
digoal=# begin transaction isolation level repeatable read;
BEGIN
digoal=# select * from t1;
 id 
----
  1
(1 row)
-- 不要退出事务.


(primary)删除t1表.
删除t1表后, 这部分xlog信息在standby上面做恢复时将和standby上面的事务冲突.
digoal=# drop table t1;
DROP TABLE

查看standby的replay_location, 这个指的是恢复点.
digoal=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 5436
usesysid         | 10
usename          | postgres
application_name | walreceiver
client_addr      | 172.16.3.39
client_hostname  | 
client_port      | 22422
backend_start    | 2013-08-15 12:36:18.844357+08
state            | streaming
sent_location    | 0/3012680
write_location   | 0/3012680
flush_location   | 0/3012680
replay_location  | 0/3012140
sync_priority    | 0
sync_state       | async


(primary)执行大量的写操作, 意为产生大量的pg_xlog.
digoal=# create table test(id int, info text, crt_time timestamp);
CREATE TABLE
digoal=# insert into test select generate_series(1,1000000),'test',now();
INSERT 0 1000000
digoal=# checkpoint;
CHECKPOINT

查询 standby的replay_location, 这个指的是恢复点. 
从write_location看出, 数据在发给standby, 但是standby的replay_location不变. 也就是说现在standby只接收xlog, 但是没有将接收到的xlog做recovery处理.
因为在standby上设置了如下参数 :
max_standby_archive_delay = -1  # max delay before canceling queries
max_standby_streaming_delay = -1        # max delay before canceling queries

这个查询和recovery冲突时, 不会被kill掉, recovery将持续等待.
digoal=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 5436
usesysid         | 10
usename          | postgres
application_name | walreceiver
client_addr      | 172.16.3.39
client_hostname  | 
client_port      | 22422
backend_start    | 2013-08-15 12:36:18.844357+08
state            | streaming
sent_location    | 0/8CA2BA0
write_location   | 0/8CA2BA0
flush_location   | 0/8CA2BA0
replay_location  | 0/3012140
sync_priority    | 0
sync_state       | async


(standby)开启另外一个会话, 查询是否存在test表.
显然不可能存在, 因为pg_xlog只接收, 却为recovery. 所以standby和primary延迟也随着时间越来越大.
pg94@db-172-16-3-39-> psql
psql (9.4devel)
Type "help" for help.
digoal=# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t1   | table | postgres
(1 row)

代码如下 : 
1. src/backend/storage/ipc/standby.c
/*
 * Determine the cutoff time at which we want to start canceling conflicting
 * transactions.  Returns zero (a time safely in the past) if we are willing
 * to wait forever.
 */
static TimestampTz
GetStandbyLimitTime(void)
{
        TimestampTz rtime;
        bool            fromStream;

        /*
         * The cutoff time is the last WAL data receipt time plus the appropriate
         * delay variable.  Delay of -1 means wait forever.
         */
        GetXLogReceiptTime(&rtime, &fromStream);
        if (fromStream)
        {
                if (max_standby_streaming_delay < 0)
                        return 0;                       /* wait forever */
                return TimestampTzPlusMilliseconds(rtime, max_standby_streaming_delay);
        }
        else
        {
                if (max_standby_archive_delay < 0)
                        return 0;                       /* wait forever */  // 小于0永久等待.
                return TimestampTzPlusMilliseconds(rtime, max_standby_archive_delay);
        }
}

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
存储 负载均衡 NoSQL
|
3月前
|
人工智能 JavaScript 开发工具
ModelGate 支持 Claude Code ,一键设置 AI 编程助手,开发效率极速提升!
ModelGate 新增支持 Claude Code,开发者可一键部署 AI 编程助手,大幅提升开发效率。通过简单几步即可安装配置,轻松使用 AI 编程工具,快速完成复杂任务,让高效智能编程触手可及。
|
8月前
|
SQL 人工智能 容灾
诚邀您参加《SQL Server 2025 AI 革新与云上容灾新探索》闭门活动!
诚邀您参加2月21日(周五)14:00-17:00在上海举行的《SQL Server 2025 AI革新,云上容灾新探索》闭门活动。线上、线下同步进行,免费报名并有机会获得精美礼品。活动将深入探讨SQL Server 2025的人工智能新功能及阿里云 RDS SQL Server 的容灾解决方案。
诚邀您参加《SQL Server 2025 AI 革新与云上容灾新探索》闭门活动!
可控细节的长文档摘要,探索开源LLM工具与实践
本文通过将文档分为几部分来解决这个问题,然后分段生成摘要。在对大语言模型进行多次查询后,可以重建完整的摘要。通过控制文本块的数量及其大小,我们最终可以控制输出中的细节级别。
|
12月前
|
NoSQL 安全 Redis
AWS迁移教程,Redis迁移到Elasticache
AWS迁移教程,Redis迁移到Elasticache
|
运维 监控 关系型数据库
【一文搞懂PGSQL】7. PostgreSQL + repmgr + witness 高可用架构
该文档介绍了如何构建基于PostgreSQL的高可用架构,利用repmgr进行集群管理和故障转移,并引入witness节点增强网络故障检测能力。repmgr是一款轻量级的开源工具,支持一键部署、自动故障转移及分布式节点管理。文档详细描述了环境搭建步骤,包括配置postgresql参数、安装与配置repmgr、注册集群节点以及配置witness节点等。此外,还提供了故障手动与自动切换的方法及常用命令,确保集群稳定运行。
|
关系型数据库 数据库 PostgreSQL
pg下delete数据后。除了使用VACUUM FULL TABLE 才能释放磁盘空间外的方法。
【8月更文挑战第12天】pg下delete数据后。除了使用VACUUM FULL TABLE 才能释放磁盘空间外的方法。
712 1
|
监控 安全 数据安全/隐私保护
【Docker专栏】Docker容器安全:防御与加固策略
【5月更文挑战第7天】本文探讨了Docker容器安全,指出容器化技术虽带来便利,但也存在安全隐患,如不安全的镜像、容器逃逸、网络配置不当等。建议采取使用官方镜像、镜像扫描、最小权限原则等防御措施,并通过安全的Dockerfile编写、运行时安全策略、定期更新和访问控制等加固容器安全。保持警惕并持续学习安全实践至关重要。
1075 7
【Docker专栏】Docker容器安全:防御与加固策略
|
安全 Windows
【项目问题解决】windows10 删除文件有完全控制权限 你需要权限来执行此操作
在Windows 10中遇到删除文件提示需管理员权限时,问题可能源于用户权限不足或文件夹权限设置。解决方法包括:右键文件→属性→安全→高级→更改所有者为管理员→保存设置→回到安全选项卡,选中管理员权限,勾选“完全控制”,确定保存。若仍无法删除,先尝试删除子文件,再删除文件夹。
1227 0
|
SQL 关系型数据库 MySQL
事务隔离大揭秘:MySQL中的四种隔离级别解析
事务隔离大揭秘:MySQL中的四种隔离级别解析
3221 0