Skip to content

Instantly share code, notes, and snippets.

@Whitespace
Last active December 20, 2016 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Whitespace/81ad8d31e099c967ceeb507ce48bb82e to your computer and use it in GitHub Desktop.
Save Whitespace/81ad8d31e099c967ceeb507ce48bb82e to your computer and use it in GitHub Desktop.
Common Postgres SQL Troubleshooting Queries
-- Heroku has a bunch of awesome queries
-- https://github.com/heroku/heroku-pg-extras/tree/master/commands
-- View slow queries for widgets table
SELECT query, total_time/calls AS avg_time
FROM pg_stat_statements
WHERE query LIKE 'SELECT%'
AND query LIKE '%widgets%'
ORDER BY avg_time DESC;
-- View size of indexes for widgets table
SELECT c2.relname, c2.relpages * 8 AS bytes
FROM pg_class c, pg_class c2, pg_index i
WHERE c.relname = 'widgets' AND
c.oid = i.indrelid AND
c2.oid = i.indexrelid
ORDER BY bytes DESC;
-- creating indexes concurrently
-- functional indexes (uses lower(email))
CREATE INDEX CONCURRENTLY index_users_on_lower_email ON users (lower(email)) WHERE deleted_at IS NOT NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment