Skip to content

fix(migrations): use ALTER TABLE DISABLE TRIGGER instead of SET session_replication_role#831

Merged
rickyrombo merged 1 commit into
mainfrom
mp/fix-trigger-disable-cloudsql
May 19, 2026
Merged

fix(migrations): use ALTER TABLE DISABLE TRIGGER instead of SET session_replication_role#831
rickyrombo merged 1 commit into
mainfrom
mp/fix-trigger-disable-cloudsql

Conversation

@rickyrombo
Copy link
Copy Markdown
Contributor

Summary

  • Replaces SET LOCAL session_replication_role = replica with ALTER TABLE sol_reward_disbursements DISABLE TRIGGER on_sol_reward_disbursement (and a matching ENABLE TRIGGER after the INSERT) in 0201_backfill_missing_reward_disbursements.sql.

Why

The migration Job in prod hit:

ERROR:  permission denied to set parameter "session_replication_role"

session_replication_role requires a true Postgres superuser. CloudSQL doesn't expose one — the cloudsqlsuperuser role the migration runs as can do most operations but specifically not this one.

ALTER TABLE ... DISABLE TRIGGER only needs table-owner privilege (which the migration role has) and is rolled back with the surrounding transaction on failure, so the trigger's enable state is preserved if anything goes wrong before COMMIT.

Targeting the specific trigger by name (rather than DISABLE TRIGGER USER) keeps the change surgical — unrelated triggers (none in practice on this table, but defensive) are untouched.

Test plan

  • Roll the new image; the migration Job should run through without the permission error.
  • Confirm on_sol_reward_disbursement is tgenabled = 'O' (enabled) after the migration completes:
    sql SELECT tgname, tgenabled FROM pg_trigger WHERE tgname = 'on_sol_reward_disbursement';
  • Confirm no rogue challenge_reward notifications were created from this backfill:
    sql SELECT COUNT(*) FROM notification WHERE type = 'challenge_reward' AND timestamp > NOW() - INTERVAL '1 hour';
    Expected: small (only from concurrent live writes that fired the trigger normally), not ~29k.

🤖 Generated with Claude Code

…on_replication_role

The previous version of 0201 used `SET LOCAL session_replication_role
= replica` to skip the on_sol_reward_disbursement trigger during the
backfill. That parameter requires a true superuser, which CloudSQL
doesn't expose — `cloudsqlsuperuser` (which the migration role is in)
can't set it, producing:

  ERROR: permission denied to set parameter "session_replication_role"

Switch to `ALTER TABLE ... DISABLE TRIGGER on_sol_reward_disbursement`
+ re-enable after the INSERT. Both DDL statements only require table-
owner privilege (which the migration role has) and are rolled back
with the transaction on failure, so the trigger state is preserved if
anything goes wrong.

Targets the specific trigger by name rather than DISABLE TRIGGER USER
so unrelated triggers (none in practice on this table, but defensive)
aren't affected.
@rickyrombo rickyrombo merged commit 6190f64 into main May 19, 2026
4 checks passed
@rickyrombo rickyrombo deleted the mp/fix-trigger-disable-cloudsql branch May 19, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant