os: ubuntu 16.04
db: postgresql 9.6.8
pg_repack: 1.4.4

postgresql 的 vacuum full 需要 access exclusive lock,获得这个锁后,针对该表的所有操作均无法执行,包括 select

pg_repack is a fork of the pg_reorg project, which has proven hugely successful. Unfortunately new feature development on pg_reorg has slowed or stopped since late 2011.

版本

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:   xenial$ psql
psql (9.6.8)
Type "help" for help.postgres=# select version();version
----------------------------------------------------------------------------------------------------------------------------------------------PostgreSQL 9.6.8 on x86_64-pc-linux-gnu (Ubuntu 9.6.8-1.pgdg16.04+1), compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609, 64-bit
(1 row)postgres=#

下载安装

在 https://github.com/reorg/pg_repack/releases
或者 https://pgxn.org/dist/pg_repack
都可以下载

$ wget https://github.com/reorg/pg_repack/archive/ver_1.4.4.zip

或者

$ wget http://api.pgxn.org/dist/pg_repack/1.4.4/pg_repack-1.4.4.zip

安装依赖包

# apt install cmake libssl-dev
$ unzip  pg_repack-ver_1.4.4.zip
$ ls -l
drwxrwxr-x 8 postgres postgres   4096 Oct 18 19:30 pg_repack-ver_1.4.4
-rw-r--r-- 1 postgres postgres 124311 Dec 14  2018 pg_repack-ver_1.4.4.zip
$ cd pg_repack-ver_1.4.4
$ makemake[1]: Entering directory '/var/lib/postgresql/pg_repack-ver_1.4.4/bin'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer pg_repack.o pgut/pgut.o pgut/pgut-fe.o  -L/usr/lib/x86_64-linux-gnu -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now  -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed  -L/usr/lib/x86_64-linux-gnu -lpq -L/usr/lib/postgresql/9.6/lib -lpgcommon -lpgport -lssl -lcrypto -lz -lrt -lcrypt -ldl -lm -o pg_repack
make[1]: Leaving directory '/var/lib/postgresql/pg_repack-ver_1.4.4/bin'
make[1]: Entering directory '/var/lib/postgresql/pg_repack-ver_1.4.4/lib'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer -fPIC -DREPACK_VERSION=1.4.4 -I. -I./ -I/usr/include/postgresql/9.6/server -I/usr/include/postgresql/internal -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o repack.o repack.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer -fPIC -DREPACK_VERSION=1.4.4 -I. -I./ -I/usr/include/postgresql/9.6/server -I/usr/include/postgresql/internal -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o pgut/pgut-spi.o pgut/pgut-spi.c
( echo '{ global:'; mawk '/^[^#]/ {printf "%s;\n",$1}' exports.txt; echo ' local: *; };' ) >exports.list
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -pie -fno-omit-frame-pointer -fPIC -shared -Wl,--version-script=exports.list -o pg_repack.so repack.o pgut/pgut-spi.o -L/usr/lib/x86_64-linux-gnu  -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now  -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed
sed 's,REPACK_VERSION,1.4.4,g' pg_repack.sql.in > pg_repack--1.4.4.sql;
sed 's,REPACK_VERSION,1.4.4,g' pg_repack.control.in > pg_repack.control
make[1]: Leaving directory '/var/lib/postgresql/pg_repack-ver_1.4.4/lib'
make[1]: Entering directory '/var/lib/postgresql/pg_repack-ver_1.4.4/regress'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/var/lib/postgresql/pg_repack-ver_1.4.4/regress'

使用apt安装就需要在root下 make install(或者使用sudo),主要是权限的问题。
pg_repack 依赖 postgresql-server-dev-X.Y
make时出错,一般都是缺少依赖包导致,按照错误提示按照即可。

$ sudo make installmake[1]: Entering directory '/var/lib/postgresql/pg_repack-ver_1.4.4/bin'
/bin/mkdir -p '/usr/lib/postgresql/9.6/bin'
/usr/bin/install -c  pg_repack '/usr/lib/postgresql/9.6/bin'
make[1]: Leaving directory '/var/lib/postgresql/pg_repack-ver_1.4.4/bin'
make[1]: Entering directory '/var/lib/postgresql/pg_repack-ver_1.4.4/lib'
/bin/mkdir -p '/usr/lib/postgresql/9.6/lib'
/bin/mkdir -p '/usr/share/postgresql/9.6/extension'
/bin/mkdir -p '/usr/share/postgresql/9.6/extension'
/usr/bin/install -c -m 755  pg_repack.so '/usr/lib/postgresql/9.6/lib/pg_repack.so'
/usr/bin/install -c -m 644 .//pg_repack.control '/usr/share/postgresql/9.6/extension/'
/usr/bin/install -c -m 644  pg_repack--1.4.4.sql pg_repack.control '/usr/share/postgresql/9.6/extension/'
make[1]: Leaving directory '/var/lib/postgresql/pg_repack-ver_1.4.4/lib'
make[1]: Entering directory '/var/lib/postgresql/pg_repack-ver_1.4.4/regress'
make[1]: Nothing to be done for 'install'.
make[1]: Leaving directory '/var/lib/postgresql/pg_repack-ver_1.4.4/regress'

测试

pg_repack 是以 extension 的方式工作的。

peiybdb=# select * from pg_available_extensions where name like '%repack%' order by name;name    | default_version | installed_version |                           comment
-----------+-----------------+-------------------+--------------------------------------------------------------pg_repack | 1.4.4           |                   | Reorganize tables in PostgreSQL databases with minimal locks
(1 row)peiybdb=# create extension pg_repack;
CREATE EXTENSIONpeiybdb=# \dnList of schemasName  |  Owner
--------+----------public | postgresrepack | postgres
(2 rows)
$ which pg_repack
/usr/lib/postgresql/9.6/bin/pg_repack

online vacuum full (include table indexes)

peiybdb=# \d+ tmp_t0Table "public.tmp_t0"Column |  Type  | Modifiers | Storage | Stats target | Description
--------+--------+-----------+---------+--------------+-------------c0     | bigint |           | plain   |              | c1     | bigint |           | plain   |              |
Indexes:"idx_tmp_t0_c1" btree (c1)
$ pg_repack --no-order --table tmp_t0 peiybdb
WARNING: relation "public.tmp_t0" must have a primary key or not-null unique keys

添加主键

peiybdb=# drop index idx_tmp_t0_c1;
peiybdb=# ALTER TABLE public.tmp_t0 ADD CONSTRAINT tmp_t0_pk PRIMARY KEY (c1) ;peiybdb=# with tmp_0 as (select pc.oid as reloid,pn.nspname,pc.relname,pn.nspname||'.'||pc.relname as schema_relname,pc.relkindfrom pg_class pc,pg_namespace pnwhere 1=1and pc.relnamespace=pn.oid
)
select t0.reloid as tableoid,pt.schemaname||'.'||pt.tablename as schema_tablename,pg_relation_filepath(pt.schemaname||'.'||pt.tablename),pg_table_size(pt.schemaname||'.'||pt.tablename),pg_relation_size(pt.schemaname||'.'||pt.tablename),pg_total_relation_size(pt.schemaname||'.'||pt.tablename),t1.reloid as indexoid,pi.schemaname||'.'||pi.indexname,pg_relation_filepath(pi.schemaname||'.'||pi.indexname),pg_relation_size(pi.schemaname||'.'||pi.indexname),pg_indexes_size(pi.schemaname||'.'||pi.tablename)from pg_tables ptleft outer join pg_indexes pi on pt.schemaname||'.'||pt.tablename = pi.schemaname||'.'||pi.tablenameleft outer join tmp_0 t0on pt.schemaname||'.'||pt.tablename = t0.schema_relnameleft outer join tmp_0 t1on pi.schemaname||'.'||pi.indexname = t1.schema_relname
where 1=1and pt.schemaname='public'and pt.tablename='tmp_t0'
;tableoid | schema_tablename | pg_relation_filepath | pg_table_size | pg_relation_size | pg_total_relation_size | indexoid |     ?column?     | pg_relation_filepath | pg_relation_size | pg_indexes_size
----------+------------------+----------------------+---------------+------------------+------------------------+----------+------------------+----------------------+------------------+-----------------1447603 | public.tmp_t0    | base/1406609/1447745 |      44318720 |         44285952 |               66805760 |  1447752 | public.tmp_t0_pk | base/1406609/1447752 |         22487040 |        22487040
(1 row)

再次执行

$ pg_repack --no-order --table tmp_t0 -d peiybdbtableoid | schema_tablename | pg_relation_filepath | pg_table_size | pg_relation_size | pg_total_relation_size | indexoid |     ?column?     | pg_relation_filepath | pg_relation_size | pg_indexes_size
----------+------------------+----------------------+---------------+------------------+------------------------+----------+------------------+----------------------+------------------+-----------------1447603 | public.tmp_t0    | base/1406609/1447769 |      44318720 |         44285952 |               66805760 |  1447752 | public.tmp_t0_pk | base/1406609/1447772 |         22487040 |        22487040
(1 row)

可以看到,不管是 tmp_t0 (oid=1447603) 还是 tmp_t0_pk (oid=1447752),对应的 oid 都没有发生变化,但是在磁盘上的文件发生了变化。

online repack database

$ pg_repack --wait-timeout 3600 --jobs 10 --no-order -d peiybdb

online repack schema

$ pg_repack --wait-timeout 3600 --jobs 10 --no-order --schema=public -d peiybdb

online repack table and indexes

$ pg_repack --wait-timeout 3600 --no-order --table public.tmp_t0 -d peiybdb 

online repack only all index

$ pg_repack --only-indexes --table public.tmp_t0 -d peiybdb 

online repack only some index

$ pg_repack --index public.tmp_t0_pk -d peiybdb 

参考:
http://reorg.github.com/pg_repack
https://pgxn.org/dist/pg_repack
https://github.com/reorg/pg_repack

http://reorg.github.io/pg_repack/

pg_repack 处理表和索引的膨胀相关推荐

  1. PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack

    PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack 作者 digoal 日期 2016-10-30 标签 PostgreSQL , pg_repack , pg ...

  2. 超全的数据库建表/SQL/索引规范,适合贴在工位上!

    背景 ​ 因为工作岗位的原因,负责制定了关于后端组数据库的规约规范,作为所有产品线的规范,历经几版的修改,最终形成下边的文本,规范在整个后端执行也有大半年的时间,对于整个团队在开发阶段就减少不恰当的建 ...

  3. mysql多表连接 索引_MySQL多表查询之外键、表连接、子查询、索引

    一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, ...

  4. Oracle表与索引的分析及索引重建

    1.分析表与索引(analyze 不会重建索引) analyze table tablename compute statistics 等同于 analyze table tablename comp ...

  5. 一上来,就问原理,问上亿(MySQL)大表的索引优化...

    你经历过地狱级的面试吗?血淋淋的教训告诉你 你好,x先生,欢迎来我们公司面试,不用太紧张,我们简单聊一个 好的,谢谢您 看了你的简历,之前对mysql数据库了解挺多的,我们先来聊聊数据库怎么样? 好的 ...

  6. 如何查看表和索引的统计信息

    如何查看表和索引的统计信息 原文:如何查看表和索引的统计信息 这几天要求做一个服务器的统计信息,主要针对表和索引.下面我就简单分享几个查询数据表和索引统计信息的方法: 1.使用T-SQL 语句实现: ...

  7. SQL Server 表和索引存储结构

    SQL Server 表和索引存储结构 杜飞 在上一篇文章中,我们介绍了SQL Server数据文件的页面类型,系统通过96个字节的头部信息和系统表从逻辑层面上将表的存储结构管理起来,具体到表的存储结 ...

  8. imp 只导入索引_使用imp导入表和索引至不同表空间方法

    在以前我的blog中,曾经记录过使用expdp/impdp方式将表和索引导入至不同表空间的方法,这里再提一下: 导出: expdp test/test directory=dump_dir dumpf ...

  9. mysql主键创建非聚集索引_什么是聚集索引,非聚集索引,索引覆盖,回表,索引下推...

    聚集索引 我们先建如下的一张表 CREATE TABLE `student` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',`name` var ...

最新文章

  1. edge chrome 浏览器 底色调黑 反色
  2. 组网中交换机虚拟化技术的应用
  3. 【Crash Course Psychology】1. Intro to psychology笔记
  4. mysql显示错误代码1067_每日一记--Mysql错误代码1067
  5. Linux配置ospf命令,OSPF单区域配置
  6. 【Visual C++】游戏开发笔记三十四 浅墨DirectX提高班之三 起承转合的艺术:Direct3D渲染五步曲...
  7. 词法分析 有穷自动机
  8. 大话企业上云之第二篇
  9. python函数超时,用装饰器解决 func_timeout
  10. 后端如何接收对象类型的数据_javascript基本数据类型赋值和对象引用的内存情况分析...
  11. 基本知识 100176
  12. 扎实干货!PP-Tracking:百度提出实时目标跟踪系统(附源码教程)
  13. 【序列化】Kryo 的几种常见序列化实现方式,及其兼容性
  14. 计算机打印预览在哪,excel打印在哪里,EXCEL怎么退出成打印预览的画面
  15. 删除字符串中的数字字符
  16. CMU硕士101-千奇百怪的排序算法
  17. 能力、态度、业绩——绩效考核三要素
  18. random seed什么意思
  19. 生化分析基本原理及 怎么看生化反应曲线?
  20. 乌镇世界互联网大会重点关注网络安全建设,声纹识别技术或成关键

热门文章

  1. 虚拟机(vmware)添加新硬盘
  2. org.apache.flink.util.FlinkRuntimeException: Exceeded checkpoint tolerable failure threshold
  3. 启动报错java.net.ConnectException: [NACOS HTTP-POST] The maximum number of tolerable
  4. 全文检索Elasticsearch研究
  5. PDF文档太大怎么办?
  6. 【Shell】Shell脚本中调用另一个脚本的三种方式(fork/exec/source)
  7. 全球及中国医疗众筹行业发展战略与投资规划分析报告2022-2028年
  8. 软件开发延期引发纠纷-律师随笔
  9. 电子与计算机工程导论,BGPLUS科研荟萃 | 杜克大学 | 电子工程、计算机工程:电子与计算机工程导论...
  10. 好玩游戏不上学去学计算机,如果你这样学习,不如去玩游戏!