Skip to content

Invariant checks hit Postgres statement timeout on PayoutTransactionsAmountInvariant and SubscriptionsCanceledDeletedCustomerInvariant #13433

Description

@stilla

Problem

The observability.invariants.check worker task (polar/observability/invariants/tasks.py) runs every 15 minutes and consistently times out for two rules:

  • PayoutTransactionsAmountInvariant
  • SubscriptionsCanceledDeletedCustomerInvariant

Logfire traces show the inner SELECT for both queries runs for exactly ~30.03-30.04s before erroring with an empty-message TimeoutError at asyncpg's bind_execute, indicating a fixed ~30s Postgres statement timeout is being hit consistently, not intermittently.

In a 6h production sample: 17 of 185 check task executions failed this way (~8%), concentrated entirely on these two rules (9 and 8 failures respectively); the other 5 invariant rules had zero failures.

Because these tasks are configured with max_retries=0, a timeout doesn't retry, it's silently dropped until the next scheduled cycle (surfaced only as a polar.worker.sqs_retry_exhausted log, no InvariantError/Slack alert since the query never completes).

Example traces

  • PayoutTransactionsAmountInvariant: trace 019fade880e8ad9f7c930f2682ed2d9e, span ced74fb7cb78a1ef
  • SubscriptionsCanceledDeletedCustomerInvariant: trace 019fade8ba029816dffec21748716dac, span 3862eb0d06175ff4

Likely cause

PayoutTransactionsAmountInvariant's query does an anti-join subquery on transactions.payout_id plus a GROUP BY aggregation over all rows with payout_transaction_id set, likely a large scan. SubscriptionsCanceledDeletedCustomerInvariant joins subscriptions to customers filtering on customers.deleted_at IS NOT NULL AND subscriptions.active IS TRUE, which may lack a supporting index.

Related prior art

Similar Postgres statement-timeout issues in worker tasks were previously fixed by:

Suggested fix

Sent by @joebon from Logfire polar-production-worker event investigation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions