gp_gather_object_size script,zerosizeobject

來源:互聯網
上載者:User

gp_gather_object_size script,zerosizeobject

由於資料庫物件(table)太多太大,而且業務比較繁忙,在收集統計對象大小資訊的過程中經常會增刪改對象,導致資料庫報對象不存在的錯誤,於是寫了個指令碼用於完成上述功能,併到處到csv檔案便於分發相關維護、開發人員。

gp_gather_object_size script

#!/usr/bin/env python# -*- coding: UTF-8 -*-## Copyright [Gtlions Lai].# Create Date:# Update Date:"""summarization ahout this script.detail ahout this script   Class(): summarization about Class   ...   function(): summarization about function   ..."""__authors__ = '"Gtlions Lai" <gtlions.l@qq.com>'import psycopg2import csvdb = psycopg2.connect(dbname="gtlions", user="gpadmin", host="10.1.1.1")# db = psycopg2.connect(dbname="gtlions", user="gpadmin", host="10.1.1.1")# db = psycopg2.connect(dbname="gtlions", user="gpadmin", host="10.1.1.1")cur = db.cursor()cur.execute('select current_database()')current_database = cur.fetchone()f = open("gp_object_size" + current_database[0] + ".csv", "w")writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)cur.execute(    '''select a.schemaname ,a.tablename ,a.tableowner from pg_tables a where a.schemaname not like 'pg_temp%' and a.schemaname not in ('gp_toolkit','information_schema','pg_catalog','gpmg') order by 1,2;''')writer.writerow(("schemaname", "tablename", "tableowner", "size-1", "size-byte"), )for object in cur.fetchall():    objectname = object[0] + '.' + object[1]    try:        cur.execute(            "select pg_size_pretty(pg_total_relation_size('" + objectname + "')),pg_total_relation_size('" + objectname + "');")        sizeinfo = cur.fetchone()        writer.writerow(object + sizeinfo)    except psycopg2.ProgrammingError, e:        print ef.close()cur.close()db.commit()db.close()


-E0F-

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.