pgsql: zabbix 历史表history_uint变更为分区表

简介: pgsql: zabbix 历史表history_uint变更为分区表

涉及表:

history_uint

连接信息

PGPASSWORD=123  /usr/local/pg12/bin/psql -d zabbix  -U zabbixadmin  -p 5435 -h localhost

备份表

这里的备份只是应急用大概率用不到

-- 创建备份表
CREATE TABLE history_uint_20230816 (
    itemid bigint not null,
    clock integer not null default 0,
    value numeric(20,0) not null default 0,
    ns integer not null default 0
);
-- 插入数据
insert into history_uint_20230816 select * from history_uint;
-- 对比数据
select count(*) from history_uint;
select count(*) from history_uint_20230816;

获取时间最久的值

按时间排序获取最久的时间的数据值,这里确定的是分区开始的时间

2023-05-18 12:14:16
zabbix=> select * from history_uint order by clock limit 1;
 itemid |   clock    | value |    ns
--------+------------+-------+-----------
  57376 | 1684383256 |     0 | 575701329

获取数据量

获取数据量

select count(*) from history_uint;
zabbix=> select count(*) from history;
   count
-----------
 110263979
(1 row) 3亿1千万

停止zabbix

systemctl stop zabbix-server

修改表名为老表

-- history_uint
ALTER TABLE history_uint RENAME TO history_uint_old;

创建父表

-- 创建父表
CREATE TABLE history_uint (
    itemid bigint not null,
    clock integer not null default 0,
    value numeric(20,0) not null default 0,
    ns integer not null default 0
)PARTITION BY RANGE (clock);
-- 删除表
-- drop table history_uint;

创建分区表

使用函数计算并拼接出创建分区的SQL语句

DO $$ 
DECLARE
    -- 开始时间
    start_date timestamp := '2023-05-14'::timestamp;
    -- 结束时间
    end_date timestamp := '2023-12-31'::timestamp;
    current_datea timestamp;
    table_names text;
BEGIN
    FOR current_datea IN 
        SELECT generate_series(start_date, end_date, '1 week'::interval) AS week_start
    LOOP
        -- 将每周的开始时间定义为 每周1的00:00:00 
        current_datea := date_trunc('week', current_datea) - interval '8 hours';
        -- 定义表名并拼接为年份和周数
        table_names := 'history_uint_week_' || EXTRACT(YEAR FROM current_datea) || EXTRACT(WEEK FROM current_datea);
        -- 生成创建分区表的SQL
        RAISE NOTICE '%', format('CREATE TABLE %I PARTITION OF history_uint FOR VALUES FROM (%s) TO (%s);', 
                           table_names,
                           EXTRACT(EPOCH FROM current_datea), 
                           EXTRACT(EPOCH FROM current_datea) + 604800);
    END LOOP;
END $$;

SQL详情

使用上面的循环得来的SQL语句

CREATE TABLE history_uint_week_202318 PARTITION OF history_uint FOR VALUES FROM (1683475200) TO (1684080000);
CREATE TABLE history_uint_week_202319 PARTITION OF history_uint FOR VALUES FROM (1684080000) TO (1684684800);
CREATE TABLE history_uint_week_202320 PARTITION OF history_uint FOR VALUES FROM (1684684800) TO (1685289600);
CREATE TABLE history_uint_week_202321 PARTITION OF history_uint FOR VALUES FROM (1685289600) TO (1685894400);
CREATE TABLE history_uint_week_202322 PARTITION OF history_uint FOR VALUES FROM (1685894400) TO (1686499200);
CREATE TABLE history_uint_week_202323 PARTITION OF history_uint FOR VALUES FROM (1686499200) TO (1687104000);
CREATE TABLE history_uint_week_202324 PARTITION OF history_uint FOR VALUES FROM (1687104000) TO (1687708800);
CREATE TABLE history_uint_week_202325 PARTITION OF history_uint FOR VALUES FROM (1687708800) TO (1688313600);
CREATE TABLE history_uint_week_202326 PARTITION OF history_uint FOR VALUES FROM (1688313600) TO (1688918400);
CREATE TABLE history_uint_week_202327 PARTITION OF history_uint FOR VALUES FROM (1688918400) TO (1689523200);
CREATE TABLE history_uint_week_202328 PARTITION OF history_uint FOR VALUES FROM (1689523200) TO (1690128000);
CREATE TABLE history_uint_week_202329 PARTITION OF history_uint FOR VALUES FROM (1690128000) TO (1690732800);
CREATE TABLE history_uint_week_202330 PARTITION OF history_uint FOR VALUES FROM (1690732800) TO (1691337600);
CREATE TABLE history_uint_week_202331 PARTITION OF history_uint FOR VALUES FROM (1691337600) TO (1691942400);
CREATE TABLE history_uint_week_202332 PARTITION OF history_uint FOR VALUES FROM (1691942400) TO (1692547200);
CREATE TABLE history_uint_week_202333 PARTITION OF history_uint FOR VALUES FROM (1692547200) TO (1693152000);
CREATE TABLE history_uint_week_202334 PARTITION OF history_uint FOR VALUES FROM (1693152000) TO (1693756800);
CREATE TABLE history_uint_week_202335 PARTITION OF history_uint FOR VALUES FROM (1693756800) TO (1694361600);
CREATE TABLE history_uint_week_202336 PARTITION OF history_uint FOR VALUES FROM (1694361600) TO (1694966400);
CREATE TABLE history_uint_week_202337 PARTITION OF history_uint FOR VALUES FROM (1694966400) TO (1695571200);
CREATE TABLE history_uint_week_202338 PARTITION OF history_uint FOR VALUES FROM (1695571200) TO (1696176000);
CREATE TABLE history_uint_week_202339 PARTITION OF history_uint FOR VALUES FROM (1696176000) TO (1696780800);
CREATE TABLE history_uint_week_202340 PARTITION OF history_uint FOR VALUES FROM (1696780800) TO (1697385600);
CREATE TABLE history_uint_week_202341 PARTITION OF history_uint FOR VALUES FROM (1697385600) TO (1697990400);
CREATE TABLE history_uint_week_202342 PARTITION OF history_uint FOR VALUES FROM (1697990400) TO (1698595200);
CREATE TABLE history_uint_week_202343 PARTITION OF history_uint FOR VALUES FROM (1698595200) TO (1699200000);
CREATE TABLE history_uint_week_202344 PARTITION OF history_uint FOR VALUES FROM (1699200000) TO (1699804800);
CREATE TABLE history_uint_week_202345 PARTITION OF history_uint FOR VALUES FROM (1699804800) TO (1700409600);
CREATE TABLE history_uint_week_202346 PARTITION OF history_uint FOR VALUES FROM (1700409600) TO (1701014400);
CREATE TABLE history_uint_week_202347 PARTITION OF history_uint FOR VALUES FROM (1701014400) TO (1701619200);
CREATE TABLE history_uint_week_202348 PARTITION OF history_uint FOR VALUES FROM (1701619200) TO (1702224000);
CREATE TABLE history_uint_week_202349 PARTITION OF history_uint FOR VALUES FROM (1702224000) TO (1702828800);
CREATE TABLE history_uint_week_202350 PARTITION OF history_uint FOR VALUES FROM (1702828800) TO (1703433600);
CREATE TABLE history_uint_week_202351 PARTITION OF history_uint FOR VALUES FROM (1703433600) TO (1704038400);

导入数据

insert into history_uint  select * from history_uint_old;
-- 按月导入
-- 小于等于1687017600
insert into history_uint  select * from history_uint_old where clock<=1687017600;
-- 大于1687017600 and 小于1689609600
insert into history_uint  select * from history_uint_old where clock>1687017600 and clock<1689609600;
-- 大于1689609600
insert into history_uint  select * from history_uint_old where clock>=1689609600;

对比数据

zabbix=> select count(*) from history_uint;
   count
-----------
 307234801
(1 row)
zabbix=> select count(*) from history_uint_old;
   count
-----------
 307234801
(1 row)

创建索引

pg中索引名全局不能重复

-- history
CREATE INDEX "idx_history_uint_new_itemid_clock1" on "history_uint" USING btree(itemid, clock);

启动zabbix

systemctl start zabbix-server

定时创建分区表

定时脚本创建分区表每周一,添加到定时任务即可 !

vim history_uint_create_partiton_table.sh
#! /bin/bash
TableName='history_uint'
# 获取下周年份
NextYear=$(date -d "next week" +%Y)
# 获取下周时间
NextWeek=$(date -d "next week" +%V)
NextWeekDate=$(date -d "next week" +%Y-%m-%d)
# 下下周的时间
NextNextWeek=$(date -d "+2 week" +%V)
NextNextWeekDate=$(date -d "+2 weeks" +%Y-%m-%d)
# 本周时间戳
WeekTimestamp=$(date -d "${WeekDate}" +%s)
# 下周时间戳
NextWeekTimestamp=$(date -d "${NextWeekDate}" +%s)
# 下下周时间戳
NextNextWeekTimestamp=$(date -d "${NextNextWeekDate}" +%s)
# 创建分区表
#PGPASSWORD=123  /usr/local/pg12/bin/psql -d zabbix  -U zabbixadmin  -p 5435 -h localhost  -c "CREATE TABLE ${TableName}_week_${NextYear}${NextWeek}  PARTITION OF ${TableName} FOR VALUES FROM (${NextWeekTimestamp}) TO (${NextNextWeekTimestamp});"   >>/data/pg_data/5435/scripts/${TableName}.log 2>&1
# 显示创建分区表SQL
echo "CREATE TABLE ${TableName}_week_${NextYear}${NextWeek}  PARTITION OF ${TableName} FOR VALUES FROM (${NextWeekTimestamp}) TO (${NextNextWeekTimestamp});"

添加到定时任务

# crontab -e
0 0 * * 1  /bin/sh /data/pg_data/5435/scripts/history_create_partiton_table.sh
0 0 * * 1  /bin/sh /data/pg_data/5435/scripts/history_uint_create_partiton_table.sh


相关文章
|
9月前
|
人工智能 机器人 数据挖掘
“龙虾”OpenClaw进阶实战指南:阿里云/本地部署+从0到1搭建生产力团队+百炼api配置全攻略
2026年,OpenClaw(俗称“龙虾”,曾用名Clawdbot)已从单纯的AI工具进化为可定制化的“数字员工”底座。但多数用户初次部署后都会陷入困惑:明明安装成功,却只能像普通聊天机器人一样问答,完全发挥不出其自动化生产力价值。其实,OpenClaw的核心魅力不在于“开箱即用”,而在于“按需培养”——它就像一套毛坯房,需要通过定性格、建记忆、装技能、磨合作、做复制的五步SOP,才能打造成贴合自身需求的生产力团队。本文将从**多端部署实操**、**五步养成体系**、**阿里云百炼API配置**、**常见问题排查**四大维度,搭配可直接执行的代码命令,带您从0到1搭建专属OpenClaw生产力
918 2
|
10月前
|
域名解析 网络协议 安全
详细介绍Linux命令dig和nslookup
本文介绍 Linux 下两大 DNS 查询工具:dig(功能强大,支持详尽选项与追踪)和 nslookup(简洁易用,含交互模式)。涵盖安装、常用语法、记录类型(A/MX/NS等)、典型示例及输出解析,助你高效诊断域名解析与网络问题。(239字)
1727 3
|
11月前
|
SQL 监控 关系型数据库
我们来说一下 MySQL 的慢查询日志
我是小假 期待与你的下一次相遇 ~
687 0
|
SQL 人工智能 安全
别让AI做它不擅长的事:Agent在业务场景中的工程实践
本文通过分享将AI Agent技术应用于“智能播报助手”和“批量建任务”两个真实业务场景的实践历程,深刻阐述了当下将AI Agent与传统工程系统深度融合,而非追求完全替代,才是实现业务提效和价值落地的有效路径。
1831 34
|
数据采集 人工智能 定位技术
分享一个开源的MCP工具使用的AI Agent 支持常用的AI搜索/地图/金融/浏览器等工具
介绍一个开源可用的 MCP Tool Use 通用工具使用的 AI Agent (GitHub: https://github.com/AI-Agent-Hub/mcp-marketplace ,Web App https://agent.deepnlp.org/agent/mcp_tool_use,支持大模型从Open MCP Marketplace (http://deepnlp.org/store/ai-agent/mcp-server) 的1w+ 的 MCP Server的描述和 Tool Schema 里面,根据用户问题 query 和 工具 Tool描述的 相关性,选择出来可以满足
|
Ubuntu Linux 虚拟化
ubunt配置samba服务器,匿名访问
ubunt配置samba服务器,匿名访问
1162 1
ubunt配置samba服务器,匿名访问
|
NoSQL Redis Docker
使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)
使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)
3046 0
|
Web App开发 缓存 网络协议
nginx 限流模块和fail2ban搭配使用
ngnix的限流模块主要有三个: limit_conn 限制某个ip的tcp连接数目或者限制某个server(网站)整体的连接数目 limit_rate 现在每个请求的数据大小 limit_req 限制某个ip的请求次数 其中效果最明显的是第三个limit_req,但是宝塔面板中的流量限制只有前面两项的配置,所以之前一直流量限制没什么用。
1211 0
nginx 限流模块和fail2ban搭配使用