pg_repack插件安装使用
获取代码
在 这个链接里 可以选择需要下载的pg_repack版本
wget http://api.pgxn.org/dist/pg_repack/1.4.4/pg_repack-1.4.4.zip
安装相关依赖包
apt-get install cmake libssl-dev
apt-get install libreadline6 libreadline-dev postgresql-server-dev-10
对下载的pg_repack包进行解压
unzip pg_repack-1.4.4.zip
进入解压好的文件夹中,进行编译安装
cd pg_repack-1.4.4
make
sudo make install
复制pg_repack执行程序到/usr/local/bin/路径下
sudo cp pg_repack-1.4.4/bin/pg_repack /usr/local/bin/
检查pg_repack是否已经安装
pg_repack --version # 如果出现对应的版本号就说明已经安装完成
使用方法
在数据库中创建扩展
create extension pg_repack
在外部使用pg_repack对表空间进行回收
pg_repack -t public.tmp_t1 -j 2 -D -k -d postgres
使用pg_repack回收表空间前
在外部使用pg_repack对表空间进行回收
使用pg_repack回收表空间后
pg_repack参数说明
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
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
pg_ repack原理
pg_ repack原理其实和vacuum full是类似的,都是新建一个文件,然后将老文件数据拷贝过来,然后进行文件切换,它不阻塞读写的秘诀就是新建文件和拷贝的过程是在线做的,在没有完成拷贝之前,原来的文件还是可以读写的,只有在切表那一瞬间可能会有影响。
源库的数据文件一直在变,所以表文件其实分为两部分,一部分是基础数据,一部分是增量数据,基础数据的拷贝就是正常的拷贝,增量数据是通过创建触发器来捕获在该表上的读写操作来实现的,待基础数据拷贝完后再将trigger捕获的增量sql进行应用,达到最终结果。