暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Postgresql 检测表大小 与 PG13 安装pycopg 问题

AustinDatabases 2021-11-24
387

PG13 上安装pycopg2后,报找不到 libpq.so.5 的问题,之前在PG11 PG12上没有此问题,解决问题的方案为在使用 pycopg2的机器上安装 PostgreSQL 13 x86_64 


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 并比对目前剩余的空间后,在进行操作。




最后修改时间:2021-11-24 10:13:03
文章转载自AustinDatabases,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论