Skip to main content

Customer meter usage aggregation

The PlanetScale query fingerprint sums a numeric field from events.user_metadata. It combines two sums with UNION ALL: one filters by customer_id, the other by external_customer_id. Both filter by organization, event name, and source. The query has no time bound. The indices below cover the external_customer_id branch. The partial predicate limits each index to one organization and event name. Including user_metadata lets PostgreSQL check the JSON value’s type and compute the sum from the index when an index-only scan is possible.
Use the ix_events_meter_ prefix for these manually managed indices. Alembic autogeneration ignores database-only indices with this prefix on events.