欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何為PostgreSQL的表自動(dòng)添加分區(qū)

 更新時(shí)間:2021年01月04日 17:08:17   作者:lottu  
這篇文章主要介紹了如何為PostgreSQL的表自動(dòng)添加分區(qū),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

演示目的:利用 python 來為 PostgreSQL 的表自動(dòng)添加分區(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');

上面示例單獨(dú)為表tbl_game_android_step_log;創(chuàng)建分區(qū);若多個(gè)表;用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')

五、加入定時(shí)任務(wù)

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

相關(guān)文章

  • postgresql 修改字段長度的操作

    postgresql 修改字段長度的操作

    這篇文章主要介紹了postgresql 修改字段長度的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL自動(dòng)更新時(shí)間戳實(shí)例代碼

    PostgreSQL自動(dòng)更新時(shí)間戳實(shí)例代碼

    最近有這么一個(gè)工程,需要使用postgresql數(shù)據(jù)庫,在數(shù)據(jù)庫中的好幾個(gè)表中都需要時(shí)間戳這個(gè)字段,這篇文章主要給大家介紹了關(guān)于PostgreSQL自動(dòng)更新時(shí)間戳的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • postgreSQL 使用timestamp轉(zhuǎn)成date格式

    postgreSQL 使用timestamp轉(zhuǎn)成date格式

    這篇文章主要介紹了postgreSQL 使用timestamp轉(zhuǎn)成date格式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Postgresql 如何清理WAL日志

    Postgresql 如何清理WAL日志

    這篇文章主要介紹了Postgresql 實(shí)現(xiàn)清理WAL日志的方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL表操作之表的創(chuàng)建及表基礎(chǔ)語法總結(jié)

    PostgreSQL表操作之表的創(chuàng)建及表基礎(chǔ)語法總結(jié)

    在PostgreSQL中創(chuàng)建表命令用于在任何給定的數(shù)據(jù)庫中創(chuàng)建新表,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL表操作之表的創(chuàng)建及表基礎(chǔ)語法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • PostgreSQL怎么創(chuàng)建分區(qū)表詳解

    PostgreSQL怎么創(chuàng)建分區(qū)表詳解

    數(shù)據(jù)庫表分區(qū)把一個(gè)大的物理表分成若干個(gè)小的物理表,并使得這些小物理表在邏輯上可以被當(dāng)成一張表來使用,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL怎么創(chuàng)建分區(qū)表的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • PostgreSQL索引失效會發(fā)生什么

    PostgreSQL索引失效會發(fā)生什么

    什么是索引失效?如果where過濾條件設(shè)置不合理,即使索引存在,且where過濾條件中包含索引列,也會導(dǎo)致全表掃描,索引不起作用。什么條件下會導(dǎo)致索引失效呢
    2022-09-09
  • postgresql 中的序列nextval詳解

    postgresql 中的序列nextval詳解

    這篇文章主要介紹了postgresql 中的序列nextval詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • PostgreSQL查看正在執(zhí)行的任務(wù)并強(qiáng)制結(jié)束的操作方法

    PostgreSQL查看正在執(zhí)行的任務(wù)并強(qiáng)制結(jié)束的操作方法

    這篇文章主要介紹了PostgreSQL查看正在執(zhí)行的任務(wù)并強(qiáng)制結(jié)束的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 使用pg_basebackup對Postgre進(jìn)行備份與恢復(fù)的實(shí)現(xiàn)

    使用pg_basebackup對Postgre進(jìn)行備份與恢復(fù)的實(shí)現(xiàn)

    這篇文章主要介紹了使用pg_basebackup對Postgre進(jìn)行備份與恢復(fù)的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論