table of Contents
Display the index on each table
The size of a single table in the database (excluding indexes)
Find out all tables (including indexes) and sort
The most expensive IO SQL, the most expensive IO SQL for a single call TOP 5
Total most consumed IO SQL TOP 5
The most time-consuming SQL, the most time-consuming SQL for a single call TOP 5
Total most time-consuming SQL TOP 5
The most severe response time jitter SQL
Most consuming shared memory SQL
Most Temporary Space-consuming SQL
Authorization
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN schema public to "XXX_DMM_UPDATE";
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public to "XXX_DMM_UPDATE";
GRANT SELECT ON ALL TABLES IN schema public to "XXX_DMM_READONLY";
View duplicate index
SELECT pg_size_pretty(SUM(pg_relation_size(idx))::BIGINT) AS SIZE,
(array_agg(idx))[1] AS idx1, (array_agg(idx))[2] AS idx2,
(array_agg(idx))[3] AS idx3, (array_agg(idx))[4] AS idx4
FROM (
SELECT indexrelid::regclass AS idx, (indrelid::text ||E'\n'|| indclass::text ||E'\n'|| indkey::text ||E'\n'||
COALESCE(indexprs::text,'')||E'\n' || COALESCE(indpred::text,'')) AS KEY
FROM pg_index) sub
GROUP BY KEY HAVING COUNT(*)>1
ORDER BY SUM(pg_relation_size(idx)) DESC;
Display the index on each table
SELECT
t.tablename,
indexname,
c.reltuples AS num_rows,
pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
CASE WHEN indisunique THEN 'Y'
ELSE 'N'
END AS UNIQUE,
idx_scan AS number_of_scans,
idx_tup_read AS tuples_read,
idx_tup_fetch AS tuples_fetched
FROM pg_tables t
LEFT OUTER JOIN pg_class c ON t.tablename=c.relname
LEFT OUTER JOIN
( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns, idx_scan, idx_tup_read, idx_tup_fetch, indexrelname, indisunique FROM pg_index x
JOIN pg_class c ON c.oid = x.indrelid
JOIN pg_class ipg ON ipg.oid = x.indexrelid
JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid )
AS foo
ON t.tablename = foo.ctablename
WHERE t.schemaname='public'
ORDER BY 1,2;
The size of a single table in the database (excluding indexes)
select pg_size_pretty(pg_relation_size('表名'));
Find out all tables (including indexes) and sort
SELECT table_schema || '.' || table_name AS table_full_name, pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size
FROM information_schema.tables
ORDER BY
pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC limit 20
-Find out the size of the table sorted by size and separate data and index
SELECT
table_name,
pg_size_pretty(table_size) AS table_size,
pg_size_pretty(indexes_size) AS indexes_size,
pg_size_pretty(total_size) AS total_size
FROM (
SELECT
table_name,
pg_table_size(table_name) AS table_size,
pg_indexes_size(table_name) AS indexes_size,
pg_total_relation_size(table_name) AS total_size
FROM (
SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
FROM information_schema.tables
) AS all_tables
ORDER BY total_size DESC
) AS pretty_sizes
SELECT query, calls, total_time, total_time/calls, rows,
100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;
The most expensive IO SQL, the most expensive IO SQL for a single call TOP 5
select userid, dbid, query from pg_stat_statements order by (blk_read_time+blk_write_time)/calls desc limit 5;
Total most consumed IO SQL TOP 5
select userid, dbid, query from pg_stat_statements order by (blk_read_time+blk_write_time) desc limit 5;
The most time-consuming SQL, the most time-consuming SQL for a single call TOP 5
select userid, dbid, query from pg_stat_statements order by mean_time desc limit 5;
Total most time-consuming SQL TOP 5
select userid, dbid, query from pg_stat_statements order by total_time desc limit 5;
The most severe response time jitter SQL
select userid, dbid, query from pg_stat_statements order by stddev_time desc limit 5;
Most consuming shared memory SQL
select userid, dbid, query from pg_stat_statements order by (shared_blks_hit+shared_blks_dirtied) desc limit 5;
Most Temporary Space-consuming SQL
select userid, dbid, query from pg_stat_statements order by temp_blks_written desc limit 5;
Reset statistics
pg_stat_statements is cumulative statistics. If you want to view statistics for a certain period of time, you need to take a snapshot
Users can also periodically clean up historical statistical information by calling the following SQL
select pg_stat_statements_reset();