首页/新闻资讯/正文详情

PostgreSQL Vacuum Analyze vs VACUUM FULL: Locks and Stats

发布时间:2026/11/4 7:17:31 来源:云帆数科 栏目:资讯中心
PostgreSQL Vacuum Analyze vs VACUUM FULL: Locks and Stats
Four commands, four different jobs. Measured on PostgreSQL 17.10:VACUUM t;— marks dead row versions reusableinsidethe table. Takes aSHARE UPDATE EXCLUSIVElock, so reads and writes keep working. Doesnotcollect planner statistics.ANALYZE t;— collects the column statistics the query planner uses. Reclaims nothing. Same lock.VACUUM ANALYZE t;— both in one pass, and the one to run by hand.VACUUM FULL t;— rewrites the table into a new file and returns space to the OS. Takes anACCESS EXCLUSIVElock, so nothing can touch the table while it runs, and needs free disk space roughly the size of the table.If you remember one thing: on a default install you probably need none of them. Autovacuum is on out of the box and, in the test below, cleaned a scratch table 37 seconds after it went dirty, unprompted.Table of ContentsVACUUM reuses space; VACUUM FULL returns itThe locks, demonstrated rather than assertedStatistics, permissions and parallel vacuumAutovacuum already does thisVACUUM reuses space; VACUUM FULL returns itA 300,000-row table with every second row deleted, so the dead tuples are spread through the file and cannot simply be chopped off the end: after deleting every 2nd row (before any VACUUM) heap | total | filenode | n_live_tup | n_dead_tup ------------------------------------------------ 69 MB | 75 MB | 16398 | 150000 | 150000 (1 row) after plain VACUUM VACUUM heap | total | filenode | n_dead_tup ------------------------------------ 69 MB | 75 MB | 16398 | 0 (1 row) after VACUUM FULL VACUUM heap | total | filenode | n_dead_tup ------------------------------------ 34 MB | 38 MB | 16405 | 0 (1 row)PlainVACUUMcleared all 150,000 dead tuples and the file stayed at 69 MB. That space is a free list, not waste. On a separate table, inserting 300,000 fresh rows after a vacuum consumed the reclaimed space and the file did not grow at all: 138 MB before, 138 MB after, 600,000 rows.Note thefilenodecolumn.VACUUMleaves it alone;VACUUM FULLchanges it (16398 → 16405), because it is a rewrite. That is the mechanism behind the disk-space warning: the PostgreSQL 17 manual says VACUUM FULL “requires extra disk space, since it writes a new copy of the table and doesn’t release the old copy until the operation is complete.” Both copies exist at once, so a table filling 60% of its volume cannot beVACUUM FULLed on that volume.One nuance the usual advice skips: plainVACUUMcanshrink the file when the dead space sits at the end. Deleting the top half of a 300,000-row table and vacuuming took it from 69 MB to 34 MB — trailing empty pages get truncated.The locks, demonstrated rather than assertedWith one session holding an open transaction that has read the table (anAccessShareLock), a second session tried each command with a 3-secondlock_timeout: plain VACUUM while a reader holds ACCESS SHARE VACUUM VACUUM FULL while a reader holds ACCESS SHARE ERROR: canceling statement due to lock timeout ANALYZE while a reader holds ACCESS SHARE ANALYZESamplingpg_locksfrom a third session while each ran on a 489 MB table confirms the modes:AccessExclusiveLockforVACUUM FULL,ShareUpdateExclusiveLockfor plainVACUUMand forANALYZE. One idle-in-transaction session is enough to stallVACUUM FULLindefinitely, which is why it belongs in a maintenance window and plainVACUUMdoes not.Statistics, permissions and parallel vacuumVACUUM does not collect planner statistics; that is ANALYZE’s job.On a fresh 100,000-row table, a bareVACUUMleftpg_statscompletely empty andlast_analyzeNULL;VACUUM ANALYZEthen populated it: after bare VACUUM (no ANALYZE) VACUUM reltuples | relpages --------------------- 100000 | 443 (1 row) rows_in_pg_stats ------------------ 0 (1 row) after VACUUM ANALYZE VACUUM rows_in_pg_stats ------------------ 2 (1 row) attname | n_distinct | most_common_vals --------------------------------------------- grp | 7 | {g3,g1,g0,g4,g5,g6,g2} (1 row)VACUUM does refreshpg_class.reltuplesandrelpages(−1 → 100000), which the planner reads. It does not build the value distributions that drive join and index choices. RunningVACUUMand expecting better plans will not work.Since PostgreSQL 16, you do not need to be a superuser or the owner of the database to vacuum a table.A role that is neither superuser nor database owner was refused, then granted only theMAINTAINprivilege on one table and succeeded:--- as hw_vac_user, VACUUM without MAINTAIN: WARNING: permission denied to vacuum hw_vac_stats, skipping it VACUUM --- grant MAINTAIN, retry: GRANT VACUUM --- did it actually vacuum? vacuum_count before3 after4Worth knowing that the refusal is awarning, not an error —VACUUMexits 0 having silently skipped the table. A cron job vacuuming as an under-privileged role looks like it is working.Parallel vacuum is not configured with theparallel_workerstable setting.On a table with three indexes the worker count was identical whether that parameter was unset or set to 4 —launched 2 parallel vacuum workers for index vacuuming (planned: 2)either way. It tracked the index count, and happened unasked. Parallelism covers the index vacuum phases only, needs at least two indexes, and is bounded bymax_parallel_maintenance_workersand the command’s ownPARALLEL noption. It is also unavailable forVACUUM FULL: VACUUM (FULL, PARALLEL 2) ERROR: VACUUM FULL cannot be performed in parallelAutovacuum already does thisThe shipped defaults on 17.10, read frompg_settingsrather than recalled:autovacuum on,autovacuum_vacuum_threshold 50,autovacuum_vacuum_scale_factor 0.2,autovacuum_analyze_threshold 50,autovacuum_analyze_scale_factor 0.1,autovacuum_naptime 60,autovacuum_max_workers 3. A table declared withWITH (autovacuum_vacuum_scale_factor 0, autovacuum_vacuum_threshold 50), dirtied with a 5,000-row UPDATE and then left alone:autovacuum fired after 37s: 2026-08-10 13:10:11.412763-05 last_autovacuum | autovacuum_count | n_dead_tup ------------------------------------------------------------- 2026-08-10 13:10:11.412763-05 | 1 | 0Two repeat runs fired at 73 and 88 seconds; the delay varies with the 60-second naptime.Which command reclaims space, which one a reader blocks, who may runVACUUMand when autovacuum fires are all asserted by optimize-tables-vacuum-analyze.sh against a PostgreSQL server it sets up on port 55511 and tears down when it finishes, connecting to nothing else.ManualVACUUMearns its place after a bulk load or a mass delete, when you do not want to wait a naptime for the cleanup and statistics to catch up. Routine scheduledVACUUM FULLis close to always wrong: it blocks the application, and if autovacuum is keeping up there is nothing for it to reclaim. When the bloat is in an index rather than the table, REINDEX CONCURRENTLY rebuilds it without the exclusive lock — a narrower tool for the narrower problem.If you are checking whether itiskeeping up, note thatVACUUM FULLdoes not updatelast_vacuumorvacuum_count— confirmed again here, both unchanged across a full rewrite. Finding when vacuum and analyze last ran covers that view and its other traps.

相关推荐

周一上线 | DeepSeek 开源 Super-Agent 架构;谷歌双发新模型重登榜一?Anthropic 递交上市文件,最大 IPO 来了?
周一上线 | DeepSeek 开源 Super-Agent 架构;谷歌双发新模型重登榜一?Anthropic 递交上市文件,最大 IPO 来了?

这期的「周一上线」,AI 的工作地点突然变多了,Agent 在 3D 办公室接活,在《我的世界》排队领配给;一颗在轨 TPU 还写了首俳句。 AI 能做的事也更具体了,GPT-6.1 Terra 学着看图、操作电脑,语音模型开始边听… · 2026/11/4 7:16:31

同时开好几个 Claude Code、Codex 会话,怎么看清它们各自在干什么?Kiteline 数据流大屏使用指南
同时开好几个 Claude Code、Codex 会话,怎么看清它们各自在干什么?Kiteline 数据流大屏使用指南

Kiteline(风筝线)是一款管理 AI 编程助手的工具:Claude Code、Codex 等 Agent 在电脑上干活,你在 iPhone、iPad 或 Apple Watch 上看进度、回消息、批权限、收测试报告。 同时开着好几个编程 Agent 时,常见的问题是&am… · 2026/11/4 7:16:31

工业智能体如何用好“老师傅经验”?从工业RAG到设备运维闭环
工业智能体如何用好“老师傅经验”?从工业RAG到设备运维闭环

设备出现报警后,工程师往往需要先查设备手册,再翻维修工单,联系熟悉这台设备的同事,最后结合运行参数判断问题。资料并不少,但完成一次有效排查,依然要跨越多个系统和知识断点。这也是工业智能体落地时需要… · 2026/11/4 7:16:31

ZCode:面向TypeScript工程师的本地化开发加速器
ZCode:面向TypeScript工程师的本地化开发加速器

1. ZCode 是什么?一个被误读但值得深挖的 TypeScript 开发加速器最近刷技术社区,ZCode 这个词突然密集出现——不是因为某家大厂发布了新 IDE,也不是某个明星开源项目破了 Star 数纪录,而是它像一滴墨汁掉进清水里,迅速… · 2026/11/4 10:06:03

Lodop 6.057 在 Chrome 120+ 环境下的兼容安装与打印实践
Lodop 6.057 在 Chrome 120+ 环境下的兼容安装与打印实践

简介:这是一份面向Web前端开发者与企业级打印功能集成工程师的Lodop 6.057浏览器通用打印控件资源包,专为解决跨浏览器(Chrome、Firefox、IE)环境下复杂报表、标签及自定义页面的本地打印需求而设计。资源包含50个文件&#xff0c… · 2026/11/4 10:06:03

Logisim数字逻辑电路仿真软件安装与入门指南
Logisim数字逻辑电路仿真软件安装与入门指南

数字逻辑电路这门课,很多人第一次接触时都会被“画电路图”这件事卡住。课本上的门电路、触发器、寄存器画起来倒还好,一旦要做稍大一点的实验,比如设计一个简易CPU或者实现一个状态机,手绘就完全不够用了。这时候就需要一款能仿真… · 2026/11/4 10:06:03

Lodop 6.057 打印方案:绕过浏览器限制的工业级精确打印实现
Lodop 6.057 打印方案:绕过浏览器限制的工业级精确打印实现

简介:这是一份面向Web前端开发者与企业级打印功能集成工程师的Lodop 6.057浏览器通用打印控件资源包,专为解决跨浏览器(Chrome、Firefox、IE)环境下复杂报表、标签及自定义页面的本地打印需求而设计。资源包含50个文件&#xff0c… · 2026/11/4 10:06:03

智能客服语音识别优化:从声学模型到系统级实践
智能客服语音识别优化:从声学模型到系统级实践

1. 语音识别技术现状与挑战当前智能客服系统中,语音识别模块的准确率直接影响用户体验和业务转化率。根据行业实测数据,在电话客服场景下,当语音识别错误率超过15%时,用户满意度会显著下降40%以上。而现实情况是,在复杂… · 2026/11/4 10:06:03

AI驱动产品需求评估:RICE与KANO模型智能化改造
AI驱动产品需求评估:RICE与KANO模型智能化改造

1. 项目概述:当产品需求遇上AI决策引擎在数字化产品迭代过程中,需求优先级排序永远是产品经理最头疼的难题。传统RICE(Reach, Impact, Confidence, Effort)和KANO模型虽然提供了理论框架,但在实际应用中常面临三大痛点… · 2026/11/4 10:05:02

双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试
双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/4 3:16:49

STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析
STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/4 6:59:29

DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案
DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/4 7:02:38

AI辅助技术栈评估:提升研发决策效率60%
AI辅助技术栈评估:提升研发决策效率60%

1. 项目背景与核心价值技术决策一直是研发团队面临的重大挑战。随着技术生态的快速演进,技术栈的选择、维护和迭代变得愈发复杂。我们团队在过去两年里,先后经历了三次重大技术栈迁移,每次都需要投入大量人力进行技术评估和风险分析。直到去年… · 2026/11/4 0:00:17

从生成Token到直接决策:Jev如何重塑AI Agent工程实践
从生成Token到直接决策:Jev如何重塑AI Agent工程实践

1. 从"生成文字"到"拍板决策":Jev 到底在解决什么问题大多数人第一次接触大模型,脑子里建立的模型是"输入问题、输出文字"。你问它今天天气怎么样,它给你一段描述;你让它写个周报,它给你… · 2026/11/4 0:00:17

32G统一内存跑大模型:内存账、TPS实测与端云分流
32G统一内存跑大模型:内存账、TPS实测与端云分流

想在一台 32G 统一内存的小主机上把大模型跑成"日常可用",这件事我前后折腾了小半年。标题里说的这台 Mac mini M6,是我们这次讨论的目标平台,具体到手规格以实物为准;但下面这套推算方法和实测思路,对任何统… · 2026/11/4 0:00:17

双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试
双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/4 3:16:49

STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析
STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/4 6:59:29

DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案
DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/4 7:02:38

了解更多?预约专属演示

我们的顾问将为您一对一讲解产品与方案

企业微信二维码