zabbix5.0之postgresql表分割槽操作詳情(儲存過程、定時任務)

dba_sam發表於2021-06-15

刪除原表
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章