PostgreSQL and bloat

The bucardo project has released its nagios plugins for PostgreSQL and we can extract from them this nice view in order to check for table and index bloatinto our PostgreSQL databases:

下面是检查表空间的使用情况的脚本:

CREATE OR REPLACE VIEW bloat AS
      SELECT
        schemaname, tablename, reltuples::bigint, relpages::bigint, otta,
        ROUND(CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages/otta::numeric END,1) AS tbloat,
        relpages::bigint - otta AS wastedpages,
        bs*(sml.relpages-otta)::bigint AS wastedbytes,
        pg_size_pretty((bs*(relpages-otta))::bigint) AS wastedsize,
        iname, ituples::bigint, ipages::bigint, iotta,
        ROUND(CASE WHEN iotta=0 OR ipages=0 THEN 0.0 ELSE ipages/iotta::numeric END,1) AS ibloat,
        CASE WHEN ipages < iotta THEN 0 ELSE ipages::bigint - iotta END AS wastedipages,
        CASE WHEN ipages < iotta THEN 0 ELSE bs*(ipages-iotta) END AS wastedibytes,
        CASE WHEN ipages < iotta THEN pg_size_pretty(0::bigint) ELSE pg_size_pretty((bs*(ipages-iotta))::bigint) END AS wastedisize
      FROM (
        SELECT
          schemaname, tablename, cc.reltuples, cc.relpages, bs,
          CEIL((cc.reltuples*((datahdr+ma-
            (CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))/(bs-20::float)) AS otta,
          COALESCE(c2.relname,‘?‘) AS iname, COALESCE(c2.reltuples,0) AS ituples, COALESCE(c2.relpages,0) AS ipages,
          COALESCE(CEIL((c2.reltuples*(datahdr-12))/(bs-20::float)),0) AS iotta -- very rough approximation, assumes all cols
        FROM (
          SELECT
            ma,bs,schemaname,tablename,
            (datawidth+(hdr+ma-(case when hdr%ma=0 THEN ma ELSE hdr%ma END)))::numeric AS datahdr,
            (maxfracsum*(nullhdr+ma-(case when nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2
          FROM (
            SELECT
              schemaname, tablename, hdr, ma, bs,
              SUM((1-null_frac)*avg_width) AS datawidth,
              MAX(null_frac) AS maxfracsum,
              hdr+(
                SELECT 1+count(*)/8
                FROM pg_stats s2
                WHERE null_frac<>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename
              ) AS nullhdr
            FROM pg_stats s, (
              SELECT
                (SELECT current_setting(‘block_size‘)::numeric) AS bs,
                CASE WHEN substring(v,12,3) IN (‘8.0‘,‘8.1‘,‘8.2‘) THEN 27 ELSE 23 END AS hdr,
                CASE WHEN v ~ ‘mingw32‘ THEN 8 ELSE 4 END AS ma
              FROM (SELECT version() AS v) AS foo
            ) AS constants
            GROUP BY 1,2,3,4,5
          ) AS foo
        ) AS rs
        JOIN pg_class cc ON cc.relname = rs.tablename
        JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname
        LEFT JOIN pg_index i ON indrelid = cc.oid
        LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid
      ) AS sml
      WHERE sml.relpages - otta > 0 OR ipages - iotta > 10
      ORDER BY wastedbytes DESC, wastedibytes DESC;

更详细可以参见check_postgres脚本:

http://bucardo.org/wiki/Check_postgres

参考:

http://pgsql.tapoueh.org/site/html/news/20080131.bloat.html

时间: 2024-10-12 09:19:43

PostgreSQL and bloat的相关文章

PostgreSQL bloat 检查与处理

1.工具软件 pg_bloat_check.py(pg_bloat_check-master.zip) https://github.com/keithf4/pg_bloat_check 软件包需求: 1).pgstattuple,Pg源码crontrib目录. 2).python 2.6以上. 3).argparse-1.4.0.tar.gz 4).psycopg2-2.6.2.tar.gz 5).setuptools-23.1.0.tar.gz 2.安装步骤: 1).安装Pg扩展pgstat

pg_repack bloat 处理测试初步

一.软件安装 1.软件需求: postgresql-9.5.2.tar.gz pg_repack-1.3.4.zip 2.安装pg_repack [[email protected] pg_repack-1.3.4]# export PATH=/opt/pgsql/9.5.2/bin:$PATH [[email protected] pg_repack-1.3.4]# export LD_LIBRARY_PATH=/opt/pgsql/9.5.2/lib [[email protected] p

PostgreSQL index types and index bloating

warehouse_db=# create table item (item_id integer not null,item_name text,item_price numeric,item_data text);CREATE TABLEwarehouse_db=# create index item_idx on item(item_id);CREATE INDEX warehouse_db=# \di item_idx List of relations Schema | Name |

[翻译]开源PostgreSQL监控工具OPM

一个好消息:九月,PostgreSQL OPM开发小组发布了开源的PostgreSQL数据库监控套件的第一个RELEASE版本OPM v2.3.PostgreSQL是先进的高级数据库,但它的一个重要的缺陷是缺乏开源的专门的监控工具.与它的竞争对手相比,PostgreSQL社区没有能提供一套与Oracle Grid Control,MySQL Workbench 或 SQL Server Management Studio类似的产品化的监控工具.OPM项目正是一个完整的开源管理套件,它提供图形化的

PostgreSQL hot_standby_feedback 求证记录

今天看了同事写的的关于复制槽和hot_standby_feedback的帖子,将以前不是很清楚的地方和他一起讨论了一下,在这里做个记录,以防再次混乱: 参考同事的公众号文章(搜索公众号:PostgreSQL学徒): https://mp.weixin.qq.com/s?__biz=MzUyOTAyMzMyNg==&mid=2247483746&idx=1&sn=0dc832d9f3b65877f4605964e4fbc644&chksm=fa662953cd11a04561

postgresql Linux安装

1,改权限,执行命令:chmod 755 postgresql-9.2.4-1-linux-x64.run 2,执行命令安装数据库.进入文件所在目录,输入./postgresql-9.2.4-1-linux-x64.run .然后选择数据库安装目录. 本篇默认直接回车.即安装路径为/opt/PostgreSQL/9.2/ 3,选择数据保存的路径.本篇也默认.直接回车.即路径为:/opt/PostgreSQL/9.2/data 目录. 4,设置postgres账户的密码,以及设置端口号.本篇端口号

seam2.2根据已有数据库(postgresql)生成项目

首先呢,这是我向同哥请教的一个文题,然后同哥把整个过程给我解答了一遍,谢谢同哥的乃森及引针啦---- seam2.2根据已有数据库(postgresql)生成项目 一,建数据库 进入pgAdmin新建一个数据库然后用eclipse的插件ErMaster导出sql或者是自己手写sql新建数据表 ,这里新建了一个名为test的数据库 里面有两个表,member和department,为多对一的关系,注意不要忘了设置主键 二,进入seam目录运行 ./seam setup ./seam create-

postgresql学习之安装篇

---恢复内容开始--- 安装方法: 1.可以使用操作系统自带的安装源 2.可以使用官网下载的源码进行安装 3.可以使用编译好的包入.run格式的安装包安装(本文使用的是这种安装方法,下载地址http://www.postgres.cn/download) 安装之前首先需要为postgresql数据库新建一个管理员用户: groupadd postgres mkdir /home/postgres useradd postgres -g postgres -s /bin/bash -b /hom

PostgreSQL数据库中跨库访问解决方案

PostgreSQL跨库访问有3种方法:Schema,dblink,postgres_fdw. 方法A:在PG上建立不同SCHEMA,将数据和存储过程分别放到不同的schema上,经过权限管理后进行访问. 方法A的示例如下: 测试1(测试postgres超级用户对不同schema下对象的访问) 查看当前数据库中的schema postgres=# \dn List of schemas Name | Owner -------------------+--------- dbms_job_pro