zabbix5.0之postgresql表分割槽操作詳情(儲存過程、定時任務)
刪除原表
psql -U zabbix -d zabbix //登入資料庫
drop table history;
drop table history_str;
drop table history_log;
drop table history_text;
drop table history_uint;
drop table trends;
drop table trends_uint;
建立獨立的表空間
CREATE TABLESPACE zabbix_tbs
OWNER zabbix
LOCATION '/app/postgresql-12.3/data';
ALTER TABLESPACE zabbix_tbs
OWNER TO zabbix;
重建刪除的表
--history
表
CREATE TABLE public.history
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
value double precision NOT NULL DEFAULT '0'::double precision,
ns integer NOT NULL DEFAULT 0
) PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.history
OWNER to zabbix;
CREATE INDEX history_1
ON public.history USING btree
(itemid ASC NULLS LAST, clock ASC NULLS LAST)
TABLESPACE zabbix_tbs;
-- Partitions SQL
CREATE TABLE public.history_default PARTITION OF public.history
DEFAULT;
--history_str
表
CREATE TABLE public.history_str
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
value character varying(255) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::character varying,
ns integer NOT NULL DEFAULT 0
)PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.history_str
OWNER to zabbix;
CREATE INDEX history_str_1
ON public.history_str USING btree
(itemid ASC NULLS LAST, clock ASC NULLS LAST)
TABLESPACE zabbix_tbs;
-- Partitions SQL
CREATE TABLE public.history_str_default PARTITION OF public.history_str
DEFAULT;
--history_log
表
CREATE TABLE public.history_log
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
"timestamp" integer NOT NULL DEFAULT 0,
source character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::character varying,
severity integer NOT NULL DEFAULT 0,
value text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text,
logeventid integer NOT NULL DEFAULT 0,
ns integer NOT NULL DEFAULT 0
) PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.history_log
OWNER to zabbix;
CREATE INDEX history_log_1
ON public.history_log USING btree
(itemid ASC NULLS LAST, clock ASC NULLS LAST)
TABLESPACE zabbix_tbs;
-- Partitions SQL
CREATE TABLE public.history_log_default PARTITION OF public.history_log
DEFAULT;
--history_text
表
CREATE TABLE public.history_text
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
value text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text,
ns integer NOT NULL DEFAULT 0
)PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.history_text
OWNER to zabbix;
CREATE INDEX history_text_1
ON public.history_text USING btree
(itemid ASC NULLS LAST, clock ASC NULLS LAST)
TABLESPACE zabbix_tbs;
-- Partitions SQL
CREATE TABLE public.history_text_default PARTITION OF public.history_text
DEFAULT;
--history_uint
表
CREATE TABLE public.history_uint
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
value numeric(20,0) NOT NULL DEFAULT '0'::numeric,
ns integer NOT NULL DEFAULT 0
)PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.history_uint
OWNER to zabbix;
CREATE INDEX history_uint_1
ON public.history_uint USING btree
(itemid ASC NULLS LAST, clock ASC NULLS LAST)
TABLESPACE zabbix_tbs;
-- Partitions SQL
CREATE TABLE public.history_uint_default PARTITION OF public.history_uint
DEFAULT;
--trends
表
CREATE TABLE public.trends
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
num integer NOT NULL DEFAULT 0,
value_min double precision NOT NULL DEFAULT '0'::double precision,
value_avg double precision NOT NULL DEFAULT '0'::double precision,
value_max double precision NOT NULL DEFAULT '0'::double precision,
CONSTRAINT trends_pkey PRIMARY KEY (itemid, clock)
)PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.trends
OWNER to zabbix;
-- Partitions SQL
CREATE TABLE public.trends_default PARTITION OF public.trends
DEFAULT;
--trends_uint
表
CREATE TABLE public.trends_uint
(
itemid bigint NOT NULL,
clock integer NOT NULL DEFAULT 0,
num integer NOT NULL DEFAULT 0,
value_min numeric(20,0) NOT NULL DEFAULT '0'::numeric,
value_avg numeric(20,0) NOT NULL DEFAULT '0'::numeric,
value_max numeric(20,0) NOT NULL DEFAULT '0'::numeric,
CONSTRAINT trends_uint_pkey PRIMARY KEY (itemid, clock)
)PARTITION BY RANGE (clock)
TABLESPACE zabbix_tbs;
ALTER TABLE public.trends_uint
OWNER to zabbix;
-- Partitions SQL
CREATE TABLE public.trends_uint_default PARTITION OF public.trends_uint
DEFAULT;
建立儲存過程
psql -U zabbix -d zabbix
--建立儲存過程partition_create
CREATE OR REPLACE PROCEDURE public.partition_create(
tablename character varying,
partitionname character varying,
clock integer)
LANGUAGE 'plpgsql'
AS $BODY$DECLARE RETROWS INT;
DECLARE STRCLOCK VARCHAR = CLOCK;
DECLARE LESSCLOCK VARCHAR = CLOCK + 86400;
DECLARE tableCreateSQL VARCHAR;
BEGIN
SELECT COUNT(1) INTO RETROWS
FROM pg_inherits JOIN pg_class parent
ON pg_inherits.inhparent = parent.oid JOIN pg_class child
ON pg_inherits.inhrelid = child.oid JOIN pg_namespace nmsp_parent
ON nmsp_parent.oid = parent.relnamespace JOIN pg_namespace nmsp_child
ON nmsp_child.oid = child.relnamespace WHERE parent.relname = TABLENAME and child.relname = PARTITIONNAME;
IF RETROWS = 0 THEN
tableCreateSQL := 'create table ' || PARTITIONNAME || ' PARTITION OF public.' || tablename || '
FOR VALUES FROM (' || STRCLOCK || ') TO (' || LESSCLOCK || ')';
RAISE NOTICE '開始建立表%',tableCreateSQL;
EXECUTE(tableCreateSQL);
RAISE NOTICE '成功建立表%',tableCreateSQL;
END IF;
END
$BODY$;
--建立儲存過程partition_drop
CREATE OR REPLACE PROCEDURE public.partition_drop(
tablename character varying,
partitionname character varying)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE RETROWS INT;
DECLARE tableDropSQL VARCHAR;
BEGIN
SELECT COUNT(1) INTO RETROWS
FROM pg_inherits JOIN pg_class parent
ON pg_inherits.inhparent = parent.oid JOIN pg_class child
ON pg_inherits.inhrelid = child.oid JOIN pg_namespace nmsp_parent
ON nmsp_parent.oid = parent.relnamespace JOIN pg_namespace nmsp_child
ON nmsp_child.oid = child.relnamespace WHERE parent.relname = tablename and child.relname = partitionname;
IF RETROWS = 1 THEN
tableDropSQL := 'drop table ' || PARTITIONNAME;
RAISE NOTICE '開始刪除表%',tableDropSQL;
EXECUTE(tableDropSQL);
RAISE NOTICE '成功刪除表%',tableDropSQL;
END IF;
END
$BODY$;
--建立儲存過程partition_maintenance
CREATE OR REPLACE PROCEDURE public.partition_maintenance(
table_name character varying,
keep_data_days integer)
LANGUAGE 'plpgsql'
AS $BODY$ DECLARE PARTITION_NAME VARCHAR(64);
DECLARE NEXT_DATE VARCHAR;
DECLARE NEXT_TIMESTAMP INT;
DECLARE HISTORY_PARTITION_NAME VARCHAR(64);
DECLARE RETROWS INT;
DECLARE DATE_TIMESTAMP INT;
DECLARE DATE_PARTITION_NAME VARCHAR(64);
BEGIN
select to_char(now() + interval '1 d','yyyyMMdd') INTO NEXT_DATE;
select floor(extract(epoch from to_timestamp(to_char(now() + interval '1 d','yyyyMMdd'),'yyyyMMdd'))) INTO NEXT_TIMESTAMP;
select TABLE_NAME||to_char(now() + interval '1 d','yyyyMMdd') INTO PARTITION_NAME;
CALL partition_create(TABLE_NAME, PARTITION_NAME, NEXT_TIMESTAMP);
select TABLE_NAME||to_char(now(),'yyyyMMdd') INTO DATE_PARTITION_NAME;
SELECT COUNT(1) INTO RETROWS
FROM pg_inherits JOIN pg_class parent
ON pg_inherits.inhparent = parent.oid JOIN pg_class child
ON pg_inherits.inhrelid = child.oid JOIN pg_namespace nmsp_parent
ON nmsp_parent.oid = parent.relnamespace JOIN pg_namespace nmsp_child
ON nmsp_child.oid = child.relnamespace WHERE parent.relname = table_name and child.relname = DATE_PARTITION_NAME;
IF RETROWS = 0 THEN
select floor(extract(epoch from to_timestamp(to_char(now(),'yyyyMMdd'),'yyyyMMdd'))) INTO DATE_TIMESTAMP;
CALL partition_create(TABLE_NAME, DATE_PARTITION_NAME, DATE_TIMESTAMP);
END IF;
EXECUTE 'select to_char(now() - interval ''' || KEEP_DATA_DAYS || ' d'''||','||'''yyyyMMdd'''||')'|| '' INTO HISTORY_PARTITION_NAME;
HISTORY_PARTITION_NAME := table_name||HISTORY_PARTITION_NAME;
CALL partition_drop(table_name,HISTORY_PARTITION_NAME);
END
$BODY$;
--建立儲存過程partition_maintenance_all
CREATE OR REPLACE PROCEDURE public.partition_maintenance_all(
)
LANGUAGE 'plpgsql'
AS $BODY$ begin
CALL partition_maintenance('history', 90);
CALL partition_maintenance('history_log', 90);
CALL partition_maintenance('history_str', 90);
CALL partition_maintenance('history_text', 90);
CALL partition_maintenance('history_uint', 90);
CALL partition_maintenance('trends', 90);
CALL partition_maintenance('trends_uint', 90);
end;
$BODY$;
首次執行過程
call partition_maintenance_all();
新增定時任務
su - postgres
vi /home/postgres/.procedure
#!/bin/bash
source ~/.bash_profile
psql "user=zabbix password=zabbix123 host=localhost port=5432" -c "call partition_maintenance_all()";
儲存並退出編輯
新增定時任務
crontab -e //編輯定時任務,並加入如下內容,儲存退出(新增多條,防止執行失敗未建立分割槽)
30 10 * * * /bin/sh /home/postgres/.procedure
30 21 * * * /bin/sh /home/postgres/.procedure
30 23 * * * /bin/sh /home/postgres/.procedure
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70000068/viewspace-2776789/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PostgreSQL/LightDB 分割槽表之分割槽裁剪SQL
- Sql儲存過程分頁--臨時表儲存SQL儲存過程
- PostgreSQL獲取建表語句儲存過程SQL儲存過程
- PostgreSQL:傳統分割槽表SQL
- PostgreSQL:內建分割槽表SQL
- SQL Server表分割槽刪除詳情DSCCSQLServer
- PostgreSQL/LightDB分割槽表之常見問題SQL
- LightDB/PostgreSQL 相容Oracle儲存過程SQLOracle儲存過程
- 定時任務操作
- MySql資料分割槽操作之新增分割槽操作MySql
- MySQL的nnodb引擎表資料分割槽儲存MySql
- PostgreSQL分割槽表更新思路SQL
- EMC儲存重灌系統丟失分割槽的資料恢復過程資料恢復
- MySQL之儲存過程MySql儲存過程
- mongo 儲存過程詳解Go儲存過程
- Spark操作Hive分割槽表SparkHive
- PostgreSQL使用表繼承實現分割槽表SQL繼承
- Linux 定時任務排程Linux
- 關於Entity Freamwork 儲存過程操作儲存過程
- 記一次分割槽表update調優過程
- 分割槽儲存管理模擬實驗
- laravel框架任務排程(定時執行任務)Laravel框架
- mysql 儲存過程中變數的定義與賦值操作MySql儲存過程變數賦值
- mysql 儲存過程 procedure 批次建表MySql儲存過程
- oracle分割槽表和分割槽表exchangeOracle
- celery筆記八之資料庫操作定時任務筆記資料庫
- PostgreSQL分割槽表、繼承表記錄去重方法SQL繼承
- 分割槽表之自動增加分割槽(11G)
- python 定時任務之 schedulePython
- SQL Server大分割槽表沒有空分割槽的情況下如何擴充套件分割槽的方法SQLServer套件
- MySQL 儲存過程進行切換表MySql儲存過程
- Spring排程定時任務的方式Spring
- Android 中的定時任務排程Android
- Oracle儲存過程乾貨(一):儲存過程基礎Oracle儲存過程
- SQL 儲存過程裡呼叫另一個儲存過程SQL儲存過程
- 輕量ORM-SqlRepoEx (五) 儲存過程操作ORMSQL儲存過程
- oracle分割槽表和非分割槽表exchangeOracle
- dbms_lob儲存過程導致臨時表空間100%儲存過程