首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PostgreSQL 10中自动化Zabbix3.4的本地范围分区

在PostgreSQL 10中自动化Zabbix3.4的本地范围分区
EN

Stack Overflow用户
提问于 2018-05-16 19:39:01
回答 2查看 1.4K关注 0票数 0

我想使用PostgreSQL的本机范围分区自动化Zabbix3.4数据库的分区过程。

编写SQL函数来执行下面的操作还是使用shell/python脚本更明智呢?

  • 确保至少在需要之前创建了一个分区。
  • 删除任何早于x周/月的分区;对于history 7天和trends 1年
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-09 17:10:59

我已经编写了详细的说明,说明如何使用PostgreSQL version 11和帕巴曼作为使用Zabbix进行本机表分区的机制(本文撰写时的版本为3.4 )。

zabbix-postgres-分区

票数 0
EN

Stack Overflow用户

发布于 2018-05-31 16:12:16

下面是我从一个填充了PSQL 9.4、没有分区的数据库中转换到PSQL 10本机范围分区的解决方案。

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

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

代码语言:javascript
运行
复制
# 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中创建表,并为clock列实现本机范围分区。下面是一个手动SQL脚本的示例,它可以为history表提供乐趣。对要通过范围分区的所有history表执行此操作。

代码语言:javascript
运行
复制
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的Debian8FlavorOS上,确保脚本位于具有正确权限( /usr/local/bin)的特定目录(我使用了chown postgres:postgres /usr/local/bin/auto_history_tables_monthly.sh),并使其可执行(chmod u+x /usr/local/bin/auto_history_tables_monthly.sh作为postgres用户)。

使用以下内容为postgres用户创建一个cron作业(postgres):

代码语言:javascript
运行
复制
0 0 1 * * /usr/local/bin/auto_history_tables_monthly.sh | psql -d zabbix

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

下面是剧本。它使用date命令来利用UTC时代值。它提前一个月创建一个表,并在两个月前删除一个分区。这似乎与为history定制的31天的保留时间配合得很好。确保该用例的PSQL 10 DB处于UTC时间。

代码语言:javascript
运行
复制
#!/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

然后将旧数据库中的数据转储到。我用的是pg_dump/pg_restore

我肯定还有更复杂的解决方案,但我发现这对于使用PostgreSQL 10原生范围分区功能自动生成Zabbix数据库的需求来说是最简单的。

如果你需要更多的细节,请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50378743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档