亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

利用python為PostgreSQL的表自動添加分區

瀏覽:4日期:2022-06-29 16:07:20

PostgreSQL引進“分區”表特性,解放了之前采用“表繼承”+“觸發器”來實現分區表的繁瑣、低效。而添加分區,都是手動執行SQL。

演示目的:利用python來為PostgreSQL的表自動添加分區。python版本:python3+

pip3 install psycopg2一、配置數據源

database.ini 文件:記錄數據庫連接參數

[adsas]host=192.168.1.201database=adsasuser=adsaspassword=adsas123port=5432[test]host=192.168.1.202database=adsasuser=adsaspassword=adsas123port=5432二、config 腳本

config.py 文件:下面的 config() 函數讀取 database.ini 文件并返回連接參數。config() 函數位于config.py文件中

#!/usr/bin/python3from configparser import ConfigParser def config(section ,filename=’database.ini’): # create a parser parser = ConfigParser() # read config file parser.read(filename) # get section, default to postgresql db = {} if parser.has_section(section): params = parser.items(section) for param in params: db[param[0]] = param[1] else: raise Exception(’Section {0} not found in the {1} file’.format(section, filename)) return db三、創建子表腳本

pg_add_partition_table.py 文件:其中 create_table函數是創建子表SQL。其中參數

參數名 含義 db 指向數據庫 table 主表 sub_table 正要新建的子表名 start_date 范圍分界開始值 end_date 范圍分界結束值

#!/usr/bin/python3import psycopg2from config import config#example: create table tbl_game_android_step_log_2021_07 PARTITION OF tbl_game_android_step_log FOR VALUES FROM (’2021-07-01’) TO (’2021-08-01’);def create_table(db, table, sub_table, start_date, end_date): ''' create subtable in the PostgreSQL database''' command = 'create table {0} PARTITION OF {1} FOR VALUES FROM (’{2[0]}’) TO (’{2[1]}’);'.format(sub_table, table, (start_date, end_date)) conn = None try: # read the connection parameters params = config(section = db) # connect to the PostgreSQL server conn = psycopg2.connect(**params) cur = conn.cursor() # create table one by one cur.execute(command) # close communication with the PostgreSQL database server cur.close() # commit the changes conn.commit() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close()四、執行文件main.py

main.py:主文件;通過執行main生成分區表。

示例:

#!/usr/bin/python3import datetimefrom datetime import datefrom dateutil.relativedelta import *from pg_add_partition_table import create_table#Get the 1st day of the next monthdef get_next_month_first_day(d): return date(d.year + (d.month == 12), d.month == 12 or d.month + 1 , 1) def create_sub_table(db, table): # Get current date d1 = date.today() # Get next month’s date d2 = d1 + relativedelta(months=+1) # Get the 1st day of the next month;As the starting value of the partitioned table start_date = get_next_month_first_day(d1) # Gets the 1st of the next two months as the end value of the partitioned table end_date = get_next_month_first_day(d2) # get sub table name getmonth = datetime.datetime.strftime(d2, ’%Y_%m’) sub_table = table + ’_’ + getmonth create_table(db, table, sub_table, start_date, end_date) if __name__ == ’__main__’: create_sub_table(’test’, ’tbl_game_android_step_log’);

上面示例單獨為表tbl_game_android_step_log;創建分區;若多個表;用for語句處理

# 多表操作 for table in [’tbl_game_android_step_log’, ’tbl_game_android_game_log’,’tbl_game_android_pay_log’]: create_sub_table(’test’, table);演示之前:adsas=> select * from pg_partition_tree(’tbl_game_android_step_log’);relid| parentrelid | isleaf | level -----------------------------------+---------------------------+--------+------- tbl_game_android_step_log | | f | 0 tbl_game_android_step_log_2020_12 | tbl_game_android_step_log | t | 1(2 rows)

演示之后:

adsas=> select * from pg_partition_tree(’tbl_game_android_step_log’);relid| parentrelid | isleaf | level -----------------------------------+---------------------------+--------+------- tbl_game_android_step_log | | f | 0 tbl_game_android_step_log_2020_12 | tbl_game_android_step_log | t | 1 tbl_game_android_step_log_2021_01 | tbl_game_android_step_log | t | 1Partition key: RANGE (visit_time)Partitions: tbl_game_android_step_log_2020_12 FOR VALUES FROM (’2020-12-01 00:00:00’) TO (’2021-01-01 00:00:00’), tbl_game_android_step_log_2021_01 FOR VALUES FROM (’2021-01-01 00:00:00’) TO (’2021-02-01 00:00:00’)

到此這篇關于利用python為PostgreSQL的表自動添加分區的文章就介紹到這了,更多相關python PostgreSQL添加分區內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美一级毛片免费看 | 亚洲国产欧美国产第一区二区三区 | 亚洲欧美中文v日韩v在线 | 黄色成人免费网站 | 国内精品在线观看视频 | 亚州αv| 国产精品主播在线观看 | 国产成人精品午夜免费 | 中文字幕在线国产 | 99精品国产三级在线观看 | 99久热只有精品视频免费观看17 | 在线播放一区二区三区 | 色综合色 | 91福利国产在线观一区二区 | 国产一区日韩二区欧美三区 | 欧美亚洲在线视频 | 久久人体做爰大胆 | 欧美亚洲国产精品第一页 | 久久精品国产免费看久久精品 | 有人有片的观看免费视频 | 毛片大全在线观看 | 欧美freexx| 91精品国产免费久久久久久青草 | 国产欧美日韩视频 | 特黄特级a级黄毛片免费观看多人 | 亚洲精品一区二区三区 | 精品视频麻豆入口 | 国产va免费精品观看 | 国产午夜精品一二区理论影院 | 国产国产人在线成免费视频69 | 国产亚洲网站 | 欧美精品一区二区三区久久 | 永久免费在线视频 | 国产私密视频 | 美女在线看永久免费网址 | 尤物视频在线观看视频 | 男人你懂的网站 | 国产成人精品综合网站 | 亚洲图片综合区另类图片 | 免费视频日韩 | 国产一区二区三区高清视频 |