官方地址:https://github.com/postgrespr...
關於pathman的原理和優化問題,請移步至https://yq.aliyun.com/article...git
檢查環境變量
若是直接執行psql
命令提示command not found
則執行下面的命令設置環境變量github
root@host# PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin # 就是postgresql的安裝路徑 root@host# export PATH
主要是PG_CONFIG
sql
安裝shell
root@host# tar xf pg_pathman.tar.gz root@host# cd pg_pathman root@host# make USE_PGXS=1 root@host# make install
安裝插件到數據庫數據庫
# psql -U 用戶名 -h 主機localhost -d 數據庫名稱 -p 端口5432 -c "create extension pg_pathman"
建立主表post
# psql -U 用戶名 -h 主機localhost -d 數據庫名稱 -p 端口5432 -c "create table test (id serial8 primary key, area_id bigint not null, name varchar(100) not null, age integer not null default 0)"
建立一個地區表,area_id爲地區id優化
建立分區插件
建立hash分區postgresql
# psql -U 用戶名 -h 主機localhost -d 數據庫名稱 -p 端口5432 -c "select create_hash_partitions(test,'area_id',10,false)"
參數解析:create_hash_partitions(表名,'分區字段',分幾個區,是否當即開始轉移數據)
code
建立range分區
# psql -U 用戶名 -h 主機localhost -d 數據庫名稱 -p 端口5432 -c "select create_range_partitions(test,'age',0,100,1,false)"
參數解析:select create_range_partitions(表名,分區字段,從幾開始,到幾結束,數據間隔,每間隔X建立一個表,是否當即遷移數據)
range分區還有一種方法按照時間好比:
select create_range_partitions(表名,分區字段, 從什麼時間開始如'YYYY-mm-dd HH:ii:ss'::timestamp, interval '1 month', 總共建立幾個表, 是否當即遷移數據 )
同一個表hash和range只能建立一個規則的分區
分區命令執行完畢後能馬上看到已經建立完成了N個子表,可是因爲剛纔設置是否當即遷移數據
都是false,因此還須要執行select partition_table_concurrently(表名,每次處理幾條數據,失敗嘗試等待秒數)
開始遷移數據
遷移完成後,執行select count(*) from only test
能夠看到已經沒有任何數據了(注意only
)
count ------- 0 (1 row)
使用SQLselect * from test where area_id = 1 order by id asc limit 10
數據庫會自動的根據area_id從某一個分片中讀取數據。
使用SQLselect * from test where age > 10 order by id asc limit 10
數據庫會自動的根據age從某幾個片中讀取數據。
更加詳細的請參考 德哥文章:https://yq.aliyun.com/article...