PostgreSQL怎么设置表自动添加分区

  介绍

这篇文章给大家介绍PostgreSQL怎么设置表自动添加分区,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

PostgreSQL引进“分区”表特性,解放了之前采用“表继承”+“触发器”来实现分区表的繁琐,低效。而添加分区,都是手动执行SQL。

演示目的:利用python来为PostgreSQL的表自动添加分区。

python版本:python3 +

pip3  install  psycopg2

一、配置数据源

数据库。ini文件:记录数据库连接参数

[adsas]   主机=192.168.1.201   数据库=adsas   用户=adsas   密码=adsas123   端口=5432   (测试)   主机=192.168.1.202   数据库=adsas   用户=adsas   密码=adsas123   端口=5432

二、配置脚本

配置。py文件:下面的配置()函数读取database.ini文件并返回连接参数。config()函数位于配置。py文件中

# !/usr/bin/python3   得到configparser  import  ConfigParser   ,   def 配置(section 文件名=& # 39;database.ini& # 39;):   #,才能create  a 解析器   时间=parser 才能;ConfigParser ()   #,才能read  config 文件   parser.read才能(文件名)   ,   #,才能get 部分,default 用postgresql   db 才能=,{}   if 才能parser.has_section(部分):   ,,,params =, parser.items(部分)   ,,,for  param 拷贝参数:   ,,,,,db (param[0]),=,参数[1]   其他的才能:   ,,,raise 例外(& # 39;Section  {0}, not  found 拷贝,{1},文件# 39;.format(节,文件名))   ,   return 才能db

三、创建子表脚本

pg_add_partition_table。py文件:其中create_table函数是创建子表的SQL。其中参数


参数名含义db指向数据库表主表sub_table正要新建的子表名start_date范围分界开始值end_date范围分界结束值 # !/usr/bin/python3   import  psycopg2   得到config  import 配置   #,例子:,create  table  tbl_game_android_step_log_2021_07  PARTITION  OF  tbl_game_android_step_log  FOR  VALUES 得到(& # 39;2021 - 07 - 01 & # 39;),用(& # 39;2021 - 08 - 01 & # 39;);   def  create_table (db,表,sub_table,, start_date,, end_date):   “““才能,create  subtable 拷贝,PostgreSQL  database"““   command =,才能“create  table  {0}, PARTITION  OF , {1}, FOR  VALUES 得到(& # 39;[0]{2}& # 39;),用(& # 39;[1]{2}& # 39;);“.format (sub_table,表,(start_date, end_date)),   时间=conn 才能;没有   尝试才能:   ,,,#,read 从而connection 参数   ,,,params =,配置(section =, db)   ,,,#,connect 用,PostgreSQL 服务器   ,,,conn =, psycopg2.connect (* * params)   ,,,cur =, conn.cursor ()   ,,,#,create  table  one  by 一个   ,,,cur.execute(命令)   ,,,#,close  communication  with 从而PostgreSQL  database 服务器   ,,,cur.close ()   ,,,# commit 从而改变   ,,,conn.commit ()   except 才能;(例外,,psycopg2.DatabaseError), as 错误:   ,,,print(错误)   最后才能:   ,,,if  conn  is  not 没有:   ,,,,,conn.close ()

四,执行文件main.py

主要。py:主文件,通过执行主要生成分区表。

示例:

# !/usr/bin/python3   import  datetime   得到datetime  import 日期   得到dateutil.relativedelta  import  *   得到pg_add_partition_table  import  create_table   #,Get 从而;1 st  day  of 从而next 月   def  get_next_month_first_day (d):   return 才能;日期(d.year  +, (==d.month  12),, d.month ==, 12,趁机d.month  +, 1,,, 1)   ,,   def  create_sub_table (db,表):   #,才能Get  current 日期   时间=d1 才能;date.today ()   #,才能Get  next 月# 39;s 日期   d2 才能=,d1  +, relativedelta(个月=+ 1)   #,才能Get 从而1 st  day  of 从而next ; As 从而starting  value  of 从而partitioned 表   时间=start_date 才能;get_next_month_first_day (d1)   #,才能Gets 从而1 st  of 从而next  two  months  as 从而最终获得value  of 从而partitioned 表   时间=end_date 才能;get_next_month_first_day (d2)   #,才能get  sub  table 名字   getmonth 才能=,datetime.datetime.strftime (d2, & # 39; %猴Y_ % # 39;)   sub_table 才能=,table  +, & # 39; _ # 39;, + getmonth   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

PostgreSQL怎么设置表自动添加分区