ankane/pghero · warning · PgHero::NotEnabled

Space stats not enabled

Error message

Space stats not enabled

What it means

PgHero raises this from PgHero::Methods::Space#space_growth when space_stats_enabled? is false. space_growth computes per-relation growth_bytes by diffing the earliest snapshot in pghero_space_stats (last `days` days) against current relation_sizes, so it needs the pghero_space_stats capture table, which only exists after running the optional space-stats migration. PgHero raises NotEnabled instead of returning fake zero growth when the table is missing.

Source

Thrown at lib/pghero/methods/space.rb:89

              relation,
              sizes[1] AS size_bytes
            FROM
              t
            ORDER BY
              1, 2
          SQL
          stats = select_all_stats(sql, {id: id, start_at: start_at})

          stats.each do |r|
            relation = [r[:schema], r[:relation]]
            if sizes[relation]
              r[:growth_bytes] = sizes[relation] - r[:size_bytes]
            end
            r.delete(:size_bytes)
          end
          stats
        else
          raise NotEnabled, "Space stats not enabled"
        end
      end

      def relation_space_stats(relation, schema: "public")
        if space_stats_enabled?
          relation_sizes ||= self.relation_sizes
          sizes = relation_sizes.map { |r| [[r[:schema], r[:relation]], r[:size_bytes]] }.to_h
          start_at = 30.days.ago

          sql = <<~SQL
            SELECT
              captured_at,
              size AS size_bytes
            FROM
              pghero_space_stats
            WHERE
              database = :id
              AND captured_at >= :start_at

View on GitHub (pinned to 7edb57986f)

Solutions

  1. Run `rails generate pghero:space_stats` followed by `rails db:migrate` to create pghero_space_stats.
  2. Schedule `rake pghero:capture_space_stats` daily (it records one snapshot per day) so growth over `days` is measurable.
  3. If the feature is intentionally off, guard callers with `PgHero.space_stats_enabled?` or rescue PgHero::NotEnabled and use PgHero.relation_sizes for current sizes only.

Example fix

# before - raises PgHero::NotEnabled: Space stats not enabled
PgHero.space_growth(days: 7)

# after
# terminal:
#   rails generate pghero:space_stats
#   rails db:migrate
# config/schedule.rb:
#   every :day do
#     rake "pghero:capture_space_stats"
#   end
growth = PgHero.space_growth(days: 7) if PgHero.space_stats_enabled?
Defensive patterns

Strategy: validation

Validate before calling

return unless PgHero.space_stats_enabled?
growth = PgHero.space_growth(days: 7)

Type guard

def space_stats_available?
  PgHero.space_stats_enabled?
end

Try / catch

begin
  PgHero.space_growth(days: 7)
rescue PgHero::NotEnabled
  PgHero.relation_sizes # current sizes only, no growth
end

Prevention

When it happens

Trigger: Calling PgHero.space_growth(days: 7) or rendering the web UI space/growth view before `rails generate pghero:space_stats && rails db:migrate` has run; or in an environment (e.g. production) where that optional migration was never applied even though it was applied in development.

Common situations: New PgHero setup where only the query-stats generator was run; multi-environment apps where the optional migration is missing on staging/prod; apps that enabled space stats in one database entry of pghero.yml but call PgHero.space_growth on the primary connection that lacks the table.

Related errors


AI-assisted analysis of ankane/pghero@7edb57986f (2026-08-21). Data as JSON: /api/errors/f1e2891e64920e2e. Report an issue: GitHub.