fix(migrations): use ALTER TABLE DISABLE TRIGGER instead of SET session_replication_role#831
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SET LOCAL session_replication_role = replicawithALTER TABLE sol_reward_disbursements DISABLE TRIGGER on_sol_reward_disbursement(and a matchingENABLE TRIGGERafter the INSERT) in0201_backfill_missing_reward_disbursements.sql.Why
The migration Job in prod hit:
session_replication_rolerequires a true Postgres superuser. CloudSQL doesn't expose one — thecloudsqlsuperuserrole the migration runs as can do most operations but specifically not this one.ALTER TABLE ... DISABLE TRIGGERonly 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
on_sol_reward_disbursementistgenabled = 'O'(enabled) after the migration completes:sql SELECT tgname, tgenabled FROM pg_trigger WHERE tgname = 'on_sol_reward_disbursement';challenge_rewardnotifications 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