2019獨角獸企業重金招聘Python工程師標準>>>
--新建主表
create table part_test(id int, info text, crt_time timestamp not null);
--插入測試數據
insert into part_test select id,md5(random()::text),clock_timestamp() + (id||' hour')::interval from generate_series(1,10000) t(id);
--所有數據都在主表
select * from part_test limit 10;
--創建分區表(range分區)
--建議
--1. 分區列必須有not null約束
--2. 分區個數必須能覆蓋已有的所有記錄
--3. 建議使用非堵塞式遷移接口
--4. 建議數據遷移完成后,禁用主表
select create_range_partitions('part_test'::regclass, -- 主表OID'crt_time', -- 分區列名'2016-10-25 00:00:00'::timestamp, -- 開始值interval '1 month', -- 間隔;interval 類型,用于時間分區表24, -- 分多少個區false) ; -- 是否轉移數據
---自動擴展分區,新插入的數據不在已有的分區范圍內,會自動創建分區
select set_auto('part_test'::regclass, true);--生成100條數據
select id,md5(random()::text),clock_timestamp() + (id||' hour')::interval from generate_series(1,100) t(id);--轉移數據
--注意:
--1. 分區列必須有not null約束
--2. 分區個數必須能覆蓋已有的所有記錄
select partition_table_concurrently('part_test'::regclass,10000,1.0);--查看遷移任務
select * from pathman_concurrent_part_tasks;--停止遷移數據
select stop_concurrent_part_task('part_test'::regclass)--設置主表不可用
select set_enable_parent('part_test'::regclass, false);--查看查詢走分區
explain select * from part_test where crt_time = '2017-8-25 00:00:00'::timestamp;--指定兩個需要合并分區,必須為相鄰分區(不是相鄰分區,報錯),合并后,會刪掉其中一個分區表
select merge_range_partitions('part_test_2'::regclass, 'part_test_12'::regclass) ;--分區分裂select split_range_partition('part_test_1'::regclass, -- 分區oid'2016-11-10 00:00:00'::timestamp, -- 分裂值'part_test_1_2'); -- 分裂后的表名--刪除分區,數據移動到主表
select drop_range_partition('part_test_2',false);--刪除分區,數據也刪除,不遷移到主表
select drop_range_partition('part_test_3',true);--刪除所有分區,數據移動到主表
select drop_partitions('part_test'::regclass, false);--將分區從主表的繼承關系中刪除, 不刪數據,刪除繼承關系,刪除約束(指定分區名,轉換為普通表)
select detach_range_partition('part_test_2');--hash分區
--注意:分區列必須有not null約束
--1. 分區列必須有not null約束
--2. 建議使用非堵塞式遷移接口
--3. 建議數據遷移完成后,禁用主表
--4. pg_pathman不會受制于表達式的寫法,所以select * from part_test where crt_time = '2016-10-25 00:00:00'::timestamp;這樣的寫法也是能走哈希分區的。
--5. hash分區列不局限于int類型的列,會使用hash函數自動轉換。
select create_hash_partitions('part_test'::regclass, -- 主表OID'crt_time', -- 分區列名128, -- 打算創建多少個分區false) ; -- 不遷移數據--查詢每個分區表初次創建時的 interval
select * from pathman_config;--添加分區,支持指定表空間(在后面追加)
append_range_partition(parent REGCLASS, -- 主表OIDpartition_name TEXT DEFAULT NULL, -- 新增的分區表名, 默認不需要輸入tablespace TEXT DEFAULT NULL) -- 新增的分區表放到哪個表空間, 默認不需要輸入--例子(默認根據分區規則往后延一個分區)
select append_range_partition('part_test'::regclass);--在頭部追加分區
select prepend_range_partition('part_test'::regclass);--分區字段要被更新,需要創建更新觸發器
select create_range_update_trigger('part_test'::regclass);--永久禁止分區表,禁用pg_pathman后,繼承關系和約束不會變化,只是pg_pathman不介入custom scan 執行計劃。
---disable_pathman_for沒有可逆操作,請慎用
select disable_pathman_for('part_test');--1. 如果在建初始分區時,需要設置分區表的表空間,可以設置會話或事務的參數
set local default_tablespace='tbs1';--2. disable_pathman_for函數沒有可逆操作,請慎用。--3. 不建議關閉pg_pathman.enable--4. 不建議開啟自動擴展范圍分區,一個錯誤的分區值可能導致創建很多分區。--5. 推薦使用set_enable_parent禁用主表。--6. 由于pg_pathman使用了custom scan接口,所以只支持9.5以及以上版本。--7. 傳統哈希分區需要輸入分區鍵值的約束條件,才能正確選擇分區。pg_pathman只要輸入鍵值即可。--8. 目前使用prepared statement會造成性能下降,跟蹤到與LWLOCK有關,并不是不支持過濾分區造成的
參考:https://yq.aliyun.com/articles/62314