Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
Last active May 24, 2024 15:24
Show Gist options
  • Select an option

  • Save amitsaxena/0115d1d28fa90a5324560716b586d496 to your computer and use it in GitHub Desktop.

Select an option

Save amitsaxena/0115d1d28fa90a5324560716b586d496 to your computer and use it in GitHub Desktop.
Useful SQL queries for debugging
// Change SQL client timeout
set statement_timeout = '0s';
set statement_timeout = '10s';
// Change timeout globally
alter database mentimeter set statement_timeout = '10s';
// Time queries
\timing
// Create multicolumn index concurrenty
CREATE INDEX CONCURRENTLY vote_texts_multi_column ON vote_texts (question_id, identifier_id);
// Drop index
DROP INDEX CONCURRENTLY vote_texts_multi_column;
// See all locks
SELECT locktype, relation::regclass, mode, pid FROM pg_locks;
// See all locks with relevant query
SELECT
S.pid,
age(clock_timestamp(), query_start),
query,
L.mode,
L.locktype,
L.granted
FROM pg_stat_activity S
inner join pg_locks L on S.pid = L.pid
order by L.granted, L.pid DESC;
// List all tables of a specific schema
\dt schema_name.*
@amitsaxena
Copy link
Copy Markdown
Author

Useful article about types of locks:
http://shiroyasha.io/understanding-postgresql-locks.html

@amitsaxena
Copy link
Copy Markdown
Author

Interesting read about how an UPDATE resulted in table level exclusive lock instead of a row level lock:
https://blog.heroku.com/curious-case-table-locking-update-query

@amitsaxena
Copy link
Copy Markdown
Author

@amitsaxena
Copy link
Copy Markdown
Author

@amitsaxena
Copy link
Copy Markdown
Author

@amitsaxena
Copy link
Copy Markdown
Author

@amitsaxena
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment