postgresql分区表代码创建方案

这里主要刷针对的范围分区,其它类型分区表的场景没有遇见过,一般来说都是使用分区插件管理。我还是提供一个代码处理的思路。

1.首先分区表中至少有一个范围分区了,这里以时间作为分区。主要是提供一个分区增长的策略。

2.通过前面的sql查询数据库中的分区表信息。

3.逐个解析分区表。

思路:分区表都是时间分区,分区范围小时、天、月、季度建立,不会到分钟的粒度。定时检测,通过配置设置,预留的分区和需要一次创建的分区个数。当前时间到达预留分区时,立即创建分区。例如以天创建分区,预留3天,一次创建90天。则当前时间与数据库最后一个分区时间对比,已经到达最后三天,则立即创建已有分区的后90天分区。如果今天是8号,则分区表中最后一个是11号,则立即创建11号后90天的分区。

给大家贴一个关键代码,代码考虑,以下代码:

java 复制代码
package org.db.partition.task;

import org.db.partition.configuration.PartDBConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class PartitionedTableTask implements CommandLineRunner {
    @Autowired
    PartDBConfig partDBConfig;

    //查询分区
    String sql="select *  FROM ( SELECT parent.relname parent,\n" +
            "        child.relname child,\n" +
            "                CASE WHEN child.relispartition THEN pg_get_expr(child.relpartbound, child.oid) ELSE NULL END AS\n" +
            " partition_bound\n" +
            "FROM pg_inherits\n" +
            "JOIN pg_class parent ON pg_inherits.inhparent = parent.oid\n" +
            "JOIN pg_class child  ON pg_inherits.inhrelid  = child.oid)  as part\n" +
            "\n" +
            "WHERE partition_bound is not null  ORDER BY partition_bound\n";

    //创建分区,参数顺序 分区表名、父表、分区范围
    String format="CREATE TABLE %s PARTITION OF %s\n" +
            "    \n" +
            "FOR VALUES FROM ('%s') TO ('%s');";

    @Override
    public void run(String... args) throws Exception {
        while (true) {
            Connection connection = DBUtil.getConnection(partDBConfig);
            try {
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(sql);
                List<ChildTable> list = new ArrayList<ChildTable>();
                while (rs.next()) {
                    ChildTable childTable = new ChildTable();
                    childTable.setParent(rs.getString("parent"));
                    childTable.setChild(rs.getString("child"));
                    childTable.setBound(rs.getString("partition_bound"));
                    list.add(childTable);
                }

                //按照父表分组
                Map<String, List<ChildTable>> groupedBy = list.stream()
                        .collect(Collectors.groupingBy(ChildTable::getParent));

                groupedBy.forEach((parent, child) -> {
                    try {
                        //取出最后一个分区
                        ChildTable table = child.get(child.size() - 1);

                        /**
                         * 获取分区范围,正则表达式提取时间
                         * FOR VALUES FROM ('2025-01-01') TO ('2025-04-01')
                         */
                        table.process();
                        if (table.getEnd() != null) {
                            LocalDateTime now = LocalDateTime.now();
                            //分区开始
                            LocalDateTime startTime = AutoDateParser.parse(table.getStart());

                            //分区结束
                            LocalDateTime endTime = AutoDateParser.parse(table.getEnd());

                            //计算一个分区时长
                            long minutesBetween = ChronoUnit.MINUTES.between(startTime, endTime);
                            //计算当前时间到最后一个分区的时间
                            long secondsBetween = ChronoUnit.SECONDS.between(now, endTime);
                            //如果当前时间到最后一个分区的时间小于预留的时间
                            if (secondsBetween < partDBConfig.getNum() * minutesBetween) {
                                //开始创建
                                LocalDateTime nextTime = endTime;
                                //需要一次创建的分区个数
                                for (int i = 0; i < partDBConfig.getPre(); i++) {
                                    LocalDateTime addTime = nextTime.plus(Duration.ofMinutes(minutesBetween));
                                    String name = addTime.format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH"));
                                    String add = String.format(format, "part_" + name, table.getParent(), nextTime, addTime);
                                    try {
                                        connection.createStatement().execute(add);
                                    } catch (SQLException e) {
                                        throw new RuntimeException(e);
                                    }
                                    //
                                    nextTime = addTime;
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                });
                DBUtil.closeJDBC(rs, statement, connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
           Thread.sleep(1000*60*50);
        }
    }
}
相关推荐
大大大大晴天3 小时前
从 HDFS 到对象存储:计算存储分离如何重塑云原生大数据底座
大数据
用户36105886261212 小时前
SparkStreaming 之 updateStateByKey 算子详解及代码实现
大数据·spark
咖啡屋和酒吧14 小时前
“隐性肩颈紧张”正在透支精力|没有酸痛,不代表肩颈处于健康状态
大数据·精选
港股研究社14 小时前
专注履约底座,顺丰同城在即时零售效率时代提升增长动能
大数据·人工智能
AI_yangxi16 小时前
短视频矩阵系统选哪家
大数据·人工智能·矩阵
lupai1 天前
手机在网状态接口实测效果与质量评估
大数据·python·智能手机·api接口
大模型丫丫1 天前
Hermes Agent:轻量级智能体框架实战指南
大数据·运维·服务器
大大大大晴天1 天前
每天认识一个组件:SQL网关Apache Kyuubi
大数据
2601_962218471 天前
万象生鲜系统冷链物联网接入技术实现生鲜企业温控管理数字化
大数据·运维·微服务·云原生·架构
2601_962074811 天前
大数据-264 实时数仓 - Canal MySQL的binlog研究 存储目录 变动信息 配置MySQL
大数据·数据库·mysql