Skip to content

Instantly share code, notes, and snippets.

@virusdave
virusdave / postgres_queries_and_commands.sql
Last active January 27, 2022 21:33 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- CHECK TIMINGS ON ACTIVE AND EXPENSIVE QUERIES
SELECT activity.*
FROM (
SELECT
pid,
CASE WHEN state = 'active' THEN AGE(clock_timestamp(), query_start)
ELSE AGE(state_change, query_start)
END as query_duration, -- This is how long the most recent query was running (or is running so far, if still active)
AGE(clock_timestamp(), xact_start) as xact_duration, -- Same, but for the currently active transaction
CASE WHEN state = 'active' THEN INTERVAL '0'