diff --git a/README.md b/README.md index aaaecd0..8c8a6e2 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,70 @@ These queries can be run periodically to send data to your monitoring system. Th ## Queries -### query_status +### current_queries_status (pg9.x) ```sql PREPARE current_queries_status AS SELECT count(pid), query, waiting from pg_stat_activity group by query, waiting; ``` +### current_queries_status_with_locks (pg9.x) +```sql +PREPARE current_queries_status_with_locks AS +SELECT count(pg_stat_activity.pid) AS number_of_queries, + substring(trim(LEADING + FROM regexp_replace(pg_stat_activity.query, '[\n\r]+'::text, + ' '::text, 'g'::text)) + FROM 0 + FOR 200) AS query_name, + max(age(CURRENT_TIMESTAMP, query_start)) AS max_wait_time, + waiting, + usename, + locktype, + mode, + granted + FROM pg_stat_activity + LEFT JOIN pg_locks ON pg_stat_activity.pid = pg_locks.pid + WHERE query != '' + AND query NOT ILIKE '%pg_%' AND query NOT ILIKE '%application_name%' AND query NOT ILIKE '%inet%' + AND age(CURRENT_TIMESTAMP, query_start) > '5 milliseconds'::interval + GROUP BY query_name, + waiting, + usename, + locktype, + mode, + granted + ORDER BY max_wait_time DESC; +``` + +### current_queries_status_with_locks (pg10) +```sql +PREPARE current_queries_status_with_locks AS +SELECT count(pg_stat_activity.pid) AS number_of_queries, + substring(trim(LEADING + FROM regexp_replace(pg_stat_activity.query, '[\n\r]+'::text, + ' '::text, 'g'::text)) + FROM 0 + FOR 200) AS query_name, + max(age(CURRENT_TIMESTAMP, query_start)) AS max_wait_time, + wait_event, + usename, + locktype, + mode, + granted + FROM pg_stat_activity + LEFT JOIN pg_locks ON pg_stat_activity.pid = pg_locks.pid + WHERE query != '' + AND query NOT ILIKE '%pg_%' AND query NOT ILIKE '%application_name%' AND query NOT ILIKE '%inet%' + AND age(CURRENT_TIMESTAMP, query_start) > '5 milliseconds'::interval + GROUP BY query_name, + wait_event, + usename, + locktype, + mode, + granted + ORDER BY max_wait_time DESC; +``` + ### query_stats ```sql PREPARE query_stats AS @@ -36,6 +94,25 @@ ORDER BY calls DESC; - This requires [pg_stat_statements](http://www.postgresql.org/docs/current/static/pgstatstatements.html) to be set up. It's a part of the contrib package, and needs to be added to `shared_preload_libraries` in `postgresql.conf`. +## Long Running Queries +``` +sql +SELECT + pid, + user, + pg_stat_activity.query_start, + now() - pg_stat_activity.query_start AS query_time, + query, + state, + wait_event_type, + wait_event +FROM pg_stat_activity +WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes'; +``` + +This enables you to know the queries running for more than x Minutes (5 here). This gives you visibility as what might be hogging up your CPU and network pool. +Crucial in determining what might take your DB down in high traffic. + ## Cache ### cache_tables ```sql @@ -257,7 +334,7 @@ ORDER BY relname; ## Replication -### replication_status +### replication_status (pg9.x) ```sql PREPARE replication_status AS SELECT application_name,client_addr,state,sent_location,write_location,replay_location, @@ -271,3 +348,18 @@ SELECT application_name,client_addr,state,sent_location,write_location,replay_lo FROM pg_stat_replication) AS s; ``` +### replication_status (pg10) +```sql +PREPARE replication_status AS +SELECT application_name,client_addr,state, \\ + (sent_offset - (replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ))::text AS byte_lag \\ + FROM (SELECT \\ + application_name,client_addr,state,sync_state,sent_lsn,write_lsn,replay_lsn, \\ + ('x' || lpad(split_part(sent_lsn::text, '/', 1), 8, '0'))::bit(32)::bigint AS sent_xlog, \\ + ('x' || lpad(split_part(replay_lsn::text, '/', 1), 8, '0'))::bit(32)::bigint AS replay_xlog, \\ + ('x' || lpad(split_part(sent_lsn::text, '/', 2), 8, '0'))::bit(32)::bigint AS sent_offset, \\ + ('x' || lpad(split_part(replay_lsn::text, '/', 2), 8, '0'))::bit(32)::bigint AS replay_offset \\ + FROM pg_stat_replication) \\ + AS s; +``` + diff --git a/monitor.sql b/monitor.sql index 18e099f..e41431a 100644 --- a/monitor.sql +++ b/monitor.sql @@ -204,9 +204,61 @@ WHERE EXISTS(SELECT * FROM pg_available_extensions WHERE name = 'pg_stat_statements') ORDER BY calls DESC; +PREPARE current_queries_status_with_locks AS +SELECT count(pg_stat_activity.pid) AS number_of_queries, + substring(trim(LEADING + FROM regexp_replace(pg_stat_activity.query, '[\n\r]+'::text, + ' '::text, 'g'::text)) + FROM 0 + FOR 200) AS query_name, + max(age(CURRENT_TIMESTAMP, query_start)) AS max_wait_time, + waiting, + usename, + locktype, + mode, + granted + FROM pg_stat_activity + LEFT JOIN pg_locks ON pg_stat_activity.pid = pg_locks.pid + WHERE query != '' + AND query NOT ILIKE '%pg_%' AND query NOT ILIKE '%application_name%' AND query NOT ILIKE '%inet%' + AND age(CURRENT_TIMESTAMP, query_start) > '5 milliseconds'::interval + GROUP BY query_name, + waiting, + usename, + locktype, + mode, + granted + ORDER BY max_wait_time DESC; + + +PREPARE current_queries_status_with_locks_pg10 AS +SELECT count(pg_stat_activity.pid) AS number_of_queries, + substring(trim(LEADING + FROM regexp_replace(pg_stat_activity.query, '[\n\r]+'::text, + ' '::text, 'g'::text)) + FROM 0 + FOR 200) AS query_name, + max(age(CURRENT_TIMESTAMP, query_start)) AS max_wait_time, + wait_event, + usename, + locktype, + mode, + granted + FROM pg_stat_activity + LEFT JOIN pg_locks ON pg_stat_activity.pid = pg_locks.pid + WHERE query != '' + AND query NOT ILIKE '%pg_%' AND query NOT ILIKE '%application_name%' AND query NOT ILIKE '%inet%' + AND age(CURRENT_TIMESTAMP, query_start) > '5 milliseconds'::interval + GROUP BY query_name, + wait_event, + usename, + locktype, + mode, + granted + ORDER BY max_wait_time DESC; -- replication -PREPARE replication_status AS +PREPARE replication_status_9 AS SELECT application_name,client_addr,state,sent_location,write_location,replay_location, (sent_offset - (replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ))::text AS byte_lag FROM (SELECT @@ -217,3 +269,15 @@ SELECT application_name,client_addr,state,sent_location,write_location,replay_lo ('x' || lpad(split_part(replay_location::text, '/', 2), 8, '0'))::bit(32)::bigint AS replay_offset FROM pg_stat_replication) AS s; + +PREPARE replication_status_10 AS +SELECT application_name,client_addr,state, \\ + (sent_offset - (replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ))::text AS byte_lag \\ + FROM (SELECT \\ + application_name,client_addr,state,sync_state,sent_lsn,write_lsn,replay_lsn, \\ + ('x' || lpad(split_part(sent_lsn::text, '/', 1), 8, '0'))::bit(32)::bigint AS sent_xlog, \\ + ('x' || lpad(split_part(replay_lsn::text, '/', 1), 8, '0'))::bit(32)::bigint AS replay_xlog, \\ + ('x' || lpad(split_part(sent_lsn::text, '/', 2), 8, '0'))::bit(32)::bigint AS sent_offset, \\ + ('x' || lpad(split_part(replay_lsn::text, '/', 2), 8, '0'))::bit(32)::bigint AS replay_offset \\ + FROM pg_stat_replication) \\ + AS s;