PostgreSQL的扩展(extensions)-常用的扩展之pg_repack

PostgreSQL的扩展(extensions)-常用的扩展之pg_repack

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

pg_repack 是一款非常有用的 PostgreSQL 扩展工具,它能够重新打包(repack)表和索引以回收空间并减少碎片,而且在这个过程中不会锁定表,允许数据库在重整过程中继续对数据进行读写操作。这是与 PostgreSQL 内建的 VACUUM FULL 命令相比的一个重大优势,因为 VACUUM FULL 在重新组织表以回收空间时会锁定表。

特性

  • 最小化锁定时间pg_repack在重组表和索引的时候减少了锁的使用时间,使得数据库对于读写操作几乎总是可用的。
  • 重新打包表和索引:不仅可以对表进行重新打包,还可以重新打包索引,减少索引碎片。
  • 兼容性:支持多个 PostgreSQL 版本。

安装 pg_repack

从源代码安装

如果你的系统没有预打包的 `pg_repack` 版本,你可以从源代码编译安装。

下载网址:https://pgxn.org/dist/pg_repack/
在这里插入图片描述

--编译安装
cd /home/pg16/resource
[pg16@test resource]$ unzip pg_repack-1.5.0.zip 
[pg16@test resource]$ cd pg_repack-1.5.0/
[pg16@test pg_repack-1.5.0]$ make
[pg16@test pg_repack-1.5.0]$ make install

当你安装好 pg_repack 后,需要在目标数据库上创建扩展,例如当前是在postgres 库上创建

[pg16@test pg_repack-1.5.0]$ psql -p 5777
psql (16.2)
Type "help" for help.

postgres=# CREATE EXTENSION pg_repack;
CREATE EXTENSION
postgres=# SELECT * FROM pg_extension;
  oid  |      extname       | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition 
-------+--------------------+----------+--------------+----------------+------------+-----------+--------------
 14270 | plpgsql            |       10 |           11 | f              | 1.0        |           | 
 16423 | pg_stat_statements |       10 |         2200 | t              | 1.10       |           | 
 16454 | pg_repack          |       10 |         2200 | f              | 1.5.0      |           | 
(3 rows)

使用 pg_repack

通过help查看帮助文档。

[pg16@test ~]$ pg_repack --help
pg_repack re-organizes a PostgreSQL database.

Usage:
  pg_repack [OPTION]... [DBNAME]
Options:
  -a, --all                     repack all databases
  -t, --table=TABLE             repack specific table only
  -I, --parent-table=TABLE      repack specific parent table and its inheritors
  -c, --schema=SCHEMA           repack tables in specific schema only
  -s, --tablespace=TBLSPC       move repacked tables to a new tablespace
  -S, --moveidx                 move repacked indexes to TBLSPC too
  -o, --order-by=COLUMNS        order by columns instead of cluster keys
  -n, --no-order                do vacuum full instead of cluster
  -N, --dry-run                 print what would have been repacked
  -j, --jobs=NUM                Use this many parallel jobs for each table
  -i, --index=INDEX             move only the specified index
  -x, --only-indexes            move only indexes of the specified table
  -T, --wait-timeout=SECS       timeout to cancel other backends on conflict
  -D, --no-kill-backend         don't kill other backends when timed out
  -Z, --no-analyze              don't analyze at end
  -k, --no-superuser-check      skip superuser checks in client
  -C, --exclude-extension       don't repack tables which belong to specific extension
      --error-on-invalid-index  don't repack tables which belong to specific extension
      --switch-threshold    switch tables when that many tuples are left to catchup

Connection options:
  -d, --dbname=DBNAME       database to connect
  -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

Generic options:
  -e, --echo                echo queries
  -E, --elevel=LEVEL        set output message level
  --help                    show this help, then exit
  --version                 output version information, then exit

Read the website for details: <https://reorg.github.io/pg_repack/>.
Report bugs to <https://github.com/reorg/pg_repack/issues>.

在安装并配置 pg_repack 之后,你可以通过命令行工具 pg_repack 来重新组织表和索引。以下是一些基本用法:

示例1

重新打包特定表,如 yewu1.t4

[pg16@test ~]$ pg_repack -d white -t yewu1.t4
INFO: repacking table "yewu1.t4"

示例2

重新打包指定数据库的所有表

[pg16@test ~]$ pg_repack -d white
INFO: repacking table "public.pgbench_accounts"
INFO: repacking table "public.pgbench_branches"
INFO: repacking table "public.pgbench_tellers"
INFO: repacking table "yewu1.t4"

示例3

重新打包索引

[pg16@test ~]$ pg_repack -d white --only-indexes -t yewu1.t4            
INFO: repacking indexes of "yewu1.t4"
INFO: repacking index "yewu1.idx_t2"
INFO: repacking index "yewu1.idx_t4"
INFO: repacking index "yewu1.t4_pkey"

请记得,使用 pg_repack 之前,确保你已经有了足够的权限来执行这些操作,并且对数据库做了适当的备份。

注意事项

  • pg_repack 运行期间,尽管它最小化了锁的时间,但在最后阶段仍然需要短暂的锁定,以便完成表的替换。因此,最好在数据库负载相对较低的时间段运行它。
  • 确保在执行大量数据重组之前备份数据库,以免出现不可预测的情况导致数据丢失。

谨记:心存敬畏,行有所止。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PostgreSQL是一种开源的关系型数据库,它提供了多种管理工具来操作数据库,其中包括psqlpg_dump命令。 psql是一种命令行工具,可以用来与PostgreSQL数据库进行交互。它不仅能够执行SQL语句,还可以支持交互式命令。在使用psql命令时,有时需要在命令中包含密码信息。可以使用以下的方式来在命令行中带密码执行SQL语句: 1. 在命令行中输入psql命令,启动psql工具。 2. 输入连接数据库的命令,例如:psql -U username -d dbname -h hostname -p port。 3. 输入密码,此时必须在命令行中输入密码。 4. 执行SQL语句,例如:SELECT * FROM tablename; pg_dump是PostgreSQL数据库备份工具,可以用来将数据库中的数据导出到一个文件中。使用pg_dump命令时,也需要在命令行中包含密码信息。可以使用以下的方式在命令行中带密码执行pg_dump命令: 1. 在命令行中输入pg_dump命令,启动pg_dump工具。 2. 输入数据库连接信息和密码,例如:pg_dump -U username -d dbname -h hostname -p port -W。 3. 执行备份操作,例如:pg_dump -U username -d dbname -h hostname -p port -W > backup.sql。 总的来说,在使用psqlpg_dump命令时,为了在命令中包含密码信息,需要使用“-W”参数将密码输入到命令行中。当然,这种方式存在一定的安全隐患,因为密码可以被其他人看到。因此,最好使用其他方式来进行密码管理,例如使用配置文件、环境变量或者其他安全的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值