Skip to content

Commit

Permalink
Use case statement in name function
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Gartner committed Oct 24, 2019
1 parent 41a07f4 commit 5c3c8cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ A flamegraph generator for Postgres `EXPLAIN ANALYZE` output.

## Usage

Generate an annotated query plan in JSON.
1. Generate a query plan in JSON by prefixing a SQL query with
`EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)`. Save the output to a file.

```
psql lob_local -qAtc 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) SELECT id FROM bears' > plan.json
psql lob_local -qAtc 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) SELECT id FROM users' > plan.json
```

Generate the flamegraph.
2. Then generate the flamegraph by passing the JSON as standard input to
`pg_flame` and direct standard output to a file.

```
cat plans.json | ./pg_flame > flamegraph.html
```

Open `flamegraph.html` in a browser of your choice.
3. Open `flamegraph.html` in a browser of your choice.

## Background

Expand All @@ -30,9 +32,9 @@ for debugging slow database queries.

Pg_flame is in extension of that work for Postgres query plans. Instead of
being used to graph CPU time of internal Postgres functions, it generates a
visual hierarchy to query plans. This visualization identifies the relative
time of each part of a query plan, helping to direct optimization efforts.
visual hierarchy of query plans. This visualization identifies the relative
time of each part of a query plan.

This tool relies on the
[`d3-flame-graph`](https://github.com/spiermar/d3-flame-graph) plugin to
[`spiermar/d3-flame-graph`](https://github.com/spiermar/d3-flame-graph) plugin to
generate the flamegraph.
11 changes: 5 additions & 6 deletions pkg/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,14 @@ func convertPlanNode(n plan.Node, color string) Flame {
}

func name(n plan.Node) string {
if n.Table != "" && n.Index != "" {
switch {
case n.Table != "" && n.Index != "":
return fmt.Sprintf("%s using %s on %s", n.Method, n.Index, n.Table)
}

if n.Table != "" {
case n.Table != "":
return fmt.Sprintf("%s on %s", n.Method, n.Table)
default:
return n.Method
}

return n.Method
}

func detail(n plan.Node) string {
Expand Down

0 comments on commit 5c3c8cf

Please sign in to comment.