最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年01月18日 09:44:44   作者:PostgreSQLChina  
這篇文章主要介紹了利用python為PostgreSQL的表自動添加分區(qū),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

pip3 install psycopg2

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

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

[adsas]
host=192.168.1.201
database=adsas
user=adsas
password=adsas123
port=5432

[test]
host=192.168.1.202
database=adsas
user=adsas
password=adsas123
port=5432

二、config 腳本

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

#!/usr/bin/python3
from 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ù)庫
table 主表
sub_table 正要新建的子表名
start_date 范圍分界開始值
end_date 范圍分界結(jié)束值

#!/usr/bin/python3

import psycopg2
from 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:主文件;通過執(zhí)行main生成分區(qū)表。

示例:

#!/usr/bin/python3
import datetime
from datetime import date
from dateutil.relativedelta import *
from pg_add_partition_table import create_table

#Get the 1st day of the next month
def 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;創(chuàng)建分區(qū);若多個表;用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   |   1

Partition 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的表自動添加分區(qū)的文章就介紹到這了,更多相關python PostgreSQL添加分區(qū)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于Python批量鑲嵌拼接遙感影像/柵格數(shù)據(jù)(示例代碼)

    基于Python批量鑲嵌拼接遙感影像/柵格數(shù)據(jù)(示例代碼)

    這篇文章主要介紹了基于Python批量鑲嵌拼接遙感影像/柵格數(shù)據(jù),使用時直接修改Mosaic_GDAL函數(shù)的入?yún)⒕托辛?選擇數(shù)據(jù)存放的路徑會自動拼接,命名也會自己設置無需額外修改,需要的朋友可以參考下
    2023-10-10
  • 如何實現(xiàn)更換Jupyter Notebook內(nèi)核Python版本

    如何實現(xiàn)更換Jupyter Notebook內(nèi)核Python版本

    這篇文章主要介紹了如何實現(xiàn)更換Jupyter Notebook內(nèi)核Python版本,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • 基于Python的微信機器人開發(fā) 微信登錄和獲取好友列表實現(xiàn)解析

    基于Python的微信機器人開發(fā) 微信登錄和獲取好友列表實現(xiàn)解析

    這篇文章主要介紹了Python微信機器人開發(fā) 微信登錄和獲取好友列表實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • 基于Python制作一副撲克牌過程詳解

    基于Python制作一副撲克牌過程詳解

    這篇文章主要介紹了基于Python制作一副撲克牌過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • 淺談openpyxl庫,遇到批量合并單元格的問題

    淺談openpyxl庫,遇到批量合并單元格的問題

    這篇文章主要介紹了淺談openpyxl庫,遇到批量合并單元格的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • pytest實戰(zhàn)技巧之參數(shù)化基本用法和多種方式

    pytest實戰(zhàn)技巧之參數(shù)化基本用法和多種方式

    本文介紹了pytest參數(shù)化的基本用法和多種方式,幫助讀者更好地使用這個功能,同時,還介紹了一些高級技巧,如動態(tài)生成參數(shù)名稱、參數(shù)化的組合和動態(tài)生成參數(shù)化裝飾器,幫助讀者更靈活地使用參數(shù)化,感興趣的朋友參考下吧
    2023-12-12
  • Python實現(xiàn)識別圖像中人物的示例代碼

    Python實現(xiàn)識別圖像中人物的示例代碼

    這篇文章主要介紹了通過face_recognition提供的demo代碼,簡單調(diào)整了一下,從而實現(xiàn)識別圖像中人物的功能,感興趣的可以跟隨小編一起試試
    2022-01-01
  • 使用python構(gòu)建一個自己的聊天室

    使用python構(gòu)建一個自己的聊天室

    這篇文章主要介紹了使用python構(gòu)建一個自己的聊天室,有助于幫助小伙伴們理解掌握Python的socket編程,需要的朋友可以參考下
    2023-04-04
  • python 提取文件指定列的方法示例

    python 提取文件指定列的方法示例

    這篇文章主要介紹了python 提取文件指定列的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • 利用Python的PyPDF2庫提取pdf中的圖片

    利用Python的PyPDF2庫提取pdf中的圖片

    本篇給大家分享一下通過Python的PyPDF2庫提取pdf中的圖片方法,文中有詳細的代碼示例和流程步驟,感興趣的同學可以閱讀一下
    2023-05-05

最新評論

富源县| 扬中市| 桂平市| 宁国市| 北辰区| 兴宁市| 长治县| 商水县| 祥云县| 迁西县| 孝义市| 达拉特旗| 日照市| 普兰县| 太原市| 太仓市| 宝丰县| 江安县| 眉山市| 芒康县| 塔城市| 镇原县| 玉溪市| 墨竹工卡县| 灵璧县| 四平市| 仁寿县| 松江区| 安多县| 临朐县| 五寨县| 岳阳县| 平乡县| 民县| 南通市| 静宁县| 济阳县| 基隆市| 全南县| 汾西县| 齐齐哈尔市|