天天看點

--Postgresql 檢測表大小 與 PG13 安裝pycopg 問題

PG13 上安裝pycopg2後,報找不到 libpq.so.5 的問題,之前在PG11 PG12上沒有此問題,解決問題的方案為在使用 pycopg2的機器上安裝 PostgreSQL 13 x86_64 

--Postgresql 檢測表大小 與 PG13 安裝pycopg 問題

​​https://centos.pkgs.org/7/postgresql-13-x86_64/postgresql13-libs-13.5-1PGDG.rhel7.x86_64.rpm.html​​

可以下載下傳rpm包進行安裝,也可以直接  sudo yum -y install postgresql13-libs

主要的問題原因在于,找不到libpg.so.5 ,安裝libs 庫即可。

下面這段程式主要的起因是,大表更新,在公司DBA接到開發的工單要對PG資料庫的表進行批量的資料UPDATE, 在通過工具的執行中,發現磁盤空間急速的降低,從剩餘700G 在不到 5分鐘的情況下就損失了20G ,并且還在持續,工具中包含的任務比較多,在這樣的情況下,直接和DBA 告知,緊急叫停,因為如果觸發磁盤空間爆滿,業務停止,事情就大了。

通過pg_terminate_backend 将程式自動執行的業務停止,後面告知DBA将剩餘沒有做的表都查一遍空間,包含索引,然後将空間* 2.5 後發現目前剩餘的磁盤空間可以HOLD住後續的UPDATE 操作。

這裡就随即寫了一個程式(比較簡陋)可以将制定的表的占用的空間進行累加,這邊也給DB建議,在以後的UPDATE 操作中,先執行程式将需要UPDATE的表的空間進行計算,如果操作完畢後,剩餘的磁盤空間不足約定的磁盤空間,則操作不能進行,必須添加磁盤空間後才能操作。

#!/usr/bin/python3

import os

import sys

import psycopg2

import re

import subprocess

def check_table_size():

    conn = None

    conn = psycopg2.connect(database="postgres",user="admin",password="admin",host="192.168.198.100",port="5432")

    table_list = ["pgbench_accounts","pgbench_branches","pgbench_history","pgbench_tellers"]

    cur = conn.cursor()

    for table in table_list:

        z1 = 'select pg_relation_size(\''

        z2 = '\');'

        #print(z1+table+z2) 

        cur.execute(z1+table+z2)

        rows = cur.fetchall()

        size = 0

        for row in rows:

            size = int(row[0]) + size

    print('表總體大小合計')

    print(str(size) + '  byte')

    print('請確定資料磁盤空間在以下空間以上')

    print(str(float(size) * 2.5) + '  byte')

    conn.commit()

    conn.close

def check_index_size():

    conn = None

    conn = psycopg2.connect(database="postgres",user="admin",password="admin",host="192.168.198.100",port="5432")

    table_list = ["pgbench_accounts","pgbench_branches","pgbench_history","pgbench_tellers"]

    cur = conn.cursor()

    for table in table_list:

        z1 = 'select pg_indexes_size(\''

        z2 = '\');'

        #print(z1+table+z2) 

        cur.execute(z1+table+z2)

        rows = cur.fetchall()

        size = 0

        for row in rows:

            size = int(row[0]) + size

    print('表索引大小合計')

    print(str(size) + '  byte')

    conn.commit()

    conn.close

def check_index_size():

    conn = None

    conn = psycopg2.connect(database="postgres",user="admin",password="admin",host="192.168.198.100",port="5432")

    table_list = ["pgbench_accounts","pgbench_branches","pgbench_history","pgbench_tellers"]

    cur = conn.cursor()

    for table in table_list:

        z1 = 'select pg_indexes_size(\''

        z2 = '\');'

        #print(z1+table+z2) 

        cur.execute(z1+table+z2)

        rows = cur.fetchall()

        size = 0

        for row in rows:

            size = int(row[0]) + size

    print('表索引大小合計')

    print(str(size) + '  byte')

    conn.commit()

    conn.close

if __name__ == "__main__":

    check_table_size()

    check_index_size()

後續需要完善PG大表批量更新的操作, 需要對操作的表先進行check 并比對目前剩餘的空間後,在進行操作。