首页 > 其他分享 >【PG】列出表以及大小

【PG】列出表以及大小

时间:2024-03-14 21:24:06浏览次数:27  
标签:inpPort db echo pgClass PG 大小 inpDBName 列出 size

This post demonstrates an example of a Bash script that connects to a DB and prints a list of tables, their records number, and size.

The result set is sorted in descending order by size and then by number of records.

In addition, the output of the script is written into a report trace file, placed in the /tmp directory.


#!/bin/bash

##########################################
#
# check_size_db_objects_and_rowsnum.sh
#
# This script connects to the DB and
# retrives the info 
# about tables number of records and size.
#
# Date: 11-Nov-2022
#
# Author: Dmitry
#
##########################################

helpFunction()
{
   echo ""
   echo "Usage: $0 -h db_hostname -p port -U db_username -d db_name"
   echo -e "\t-h Postgres db hostname"
   echo -e "\t-p Postgers db port"
   echo -e "\t-U Postgres db username"
   echo -e "\t-d Postgres db name"
   echo -e " "
   echo -e "Example how to run: $0 -h localhost -p 5432 -U my_db_user -d my_db_name "
   echo -e " "
   exit 1 # Exit script after printing help
}

while getopts "h:p:U:d:" opt
do
   case "$opt" in
      h ) inpHost="$OPTARG" ;;
      p ) inpPort="$OPTARG" ;;
      U ) inpDBUser="$OPTARG" ;;
      d ) inpDBName="$OPTARG" ;;
      ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
   esac
done

# Print helpFunction in case parameters are empty
if [ -z "$inpHost" ] || [ -z "$inpPort" ] || [ -z "$inpDBUser" ] || [ -z "$inpDBName" ] 
then
   echo "Some or all of the parameters are empty";
   helpFunction
fi

echo " " 
echo "Input parameters:"
echo "---------------- " 
echo "inpHost=$inpHost"
echo "inpPort=$inpPort" 
echo "inpDBUser=$inpDBUser"
echo "inpDBName=$inpDBName"
echo "---------------- "

export the_yyyymmdd=$(date '+%Y%m%d')
export hh24miss=$(date '+%H%M%S')

psql -h $inpHost -p $inpPort -U $inpDBUser -d $inpDBName -qtX << EOF 
SELECT  '$the_yyyymmdd', '$hh24miss', '$inpPort', '$inpDBUser', '$inpDBName',
  pgClass.relname, 
  to_char(pgClass.reltuples, '999999999999999999') row_nums, 
  to_char(pg_relation_size(pgClass.oid)/1024/1024, '999999999999999999') AS tablesize_mega_bytes
FROM pg_class pgClass
INNER JOIN pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND pgClass.relkind='r'
ORDER BY cast(to_char(pgClass.reltuples, '999999999999999999') as double precision) DESC,
         cast(to_char(pg_relation_size(pgClass.oid)/1024/1024, '999999999999999999') as double precision) DESC;
EOF

output_name="/tmp/pg_object_size_${the_yyyymmdd}_${hh24miss}.trc"

psql -h $inpHost -p $inpPort -U $inpDBUser -d $inpDBName -qtX << EOF >> ${output_name}
SELECT  '$the_yyyymmdd', '$hh24miss', '$inpPort', '$inpDBUser', '$inpDBName',
  pgClass.relname, 
  to_char(pgClass.reltuples, '999999999999999999') row_nums, 
  to_char(pg_relation_size(pgClass.oid)/1024/1024, '999999999999999999') AS tablesize_mega_bytes
FROM pg_class pgClass
INNER JOIN pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND pgClass.relkind='r'
ORDER BY cast(to_char(pgClass.reltuples, '999999999999999999') as double precision) DESC,
         cast(to_char(pg_relation_size(pgClass.oid)/1024/1024, '999999999999999999') as double precision) DESC;
EOF

echo " "
echo "End"
echo " "

标签:inpPort,db,echo,pgClass,PG,大小,inpDBName,列出,size
From: https://www.cnblogs.com/Jeona/p/18074000

相关文章

  • 【PG】查询数据大小
    #!/bin/bash##################################################################calculate_a_pure_size_of_the_postgres_db.sh##ThisscriptcalculatesapuresizeofthePostgresDB##DB_Pure_Size=DB_Size-DB_Tables_Bloat-DB_Indexes_Bloat##Aut......
  • 【PG】查看PG对象大小增长情况
    #!/bin/bash####################################################check_postgresql_db_table_rowsnum_and_sizing.sh##Thisscriptsiteratesartidatabasesand#populatessometargetdbtablewith#infoabouttablesrownumandsizing##Date:10-Aug-......
  • shell脚本实现子母大小写转换
    文章目录把变量中的第一个字符换成大写把变量中的所有小写字母,全部替换为大写把变量中的第一个字符换成小写把变量中的所有大写字母,全部替换为小写用tr需要新增变量,用declare或typeset需要在变量赋值前或者赋值后单独声明,都有些麻烦此方法为bash4.0以后新增,bash4......
  • 文本的适应父部件的大小
    使用FittedBox:FittedBox部件可以根据其子部件的大小调整其大小。您可以将Text部件放置在FittedBox中,并将fit属性设置为BoxFit.contain,以便在父部件大小变化时,文本会自动调整大小以适应父部件。FittedBox(fit:BoxFit.contain,child:Text('ResizableContent'),),使用L......
  • PG14:auth_delay 插件源码分析
    auth_delay让服务器在报告身份验证失败前短暂暂停,以增加对数据库密码进行暴力破解的难度。需要注意的是,这对阻止拒绝服务攻击毫无帮助,甚至可能加剧攻击,因为在报告身份验证失败前等待的进程仍会占用连接。要使用这个模块必须要在postgresql.conf中配置参数shared_preload_libr......
  • MSSQL 查询每个表的占用空间大小
     SELECTt.NAMEASTableName,p.rowsASRowCounts,SUM(a.total_pages)*8ASTotalSpaceKB,SUM(a.used_pages)*8ASUsedSpaceKB,(SUM(a.total_pages)-SUM(a.used_pages))*8ASUnusedSpaceKBFROMsys.tablestI......
  • PG14:adminpack 插件源码分析
    adminpack提供了大量支持功能,pgAdmin和其他管理工具可以使用这些功能提供额外功能,例如远程管理服务器日志文件。默认情况下,只有数据库超级用户才能使用所有这些功能,但其他用户也可以使用GRANT命令使用这些功能。我们先来看一下他支持的函数,可以通过\dx+adminpack来进行查......
  • (笔记)FPGA多周期路径及set_multicycle_path详解
    默认情况下综合工具会把每条路径定义为单周期路径,即源触发器在时钟的任一边沿启动(launch)的数据都应该由目的触发器在时钟的下一上升沿捕获(capture)。有的设计可能存在时序例外(timingexceptions),如多周期路径、虚假路径等。数据从起点到终点的传输时间需要一个时钟周期以上才能稳定......
  • FPGA交通信号灯设计报告(VHDL语言)
    FPGA的大作业我选择了交通灯控制系统的设计,此课程只有2个学分,因此只需要在相应软件仿真出结果即可。以下是我的设计报告,当时写的匆忙,没有对代码进行优化改进,但仿真结果是正确的,可以给大家提供一下思路。一、任务分析1.输入和输出2.多进程3.特殊情况4.注意二、quartus......
  • m基于FPGA的Alamouti编码verilog实现,包含testbench测试文件
    1.算法仿真效果 本系统进行了Vivado2019.2平台的开发,结果如下:   2.算法涉及理论知识概要        在无线通信领域,多天线技术是提高系统容量和可靠性的关键手段之一。Alamouti编码是空时编码(STC)的一种,它为两发射天线的系统提供了一种全速率、全分集的简单编码方......