pg_flame postgresql EXPLAIN ANALYZE 火焰圖工具

pg_flame 是golang 編寫的一個將pg的EXPLAIN ANALYZE 轉換爲火焰圖,使用簡單
如下是一個簡單的demohtml

環境準備

  • docker-compose 文件
 
version: "3"
services:
  postgres:
    image: postgres:9.6.11
    ports:
    - "5432:5432"
    environment:
    - "POSTGRES_PASSWORD:dalong"
 
 
  • 源碼編譯
    clone 代碼
 
git clone <a href="https://github.com/mgartner/pg_flame.git">https://github.com/mgartner/pg_flame.git</a>
goreleaser release --snapshot --skip-publish --rm-dist

簡單修改.goreleaser.yml支持windowslinux

# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
builds:
- env:
  - CGO_ENABLED=0
  goos:
    - linux
    - darwin
    - windows
archives:
- replacements:
    darwin: Darwin
    linux: Linux
    windows: windows
    386: i386
    amd64: x86_64
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ .Tag }}-next"
changelog:
  sort: asc
 
  • sql 腳本
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    userename text,
    userage integer
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX users_pkey ON users(id int4_ops);
CREATE TABLE userapps (
    id SERIAL PRIMARY KEY,
    appname text,
    appversion text,
    userid integer
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX userapps_pkey ON userapps(id int4_ops);
INSERT INTO "public"."userapps"("id","appname","appversion","userid")
VALUES
(1,E'login',E'v1',1),
(2,E'view',E'v2.0',2);
INSERT INTO "public"."users"("id","userename","userage")
VALUES
(1,E'dalong',11),
(2,E'app',22);
 
 

使用

  • 啓動pg
docker-compose up -d
  • 生成json 格式的 EXPLAIN ANALYZE
psql -U postgres -h 127.0.0.1 postgres -qAtc 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) select * from userapps a join users b on a.id=b.id' > plan.json
  • 生成報告結果
cat plan.json | ./dist/pg_flame_darwin_amd64/pg_flame > result.html
  • 效果

 

 

 

說明

pg_flame 多平臺構建使用了goreleaser,若是須要構建跨平臺的語言包,須要安裝,並且上邊數據比較少,看的不是很明顯git

參考資料

https://github.com/mgartner/pg_flamegithub

相關文章
相關標籤/搜索