postgres删除表数据后,并没有释放磁盘空间, 可以用VACUUM 来释放磁盘


VACUUM - 垃圾收集并可选地分析数据库

话不多说, 如果不想往下看,选择数据库后, 执行:

VACUUM full table_name;

概要

VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE | DISABLE_PAGE_SKIPPING } [, ...] ) ] [ table_name [ (column_name [, ...] ) ] ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table_name ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table_name [ (column_name [, ...] ) ] ]

描述

VACUUM收回死元组占用的存储空间。在正常的PostgreSQL操作中,更新删除或废弃的元组不会从其表中物理删除; 它们一直存在直到VACUUM完成。因此,有必要VACUUM定期进行,特别是在频繁更新的表上。

如果没有参数,则VACUUM处理当前用户有权抽真空的当前数据库中的每个表。使用参数,VACUUM仅处理该表。

VACUUM ANALYZE为每个选定的表执行a VACUUM然后执行a ANALYZE这是日常维护脚本的便捷组合形式。有关其处理的更多详细信息,请参见ANALYZE

普通VACUUM(不FULL)简单地回收空间并使其可以重复使用。这种形式的命令可以与表的正常读取和写入并行操作,因为不能获得排他锁。但是,额外的空间不会返回给操作系统(在大多数情况下); 它只是在同一个表中可以重复使用。VACUUM FULL将表的全部内容重写为没有额外空间的新磁盘文件,允许将未使用的空间返回给操作系统。此表单速度要慢得多,并且在处理每个表时需要对其进行独占锁定。

当选项列表被括号括起时,可以按任何顺序写入选项。如果没有括号,则必须按照上面显示的顺序指定选项。PostgreSQL 9.0 中添加了带括号的语法不建议使用的语法已弃用。

参数

FULL

选择“ 完全 ”真空,可以回收更多空间,但需要更长时间并专门锁定桌子。此方法还需要额外的磁盘空间,因为它会写入表的新副本,并且在操作完成之前不会释放旧副本。通常,只有在需要从表格中回收大量空间时才应使用此选项。

FREEZE

选择积极的“ 冻结 ”元组。指定FREEZE等同于VACUUMvacuum_freeze_min_agevacuum_freeze_table_age参数设置为零执行。在重写表时始终执行积极冻结,因此在FULL指定时此选项是多余的

VERBOSE

为每个表打印详细的真空活动报告。

ANALYZE

更新计划程序用于确定执行查询的最有效方法的统计信息。

DISABLE_PAGE_SKIPPING

通常,VACUUM将根据可见性图跳过页面可以跳过已知冻结所有元组的页面,并且可以跳过已知所有元组都可见的元组的页面,除非执行激进的真空。此外,除了执行激进的真空以外,可以跳过某些页面以避免等待其他会话完成使用它们。此选项禁用所有页面跳过行为,并且仅用于可见性映射的内容被认为是可疑的,只有在存在导致数据库损坏的硬件或软件问题时才会发生这种情况。

table_name

真空的特定表的名称(可选择模式限定)。如果省略,则清除当前数据库中的所有常规表和物化视图。如果指定的表是分区表,则对其所有叶子分区进行清理。

column_name

要分析的特定列的名称。默认为所有列。如果指定了列列表,ANALYZE则暗示。

输出

VERBOSE指定时,VACUUM发出进度信息,表明其表当前正被处理。还打印有关表格的各种统计数据。

笔记

要真空吸尘桌子,通常必须是桌子的主人或超级用户。但是,允许数据库所有者清空其数据库中的所有表,共享目录除外。(共享目录的限制意味着真正的数据库范围VACUUM只能由超级用户执行。)VACUUM将跳过调用用户无权真空的任何表。

VACUUM 无法在事务块内执行。

对于具有GIN索引的VACUUM(以任何形式)还可以通过将挂起的索引条目移动到主GIN索引结构中的适当位置来完成任何挂起的索引插入有关详细信息,请参见第64.4.1节

我们建议经常对活动的生产数据库进行抽真空(至少每晚一次),以便删除死行。添加或删除大量行后,最好VACUUM ANALYZE为受影响的表发出命令。这将使用所有最近更改的结果更新系统目录,并允许PostgreSQL查询计划程序在规划查询时做出更好的选择。

FULL建议不要将该选项用于日常使用,但在特殊情况下可能有用。例如,当您删除或更新表中的大多数行并希望表实际缩小以占用更少的磁盘空间并允许更快的表扫描。VACUUM FULL通常会缩小表格而不是平原VACUUM

VACUUM导致I / O流量大幅增加,这可能导致其他活动会话的性能不佳。因此,有时建议使用基于成本的真空延迟功能。有关详细信息,请参见第19.4.4节

PostgreSQL包括一个“ autovacuum ”设施,可以自动化常规真空维护。有关自动和手动吸尘的更多信息,请参阅第24.1节

例子

要清理单个表onek,请为优化程序分析它并打印详细的真空活动报告:

VACUUM (VERBOSE, ANALYZE) onek;

兼容性

VACUUMSQL标准中没有声明。



原文:

VACUUM

VACUUM — garbage-collect and optionally analyze a database

Synopsis

VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE | DISABLE_PAGE_SKIPPING } [, ...] ) ] [ table_name [ (column_name [, ...] ) ] ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table_name ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table_name [ (column_name [, ...] ) ] ]

Description

VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a VACUUM is done. Therefore it's necessary to do VACUUM periodically, especially on frequently-updated tables.

With no parameter, VACUUM processes every table in the current database that the current user has permission to vacuum. With a parameter, VACUUM processes only that table.

VACUUM ANALYZE performs a VACUUM and then an ANALYZE for each selected table. This is a handy combination form for routine maintenance scripts. See ANALYZE for more details about its processing.

Plain VACUUM (without FULL) simply reclaims space and makes it available for re-use. This form of the command can operate in parallel with normal reading and writing of the table, as an exclusive lock is not obtained. However, extra space is not returned to the operating system (in most cases); it's just kept available for re-use within the same table. VACUUM FULL rewrites the entire contents of the table into a new disk file with no extra space, allowing unused space to be returned to the operating system. This form is much slower and requires an exclusive lock on each table while it is being processed.

When the option list is surrounded by parentheses, the options can be written in any order. Without parentheses, options must be specified in exactly the order shown above. The parenthesized syntax was added in PostgreSQL 9.0; the unparenthesized syntax is deprecated.

Parameters

FULL

Selects “full” vacuum, which can reclaim more space, but takes much longer and exclusively locks the table. This method also 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. Usually this should only be used when a significant amount of space needs to be reclaimed from within the table.

FREEZE

Selects aggressive “freezing” of tuples. Specifying FREEZE is equivalent to performing VACUUM with the vacuum_freeze_min_age and vacuum_freeze_table_age parameters set to zero. Aggressive freezing is always performed when the table is rewritten, so this option is redundant when FULL is specified.

VERBOSE

Prints a detailed vacuum activity report for each table.

ANALYZE

Updates statistics used by the planner to determine the most efficient way to execute a query.

DISABLE_PAGE_SKIPPING

Normally, VACUUM will skip pages based on the visibility map. Pages where all tuples are known to be frozen can always be skipped, and those where all tuples are known to be visible to all transactions may be skipped except when performing an aggressive vacuum. Furthermore, except when performing an aggressive vacuum, some pages may be skipped in order to avoid waiting for other sessions to finish using them. This option disables all page-skipping behavior, and is intended to be used only the contents of the visibility map are thought to be suspect, which should happen only if there is a hardware or software issue causing database corruption.

table_name

The name (optionally schema-qualified) of a specific table to vacuum. If omitted, all regular tables and materialized views in the current database are vacuumed. If the specified table is a partitioned table, all of its leaf partitions are vacuumed.

column_name

The name of a specific column to analyze. Defaults to all columns. If a column list is specified, ANALYZE is implied.

Outputs

When VERBOSE is specified, VACUUM emits progress messages to indicate which table is currently being processed. Various statistics about the tables are printed as well.

Notes

To vacuum a table, one must ordinarily be the table's owner or a superuser. However, database owners are allowed to vacuum all tables in their databases, except shared catalogs. (The restriction for shared catalogs means that a true database-wide VACUUM can only be performed by a superuser.) VACUUM will skip over any tables that the calling user does not have permission to vacuum.

VACUUM cannot be executed inside a transaction block.

For tables with GIN indexes, VACUUM (in any form) also completes any pending index insertions, by moving pending index entries to the appropriate places in the main GIN index structure. See Section 64.4.1 for details.

We recommend that active production databases be vacuumed frequently (at least nightly), in order to remove dead rows. After adding or deleting a large number of rows, it might be a good idea to issue a VACUUM ANALYZE command for the affected table. This will update the system catalogs with the results of all recent changes, and allow the PostgreSQLquery planner to make better choices in planning queries.

The FULL option is not recommended for routine use, but might be useful in special cases. An example is when you have deleted or updated most of the rows in a table and would like the table to physically shrink to occupy less disk space and allow faster table scans. VACUUM FULL will usually shrink the table more than a plain VACUUM would.

VACUUM causes a substantial increase in I/O traffic, which might cause poor performance for other active sessions. Therefore, it is sometimes advisable to use the cost-based vacuum delay feature. See Section 19.4.4 for details.

PostgreSQL includes an “autovacuum” facility which can automate routine vacuum maintenance. For more information about automatic and manual vacuuming, see Section 24.1.

Examples

To clean a single table onek, analyze it for the optimizer and print a detailed vacuum activity report:

VACUUM (VERBOSE, ANALYZE) onek;

Compatibility

There is no VACUUM statement in the SQL standard.

1条评论 顺序楼层
请先登录再回复