news 2026/4/23 9:50:47

pg数据库wal增长过快的处理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
pg数据库wal增长过快的处理

1.关闭归档模式:需重启pg

2.非活跃的复制槽会阻止WAL日志清理。检查复制槽状态:

如果发现active=false的复制槽且delay_size很大,说明该复制槽阻塞了WAL清理。根据业务需求决定是否删除:

处理过程如下:

[root@pg pg_wal]# du -sh *|wc -l
1569
[root@pg pg_wal]# pwd
/opt/pgsql/data/pg_wal
[root@pg pg_wal]# ll |head
total 25776640
-rw------- 1 postgres postgres 16777216 Dec 16 08:48 0000000100000A6E00000003
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000004
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000005
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000006
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000007
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000008
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000009
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E0000000A
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E0000000B
[root@pg pg_wal]# ls -lt|head
total 25891328
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A740000002E
drwx------ 2 postgres postgres 3764224 Dec 16 09:33 archive_status
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A740000002D
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A740000002C
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A740000002B
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A740000002A
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A7400000029
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A7400000028
-rw------- 1 postgres postgres 16777216 Dec 16 09:33 0000000100000A7400000027
[root@pg pg_wal]# ls -l|head
total 25989632
-rw------- 1 postgres postgres 16777216 Dec 16 08:48 0000000100000A6E00000003
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000004
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000005
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000006
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000007
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000008
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E00000009
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E0000000A
-rw------- 1 postgres postgres 16777216 Dec 16 08:49 0000000100000A6E0000000B
[root@pg pg_wal]# ls|wc -l
1593
[root@pg pg_wal]# ls|wc -l
1594
[root@pg pg_wal]# ls|wc -l
1595
[root@pg pg_wal]# ls|wc -l
1595
[root@pg pg_wal]# ls|wc -l
1595
[root@pg pg_wal]# ls|wc -l
1595
[root@pg pg_wal]# pwd
/opt/pgsql/data/pg_wal
[root@pg pg_wal]# ls|wc -l
1596
[root@pg pg_wal]# su - postgres
Last login: Tue Dec 16 09:31:10 CST 2025 on pts/1
-bash-4.2$ psql
Password for user postgres:
psql (12.2)
Type "help" for help.

postgres=# SELECT * FROM pg_stat_archiver;
archived_count | last_archived_wal | last_archived_time | failed_count | last_failed_wal | last_failed_time | stats_r
eset
----------------+--------------------------+-------------------------------+--------------+--------------------------+-------------------------------+-----------------
--------------
1599 | 0000000100000A7400000040 | 2025-12-16 09:35:34.808741+08 | 173 | 0000000100000A6E00000003 | 2025-12-16 08:50:01.139606+08 | 2025-12-16 08:48
:25.828041+08
(1 row)

postgres=# SELECT * FROM pg_stat_activity WHERE state = 'active' AND now() - xact_start > interval '1 hour';
datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change
| wait_event_type | wait_event | state | backend_xid | backend_xmin | query | backend_type
-------+---------+-----+----------+---------+------------------+-------------+-----------------+-------------+---------------+------------+-------------+--------------
+-----------------+------------+-------+-------------+--------------+-------+--------------
(0 rows)

postgres=#SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_insert_lsn(), restart_lsn)) AS delay_size
postgres-# FROM pg_replication_slots;

slot_name | active | delay_size
---------------+--------+------------
recovery_slot|f|1206GB
(1 row)


postgres=#SELECT pg_drop_replication_slot('recovery_slot');
pg_drop_replication_slot
--------------------------

(1 row)

postgres=# SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_insert_lsn(), restart_lsn)) AS delay_size
FROM pg_replication_slots;
slot_name | active | delay_size
-----------+--------+------------
(0 rows)

postgres=# SELECT * FROM pg_stat_archiver;
archived_count | last_archived_wal | last_archived_time | failed_count | last_failed_wal | last_failed_time | stats_r
eset
----------------+--------------------------+-------------------------------+--------------+--------------------------+-------------------------------+-----------------
--------------
1634 | 0000000100000A7400000063 | 2025-12-16 09:38:31.708956+08 | 173 | 0000000100000A6E00000003 | 2025-12-16 08:50:01.139606+08 | 2025-12-16 08:48
:25.828041+08
(1 row)

postgres=# SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_insert_lsn(), restart_lsn)) AS delay_size
FROM pg_replication_slots;
slot_name | active | delay_size
-----------+--------+------------
(0 rows)

postgres=# \q
-bash-4.2$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 16G 0 16G 0% /dev
tmpfs tmpfs 16G 5.5M 16G 1% /dev/shm
tmpfs tmpfs 16G 1.6G 15G 11% /run
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 1.2T 984G 231G 82% /
/dev/sda1 xfs 1014M 150M 865M 15% /boot
tmpfs tmpfs 3.2G 0 3.2G 0% /run/user/1000
-bash-4.2$ psql
Password for user postgres:
psql (12.2)
Type "help" for help.

postgres=# SELECT pg_size_pretty(SUM(size)) AS total_wal_size FROM pg_ls_waldir();
total_wal_size
----------------
26 GB
(1 row)

postgres=# SELECT pg_size_pretty(SUM(size)) AS total_wal_size FROM pg_ls_waldir();
total_wal_size
----------------
26 GB
(1 row)

postgres=# SELECT name, setting FROM pg_settings WHERE name = 'archive_mode';
name | setting
--------------+---------
archive_mode | on
(1 row)

postgres=# SELECT name, setting FROM pg_settings WHERE name like 'archive%';
name | setting
-------------------------+----------------------------------------------------------------
archive_cleanup_command |
archive_command | test ! -f /opt/pgsql/archive/%f && cp %p /opt/pgsql/archive/%f
archive_mode | on
archive_timeout | 0
(4 rows)

postgres=# alter system set archive_command='';
ALTER SYSTEM
postgres=# alter system set archive_mode=OFF;
ALTER SYSTEM
postgres=# SELECT name, setting FROM pg_settings WHERE name like 'archive%';
name | setting
-------------------------+----------------------------------------------------------------
archive_cleanup_command |
archive_command | test ! -f /opt/pgsql/archive/%f && cp %p /opt/pgsql/archive/%f
archive_mode | on
archive_timeout | 0
(4 rows)

postgres=# \q
-bash-4.2$ cd /opt/pgsql/data/
-bash-4.2$ cp postgresql.conf postgresql.conf.20251216
-bash-4.2$ grep archive postgresql.conf
archive_mode = on # enables archiving; off, on, or always
archive_command = 'test ! -f /opt/pgsql/archive/%f && cp %p /opt/pgsql/archive/%f' # command to use to archive a logfile segment
# placeholders: %p = path of file to archive
# e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
#archive_timeout = 0 # force a logfile segment switch after this
#restore_command = '' # command to use to restore an archived logfile segment
# e.g. 'cp /mnt/server/archivedir/%f %p'
#archive_cleanup_command = '' # command to execute at every restartpoint
#max_standby_archive_delay = 30s # max delay before canceling queries
# when reading WAL from archive;
-bash-4.2$ ll /opt/pgsql/archive
total 32768
-rw------- 1 postgres postgres 16777216 Dec 16 09:40 0000000100000A7400000070
-rw------- 1 postgres postgres 16777216 Dec 16 09:43 0000000100000A7400000071
-bash-4.2$ vi postgresql.conf
wal_level = logical # minimal, replica, or logical
# (change requires restart)
#fsync = on # flush data to disk for crash safety
# (turning this off can cause
# unrecoverable data corruption)
#synchronous_commit = on # synchronization level;
# off, local, remote_write, remote_apply, or on
#wal_sync_method = fsync # the default is the first option
# supported by the operating system:
# open_datasync
# fdatasync (default on Linux)
# fsync
# fsync_writethrough
# open_sync
#full_page_writes = on # recover from partial page writes
#wal_compression = off # enable compression of full-page writes
#wal_log_hints = off # also do full page writes of non-critical updates
# (change requires restart)
#wal_init_zero = on # zero-fill new WAL files
#wal_recycle = on # recycle WAL files
#wal_buffers = -1 # min 32kB, -1 sets based on shared_buffers
# (change requires restart)
#wal_writer_delay = 200ms # 1-10000 milliseconds
#wal_writer_flush_after = 1MB # measured in pages, 0 disables

#commit_delay = 0 # range 0-100000, in microseconds
#commit_siblings = 5 # range 1-1000

# - Checkpoints -

#checkpoint_timeout = 5min # range 30s-1d
max_wal_size = 1GB
#min_wal_size = 80MB
#checkpoint_completion_target = 0.5 # checkpoint target duration, 0.0 - 1.0
#checkpoint_flush_after = 256kB # measured in pages, 0 disables
#checkpoint_warning = 30s # 0 disables

# - Archiving -

archive_mode = off # enables archiving; off, on, or always
archive_command = '' # (change requires restart)
#archive_command = 'test ! -f /opt/pgsql/archive/%f && cp %p /opt/pgsql/archive/%f' # command to use to archive a logfile segment
# placeholders: %p = path of file to archive
# %f = file name only
# e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
#archive_timeout = 0 # force a logfile segment switch after this
# number of seconds; 0 disables

# - Archive Recovery -

# These are only used in recovery mode.

#restore_command = '' # command to use to restore an archived logfile segment
# placeholders: %p = path of file to restore
# %f = file name only
# e.g. 'cp /mnt/server/archivedir/%f %p'
# (change requires restart)
#archive_cleanup_command = '' # command to execute at every restartpoint
#recovery_end_command = '' # command to execute at completion of recovery

# - Recovery Target -

# Set these only when performing a targeted recovery.
"postgresql.conf" 754L, 26874C written
-bash-4.2$ grep archive postgresql.conf
archive_mode = off # enables archiving; off, on, or always
archive_command = '' # (change requires restart)
#archive_command = 'test ! -f /opt/pgsql/archive/%f && cp %p /opt/pgsql/archive/%f' # command to use to archive a logfile segment
# placeholders: %p = path of file to archive
# e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
#archive_timeout = 0 # force a logfile segment switch after this
#restore_command = '' # command to use to restore an archived logfile segment
# e.g. 'cp /mnt/server/archivedir/%f %p'
#archive_cleanup_command = '' # command to execute at every restartpoint
#max_standby_archive_delay = 30s # max delay before canceling queries
# when reading WAL from archive;
-bash-4.2$ ll postgresql*
-rw------- 1 postgres postgres 221 Dec 16 09:44 postgresql.auto.conf
-rw------- 1 postgres postgres 26874 Dec 16 09:46 postgresql.conf
-rw------- 1 postgres postgres 26852 Dec 16 09:45 postgresql.conf.20251216
-bash-4.2$ more postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
max_connections = '1000'
search_path = '"$user", public, mt, zdmh, yjya, ghkt, tjgx, zxkt'
archive_command = ''
archive_mode = 'off'

-bash-4.2$ psql
Password for user postgres:
psql (12.2)
Type "help" for help.

postgres=# SHOW archive_command;
archive_command
----------------------------------------------------------------
test ! -f /opt/pgsql/archive/%f && cp %p /opt/pgsql/archive/%f
(1 row)

postgres=# SHOW archive_mode;
archive_mode
--------------
on
(1 row)

postgres=#

-bash-4.2$ pwd
/opt/pgsql/data
-bash-4.2$ du -sh pg_wal
1013M pg_wal
-bash-4.2$ psql
Password for user postgres:
psql (12.2)
Type "help" for help.

postgres=# SELECT pg_size_pretty(SUM(size)) AS total_wal_size FROM pg_ls_waldir();
total_wal_size
----------------
1008 MB
(1 row)

postgres=#

-bash-4.2$ ll pg_wal|wc -l
65

相关参考:

"/opt/pgsql/data/pg_w..."点击查看元宝的回答
https://yb.tencent.com/s/xl7VZQUqwXRd

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/21 17:29:14

ReadCat:极致纯净的开源小说阅读器,打造你的专属阅读空间

ReadCat:极致纯净的开源小说阅读器,打造你的专属阅读空间 【免费下载链接】read-cat 一款免费、开源、简洁、纯净、无广告的小说阅读器 项目地址: https://gitcode.com/gh_mirrors/re/read-cat 在信息爆炸的数字时代,寻找一款纯净无广…

作者头像 李华
网站建设 2026/4/22 18:44:19

PDF电路图AI解析实战:电子元件智能识别技术深度解析

PDF电路图AI解析实战:电子元件智能识别技术深度解析 【免费下载链接】PDF-Extract-Kit A Comprehensive Toolkit for High-Quality PDF Content Extraction 项目地址: https://gitcode.com/gh_mirrors/pd/PDF-Extract-Kit 在电子工程和电路设计领域&#xff…

作者头像 李华
网站建设 2026/4/13 0:35:44

phpstorm 2022.3 如何修改快捷键单行注释开始的位置

在PhpStorm 2022.3中,使用快捷键(如Ctrl/)进行单行注释时,注释默认会紧跟在代码行左侧。但可以通过调整代码样式设置来实现注释与代码内容之间保留一定间距。‌‌1具体操作路径为:进入‌File‌ > ‌Settings‌&…

作者头像 李华
网站建设 2026/4/19 19:43:32

FLUX.1 Kontext Dev:本地化AI图像生成解决方案深度解析

随着人工智能技术的快速发展,开源AI图像生成模型正成为技术社区的热点话题。2025年10月,Black Forest Labs正式开源了FLUX.1 Kontext Dev模型,这款拥有120亿参数的扩散变换器为开发者提供了完整的本地化图像生成能力。 【免费下载链接】FLUX.…

作者头像 李华
网站建设 2026/4/19 7:21:16

MNN框架多模型部署与智能流量分配技术指南

MNN框架多模型部署与智能流量分配技术指南 【免费下载链接】MNN MNN is a blazing fast, lightweight deep learning framework, battle-tested by business-critical use cases in Alibaba 项目地址: https://gitcode.com/GitHub_Trending/mn/MNN 作为阿里业务验证的深…

作者头像 李华
网站建设 2026/4/19 14:01:45

Vim高效光标移动方案:EasyMotion与Sneak插件深度对比

Vim高效光标移动方案:EasyMotion与Sneak插件深度对比 【免费下载链接】vim-galore :mortar_board: All things Vim! 项目地址: https://gitcode.com/gh_mirrors/vi/vim-galore 你是否曾因在长代码文件中频繁使用方向键而打断编程思路?是否在调试时…

作者头像 李华