

ROUND((CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages::FLOAT/otta END)::NUMERIC,1) AS tbloat,ĬASE WHEN relpages < otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT END AS wastedbytes, LEFT JOIN pg_class c2 ON c2.oid = i.indexrelidĬurrent_database(), schemaname, tablename, /*reltuples::bigint, relpages::bigint, otta,*/ LEFT JOIN pg_index i ON indrelid = cc.oid JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname 'information_schema' JOIN pg_class cc ON cc.relname = rs.tablename (CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))/(bs-20::FLOAT)) AS otta,ĬOALESCE(c2.relname,'?') AS iname, COALESCE(c2.reltuples,0) AS ituples, COALESCE(c2.relpages,0) AS ipages,ĬOALESCE(CEIL((c2.reltuples*(datahdr-12))/(bs-20::FLOAT)),0) AS iotta - very rough approximation, assumes all cols Schemaname, tablename, cc.reltuples, cc.relpages, bs, (maxfracsum*(nullhdr+ma-(CASE WHEN nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2 (datawidth+(hdr+ma-(CASE WHEN hdr%ma=0 THEN ma ELSE hdr%ma END)))::NUMERIC AS datahdr, (SELECT current_setting('block_size')::NUMERIC) AS bs,ĬASE WHEN SUBSTRING(v,12,3) IN ('8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr,ĬASE WHEN v ~ 'mingw32' THEN 8 ELSE 4 END AS ma WHERE null_frac0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename SUM((1-null_frac)*avg_width) AS datawidth, Do note that this is an estimate, not an actual figure. The way it works is it estimates the optimized size of the table/index by a calculation from each row sizes times total rows, and compare that against the actual table size.

The value represents the number of "wasted bytes", or the difference between what is actually used by the table and index, and what we compute that it should be. This query will show you list of tables and indexes with the most bloats. Overtime due to MVCC, your table will grow in size (called table bloat) - this is why regular VACUUM is needed. Pg_size_pretty(pg_database_size(datname)) as size This query returns list of the largest databases in your cluster. Show All PostgreSQL Databases And Their Size WHERE nspname NOT IN ('pg_catalog', 'information_schema')

LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) Pg_size_pretty(pg_relation_size(C.oid)) AS "size" This query returns list of the largest (in file sizes) tables and indexes in your database SELECT Show Biggest PostgreSQL Tables/Indexes And Their Size This will send a SIGINT to the current process. To kill a particular query, simply get its pid (using the above query) and run: SELECT pg_cancel_backend(pid) SELECTĭo note that this query will return multiple records for the same process, since it will list down all the corresponding locks that goes with the query. The below display currently running queries, and a granted column indicate whether the query has actually started running (or still waiting to acquire locks). Note: if you're looking for a simple tool to run queries & visualize PostgreSQL results, check out Holistics.io Get Running Queries (And Lock statuses) in PostgreSQL Below are some useful Postgres queries for both troubleshooting, monitoring and analytics purpose.
