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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

利用python為PostgreSQL的表自動(dòng)添加分區(qū)

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

PostgreSQL引進(jìn)“分區(qū)”表特性,解放了之前采用“表繼承”+“觸發(fā)器”來(lái)實(shí)現(xiàn)分區(qū)表的繁瑣、低效。而添加分區(qū),都是手動(dòng)執(zhí)行SQL。

演示目的:利用python來(lái)為PostgreSQL的表自動(dòng)添加分區(qū)。python版本:python3+

pip3 install psycopg2一、配置數(shù)據(jù)源

database.ini 文件:記錄數(shù)據(jù)庫(kù)連接參數(shù)

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

config.py 文件:下面的 config() 函數(shù)讀取 database.ini 文件并返回連接參數(shù)。config() 函數(shù)位于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三、創(chuàng)建子表腳本

pg_add_partition_table.py 文件:其中 create_table函數(shù)是創(chuàng)建子表SQL。其中參數(shù)

參數(shù)名 含義 db 指向數(shù)據(jù)庫(kù) table 主表 sub_table 正要新建的子表名 start_date 范圍分界開(kāi)始值 end_date 范圍分界結(jié)束值

#!/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()四、執(zhí)行文件main.py

main.py:主文件;通過(guò)執(zhí)行main生成分區(qū)表。

示例:

#!/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’);

上面示例單獨(dú)為表tbl_game_android_step_log;創(chuàng)建分區(qū);若多個(gè)表;用for語(yǔ)句處理

# 多表操作 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’)

到此這篇關(guān)于利用python為PostgreSQL的表自動(dòng)添加分區(qū)的文章就介紹到這了,更多相關(guān)python PostgreSQL添加分區(qū)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产一区二区三区免费视频 | 黑人超大| 在线免费污视频 | 欧美黄网站| 污污的视频在线播放 | 美女精品在线 | 看一级毛片一区二区三区免费 | 午夜国产精品理论片久久影院 | 老司机美女一级毛片 | 久久亚洲私人国产精品 | 中文字幕亚洲欧美日韩不卡 | 国产精品视频第一区二区 | 夜夜女人国产香蕉久久精品 | 亚洲最大网站 | 青青草视频在线观看免费 | 亚洲综合激情另类图片专区 | 免费一级毛片在线播放傲雪网 | 九九热视频在线播放 | 999国产精品视频 | 香蕉视频黄在线观看 | 中文字幕在线观看第一页 | 91av国产精品| 免费高清观看青青草原 | 亚洲国产欧美日韩第一香蕉 | 国产精品无码专区在线观看 | 国产福利视频一区二区微拍 | 一级黄色录像免费 | 亚洲女视频 | 一级香蕉免费毛片 | 国产精品lululu在线观看 | 国产精品国产高清国产专区 | 日日综合网 | 国产福利兔女郎在线观看 | 日韩免费视频播播 | 欧美成人看片黄a免费看 | 国产青草视频在线观看 | 特级黄国产片一级视频播放 | 欧美在线视频 一区二区 | 国产精品久久久精品三级 | 一级做a爰片性色毛片2021 | 91久久香蕉国产线看观看软件 |