Skip to content

fix(db): give service_role a 120s statement timeout - #860

Merged
gkhngyk merged 1 commit into
mainfrom
fix/service-role-statement-timeout
Sep 30, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/service-role-statement-timeout

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Summary

service_role had no statement_timeout of its own, so requests made over PostgREST with the service-role key inherited authenticator's 8s. That is the same cap as a user's page load. Several code comments assume the service role runs without a timeout; that assumption was wrong.

Impact found

  • On the largest brand, refresh_insights_daily returned 500 (canceling statement due to statement timeout) every day, both in the nightly sweep at the start of the cycle and when the brand's run stamped.
  • As a result that brand's current day never reached the Insights rollups. On 30 Sep there were 3,965 results in prompt_results and none in insights_brand_daily, so the 7/30/90-day Insights views were missing the day.
  • Pulse, signal-pass and report RPCs run under the same cap. The pulse engine already retries on statement timeouts for this reason.

Change (migration 00100_service_role_statement_timeout)

  • alter role service_role set statement_timeout = '120s' gives background work two minutes.
  • User-facing roles are unchanged: authenticated stays at 8s and anon at 3s.
  • The migration also sends notify pgrst, 'reload config' so PostgREST picks up the per-role setting without a restart.

Already applied to production. pg_roles shows the new setting. The missing day was rebuilt through the service role, and the rollup now matches the raw results for 30 Sep (3,965 answers). This PR brings the repo in line with the database.

Related issue

None. Found while investigating a Topics detail page failure.

Type of change

  • Bug fix

How to test

  1. select rolname, rolconfig from pg_roles where rolname in ('service_role', 'authenticated', 'anon'); shows statement_timeout=120s for service_role, with the other two unchanged.
  2. After the next nightly cycle, refresh_insights_daily no longer returns 500 for the largest brand, either in the 02:00 sweep or at the run's stamp.
  3. insights_brand_daily holds the current day for that brand, matching the raw result count.

Checklist

  • Migration applied via MCP and committed as a numbered file
  • supabase/schema.sql regenerated with build-schema.sh

🤖 Generated with Claude Code

service_role had no statement_timeout of its own, so over PostgREST it
inherited authenticator's 8s. On the largest brand refresh_insights_
daily timed out every day, both in the nightly sweep and when the run
stamped, so that brand's current day never reached the Insights rollups
(30 Sep: 3,965 results, none rolled up). Pulse, signal and report work
runs under the same cap.

Background jobs now get two minutes; authenticated stays at 8s and anon
at 3s. Applied to production, with the missing day rebuilt.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Sep 30, 2026
@gkhngyk
gkhngyk merged commit 5510c80 into main Sep 30, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant