"""admin_table_stats Revision ID: c2633c094def Revises: 81c449dfcd68 Create Date: 2019-01-18 16:16:39.812128 """ # revision identifiers, used by Alembic. revision = 'c2633c094def' down_revision = '81c449dfcd68' branch_labels = None depends_on = None from alembic import op import sqlalchemy as sa def upgrade(): op.execute(''' create table admin_table_stats as SELECT current_date::date as_of, sizes.*, total_bytes - index_bytes - COALESCE(toast_bytes, 0) AS table_bytes, ut.last_analyze, ut.last_autoanalyze, ut.n_live_tup FROM ( SELECT c.oid, nspname AS table_schema, relname AS table_name, c.reltuples AS row_estimate, pg_total_relation_size(c.oid) AS total_bytes, pg_indexes_size(c.oid) AS index_bytes, pg_total_relation_size(reltoastrelid) AS toast_bytes FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE relkind = 'r' ) sizes JOIN pg_stat_user_tables ut on ut.relname = sizes.table_name; ''') def downgrade(): op.execute(''' drop table admin_table_stats ''')