PostgreSQL学徒

又一款开源巡检工具,不愧为postgres.ai出品,给力~

前言

针对 PostgreSQL 的优秀开源巡检工具有很多,之前也都有所介绍

今天介绍另外一款开源的巡检工具:postgres-checkup,不同于 pg_gather 与 pg_collector,postgres-checkup 更加着重与问题的分析与解决。

Postgres Checkup (postgres-checkup) is a new kind of diagnostics tool for a deep analysis of a Postgres database health. It detects current and potential issues with database performance, scalability and security. It also produces recommendations on how to resolve or prevent them.

每份巡检报告都包含三部分内容:

  1. "Observations": automatically collected data. This is to be consumed by an expert DBA.
  2. "Conclusions": what we conclude from the Observations, stated in plain English in the form that is convenient for engineers who are not DBA experts.
  3. "Recommendations": action items, what to do to fix the discovered issues.

我们可以根据这三项内容来决定来决定优化什么、如何优化以及何时优化。

安装

安装步骤不花太多笔墨,主要一些前置依赖以及在编译过程中需要调整一下 go 的代理。

sudo yum install -y git postgresql coreutils jq golang

# Optional (to generate PDF/HTML reports)
sudo yum install -y pandoc
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar xvf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
sudo mv wkhtmltox/bin/wkhtmlto* /usr/local/bin
sudo yum install -y libpng libjpeg openssl icu libX11 libXext libXrender xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi

git clone https://gitlab.com/postgres-ai/postgres-checkup.git
# Use --branch to use specific release version. For example, to use version 1.1:
#   git clone --branch 1.1 https://gitlab.com/postgres-ai/postgres-checkup.git
cd postgres-checkup

目前的主要巡检项包括

  • System Information
  • Version Information
  • Postgres Settings
  • Cluster Information
  • Extensions
  • Postgres Setting Deviations
  • Altered Settings
  • Disk Usage and File System Type
  • pg_stat_statements and pg_stat_kcache Settings
  • Autovacuum: Current Settings
  • Autovacuum: Transaction ID Wraparound Check
  • Globally Aggregated Query Metrics
  • Workload Type ("The First Word" Analysis)
  • Top-50 Queries by total_time
  • Table Sizes
  • Integer (int2, int4) Out-of-range Risks in PKs

并且还在不断完善中,以下 ✅ 的内容便是当前支持的巡检项。

Image

小试牛刀

报告最开始处便让人眼前一亮

Image

比如此处的 Version Infomation,显式有 P2,提示需要进行小版本升级

[P2] The minor version being used (16.1) is not up-to-date (the newest version: 16.4). See the full list of changes between 16.1 and 16.4.

Image

而 Autovacuum 的话,则有一个更为严重的 P1

Autovacuum is not well-tuned. The following parameters are default, meaning that autovacuum behavior is far from optimal for an OLTP workload leading to higher levels of bloat in tables and indexes, lagging statistics:

Autovacuum 未经过良好调优。以下参数为默认参数,这意味着 autovacuum 行为远非 OLTP 工作负载的最佳选择,导致表和索引膨胀程度更高,统计数据滞后

Image

并且十分贴心地贴出了一些最佳实践的文章。在报告最低处,还有一项  Integer (int2, int4) Out-of-range Risks in PKs 的检测项,这一点往往被许多开发人员甚至 DBA 所忽视,由于主键的特殊性,int4 在稍微繁忙一点的系统中,很快便会消耗完,如下这种暴力操作当然可以

ALTER TABLE your_table
DROP CONSTRAINT your_table_pkey;

ALTER TABLE your_table
ADD PRIMARY KEY (new_column);

但是这种行为是为加 8 级锁的,很容易导致锁阻塞,恶性循环。因此最佳实践当然不能如此操作,应该这样👇🏻

Image

除此之外,负载分析也是让我眼前一亮的功能 (需要借助 pg_stat_kcache 和 pg_stat_statements)

Image

将查询进行了归类,包括基于时间的微分、基于调用次数的微分等等,以一个宏观视角,对数据库进行诊断分析,宏观优化旨在减少资源消耗以及改善用户体验,因此这一项巡检项也是使得 postgres-checkup 特别吸引我的地方。

另外,像其他的一些常规巡检项,比如表膨胀,索引膨胀,慢查询分析,负载分析等等,就不再介绍,感兴趣的读者请自行体验。

小结

不愧是 postgres.ai 出品,这个巡检工具可以看到大部分 postgres-howto 的影子,浓缩于其中,不失为一款小而精的巡检工具!

另外,前阵子白鳝老师分享了一篇"对想学习PG数据库的朋友的一些建议",当下 PostgreSQL 的势头以及机遇不用多说,DDDD,市面上关于 PostgreSQL 的人才需求迫在眉睫,因此,也借此机会,分享一点笔者学习 PostgreSQL 的方式方法,如何快速精进,成为一名 PostgreSQL 的高手。

周三晚上,不见不散。

Image