Skip to content

Commit

Permalink
Enable PgHero Query Stats
Browse files Browse the repository at this point in the history
This does the work to enable historical query stats in PgHero, but the
Postgres instance backing the service must also be configured, see:

https://github.com/ankane/pghero/blob/master/guides/Rails.md#query-stats

We are:

- adding the tables to store stats
- setting up a GoodJob cron job to collect the stats and clean old ones
(the demo app generates a lot of queries)

For reference, the recommended settings for `postgres.conf`:

```
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all
pg_stat_statements.max = 10000
track_activity_query_size = 2048
```

See:

https://github.com/ankane/pghero/blob/master/guides/Query-Stats.md
  • Loading branch information
mec committed Mar 21, 2024
1 parent d3ae492 commit 639323f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
15 changes: 15 additions & 0 deletions demo/app/jobs/pg_hero_maintenance_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class PgHeroMaintenanceJob < ApplicationJob
include GoodJob::ActiveJobExtensions::Concurrency

good_job_control_concurrency_with(
key: "pg_hero_maintenance",
total_limit: 1
)

discard_on StandardError

def perform
PgHero.capture_query_stats
PgHero.clean_query_stats
end
end
5 changes: 5 additions & 0 deletions demo/config/initializers/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
complex_schedule: {
cron: -> (last_ran) { last_ran ? last_ran + 17.hours : Time.now},
class: "OtherJob",
},
pg_hero_maintenance: {
cron: "*/10 * * * *", # Every 10 minutes
class: "PgHeroMaintenanceJob",
description: "Runs PG Hero maintenance",
}
}
end
Expand Down
15 changes: 15 additions & 0 deletions demo/db/migrate/20240321162554_create_pghero_query_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreatePgheroQueryStats < ActiveRecord::Migration[7.1]
def change
create_table :pghero_query_stats do |t|
t.text :database
t.text :user
t.text :query
t.integer :query_hash, limit: 8
t.float :total_time
t.integer :calls, limit: 8
t.timestamp :captured_at
end

add_index :pghero_query_stats, [:database, :captured_at]
end
end
14 changes: 13 additions & 1 deletion demo/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_01_14_221613) do
ActiveRecord::Schema.define(version: 2024_03_21_162554) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pgcrypto"
enable_extension "plpgsql"

Expand Down Expand Up @@ -94,4 +95,15 @@
t.index ["scheduled_at"], name: "index_good_jobs_on_scheduled_at", where: "(finished_at IS NULL)"
end

create_table "pghero_query_stats", force: :cascade do |t|
t.text "database"
t.text "user"
t.text "query"
t.bigint "query_hash"
t.float "total_time"
t.bigint "calls"
t.datetime "captured_at", precision: nil
t.index ["database", "captured_at"], name: "index_pghero_query_stats_on_database_and_captured_at"
end

end

0 comments on commit 639323f

Please sign in to comment.