QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

What does QA check before, during and after a schema migration: compatibility, rollback, locking?

QA checks a schema migration in three directions: compatibility, rollback and locking. During a deployment both old and new code run, so both pairs of versions and schemas are checked. QA watches who waits for whom in pg_stat_activity and measures how long a lock is held. After a failure it looks for invalid indexes and unvalidated constraints, and the rollback is rehearsed.

SQL · SQL in QA work

The migration is green in CI, but during the deployment half of the servers still run the old code and fail. What was not checked?

The usual migration check answers the question "is the data right after it". But in a live system a migration runs while users work, and old and new code share one database for a while. The separate questions: will the old version fail, can we go back, and how long will the table stay locked. Comparing data before and after is covered in the lesson on testing migrations; this one is about the rest: compatibility, rollback and locking.

How it works and why. During a rolling deployment two dangerous pairs are possible: the old application with the new schema (the migration has run, servers are still updating) and the new application with the old schema (a schema rollback or a delayed migration). So for every change QA asks about both pairs. Renaming a column breaks the first: in a test the old version query SELECT email after RENAME COLUMN email TO mail gave column "email" does not exist. Adding a nullable column does not break it.

PhaseQuestionEvidence
BeforeWhich lock does each statement take?rehearsal on a copy
BeforeDoes the old version work?its tests on the new schema
DuringWho waits for whom?pg_stat_activity
AfterWhat was left unfinished?system catalogs

During: who blocks whom. While the migration runs in the rehearsal, ordinary traffic or at least a few queries are run in parallel and QA watches whether they wait. The migration total time matters less than how long a strong lock is held: a batched backfill can take long while the table stays available.

SELECT pid, wait_event_type,
       pg_blocking_pids(pid) AS blockers,
       left(query, 40) AS query
FROM pg_stat_activity
WHERE cardinality(pg_blocking_pids(pid)) > 0;

The queue behind an ALTER is covered in the lesson "How do you change a schema without downtime: expand/contract, CONCURRENTLY and NOT NULL?"; here what matters is that no ordinary queries appear in the output during the rehearsal. If they do, the migration statement takes an excess lock or waits too long, and it is rewritten (lock_timeout, CONCURRENTLY).

After: what was left unfinished. A failed migration is not always rolled back completely: invalid indexes remain after a failed CREATE INDEX CONCURRENTLY, and a NOT VALID constraint without VALIDATE checks only new rows. Both states are visible in the catalogs:

-- indexes that queries ignore
SELECT indexrelid::regclass FROM pg_index
WHERE NOT indisvalid;

-- constraints that skip old rows
SELECT conname FROM pg_constraint
WHERE NOT convalidated;

In a test the first query returned an index after a uniqueness violation (indisvalid = false), the second showed a constraint before VALIDATE (convalidated = false) and true after it. An empty result of both queries is part of the done criteria.

Rollback. The rollback plan is checked, not written as "see the down script". Questions: does it lose data (dropping a column is permanent), is it compatible with the code that already runs, and is there a faster way forward. For big tables a fix forward (a new migration) is often safer than a rollback. In PostgreSQL most DDL runs in a transaction and rolls back completely, but CONCURRENTLY does not work in a transaction, so it has no rollback: its leftovers are cleaned up by hand (see the lesson "How do you change a schema without downtime…"). A rollback rehearsal on a copy: after the new code has run for a while, a query on the new column shows how many values the down script would lose. If it is not zero, a rollback without a backup of the column is unsafe and the plan becomes a fix forward.

-- before running the down script
SELECT count(*) FROM orders
WHERE note IS NOT NULL;  -- N values
ALTER TABLE orders DROP COLUMN note;
-- the N values are gone for good

Limits and alternatives. A rehearsal on a copy does not reproduce the real concurrency and long transactions of production, so locking is checked under a load of similar shape (long reports, batch jobs). For critical tables the migration is started with a wait limit and a ready retry plan. In MySQL the analogue of a lock check is the explicit ALGORITHM and LOCK in the statement: if the operation does not support the requested mode it is rejected, and that shows already in the rehearsal.

Common mistakes.

  • Testing only the new application version on the new schema: the pair "old code, new schema" is left out.
  • Considering a migration safe because it is fast: what matters is how long the lock is held and the queue behind it.
  • Not checking the catalogs after a failure: an invalid index or an unvalidated constraint stays silently.
  • Having a down script nobody has run: the first run happens during an incident.

Check yourself

A migration renames a column and every test of the new code is green. Which pair do you check additionally and what do you expect?

Answer

The old application on the new schema: its queries to the old name fail with a missing-column error. That is why a rename is split into expand/contract.

After the migration everything works. Which two catalog queries do you run before closing the task?

Answer

A search for invalid indexes (pg_index, indisvalid) and unvalidated constraints (pg_constraint, convalidated): both must return an empty result.

The down script drops a column added yesterday. What do you check before rolling back?

Answer

How much data the new code has already written to it: DROP COLUMN loses it for good, so either keep the column or fix forward.

In an interview. Junior: After a migration I check that the data is in place, the application works and there is a way back. Middle/Senior: I check both version pairs during a deployment, I see who waits for whom (pg_stat_activity) and I measure how long the lock is held, not only the duration. After a failure I look for invalid indexes and unvalidated constraints, and I do not consider a rollback safe without a rehearsal.

More articles in this chapter