Skip to main content

Overview

The pf_aggregate_resource_usage() function rolls up realtime usage into daily/monthly periods. It should be scheduled to run periodically via pg_cron.

Function

This SECURITY DEFINER function:
  1. Iterates all active quotas with reset_period = 'daily' or 'monthly'
  2. Sums realtime usage rows for the current period
  3. Upserts aggregated rows into pf_resource_usage with aggregation_period = 'daily' or 'monthly'

Scheduling via pg_cron

Prerequisites

Enable pg_cron and pg_net extensions in the Supabase Dashboard → Database → Extensions.

Via Supabase Dashboard

  1. Go to SQL Editor → New Query
  2. Paste the cron.schedule(...) SQL above
  3. Run the query

Verification

Notes

  • The aggregation function is idempotent (uses UPSERT)
  • No edge function needed — runs directly in Postgres
  • Realtime rows older than 7 days can be safely pruned
  • Monitor via pf_resource_usage row counts per aggregation_period