Database, table, index size.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Thu, 09 Apr 2015 17:53:43 +0300
changeset 1710 601c31753686
parent 1709 f34c6bc973cb
child 1711 379096db1a5b
Database, table, index size.
postgre.rst
--- a/postgre.rst	Fri Apr 03 10:57:19 2015 +0300
+++ b/postgre.rst	Thu Apr 09 17:53:43 2015 +0300
@@ -70,6 +70,30 @@
 
   set search_path to NAME;
 
+Database, table, index size.
+============================
+
+Database size::
+
+  SELECT pg_database_size('geekdb');  -- in bytes
+  SELECT pg_size_pretty(pg_database_size('dbname'));
+
+List of databases sizes::
+
+  \l+
+
+Table total size (with indexes)::
+
+  SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename'));
+
+Sole table size (without indexes and other)::
+
+  SELECT pg_size_pretty(pg_relation_size('schemaname.tablename'));
+
+Largest table in the PostgreSQL database::
+
+  SELECT relname, relpages FROM pg_class ORDER BY relpages DESC;
+
 Using psql client.
 ==================
 
@@ -84,3 +108,13 @@
 
   $ psql -U $USER -h $HOST  $SCHEMA
 
+How to view execution plan::
+
+  EXPLAIN query;
+  EXPLAIN ANALYZE query;
+
+How to redirect the output of query to a file::
+
+  \o output_file
+  SELECT * FROM pg_class;
+