PostgreSQL自带的命令行工具19- vacuumdb
基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777
vacuumdb
是 PostgreSQL 中的一个命令行工具,用于清理数据库,释放未使用的空间,并优化数据库性能。这个工具对应 SQL 命令中的 VACUUM
,用于回收已经删除或者更新了的行所占用的空间,并可能使这些空间可供将来的操作重新使用。此外,VACUUM
命令也有助于更新数据库表和索引上的统计信息,这对于查询优化器选择最佳执行计划是至关重要的。
通过help查看帮助文档。
[pg16@test ~]$ vacuumdb --help
vacuumdb cleans and analyzes a PostgreSQL database.
Usage:
vacuumdb [OPTION]... [DBNAME]
Options:
-a, --all vacuum all databases
--buffer-usage-limit=SIZE size of ring buffer used for vacuum
-d, --dbname=DBNAME database to vacuum
--disable-page-skipping disable all page-skipping behavior
-e, --echo show the commands being sent to the server
-f, --full do full vacuuming
-F, --freeze freeze row transaction information
--force-index-cleanup always remove index entries that point to dead tuples
-j, --jobs=NUM use this many concurrent connections to vacuum
--min-mxid-age=MXID_AGE minimum multixact ID age of tables to vacuum
--min-xid-age=XID_AGE minimum transaction ID age of tables to vacuum
--no-index-cleanup don't remove index entries that point to dead tuples
--no-process-main skip the main relation
--no-process-toast skip the TOAST table associated with the table to vacuum
--no-truncate don't truncate empty pages at the end of the table
-n, --schema=SCHEMA vacuum tables in the specified schema(s) only
-N, --exclude-schema=SCHEMA do not vacuum tables in the specified schema(s)
-P, --parallel=PARALLEL_WORKERS use this many background workers for vacuum, if available
-q, --quiet don't write any messages
--skip-locked skip relations that cannot be immediately locked
-t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only
-v, --verbose write a lot of output
-V, --version output version information, then exit
-z, --analyze update optimizer statistics
-Z, --analyze-only only update optimizer statistics; no vacuum
--analyze-in-stages only update optimizer statistics, in multiple
stages for faster results; no vacuum
-?, --help show this help, then exit
Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-w, --no-password never prompt for password
-W, --password force password prompt
--maintenance-db=DBNAME alternate maintenance database
Read the description of the SQL command VACUUM for details.
Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
如何使用 vacuumdb
vacuumdb
提供了很多选项,允许你对整个数据库、特定表、甚至是 PostgreSQL 实例中的所有数据库进行清理操作。
基本命令结构如下:
vacuumdb [选项]... [数据库名]
常用选项
-a
或--all
:清理所有的数据库。-d
<数据库名>
或--dbname=<数据库名>
:指定要清理的数据库。-t
<表名>
或--table=<表名>
:指定要清理的表,可与-d
一同使用。-v
或--verbose
:显示详细的操作信息。-z
或--analyze
:在清理完成后对数据库进行分析操作,更新统计信息。-j
<并发数>
或--jobs=<并发数>
:用于并行执行清理作业,适用于 PostgreSQL 9.6 及以上版本。-h
<主机>
或--host=<主机>
:指定 PostgreSQL 服务器的主机。-p
<端口>
或--port=<端口>
:指定 PostgreSQL 服务器的端口。-U
<用户名>
或--username=<用户名>
:指定用于连接数据库的用户名。-w
或--no-password
:在连接数据库时不提示输入密码。--analyze-only
:只执行分析操作,不做清理。--full
:执行一个全面的清理,这是一个阻塞操作,可能会对数据库性能有较大影响。
示例1
清理特定的数据库,如white
[pg16@test ~]$ vacuumdb -d white -v
示例2
清理并分析所有数据库
[pg16@test ~]$ vacuumdb -a -z -v
示例3
并行清理并分析所有数据库,如开4个并行
[pg16@test ~]$ vacuumdb -a -z -j 4 -v
示例4
只分析特定的数据库,如分析white
[pg16@test ~]$ vacuumdb --analyze-only -d white -v
注意事项
- 在执行
VACUUM
(或使用vacuumdb
工具)时,它通常不会锁定表,因此对数据库的正常操作影响较小。但是,VACUUM FULL
会重写表来回收空间,这将锁定被清理的表。 - 定期执行
VACUUM
(特别是带有--analyze
选项的)对于保持 PostgreSQL 数据库性能是很重要的。 - 对于大型生产数据库,可能需要考虑使用并行选项 (
-j
) 来加速VACUUM
过程,同时避免在高峰时段运行可能产生性能影响的操作。
在多数情况下,PostgreSQL 的自动清理进程(Autovacuum)会处理好大部分需要清理的情况,但在某些特定场景或者是在维护窗口期间,手动运行 vacuumdb
可以帮助确保数据库的健康和性能。
谨记:心存敬畏,行有所止。