在PostgreSQL 10中为Zabbix 3.4自动化本机范围分区

问题描述 投票:0回答:2

我想使用PostgreSQL的Native Range Partitioning自动化分区Zabbix 3.4数据库的过程。

编写SQL函数以执行以下操作或使用shell / python脚本会更明智吗?

  • 确保在需要之前创建至少一个分区。
  • 删除任何超过x周/月的分区;为history 7天和trends 1年
postgresql plpgsql zabbix database-partitioning postgresql-10
2个回答
1
投票

下面是我提出的解决方案,用于从没有分区的PSQL 9.4填充数据库转换到PSQL 10本机范围分区。

A.创建一个Zabbix空PSQL 10数据库。

确保首先创建一个空的Zabbix PSQL 10 DB。

# su postgres
postgres@<>:~$ createuser -P -s -e zabbix
postgres@<>:~$ psql
postgres# create database zabbix;
postgres# grant all privileges on database zabbix to zabbix;

B.在clock列上创建表和原生范围分区

在Zabbix DB中创建表,并为时钟列实现本机范围分区。下面是一个手动SQL脚本的示例,可以为history表带来乐趣。对要通过范围分区的所有history表执行此操作。

zabbix=# CREATE TABLE public.history
(
    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);

zabbix=# CREATE TABLE public.history_old PARTITION OF public.history
    FOR VALUES FROM (MINVALUE) TO (1522540800);
zabbix=# CREATE TABLE public.history_y2018m04 PARTITION OF public.history
    FOR VALUES FROM (1522540800) TO (1525132800);
zabbix=# CREATE TABLE public.history_y2018m05 PARTITION OF public.history
    FOR VALUES FROM (1525132800) TO (1527811200);

zabbix=# CREATE INDEX ON public.history_old USING btree (itemid, clock);
zabbix=# CREATE INDEX ON public.history_y2018m04 USING btree (itemid, clock);
zabbix=# CREATE INDEX ON public.history_y2018m05 USING btree (itemid, clock);

C.自动化!

我使用了一个shell脚本,因为它是处理在PSQL 10中创建新分区的最简单方法之一。确保在所需内容之前始终至少有一个分区。

我们称之为脚本auto_history_tables_monthly.sh

在运行PSQL 10的Debian 8 Flavor OS上,确保脚本位于具有正确权限的某个目录(我使用/usr/local/bin)(chown postgres:postgres /usr/local/bin/auto_history_tables_monthly.sh)并使其可执行(chmod u+x /usr/local/bin/auto_history_tables_monthly.shpostgres用户)。

使用以下命令为crontab -e用户创建一个cron作业(postgres):

0 0 1 * * /usr/local/bin/auto_history_tables_monthly.sh | psql -d zabbix

这将在每个月的第一天运行shell脚本。

下面是脚本。它使用date命令来利用UTC纪元值。它会提前一个月创建一个表,并在2个月之后删除一个分区。这似乎与根据我的需求定制的history保留31天相结合。确保此用例的PSQL 10 DB处于UTC时间。

#!/bin/bash

month_diff () {
        year=$1
  month=$2
        delta_month=$3
  x=$((12*$year+10#$month-1))
        x=$((x+$delta_month))
        ry=$((x/12))
        rm=$(((x % 12)+1))
        printf "%02d %02d\n" $ry $rm
}

month_start () {
        year=$1
  month=$2
        date '+%s' -d "$year-$month-01 00:00:00" -u
}

month_end () {
        year=$1
  month=$2
        month_start $(month_diff $year $month 1)
}

# Year using date
current_year=$(date +%Y)
current_month=$(date +%m)

# Math
next_date=$(month_diff $current_year $current_month 1)
next_year=$(echo $next_date|sed 's/ .*//')
next_month=$(echo $next_date|sed 's/.* //')

start=$(month_start $next_date)
end=$(month_end $next_date)

#next_month_table="public.history_y${next_year}m${next_month}"

# Create next month table for history, history_uint, history_str, history_log, history_text
sql="
    CREATE TABLE IF NOT EXISTS public.history_y${next_year}m${next_month} PARTITION OF public.history
      FOR VALUES FROM ($start) TO ($end);
    \nCREATE TABLE IF NOT EXISTS public.history_uint_y${next_year}m${next_month} PARTITION OF public.history_uint
      FOR VALUES FROM ($start) TO ($end);
    \nCREATE TABLE IF NOT EXISTS public.history_str_y${next_year}m${next_month} PARTITION OF public.history_str
      FOR VALUES FROM ($start) TO ($end);
    \nCREATE TABLE IF NOT EXISTS public.history_log_y${next_year}m${next_month} PARTITION OF public.history_log
      FOR VALUES FROM ($start) TO ($end);
    \nCREATE TABLE IF NOT EXISTS public.history_text_y${next_year}m${next_month} PARTITION OF public.history_text
      FOR VALUES FROM ($start) TO ($end);
    \nCREATE INDEX on public.history_y${next_year}m${next_month} USING btree (itemid, clock);
    \nCREATE INDEX on public.history_uint_y${next_year}m${next_month} USING btree (itemid, clock);
    \nCREATE INDEX on public.history_str_y${next_year}m${next_month} USING btree (itemid, clock);
    \nCREATE INDEX on public.history_log_y${next_year}m${next_month} USING btree (itemid, clock);
    \nCREATE INDEX on public.history_text_y${next_year}m${next_month} USING btree (itemid, clock);
    "

echo -e $sql

# Math
prev_date=$(month_diff $current_year $current_month -2)
prev_year=$(echo $prev_date|sed 's/ .*//')
prev_month=$(echo $prev_date|sed 's/.* //')

# Drop last month table for history, history_uint, history_str, history_log, history_text
sql="
    DROP TABLE public.history_y${prev_year}m${prev_month};
    \nDROP TABLE public.history_uint_y${prev_year}m${prev_month};
    \nDROP TABLE public.history_str_y${prev_year}m${prev_month};
    \nDROP TABLE public.history_log_y${prev_year}m${prev_month};
    \nDROP TABLE public.history_text_y${prev_year}m${prev_month};
    "

echo -e $sql

D.然后从旧数据库中转储数据。我用过pg_dump/pg_restore

我确信有更复杂的解决方案,但我发现这对于使用PostgreSQL 10 Native Range Partitioning功能自动分区Zabbix数据库的需求最为简单。

如果您需要更多详细信息,请与我们联系。


0
投票

我已经写了详细的注释,使用PostgreSQL版本11和pgpartman作为使用Zabbix进行本机表分区的机制(截至本文撰写时版本为3.4)。

zabbix-postgres-partitioning

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.