pg_repack安裝和使用


postgresql中使用pg_repack主要有三種原因:

1.大量刪除記錄后,從表中回收空閑空間給磁盤。

2.重新構建一個表,以重新排列記錄,並將它們壓縮/打包到更少的頁。這可能會讓查詢的IO更少,性能更高。

3.從那些因為autovacuum設置不當,而導致過度膨脹的表中回收空閑空間。

pg_repack是PostgreSQL的一個擴展,它可以幫助我們在線重建一個表。這類似於MySQL中用於在線表重建的pt-online-schema-change。然而,pg_repack只適用於具有主鍵或NOT NULL唯一鍵的表。

 

安裝pg_repack

yum install pg_repack11  

 

加載擴展

修改配置文件后重啟postgresql

shared_preload_libraries = 'pg_repack'

  

數據庫中創建擴展

$ psql 

\c xxx

CREATE EXTENSION pg_repack;

  

查看幫助

$ 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

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>.

  

參數--dry-run檢測哪些表支持pg_repack

$ pg_repack --dry-run -d percona --table scott.employee
INFO: Dry run enabled, not executing repack
INFO: repacking table "scott.employee"

如果不支持,會提示:

$ pg_repack --dry-run -d percona --table scott.sales
INFO: Dry run enabled, not executing repack
WARNING: relation "scott.sales" must have a primary key or not-null unique keys

可以並行執行

$ pg_repack -d percona -t scott.employee -j 4
NOTICE: Setting up workers.conns
INFO: repacking table "scott.employee"

  

pg_repack也支持對遠程節點執行操作。

 

工作原理

通過在原表上使用觸發器,避免執行full vacuum的時候發生宕機。以下是工作實現過程:

對表執行全表repack:

1.創建一個記錄表,用於記錄原表中的記錄的修改

2.在原表上創建一個觸發器,記錄插入、更新、和刪除操作到日志表

3.創建一個新表,包含原表中的所有的記錄

4.在新表上創建索引

5.將日志表中的變更應用到新表

6.使用system catalogs將原表和新表進行swap,包含索引和toast表

7.刪除原先的表

  

對索引執行repack

1.使用concurrently創建新索引

2.將新的索引和老的索引進行swap

3.刪除原先的索引

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM