PostgreSQL自带的命令行工具18- reindexdb
基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777
reindexdb
是一个 PostgreSQL 的命令行工具,用于重建数据库中的一个或多个索引。这个工具在处理索引膨胀或损坏时特别有用,可以帮助恢复索引的性能或修复索引相关的问题而无需手动重建每个索引。
通过help查看帮助文档。
[pg16@test ~]$ reindexdb --help
reindexdb reindexes a PostgreSQL database.
Usage:
reindexdb [OPTION]... [DBNAME]
Options:
-a, --all reindex all databases
--concurrently reindex concurrently
-d, --dbname=DBNAME database to reindex
-e, --echo show the commands being sent to the server
-i, --index=INDEX recreate specific index(es) only
-j, --jobs=NUM use this many concurrent connections to reindex
-q, --quiet don't write any messages
-s, --system reindex system catalogs only
-S, --schema=SCHEMA reindex specific schema(s) only
-t, --table=TABLE reindex specific table(s) only
--tablespace=TABLESPACE tablespace where indexes are rebuilt
-v, --verbose write a lot of output
-V, --version output version information, then exit
-?, --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 REINDEX for details.
Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
基本用法
reindexdb [选项]... [数据库]
在没有指定数据库名称时,默认会重建当前用户的默认数据库的索引。
常用选项
-d, --dbname=数据库名
:指定要重建索引的数据库。-a, --all
:重建所有数据库的索引。-i, --index=索引名
:只重建指定的索引而不是数据库中的所有索引。此选项可以多次使用来指定多个索引。-s, --system
:只重建数据库系统目录的索引而不包括用户创建的索引。-t, --table=表名
:只重建指定表的索引。-e, --echo
:显示重建索引的命令。-v, --verbose
:详细模式,显示更多重建索引的信息。-q, --quiet
:静默模式,尽量少输出信息。-j, --jobs=数量
:并行重建索引的作业数。-h, --host=主机名
:数据库服务器的主机名或IP地址。-p, --port=端口号
:数据库服务器的端口。-U, --username=用户名
:连接数据库的用户名。-W, --password
:提示输入连接数据库的密码。
示例1
重建数据库white
的所有索引
[pg16@test ~]$ reindexdb -d white
示例2
重建所有数据库的索引
[pg16@test ~]$ reindexdb --all
reindexdb: reindexing database "postgres"
reindexdb: reindexing database "template1"
reindexdb: reindexing database "white"
reindexdb: reindexing database "white2"
reindexdb: reindexing database "white3"
示例3
只重建特定表的索引,如 yewu1.t4
[pg16@test ~]$ reindexdb -d white -t yewu1.t4
示例4
使用并行重建索引来提高效率
[pg16@test ~]$ reindexdb --all --jobs=4
reindexdb: reindexing database "postgres"
reindexdb: reindexing database "template1"
reindexdb: reindexing database "white"
NOTICE: table "pgbench_history" has no indexes to reindex
NOTICE: table "t2" has no indexes to reindex
NOTICE: table "t1" has no indexes to reindex
reindexdb: reindexing database "white2"
NOTICE: table "t2" has no indexes to reindex
NOTICE: table "t1" has no indexes to reindex
NOTICE: table "t4" has no indexes to reindex
NOTICE: table "t4" has no indexes to reindex
reindexdb: reindexing database "white3"
NOTICE: table "t2" has no indexes to reindex
NOTICE: table "t4" has no indexes to reindex
NOTICE: table "t4" has no indexes to reindex
NOTICE: table "t1" has no indexes to reindex
注意事项
- 重建索引可能会需要较长时间,期间可能会影响数据库的性能,因此请考虑在数据库负载较低的时间段进行此操作。
- 在执行重建索引之前,请确保数据库有足够的磁盘空间,因为重建索引过程中会临时创建新的索引文件。
- 重建系统目录索引的操作(
--system
)应谨慎使用,只有在确定系统索引可能损坏或性能问题时才进行。 - 使用
-j
,--jobs
选项并行重建索引可以提高效率,但也可能增加数据库服务器的负载,因此请根据服务器的性能和当前负载来合理设置并行作业的数量。
reindexdb
是数据库维护中的一个重要工具,可以帮助数据库管理员管理和优化索引,从而维护数据库的性能和稳定性。
谨记:心存敬畏,行有所止。