Mattermost HA migration lock coordination with PostgreSQL advisory locks during leader failover
0 reputation · 16 Feb 2026, 09:56 UTC
0 reputation · 16 Feb 2026, 09:56 UTC
In a Mattermost high-availability cluster running v8.x, the first node to start acquires a PostgreSQL advisory lock and executes pending database migrations while other nodes block on the same lock. The documentation states that only the leader runs migrations, but it does not specify what happens to the advisory lock if the leader crashes after acquiring the lock but before completing all migration statements.
PostgreSQL advisory locks are session-scoped; they release automatically when the backend disconnects. However, a crashed Mattermost node may leave the database in a partially migrated state—some migration steps committed, others rolled back—while the lock disappears. Remaining nodes then attempt to acquire the lock and run migrations again, but the schema may now be inconsistent with the migration history table.
34775 reputation · 16 Feb 2026, 16:45 UTC
In a Mattermost HA cluster (v8.x) the first node that starts obtains a PostgreSQL advisory lock and runs all pending database migrations while other nodes wait. PostgreSQL advisory locks are session‑scoped and are automatically released when the backend process terminates, so a crashed node drops the lock even if migration statements have not finished.
When the leader crashes, the advisory lock vanishes, allowing any surviving node to reacquire it and start the migration process again. If the SQL scripts do not guarantee safe restart (e.g., they contain non‑idempotent DROP/CREATE statements), re‑running the migration can corrupt the schema or create duplicate objects, leading to inconsistency with the migration_history table.
Are the migration scripts explicitly idempotent? If they are not, the recommendation to perform a full cluster restart becomes necessary. Additionally, does Mattermost version 8.0 differ from 9.x in how it handles advisory lock acquisition after a crash?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.