SELECT distinct datname, count(*)
FROM pg_stat_activity
WHERE datname = 'database_name'
GROUP BY datname
ORDER BY 1;
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'database_name'
AND pid <> pg_backend_pid();
SELECT userid::regrole as "user",
datname as database,
calls,
round(mean_exec_time::numeric, 2) as "Avg Time (ms)",
round(total_exec_time::numeric, 2) as "Total Time (ms)",
query
FROM pg_stat_statements join pg_database
on pg_stat_statements.dbid = pg_database.oid
ORDER BY mean_exec_time DESC
LIMIT 10;
For PG v12 and below, remove
exec
frommean_exec_time
andtotal_exec_time
SELECT userid::regrole as "user",
datname as database,
calls,
pg_size_pretty((shared_blks_hit + shared_blks_dirtied)) as mem,
queryid,
query
FROM pg_stat_statements join pg_database
on pg_stat_statements.dbid = pg_database.oid
ORDER BY (shared_blks_hit + shared_blks_dirtied) DESC
LIMIT 10;
SELECT * FROM pg_database pd WHERE datallowconn;
SELECT datname FROM pg_database pd WHERE datallowconn AND datname NOT IN ('rdsadmin', 'template1');
SELECT d.datname AS database, u.usename AS owner
FROM pg_database d JOIN pg_user u ON d.datdba = u.usesysid
WHERE datname IN (SELECT datname FROM pg_stat_activity WHERE state is not null)
ORDER BY datname;
SELECT version();
SELECT current_user;
Upgrading the PostgreSQL DB engine for Amazon RDS
Troubleshooting PostGIS Extensions issues when upgrading Postgres
SELECT * FROM pg_indexes WHERE schemaname ='public'
- Consider deleting the Index if
index_scans_count
is very low
SELECT idxstats.schemaname as schema_name,
idxstats.relname AS table_name,
indexrelname AS index_name,
idxstats.idx_scan AS index_scans_count,
pg_size_pretty(pg_relation_size(idxstats.indexrelid)) AS index_size
FROM pg_stat_all_indexes AS idxstats
JOIN pg_index i ON idxstats.indexrelid = i.indexrelid
WHERE idxstats.schemaname = 'public'
AND NOT i.indisunique -- is not a UNIQUE index
ORDER BY idxstats.idx_scan;
seq_scan_count
: A high number of sequential scans may indicate that appropriate indexes are missing or underutilized.
SELECT tabstats.schemaname AS schema_name,
tabstats.relname AS table_name,
tabstats.seq_scan AS seq_scan_count,
tabstats.idx_scan AS index_scan_count,
pg_size_pretty(pg_total_relation_size(tabstats.relid)) AS table_size
FROM pg_stat_all_tables AS tabstats
WHERE tabstats.schemaname = 'public'
ORDER BY seq_scan_count DESC;
To follow the progress of Index/ReIndex
CREATE EXTENSION extension_name;
CREATE EXTENSION IF NOT EXISTS extension_name WITH SCHEMA schema_name;
CREATE EXTENSION IF NOT EXISTS extension_name VERSION '0.0.1';
CREATE EXTENSION IF NOT EXISTS extension_name WITH SCHEMA schema_name VERSION '0.0.1';
SELECT * FROM pg_extension_update_paths('extension_name') WHERE source='current_version_number' AND target NOT LIKE '%next%' AND source<target AND path LIKE '%--%';
ALTER EXTENSION extension_name UPDATE TO 'version_number';
ALTER EXTENSION extension_name UPDATE;
DROP EXTENSION extension_name;
SELECT * FROM pg_available_extensions WHERE installed_version IS NOT NULL order by 1;
SELECT * FROM pg_available_extensions WHERE default_version > installed_version;
SELECT * FROM pg_available_extension_versions WHERE name LIKE '%pg_stat_statements%';
SELECT e.extname AS "Extensions", n.nspname AS schema_name, u.usename AS "Owner"
FROM pg_extension e
JOIN pg_namespace n ON e.extnamespace = n.oid
JOIN pg_user u ON e.extowner = u.usesysid;
SELECT e.extname AS "Name",
e.extversion AS "Version",
n.nspname AS "Schema",
c.description AS "Description"
FROM pg_catalog.pg_extension e
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace
LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid
AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
WHERE e.extname LIKE '%postgis%'
ORDER BY 1;
-- Installation Path
SELECT probin FROM pg_proc WHERE proname = 'postgis_raster_lib_version';
-- If installation Path is lower than new version, run:
SELECT postGIS_extensions_upgrade();