Find blocked and blocking sessions
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity| -- ALTER OWNER OF ALL OBJECTS ON THE CONNECTED DATABASE | |
| DO $$DECLARE | |
| owner varchar = 'vokuro'; | |
| r record; | |
| BEGIN | |
| FOR r IN | |
| -- SCHEMAS | |
| SELECT 'ALTER SCHEMA '|| schema_name ||' OWNER TO ' || owner || ';' AS DDL | |
| FROM information_schema.schemata WHERE NOT schema_name IN ('pg_catalog', 'information_schema', 'pg_toast', 'pg_temp_1', 'pg_toast_temp_1') | |
| UNION ALL |
| /* | |
| * derivative work of Matheus de Oliveira's json_manipulator.sql | |
| * https://gist.github.com/matheusoliveira/9488951 | |
| * | |
| * adapted to support postgresql 9.4 jsonb type | |
| * no warranties or guarantees of any kind are implied or offered | |
| * | |
| * license is as Matheus conferred it on 4/9/2015: | |
| * matheusoliveira commented on Apr 9 | |
| * @hannes-landeholm, I'd like to take credit if you share them |
| class Jobs::Cron::Metrics::Postgres < QueueClassicPlus::Base | |
| @queue = :cron | |
| lock! | |
| def self.perform | |
| sql = <<-SQL | |
| SELECT date_part('epoch', now() - query_start) AS connection_age | |
| FROM pg_catalog.pg_stat_activity | |
| WHERE datname = '#{database}' | |
| ORDER BY connection_age DESC NULLS LAST |
| #!/bin/sh | |
| # | |
| # The MIT License | |
| # | |
| # Copyright 2014-2017 Jakub Jirutka <jakub@jirutka.cz>. | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| #! /usr/bin/env python | |
| import sys | |
| import os | |
| import time | |
| import re | |
| import smtplib | |
| from datetime import time, datetime, timedelta | |
| import time | |
| #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| to_email = 'someone@somecompany.com' |
| CREATE OR REPLACE FUNCTION public.json_append(data json, insert_data json) | |
| RETURNS json | |
| IMMUTABLE | |
| LANGUAGE sql | |
| AS $$ | |
| SELECT ('{'||string_agg(to_json(key)||':'||value, ',')||'}')::json | |
| FROM ( | |
| SELECT * FROM json_each(data) | |
| UNION ALL | |
| SELECT * FROM json_each(insert_data) |
| # Create the datadog user with select only permissions: | |
| # CREATE USER datadog WITH PASSWORD '<complex_password>'; | |
| # | |
| # Grant select permissions on a table or view that you want to monitor: | |
| # GRANT SELECT ON <schema>.<table> TO datadog; | |
| # | |
| # Grant permissions for a specific column on a table or view that you want to monitor: | |
| # GRANT SELECT (id, name) ON <schema>.<table> TO datadog; | |
| # | |
| # Let non-superusers look at pg_stat_activity in a read-only fashon. |
| -- show running queries (pre 9.2) | |
| SELECT procpid, age(clock_timestamp(), query_start), usename, current_query | |
| FROM pg_stat_activity | |
| WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%' | |
| ORDER BY query_start desc; | |
| -- show running queries (9.2) | |
| SELECT pid, age(clock_timestamp(), query_start), usename, query | |
| FROM pg_stat_activity | |
| WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' |