pg_peek 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +38 -7
- data/app/assets/stylesheets/pg_peek/application.css +33 -0
- data/app/controllers/pg_peek/application_controller.rb +12 -0
- data/app/controllers/pg_peek/databases_controller.rb +15 -9
- data/app/controllers/pg_peek/queries_controller.rb +15 -3
- data/app/helpers/pg_peek/application_helper.rb +11 -0
- data/app/helpers/pg_peek/report_helper.rb +1 -1
- data/app/models/pg_peek/database.rb +14 -6
- data/app/models/pg_peek/pg_stat_statements/setting.rb +110 -0
- data/app/models/pg_peek/pg_stat_statements.rb +67 -4
- data/app/models/pg_peek/table.rb +12 -5
- data/app/queries/pg_peek/pg15/pg_stat_statements/endpoints.sql +1 -1
- data/app/queries/pg_peek/pg15/pg_stat_statements/jobs.sql +1 -0
- data/app/queries/pg_peek/pg15/pg_stat_statements/outliers_by_endpoint.sql +1 -1
- data/app/queries/pg_peek/pg15/pg_stat_statements/outliers_by_job.sql +1 -0
- data/app/queries/pg_peek/pg15/pg_stat_statements/shared_statements.sql +23 -0
- data/app/queries/pg_peek/pg15/pg_stat_statements/shared_summary.sql +14 -0
- data/app/queries/pg_peek/pg16/pg_stat_statements/endpoints.sql +1 -1
- data/app/queries/pg_peek/pg16/pg_stat_statements/jobs.sql +1 -0
- data/app/queries/pg_peek/pg16/pg_stat_statements/outliers_by_endpoint.sql +1 -1
- data/app/queries/pg_peek/pg16/pg_stat_statements/outliers_by_job.sql +1 -0
- data/app/queries/pg_peek/pg16/pg_stat_statements/shared_statements.sql +23 -0
- data/app/queries/pg_peek/pg16/pg_stat_statements/shared_summary.sql +14 -0
- data/app/queries/pg_peek/pg17/pg_stat_statements/endpoints.sql +1 -1
- data/app/queries/pg_peek/pg17/pg_stat_statements/jobs.sql +1 -0
- data/app/queries/pg_peek/pg17/pg_stat_statements/outliers_by_endpoint.sql +1 -1
- data/app/queries/pg_peek/pg17/pg_stat_statements/outliers_by_job.sql +1 -0
- data/app/queries/pg_peek/pg17/pg_stat_statements/shared_statements.sql +23 -0
- data/app/queries/pg_peek/pg17/pg_stat_statements/shared_summary.sql +14 -0
- data/app/queries/pg_peek/pg18/pg_stat_statements/endpoints.sql +1 -1
- data/app/queries/pg_peek/pg18/pg_stat_statements/jobs.sql +1 -0
- data/app/queries/pg_peek/pg18/pg_stat_statements/outliers_by_endpoint.sql +1 -1
- data/app/queries/pg_peek/pg18/pg_stat_statements/outliers_by_job.sql +1 -0
- data/app/queries/pg_peek/pg18/pg_stat_statements/shared_statements.sql +23 -0
- data/app/queries/pg_peek/pg18/pg_stat_statements/shared_summary.sql +14 -0
- data/app/queries/pg_peek/{pg14 → pg19}/indexes/all.sql +4 -0
- data/app/queries/pg_peek/{pg14 → pg19}/pg_stat_statements/endpoints.sql +1 -1
- data/app/queries/pg_peek/{pg14 → pg19}/pg_stat_statements/jobs.sql +1 -0
- data/app/queries/pg_peek/{pg14 → pg19}/pg_stat_statements/outliers.sql +1 -1
- data/app/queries/pg_peek/{pg14 → pg19}/pg_stat_statements/outliers_by_endpoint.sql +2 -2
- data/app/queries/pg_peek/{pg14 → pg19}/pg_stat_statements/outliers_by_job.sql +2 -1
- data/app/queries/pg_peek/pg19/pg_stat_statements/shared_statements.sql +23 -0
- data/app/queries/pg_peek/pg19/pg_stat_statements/shared_summary.sql +14 -0
- data/app/queries/pg_peek/{pg14 → pg19}/tables/stats.sql +1 -0
- data/app/reports/pg_peek/column.rb +7 -1
- data/app/reports/pg_peek/report.rb +3 -0
- data/app/reports/pg_peek/reports/databases.rb +1 -1
- data/app/reports/pg_peek/reports/indexes.rb +32 -1
- data/app/reports/pg_peek/reports/sessions.rb +8 -2
- data/app/reports/pg_peek/reports/shared_queries.rb +13 -0
- data/app/views/pg_peek/databases/show.html.erb +1 -9
- data/app/views/pg_peek/endpoints/index.html.erb +2 -0
- data/app/views/pg_peek/indexes/index.html.erb +7 -0
- data/app/views/pg_peek/jobs/index.html.erb +2 -0
- data/app/views/pg_peek/pg_stat_statements/_setting.html.erb +31 -0
- data/app/views/pg_peek/pg_stat_statements/show.html.erb +25 -18
- data/app/views/pg_peek/queries/_shared_summary.html.erb +8 -0
- data/app/views/pg_peek/queries/index.html.erb +1 -0
- data/app/views/pg_peek/queries/shared.html.erb +21 -0
- data/app/views/pg_peek/reports/_report.html.erb +5 -5
- data/app/views/pg_peek/tables/show.html.erb +4 -0
- data/config/routes.rb +3 -1
- data/lib/generators/pg_peek/install/templates/initializer.rb +8 -0
- data/lib/pg_peek/configuration.rb +3 -1
- data/lib/pg_peek/query_loader.rb +2 -1
- data/lib/pg_peek/version.rb +1 -1
- metadata +29 -14
- /data/app/queries/pg_peek/{pg14 → pg19}/activity/sessions.sql +0 -0
- /data/app/queries/pg_peek/{pg14 → pg19}/activity/summary.sql +0 -0
- /data/app/queries/pg_peek/{pg14 → pg19}/activity/waiting_locks.sql +0 -0
- /data/app/queries/pg_peek/{pg14 → pg19}/database/vitals.sql +0 -0
- /data/app/queries/pg_peek/{pg14 → pg19}/tables/dead_tuples.sql +0 -0
- /data/app/queries/pg_peek/{pg14 → pg19}/tables/inline_stats.sql +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a3e5082b6ed979c884fbb044bf19e7dbe6f217a54eb8b790b705e9fe2d2d89e0
|
|
4
|
+
data.tar.gz: 1c051ab7c11f36371f00079a255648922ab9e5418b861e00dce4f73ebdcc7e87
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dd079359dd6bca2b8bf71379ea27a0c1409c8b8a0cd5c833466ca441de034d8c659f9bc0c766521755b677ceffafc962b425ccf69387ba9cc81a1f23b837dcf9
|
|
7
|
+
data.tar.gz: 5525c13bdb28e225c6d9a5c4b15be86b4e234a60dbca4bf7fca16c602095aa7349742753f44adadbeedd0a58c8c6407c0a7a68a1352612897189fe8887265330
|
data/README.md
CHANGED
|
@@ -8,7 +8,7 @@ A Rails Engine that provides a web UI for monitoring and analyzing PostgreSQL qu
|
|
|
8
8
|
- **Query Performance Analysis**: Identify slow queries using the `pg_stat_statements` extension
|
|
9
9
|
- **SQLcommenter Support**: Parses and displays SQLcommenter tags from queries
|
|
10
10
|
- **Endpoint and Active Job Breakdowns**: Groups query cost by controller/action and by job class, with a per-job drill-down into its query shapes
|
|
11
|
-
- **Multi-Database Support**: Works with multiple database configurations (primary, replica, etc.)
|
|
11
|
+
- **Multi-Database Support**: Works with multiple database configurations (primary, replica, etc.). A database declared with `replica: true` in `database.yml` is marked as a replica on its overview, since its statistics are its own. Give it an entry in `config.connections` like any other, naming a class whose connection reaches the replica
|
|
12
12
|
|
|
13
13
|
## Installation
|
|
14
14
|
|
|
@@ -135,10 +135,41 @@ guessing — but you will be left waiting for a restart.
|
|
|
135
135
|
A restart also clears all collected statistics, so expect an empty dashboard
|
|
136
136
|
until your application has run for a while afterwards.
|
|
137
137
|
|
|
138
|
+
### Statements shared across the application
|
|
139
|
+
|
|
140
|
+
`pg_stat_statements` keeps one row per query shape, with the text of whichever
|
|
141
|
+
call it saw first, and comments are not part of the shape. A statement the whole
|
|
142
|
+
application issues therefore carries the SQLcommenter tags of one caller and
|
|
143
|
+
the calls and time of all of them. In the endpoint and job breakdowns that is
|
|
144
|
+
counted under the wrong name: one endpoint appears to enqueue every job in the
|
|
145
|
+
application, or one job to spend its time on the queue's own locking.
|
|
146
|
+
|
|
147
|
+
pg_peek leaves transaction control and session statements (`BEGIN`, `COMMIT`,
|
|
148
|
+
`SAVEPOINT`, `NOTIFY`, `SET`, ...) out of the endpoint and job breakdowns for
|
|
149
|
+
this reason. Name the rest yourself; a job queue's own tables are the usual
|
|
150
|
+
case:
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
# config/initializers/pg_peek.rb
|
|
154
|
+
config.unattributed_queries = [
|
|
155
|
+
'"good_jobs"', # a String is matched as it is, anywhere in the statement
|
|
156
|
+
/FROM pg_locks/i # a Regexp is matched by PostgreSQL
|
|
157
|
+
]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
A Regexp is handed to PostgreSQL as its source, so write it in PostgreSQL's
|
|
161
|
+
dialect: `\y` is a word boundary (`\b` is a backspace there), `\m` and `\M`
|
|
162
|
+
begin and end a word, and `\A` and `\z` do not exist; use `^` and `$`.
|
|
163
|
+
|
|
164
|
+
What they cost is still worth seeing, so they are not dropped: the endpoints
|
|
165
|
+
and jobs pages say how many statements were left out and how much database
|
|
166
|
+
time they took, and link to a list of them (`queries/shared`), shown without a
|
|
167
|
+
source. The queries page is not affected and keeps listing every statement.
|
|
168
|
+
|
|
138
169
|
### Requirements
|
|
139
170
|
|
|
140
171
|
- Rails 8.1+
|
|
141
|
-
- PostgreSQL
|
|
172
|
+
- PostgreSQL 15+ with `pg_stat_statements` extension (for query analysis features)
|
|
142
173
|
- Only officially supported PostgreSQL versions are supported
|
|
143
174
|
|
|
144
175
|
### Why not pg_stat_monitor?
|
|
@@ -168,8 +199,8 @@ to be installed. One data source that works everywhere beats two that disagree.
|
|
|
168
199
|
# Install dependencies
|
|
169
200
|
bundle install
|
|
170
201
|
|
|
171
|
-
# Start PostgreSQL
|
|
172
|
-
docker compose up
|
|
202
|
+
# Start PostgreSQL 19 (default for development)
|
|
203
|
+
docker compose up postgres19 -d
|
|
173
204
|
|
|
174
205
|
# Setup the dummy app database
|
|
175
206
|
bin/rails db:setup
|
|
@@ -186,9 +217,6 @@ bin/rails s
|
|
|
186
217
|
# Run databases
|
|
187
218
|
docker compose up -d
|
|
188
219
|
|
|
189
|
-
# PostgreSQL 14
|
|
190
|
-
bin/test-pg14
|
|
191
|
-
|
|
192
220
|
# PostgreSQL 15
|
|
193
221
|
bin/test-pg15
|
|
194
222
|
|
|
@@ -200,6 +228,9 @@ bin/test-pg17
|
|
|
200
228
|
|
|
201
229
|
# PostgreSQL 18
|
|
202
230
|
bin/test-pg18
|
|
231
|
+
|
|
232
|
+
# PostgreSQL 19
|
|
233
|
+
bin/test-pg19
|
|
203
234
|
```
|
|
204
235
|
|
|
205
236
|
## Testing
|
|
@@ -96,6 +96,8 @@ nav a:hover, nav a[aria-current] { color: var(--fg); }
|
|
|
96
96
|
|
|
97
97
|
nav .current-database { margin-left: 0.5rem; padding-left: 1rem; border-left: 1px solid var(--line); }
|
|
98
98
|
|
|
99
|
+
.replica { color: color-mix(in srgb, var(--warn) 70%, var(--dim)); }
|
|
100
|
+
|
|
99
101
|
.switcher { position: relative; display: inline-block; }
|
|
100
102
|
.switcher::after { content: "▾"; color: var(--dim); position: absolute; right: 0; top: 0; pointer-events: none; }
|
|
101
103
|
|
|
@@ -194,6 +196,8 @@ tbody th {
|
|
|
194
196
|
border-bottom: 1px solid var(--line);
|
|
195
197
|
}
|
|
196
198
|
tbody th + td { text-align: right; }
|
|
199
|
+
/* The whole row answers to the pointer, not the value alone. */
|
|
200
|
+
tbody tr:hover th { background: var(--panel); }
|
|
197
201
|
|
|
198
202
|
.num { text-align: right; white-space: nowrap; }
|
|
199
203
|
.nowrap { white-space: nowrap; }
|
|
@@ -260,6 +264,35 @@ details.tags { border: 0; padding: 0; margin: 0.25rem 0 0; font-size: inherit; }
|
|
|
260
264
|
details.tags summary { display: inline; }
|
|
261
265
|
details.tags[open] summary { margin-bottom: 0.25rem; }
|
|
262
266
|
|
|
267
|
+
/* A setting and what it does. Each is a row that opens: name and (i) on the
|
|
268
|
+
left, value on the right like a label/value table, and the explanation
|
|
269
|
+
underneath at the width of the page rather than of a table cell. */
|
|
270
|
+
.settings { margin: 0.5rem 0; }
|
|
271
|
+
details.setting { border: 0; border-bottom: 1px solid var(--line); padding: 0; margin: 0; }
|
|
272
|
+
details.setting summary {
|
|
273
|
+
display: flex;
|
|
274
|
+
align-items: center;
|
|
275
|
+
gap: 1.25ch;
|
|
276
|
+
padding: 0.4rem 0.75rem 0.4rem 0;
|
|
277
|
+
}
|
|
278
|
+
details.setting[open] summary { margin-bottom: 0; }
|
|
279
|
+
details.setting .value { margin-left: auto; color: var(--fg); }
|
|
280
|
+
details.setting .info {
|
|
281
|
+
width: 1.5em;
|
|
282
|
+
height: 1.5em;
|
|
283
|
+
line-height: 1.4em;
|
|
284
|
+
text-align: center;
|
|
285
|
+
border: 1px solid currentColor;
|
|
286
|
+
border-radius: 50%;
|
|
287
|
+
font-size: 0.85em;
|
|
288
|
+
font-style: italic;
|
|
289
|
+
}
|
|
290
|
+
details.setting .about { max-width: 80ch; padding: 0.35rem 0 0.75rem; }
|
|
291
|
+
details.setting .about p { margin: 0 0 0.6rem; }
|
|
292
|
+
details.setting .about p:last-child { margin-bottom: 0; }
|
|
293
|
+
details.setting dl { display: grid; grid-template-columns: max-content 1fr; gap: 0.5rem 1.5ch; margin: 0 0 0.6rem; }
|
|
294
|
+
details.setting dd { margin: 0; }
|
|
295
|
+
|
|
263
296
|
/* Panels ------------------------------------------------------------------ */
|
|
264
297
|
|
|
265
298
|
article {
|
|
@@ -7,8 +7,20 @@ class PgPeek::ApplicationController < ActionController::Base
|
|
|
7
7
|
helper_method :switchable_databases
|
|
8
8
|
|
|
9
9
|
before_action :authenticate
|
|
10
|
+
around_action :in_english
|
|
10
11
|
|
|
11
12
|
private
|
|
13
|
+
# The interface is written in English, and so must be the words Rails
|
|
14
|
+
# supplies for it. Under a host's own locale they come out mixed -- "stats
|
|
15
|
+
# since 3 Tage ago", "4.002 statement" -- since the inflections that turn
|
|
16
|
+
# "statement" into "statements" belong to a locale too.
|
|
17
|
+
#
|
|
18
|
+
# A host may have ruled English out of its available locales, and then
|
|
19
|
+
# there is nothing better to switch to than what it already uses.
|
|
20
|
+
def in_english(&)
|
|
21
|
+
I18n.with_locale(I18n.locale_available?(:en) ? :en : I18n.locale, &)
|
|
22
|
+
end
|
|
23
|
+
|
|
12
24
|
def switchable_databases
|
|
13
25
|
@switchable_databases ||= PgPeek::Database.all.select(&:configured?)
|
|
14
26
|
end
|
|
@@ -47,6 +47,14 @@ class PgPeek::DatabasesController < PgPeek::ApplicationController
|
|
|
47
47
|
items << { text: "cache hit ratio #{ratio}% (below #{threshold}%)", href: nil }
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
# Counted since the last reset, so it stays until one: whatever was
|
|
51
|
+
# dropped is missing from every total of the window.
|
|
52
|
+
dropped = @pg_stat_statements.dealloc.to_i
|
|
53
|
+
if dropped.positive?
|
|
54
|
+
items << { text: "pg_stat_statements is full: #{helpers.pluralize(helpers.number_with_delimiter(dropped), "statement")} dropped, totals undercount",
|
|
55
|
+
href: database_pg_stat_statements_path(@database) }
|
|
56
|
+
end
|
|
57
|
+
|
|
50
58
|
@database.tables_with_dead_tuples(PgPeek.config.dead_tuple_warning_threshold).each do |table|
|
|
51
59
|
vacuumed = table["last_autovacuum"] ? "last autovacuum #{helpers.time_ago_in_words(table["last_autovacuum"])} ago" : "never autovacuumed"
|
|
52
60
|
items << { text: "#{table["relname"]}: #{table["dead_ratio"]}% dead tuples, #{vacuumed}",
|
|
@@ -56,15 +64,13 @@ class PgPeek::DatabasesController < PgPeek::ApplicationController
|
|
|
56
64
|
# Scan counts restart at zero on a statistics reset, so right after one
|
|
57
65
|
# every index looks unused. Small ones cost too little to act on; the
|
|
58
66
|
# indexes page still lists them all.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
href: database_indexes_path(@database) }
|
|
67
|
-
end
|
|
67
|
+
unused = PgPeek::Reports::Indexes.new(database: @database)
|
|
68
|
+
.unused_for(PgPeek.config.unused_index_min_stats_age)
|
|
69
|
+
.select { |row| row["size_bytes"].to_i >= PgPeek.config.unused_index_min_size }
|
|
70
|
+
if unused.any?
|
|
71
|
+
bytes = unused.sum { |row| row["size_bytes"].to_i }
|
|
72
|
+
items << { text: "#{helpers.pluralize(unused.size, "unused index")} · #{helpers.number_to_human_size(bytes)}",
|
|
73
|
+
href: database_indexes_path(@database) }
|
|
68
74
|
end
|
|
69
75
|
|
|
70
76
|
activity = database_activity_path(@database)
|
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
# https://docs.percona.com/pg-stat-monitor/comparison.html
|
|
2
2
|
class PgPeek::QueriesController < PgPeek::ApplicationController
|
|
3
|
-
|
|
4
|
-
@database = PgPeek::Database.find(params[:database_id]) or raise ActiveRecord::RecordNotFound, "Database not found"
|
|
5
|
-
@pg_stat_statements = PgPeek::PgStatStatements.new(database: @database)
|
|
3
|
+
before_action :set_database
|
|
6
4
|
|
|
5
|
+
def index
|
|
7
6
|
# The extension can be installed while the module was never preloaded, in
|
|
8
7
|
# which case querying its views raises instead of returning rows.
|
|
9
8
|
return render "pg_peek/pg_stat_statements/not_preloaded" unless @pg_stat_statements.usable?
|
|
10
9
|
|
|
11
10
|
@queries = PgPeek::Reports::Queries.new(database: @database)
|
|
12
11
|
end
|
|
12
|
+
|
|
13
|
+
def shared
|
|
14
|
+
return render "pg_peek/pg_stat_statements/not_preloaded" unless @pg_stat_statements.usable?
|
|
15
|
+
|
|
16
|
+
@queries = PgPeek::Reports::SharedQueries.new(database: @database)
|
|
17
|
+
@summary = @pg_stat_statements.shared_summary
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
def set_database
|
|
22
|
+
@database = PgPeek::Database.find(params[:database_id]) or raise ActiveRecord::RecordNotFound, "Database not found"
|
|
23
|
+
@pg_stat_statements = PgPeek::PgStatStatements.new(database: @database)
|
|
24
|
+
end
|
|
13
25
|
end
|
|
@@ -101,6 +101,17 @@ module PgPeek::ApplicationHelper
|
|
|
101
101
|
time_ago_in_words(timestamp) + " ago"
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
+
# "12 statements, 4,301 calls, 7.2h of database time (31.4% of the total)"
|
|
105
|
+
def shared_statements_total(summary)
|
|
106
|
+
exec_ms = summary["exec_time_ms"].to_f
|
|
107
|
+
all_ms = summary["all_exec_time_ms"].to_f
|
|
108
|
+
share = all_ms.positive? ? " (#{(exec_ms / all_ms * 100).round(1)}% of the total)" : ""
|
|
109
|
+
|
|
110
|
+
"#{pluralize(format_number(summary["statement_count"].to_i), "statement")}, " \
|
|
111
|
+
"#{pluralize(format_number(summary["calls"].to_i), "call")}, " \
|
|
112
|
+
"#{format_duration_from_ms(exec_ms)} of database time#{share}"
|
|
113
|
+
end
|
|
114
|
+
|
|
104
115
|
# Every pg_stat_statements figure is cumulative since the last reset. Each
|
|
105
116
|
# page says so in its header and links to the extension's page, which
|
|
106
117
|
# resets it.
|
|
@@ -52,7 +52,7 @@ module PgPeek::ReportHelper
|
|
|
52
52
|
def sql_cell(report, sql)
|
|
53
53
|
code = tag.code(PgPeek::SqlFormat.format(strip_sqlcommenter(sql)), class: "sql", data: { clamp: true })
|
|
54
54
|
tags = PgPeek::SqlComment.tags(sql)
|
|
55
|
-
return code if tags.empty?
|
|
55
|
+
return code if tags.empty? || !report.attributed?
|
|
56
56
|
|
|
57
57
|
safe_join([ code, sql_source(report, tags), sql_tags(tags) ].compact)
|
|
58
58
|
end
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
class PgPeek::Database
|
|
2
|
-
attr_reader :name, :primary
|
|
2
|
+
attr_reader :name, :primary
|
|
3
3
|
|
|
4
|
+
# Hidden ones included: that is what Rails calls the entries marked
|
|
5
|
+
# replica: true, and a replica is as much worth looking at as its primary.
|
|
4
6
|
def self.available_postgres_databases
|
|
5
|
-
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).select do |db_config|
|
|
7
|
+
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, include_hidden: true).select do |db_config|
|
|
6
8
|
db_config.adapter == "postgresql"
|
|
7
9
|
end
|
|
8
10
|
end
|
|
@@ -12,8 +14,7 @@ class PgPeek::Database
|
|
|
12
14
|
PgPeek::Database.new(
|
|
13
15
|
name: db_config.name,
|
|
14
16
|
primary: db_config.primary?,
|
|
15
|
-
replica: db_config.replica
|
|
16
|
-
schema_format: db_config.schema_format
|
|
17
|
+
replica: db_config.replica?
|
|
17
18
|
)
|
|
18
19
|
end
|
|
19
20
|
end
|
|
@@ -22,11 +23,18 @@ class PgPeek::Database
|
|
|
22
23
|
all.find { |db| db.name == name }
|
|
23
24
|
end
|
|
24
25
|
|
|
25
|
-
def initialize(name:, primary: false, replica: false
|
|
26
|
+
def initialize(name:, primary: false, replica: false)
|
|
26
27
|
@name = name
|
|
27
28
|
@primary = primary
|
|
28
29
|
@replica = replica
|
|
29
|
-
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# As database.yml declares it, with replica: true.
|
|
33
|
+
#
|
|
34
|
+
# A replica keeps statistics of its own: what it counts is what was sent to
|
|
35
|
+
# it, not what the primary did.
|
|
36
|
+
def replica?
|
|
37
|
+
@replica
|
|
30
38
|
end
|
|
31
39
|
|
|
32
40
|
def to_param
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# One pg_stat_statements.* setting: its value, and what there is to know before
|
|
2
|
+
# changing it. What the values are and how a change takes effect come from
|
|
3
|
+
# pg_settings, so they are the server's own; what the setting means for the
|
|
4
|
+
# figures pg_peek shows is written here.
|
|
5
|
+
class PgPeek::PgStatStatements::Setting
|
|
6
|
+
PREFIX = "pg_stat_statements.".freeze
|
|
7
|
+
|
|
8
|
+
ABOUT = {
|
|
9
|
+
"max" => {
|
|
10
|
+
does: "How many distinct statements the server keeps statistics for, across every database on it. " \
|
|
11
|
+
"When the table is full the least-executed statements are dropped to make room, and their calls " \
|
|
12
|
+
"and time go with them: \"dropped when full\" above counts those, and once it is above zero every " \
|
|
13
|
+
"total undercounts."
|
|
14
|
+
},
|
|
15
|
+
"save" => {
|
|
16
|
+
does: "Whether the statistics are kept across a server restart.",
|
|
17
|
+
values: {
|
|
18
|
+
"on" => "Written to disk at shutdown and loaded again at start, so the window runs on through a restart.",
|
|
19
|
+
"off" => "Every restart begins from nothing."
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"track" => {
|
|
23
|
+
does: "Which statements are counted.",
|
|
24
|
+
values: {
|
|
25
|
+
"top" => "The statements clients send. What a function or procedure runs inside is counted as part of " \
|
|
26
|
+
"the call to it.",
|
|
27
|
+
"all" => "Those, and the statements run inside functions and procedures, each as a row of its own. Their " \
|
|
28
|
+
"time is then counted twice, on their own row and in the statement that called them, so totals " \
|
|
29
|
+
"read high.",
|
|
30
|
+
"none" => "Nothing is collected, and every page that reads the extension stays empty."
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"track_planning" => {
|
|
34
|
+
does: "Whether the time spent planning a statement is recorded next to the time spent executing it. " \
|
|
35
|
+
"The figures pg_peek shows are execution time either way.",
|
|
36
|
+
values: {
|
|
37
|
+
"on" => "Planning is recorded. On a busy server it has a cost: connections running the same statement " \
|
|
38
|
+
"contend to update its one row.",
|
|
39
|
+
"off" => "Only execution is recorded."
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"track_utility" => {
|
|
43
|
+
does: "Whether utility commands are counted: everything other than SELECT, INSERT, UPDATE, DELETE and " \
|
|
44
|
+
"MERGE, so BEGIN, COMMIT, NOTIFY, SET, DDL and the rest.",
|
|
45
|
+
values: {
|
|
46
|
+
"on" => "They are counted. A command whose text carries a literal, as a NOTIFY payload does, takes a row " \
|
|
47
|
+
"for each distinct text, which can fill the table.",
|
|
48
|
+
"off" => "Only queries and data changes are counted. pg_peek leaves transaction and session commands " \
|
|
49
|
+
"out of the endpoint and job breakdowns either way."
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}.freeze
|
|
53
|
+
|
|
54
|
+
# pg_settings.context: when a new value takes effect, and who may set it.
|
|
55
|
+
CHANGES = {
|
|
56
|
+
"postmaster" => "needs a server restart",
|
|
57
|
+
"sighup" => "set in the server's configuration; takes effect on a reload, no restart",
|
|
58
|
+
"superuser" => "a superuser can change it with ALTER SYSTEM and a reload, or SET for one session; no restart",
|
|
59
|
+
"user" => "any role can SET it for its own session",
|
|
60
|
+
"internal" => "fixed when the server was built"
|
|
61
|
+
}.freeze
|
|
62
|
+
|
|
63
|
+
attr_reader :name, :value, :default
|
|
64
|
+
|
|
65
|
+
def initialize(row)
|
|
66
|
+
@row = row
|
|
67
|
+
@name = row["name"].to_s.delete_prefix(PREFIX)
|
|
68
|
+
@value = row["setting"]
|
|
69
|
+
@default = row["boot_val"]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def full_name = "#{PREFIX}#{name}"
|
|
73
|
+
|
|
74
|
+
# The server's own one-line description stands in for a setting this file
|
|
75
|
+
# has not heard of, as a newer version of the extension may add.
|
|
76
|
+
def does
|
|
77
|
+
ABOUT.dig(name, :does) || @row["short_desc"]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Each value the setting takes, with what it means where that is known.
|
|
81
|
+
# Empty for a number: it has a range instead.
|
|
82
|
+
def values
|
|
83
|
+
allowed.index_with { |value| ABOUT.dig(name, :values, value) }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def range
|
|
87
|
+
return unless numeric? && @row["min_val"].present? && @row["max_val"].present?
|
|
88
|
+
|
|
89
|
+
[ @row["min_val"], @row["max_val"] ]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def change
|
|
93
|
+
CHANGES[@row["context"]] || @row["context"]
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def pending_restart?
|
|
97
|
+
@row["pending_restart"] == true
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
def numeric? = %w[integer real].include?(@row["vartype"])
|
|
102
|
+
|
|
103
|
+
def allowed
|
|
104
|
+
case @row["vartype"]
|
|
105
|
+
when "bool" then %w[on off]
|
|
106
|
+
when "enum" then PG::TextDecoder::Array.new.decode(@row["enumvals"].to_s)
|
|
107
|
+
else []
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
class PgPeek::PgStatStatements
|
|
2
2
|
LIBRARY_NAME = "pg_stat_statements".freeze
|
|
3
3
|
|
|
4
|
+
# Transaction control and session housekeeping: issued from everywhere, so
|
|
5
|
+
# their tags never say who issued them. NOTIFY is here for a second reason:
|
|
6
|
+
# its payload is part of the statement, so each one is a row of its own,
|
|
7
|
+
# called once, which is exactly what the request estimate takes for "this
|
|
8
|
+
# endpoint was served once".
|
|
9
|
+
#
|
|
10
|
+
# A PostgreSQL regular expression, not a Ruby one: \M ends a word.
|
|
11
|
+
SHARED_STATEMENTS = '^\s*(BEGIN|START TRANSACTION|COMMIT|END|ROLLBACK|ABORT|SAVEPOINT|RELEASE|' \
|
|
12
|
+
'PREPARE TRANSACTION|NOTIFY|LISTEN|UNLISTEN|SET|RESET|SHOW|DISCARD|DEALLOCATE)\M'.freeze
|
|
13
|
+
|
|
4
14
|
attr_reader :database, :default_version, :installed_version
|
|
5
15
|
|
|
6
16
|
def initialize(database:)
|
|
@@ -116,10 +126,16 @@ class PgPeek::PgStatStatements
|
|
|
116
126
|
|
|
117
127
|
# pg_stat_statements.* settings, by name without the prefix.
|
|
118
128
|
def settings
|
|
119
|
-
|
|
129
|
+
setting_details.to_h { |setting| [ setting.name, setting.value ] }
|
|
130
|
+
end
|
|
120
131
|
|
|
121
|
-
|
|
122
|
-
|
|
132
|
+
# The same settings with what pg_settings knows of each: the values it
|
|
133
|
+
# takes, its default, and what it takes for a change to apply.
|
|
134
|
+
def setting_details
|
|
135
|
+
return [] unless usable?
|
|
136
|
+
|
|
137
|
+
@setting_details ||= connection.select_all(PgPeek::QueryLoader.mark(<<~SQL)).map { |row| Setting.new(row) }
|
|
138
|
+
SELECT name, setting, vartype, min_val, max_val, enumvals, boot_val, context, short_desc, pending_restart
|
|
123
139
|
FROM pg_settings WHERE name LIKE 'pg_stat_statements.%' ORDER BY name
|
|
124
140
|
SQL
|
|
125
141
|
end
|
|
@@ -134,6 +150,28 @@ class PgPeek::PgStatStatements
|
|
|
134
150
|
result.to_a
|
|
135
151
|
end
|
|
136
152
|
|
|
153
|
+
# The statements the endpoint and job breakdowns leave out, as one list:
|
|
154
|
+
# what they cost is real, it only cannot be put under anyone's name.
|
|
155
|
+
def shared_statements
|
|
156
|
+
return unless usable?
|
|
157
|
+
|
|
158
|
+
query = PgPeek::QueryLoader.load("pg_stat_statements/shared_statements",
|
|
159
|
+
pg_version: query_pg_version,
|
|
160
|
+
attributable: attributable,
|
|
161
|
+
limit: PgPeek.config.outliers_limit)
|
|
162
|
+
connection.execute(query).to_a
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# What the shared statements add up to, next to everything the database ran.
|
|
166
|
+
def shared_summary
|
|
167
|
+
return unless usable?
|
|
168
|
+
|
|
169
|
+
query = PgPeek::QueryLoader.load("pg_stat_statements/shared_summary",
|
|
170
|
+
pg_version: query_pg_version,
|
|
171
|
+
attributable: attributable)
|
|
172
|
+
connection.select_one(query)
|
|
173
|
+
end
|
|
174
|
+
|
|
137
175
|
def statement_count
|
|
138
176
|
return unless usable?
|
|
139
177
|
|
|
@@ -148,6 +186,7 @@ class PgPeek::PgStatStatements
|
|
|
148
186
|
|
|
149
187
|
query = PgPeek::QueryLoader.load("pg_stat_statements/endpoints",
|
|
150
188
|
pg_version: query_pg_version,
|
|
189
|
+
attributable: attributable,
|
|
151
190
|
limit: PgPeek.config.outliers_limit)
|
|
152
191
|
connection.execute(query).to_a
|
|
153
192
|
end
|
|
@@ -156,7 +195,8 @@ class PgPeek::PgStatStatements
|
|
|
156
195
|
return unless usable?
|
|
157
196
|
|
|
158
197
|
query = PgPeek::QueryLoader.load("pg_stat_statements/jobs",
|
|
159
|
-
pg_version: query_pg_version
|
|
198
|
+
pg_version: query_pg_version,
|
|
199
|
+
attributable: attributable)
|
|
160
200
|
result = connection.execute(query)
|
|
161
201
|
result.to_a
|
|
162
202
|
end
|
|
@@ -172,6 +212,7 @@ class PgPeek::PgStatStatements
|
|
|
172
212
|
query = PgPeek::QueryLoader.load("pg_stat_statements/outliers_by_job",
|
|
173
213
|
pg_version: query_pg_version,
|
|
174
214
|
job_pattern: connection.quote(pattern),
|
|
215
|
+
attributable: attributable,
|
|
175
216
|
limit: PgPeek.config.outliers_limit)
|
|
176
217
|
result = connection.execute(query)
|
|
177
218
|
result.to_a
|
|
@@ -189,12 +230,34 @@ class PgPeek::PgStatStatements
|
|
|
189
230
|
pg_version: query_pg_version,
|
|
190
231
|
controller: connection.quote(controller),
|
|
191
232
|
action: connection.quote(action),
|
|
233
|
+
attributable: attributable,
|
|
192
234
|
limit: PgPeek.config.outliers_limit)
|
|
193
235
|
connection.execute(query).to_a
|
|
194
236
|
end
|
|
195
237
|
|
|
196
238
|
private
|
|
197
239
|
|
|
240
|
+
# A condition that keeps only the statements whose tags say who issued them.
|
|
241
|
+
#
|
|
242
|
+
# pg_stat_statements keeps one row per query shape, with the text of
|
|
243
|
+
# whichever call it saw first, and comments are not part of the shape. So a
|
|
244
|
+
# statement the whole application issues carries the tags of one caller and
|
|
245
|
+
# the calls and time of all of them. In the endpoint and job breakdowns that
|
|
246
|
+
# is worse than missing: it is counted under the wrong name.
|
|
247
|
+
def attributable
|
|
248
|
+
conditions = [ "query !~* #{connection.quote(SHARED_STATEMENTS)}" ]
|
|
249
|
+
|
|
250
|
+
PgPeek.config.unattributed_queries.each do |pattern|
|
|
251
|
+
conditions << if pattern.is_a?(Regexp)
|
|
252
|
+
"query #{pattern.casefold? ? "!~*" : "!~"} #{connection.quote(pattern.source)}"
|
|
253
|
+
else
|
|
254
|
+
"strpos(query, #{connection.quote(pattern.to_s)}) = 0"
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
conditions.join(" AND ")
|
|
259
|
+
end
|
|
260
|
+
|
|
198
261
|
# The view's columns follow the extension, not the server: 1.11, shipped with
|
|
199
262
|
# PostgreSQL 17, renamed blk_read_time to shared_blk_read_time. A server
|
|
200
263
|
# upgraded without ALTER EXTENSION ... UPDATE keeps the old columns, so it
|
data/app/models/pg_peek/table.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
class PgPeek::Table
|
|
2
2
|
attr_reader :database, :name
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
SERVER_STATS_RESET_QUERY = PgPeek::QueryLoader.mark("SELECT stats_reset FROM pg_stat_bgwriter").freeze
|
|
5
5
|
|
|
6
6
|
def self.inline_stats_for(database, table_names)
|
|
7
7
|
return {} if table_names.empty?
|
|
@@ -53,11 +53,14 @@ class PgPeek::Table
|
|
|
53
53
|
@stats ||= fetch_stats
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
# Since when this table's counters have been accumulating, which is what
|
|
57
|
+
# turns them into rates.
|
|
58
|
+
#
|
|
59
|
+
# PostgreSQL 19 records it per table: set by a reset of this table alone or
|
|
60
|
+
# of its whole database, and empty until either happens. Before 19, and
|
|
61
|
+
# until then, the server's own statistics are the nearest thing there is.
|
|
56
62
|
def stats_reset_at
|
|
57
|
-
@stats_reset_at ||=
|
|
58
|
-
result = database.connection.execute(STATS_RESET_QUERY)
|
|
59
|
-
result.first&.dig("stats_reset")
|
|
60
|
-
end
|
|
63
|
+
@stats_reset_at ||= stats["stats_reset"] || server_stats_reset_at
|
|
61
64
|
end
|
|
62
65
|
|
|
63
66
|
def seconds_since_reset
|
|
@@ -227,6 +230,10 @@ class PgPeek::Table
|
|
|
227
230
|
|
|
228
231
|
private
|
|
229
232
|
|
|
233
|
+
def server_stats_reset_at
|
|
234
|
+
database.connection.execute(SERVER_STATS_RESET_QUERY).first&.dig("stats_reset")
|
|
235
|
+
end
|
|
236
|
+
|
|
230
237
|
def fetch_stats
|
|
231
238
|
query = PgPeek::QueryLoader.load("tables/stats", pg_version: database.major_version)
|
|
232
239
|
result = database.connection.exec_query(query, "PgPeek::Table Stats", [ name ])
|
|
@@ -16,10 +16,10 @@ WITH application_queries AS (
|
|
|
16
16
|
WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
|
|
17
17
|
AND dbid = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
18
18
|
AND query NOT LIKE '%/* pg_peek */%'
|
|
19
|
+
AND {{attributable}}
|
|
19
20
|
AND query ~ 'controller=''[^'']+'''
|
|
20
21
|
AND query ~ 'action=''[^'']+'''
|
|
21
22
|
AND query !~* 'pg_catalog|information_schema|pg_index|pg_class|pg_attribute'
|
|
22
|
-
AND query !~* '^\s*SHOW '
|
|
23
23
|
),
|
|
24
24
|
requests AS (
|
|
25
25
|
-- Some query runs exactly once per request, so the smallest call count for an
|
|
@@ -7,6 +7,7 @@ FROM pg_stat_statements
|
|
|
7
7
|
WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
|
|
8
8
|
AND dbid = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
9
9
|
AND query NOT LIKE '%/* pg_peek */%'
|
|
10
|
+
AND {{attributable}}
|
|
10
11
|
AND query ~ 'job=''[^'']+'''
|
|
11
12
|
GROUP BY job_class
|
|
12
13
|
ORDER BY total_exec_time_ms DESC
|
|
@@ -13,12 +13,12 @@ FROM pg_stat_statements
|
|
|
13
13
|
WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
|
|
14
14
|
AND dbid = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
15
15
|
AND query NOT LIKE '%/* pg_peek */%'
|
|
16
|
+
AND {{attributable}}
|
|
16
17
|
AND COALESCE(
|
|
17
18
|
substring(query from 'namespaced_controller=''([^'']+)'''),
|
|
18
19
|
substring(query from 'controller=''([^'']+)''')
|
|
19
20
|
) = {{controller}}
|
|
20
21
|
AND substring(query from 'action=''([^'']+)''') = {{action}}
|
|
21
22
|
AND query !~* 'pg_catalog|information_schema|pg_index|pg_class|pg_attribute'
|
|
22
|
-
AND query !~* '^\s*SHOW '
|
|
23
23
|
ORDER BY total_exec_time DESC
|
|
24
24
|
LIMIT {{limit}}
|
|
@@ -9,6 +9,7 @@ FROM pg_stat_statements
|
|
|
9
9
|
WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
|
|
10
10
|
AND dbid = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
11
11
|
AND query NOT LIKE '%/* pg_peek */%'
|
|
12
|
+
AND {{attributable}}
|
|
12
13
|
AND query LIKE {{job_pattern}}
|
|
13
14
|
ORDER BY total_exec_time DESC
|
|
14
15
|
LIMIT {{limit}}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
-- The statements the whole application issues, which the endpoint and job
|
|
2
|
+
-- breakdowns leave out: their tags name only whoever ran them first.
|
|
3
|
+
--
|
|
4
|
+
-- The share is of everything the database ran, not of this list, so it is
|
|
5
|
+
-- worked out before the shared statements are picked.
|
|
6
|
+
WITH statements AS (
|
|
7
|
+
SELECT
|
|
8
|
+
interval '1 millisecond' * total_exec_time AS total_exec_time,
|
|
9
|
+
COALESCE(to_char((total_exec_time/NULLIF(sum(total_exec_time) OVER(), 0)) * 100, 'FM990D0'), '0') || '%' AS prop_exec_time,
|
|
10
|
+
to_char(calls, 'FM999G999G999G990') AS ncalls,
|
|
11
|
+
total_exec_time/NULLIF(calls, 0) AS avg_exec_ms,
|
|
12
|
+
interval '1 millisecond' * (blk_read_time + blk_write_time) AS sync_io_time,
|
|
13
|
+
query
|
|
14
|
+
FROM pg_stat_statements
|
|
15
|
+
WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
|
|
16
|
+
AND dbid = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
17
|
+
AND query NOT LIKE '%/* pg_peek */%'
|
|
18
|
+
)
|
|
19
|
+
SELECT *
|
|
20
|
+
FROM statements
|
|
21
|
+
WHERE NOT ({{attributable}})
|
|
22
|
+
ORDER BY total_exec_time DESC
|
|
23
|
+
LIMIT {{limit}}
|