|
| 1 | +## PostgreSQL explain analyze 火山图 图形化软件 pg_flame |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2020-12-17 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , pg_flame , explain , 火山图 |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +https://github.com/mgartner/pg_flame |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +demo |
| 20 | + |
| 21 | +https://mgartner.github.io/pg_flame/flamegraph.html |
| 22 | + |
| 23 | +# pg_flame [](https://github.com/mgartner/pg_flame/releases) [](https://travis-ci.com/mgartner/pg_flame) |
| 24 | + |
| 25 | +A flamegraph generator for Postgres `EXPLAIN ANALYZE` output. |
| 26 | + |
| 27 | +<a href="https://mgartner.github.io/pg_flame/flamegraph.html"> |
| 28 | + <img width="700" src="https://user-images.githubusercontent.com/1128750/67738754-16f0c300-f9cd-11e9-8fc2-6acc6f288841.png"> |
| 29 | +</a> |
| 30 | + |
| 31 | +## Demo |
| 32 | + |
| 33 | +Try the demo [here](https://mgartner.github.io/pg_flame/flamegraph.html). |
| 34 | + |
| 35 | +## Installation |
| 36 | + |
| 37 | +### Homebrew |
| 38 | + |
| 39 | +You can install via Homebrew with the follow command: |
| 40 | + |
| 41 | +``` |
| 42 | +$ brew install mgartner/tap/pg_flame |
| 43 | +``` |
| 44 | + |
| 45 | +### Download pre-compiled binary |
| 46 | + |
| 47 | +Download one of the compiled binaries [in the releases |
| 48 | +tab](https://github.com/mgartner/pg_flame/releases). Once downloaded, move |
| 49 | +`pg_flame` into your `$PATH`. |
| 50 | + |
| 51 | +### Docker |
| 52 | + |
| 53 | +Alternatively, if you'd like to use Docker to build the program, you can. |
| 54 | + |
| 55 | +``` |
| 56 | +$ docker pull mgartner/pg_flame |
| 57 | +``` |
| 58 | + |
| 59 | +### Build from source |
| 60 | + |
| 61 | +If you'd like to build a binary from the source code, run the following |
| 62 | +commands. Note that compiling requires Go version 1.13+. |
| 63 | + |
| 64 | +``` |
| 65 | +$ git clone https://github.com/mgartner/pg_flame.git |
| 66 | +$ cd pg_flame |
| 67 | +$ go build |
| 68 | +``` |
| 69 | + |
| 70 | +A `pg_flame` binary will be created that you can place in your `$PATH`. |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +The `pg_flame` program reads a JSON query plan from standard input and writes |
| 75 | +the flamegraph HTML to standard ouput. Therefore you can pipe and direct input |
| 76 | +and output however you desire. |
| 77 | + |
| 78 | +### Example: One-step |
| 79 | + |
| 80 | +```bash |
| 81 | +$ psql dbname -qAtc 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) SELECT id FROM users' \ |
| 82 | + | pg_flame \ |
| 83 | + > flamegraph.html \ |
| 84 | + && open flamegraph.html |
| 85 | +``` |
| 86 | + |
| 87 | +### Example: Multi-step with SQL file |
| 88 | + |
| 89 | +Create a SQL file with the `EXPLAIN ANALYZE` query. |
| 90 | + |
| 91 | +```sql |
| 92 | +-- query.sql |
| 93 | +EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) |
| 94 | +SELECT id |
| 95 | +FROM users |
| 96 | +``` |
| 97 | + |
| 98 | +Then run the query and save the JSON to a file. |
| 99 | + |
| 100 | +```bash |
| 101 | +$ psql dbname -qAtf query.sql > plan.json |
| 102 | +``` |
| 103 | + |
| 104 | +Finally, generate the flamegraph HTML. |
| 105 | + |
| 106 | +``` |
| 107 | +$ cat plan.json | pg_flame > flamegraph.html |
| 108 | +``` |
| 109 | + |
| 110 | +### Example: Docker |
| 111 | + |
| 112 | +If you've followed the Docker installation steps above, you can pipe query plan |
| 113 | +JSON to a container and save the output HTML. |
| 114 | + |
| 115 | +``` |
| 116 | +$ psql dbname -qAtc 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) SELECT id FROM users' \ |
| 117 | + | docker run -i pg_flame \ |
| 118 | + > flamegraph.html |
| 119 | +``` |
| 120 | + |
| 121 | +## Background |
| 122 | + |
| 123 | +[Flamegraphs](http://www.brendangregg.com/flamegraphs.html) were invented by |
| 124 | +Brendan Gregg to visualize CPU consumption per code-path of profiled software. |
| 125 | +They are useful visualization tools in many types of performance |
| 126 | +investigations. Flamegraphs have been used to visualize Oracle database |
| 127 | +[query |
| 128 | +plans](https://blog.tanelpoder.com/posts/visualizing-sql-plan-execution-time-with-flamegraphs/) |
| 129 | +and [query |
| 130 | +executions](https://externaltable.blogspot.com/2014/05/flame-graphs-for-oracle.html) |
| 131 | +, proving useful for debugging slow database queries. |
| 132 | + |
| 133 | +Pg_flame is in extension of that work for Postgres query plans. It generates a |
| 134 | +visual hierarchy of query plans. This visualization identifies the relative |
| 135 | +time of each part of a query plan. |
| 136 | + |
| 137 | +This tool relies on the |
| 138 | +[`spiermar/d3-flame-graph`](https://github.com/spiermar/d3-flame-graph) plugin to |
| 139 | +generate the flamegraph. |
| 140 | + |
| 141 | + |
| 142 | +#### [PostgreSQL 许愿链接](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216") |
| 143 | +您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。[开不开森](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216"). |
| 144 | + |
| 145 | + |
| 146 | +#### [9.9元购买3个月阿里云RDS PostgreSQL实例](https://www.aliyun.com/database/postgresqlactivity "57258f76c37864c6e6d23383d05714ea") |
| 147 | + |
| 148 | + |
| 149 | +#### [PostgreSQL 解决方案集合](https://yq.aliyun.com/topic/118 "40cff096e9ed7122c512b35d8561d9c8") |
| 150 | + |
| 151 | + |
| 152 | +#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
0 commit comments