Skip to content

Instantly share code, notes, and snippets.

@rutvij-pandya
Last active May 8, 2018 13:23
Show Gist options
  • Save rutvij-pandya/84fda7eae359f5707267733a9ee3fdf0 to your computer and use it in GitHub Desktop.
Save rutvij-pandya/84fda7eae359f5707267733a9ee3fdf0 to your computer and use it in GitHub Desktop.
Steps to enable query stats in pgHero dashboard
## Modify postgresql.conf
# Find postgresql.conf
# ---- For mac users ----
cd /usr/local/var/postgres/
vi postgresql.conf
# ---- For ubuntu users ----
cd /etc/postgresql/<version>/main
vi postgresql.conf
# Search for term 'shared_preload_libraries' & uncomment it
/shared_preload_libraries
# Set value of 'shared_preload_libraries' & add below mentioned code
shared_preload_libraries = 'pg_stat_statements' # (change requires restart)
pg_stat_statements.track = all
pg_stat_statements.max = 10000
track_activity_query_size = 2048
# Save changes
## Restart postgreSQL
# ---- For mac users ----
brew services restart postgres
# ---- For ubuntu users ----
sudo /etc/init.d/postgresql restart
## Run below method from Rails console
PgHero.enable_query_stats
# TO verify, run
PgHero.query_stats_enabled?
# It should return 'true'
## Capture Query Stats from postgreSQL periodically
rails generate pghero:query_stats
rake db:migrate
# This adds a table 'pghero_query_stats' in your database
## Schedule below task to run every 5 minutes
rake pghero:capture_query_stats
# If you're using 'whenever' gem, add below line in /config/schedule.rb
every 5.minutes do
rake "pghero:capture_query_stats"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment