PostgreSQLDev & Eng

Content type: Hands-on

6 min read

Data Egret checklist helps avoid downtime in PostgreSQL DDL and DML migrations

Article · Dev & Eng
Reading
6 min
Code
2
In short

Data Egret, a consultancy, published a set of questions to assess, before running in production, the real risk of an ALTER TABLE, a constraint validation, or a bulk UPDATE in PostgreSQL.

A consultoria Data Egret publicou um roteiro de perguntas para avaliar, antes de rodar em produção, o risco real de um ALTER TABLE, de uma validação de constraint ou de um UPDATE em massa no PostgreSQL.

Every PostgreSQL database in production, sooner or later, needs a change: adding a column, creating an index, fixing data in bulk, validating a new constraint. Data Egret, a consultancy specializing in PostgreSQL, published a checklist called the "PostgreSQL DDL/DML Migration Checklist for the Anxious," gathering the questions that, according to the company, apply to any migration of this kind, regardless of team size or system criticality.

The document doesn't bring a new tool or an unprecedented command. It's a verification checklist, designed for anyone who will run ALTER TABLE, CREATE INDEX, a bulk UPDATE, or anything similar directly in production. For those who administer databases day to day, its value lies less in novelty and more in systematizing questions that, in practice, tend to be asked too late, when the query is already stuck waiting for a lock.

What Postgres actually does under the hood of the command

The checklist's first question is also the most ignored one: does the command that seems trivial rewrite the entire table, or does it just read the data?

Expert views

What people are saying about it.

Vercel has enabled, via Vercel MCP, permission for agents to create virtual models, adjust routing, and set spending budgets in the AI Gateway without having to open the dashboard.

Carina Ferreirain an iMasters article · Read the article: Vercel MCP now lets AI agents configure the AI Gateway in natural language

JetBrains has confirmed that CVE-2026-114938, nicknamed Java4Shell, affects the JBR embedded in all its IDEs as well as the JDK running in production. There's a patch and a workaround, but they are separate fixes.

Bisneto Bragain an iMasters article · Read the article: Java4Shell vulnerability exposes JVM in production: here's how to fix it now

Kubernetes 1.38, expected on December 16, 2026, drops environment variables used in scripts, retires a legacy cgroup mechanism, and changes swap behavior. Here's what to review before upgrading your cluster.

Rafael Oliveirain an iMasters article · Read the article: Kubernetes 1.38 comes with changes that can break CI/CD and legacy nodes

A table rewrite makes PostgreSQL create a new physical copy: every row is rewritten, indexes are rebuilt, and the table is locked for reading and writing while this runs. Changing a column's type or adding a column with a volatile DEFAULT (such as DEFAULT now() or DEFAULT random()) usually triggers this behavior.

Other operations don't rewrite the table but still read every row, a typical case being ALTER TABLE ... ADD CONSTRAINT ... CHECK without the NOT VALID clause. The result is a full scan that consumes CPU and I/O for a time proportional to the table's size, even without locking everything. And any large operation (bulk update, delete, rewrite, index creation) can generate a high volume of WAL, which translates into more I/O, more disk usage, and, in replicated environments, lag.

The lock is the detail that decides whether the application feels the change

Knowing which lock level the command requires is the checklist's second point, and perhaps the most practical one in a DBA's daily routine. PostgreSQL's documentation lists, for each DDL command, the required lock mode and which other modes it conflicts with. Different commands block reads, writes, or both, and some migration strategies exist precisely to avoid this full blocking, at the cost of running more slowly.

The problem is rarely just the migration getting stuck. If another transaction already holds an incompatible lock, the migration waits. And every subsequent transaction that would need the same resource waits behind it, forming a queue. That's how a seemingly harmless ALTER TABLE manages to stop the entire application: not because of the time it takes itself, but because of the queue it creates behind it.

The checklist recommends setting lock_timeout so as not to wait indefinitely for a lock, and statement_timeout to limit how long the migration can run. In practice:

sql
SET lock_timeout = '2s';
SET statement_timeout = '30s';
ALTER TABLE pedidos ADD COLUMN status_novo text;

If the migration can't acquire the lock within two seconds, it fails and can be retried, instead of sitting in the queue blocking everything behind it.

Is the environment ready to receive the change

Before running anything, the checklist asks to check for long-running open transactions, maintenance routines or backups already in progress, available disk space, and the actual size of the affected table. An operation that takes seconds on a table with a few thousand rows can take hours on a table with hundreds of millions, and the migration plan needs to reflect that.

It's also worth coordinating with the team: a manual VACUUM or an ETL job running at the same time doesn't necessarily conflict in terms of locks with the migration, but it competes for CPU, I/O, and memory, inflating the total execution time for no apparent reason.

Logical replication is the blind spot of many migrations

If the table takes part in logical replication, the checklist calls attention to a detail that easily goes unnoticed: removing or renaming a column, or changing a field's type, can break the consumer on the other side of the replica, because this kind of change usually requires coordinated adjustment outside PostgreSQL, on the publisher and the subscriber.

In addition, large updates and deletes, or table rewrites, tend to temporarily increase replication lag. For systems that depend on up-to-date data on the replica side (reports, cache, another service consuming the replicated data), this deserves advance notice to the team, not discovery in production.

Divide to avoid breaking

The checklist's central recommendation, and the one that most separates a smooth migration from an incident, is simple: few migrations need to be done in a single step. Breaking the process into smaller stages reduces blocking, makes it easier to monitor progress, and, most importantly, makes the process reversible midway through.

A well-known pattern for validating a new constraint without locking the entire table:

sql
-- Step 1: adds the constraint without validating history
ALTER TABLE pedidos ADD CONSTRAINT valor_positivo CHECK (valor > 0) NOT VALID;

-- Step 2, in another transaction: validates existing data
ALTER TABLE pedidos VALIDATE CONSTRAINT valor_positivo;

The first step is fast and only prevents new invalid rows. The second does the full scan, but with a more permissive lock, which doesn't block concurrent writes the same way a traditional ALTER TABLE does.

If it goes wrong: test beforehand and have an exit plan

The checklist insists on testing the migration on a realistic dataset before going to production, not just for correctness, but to get a reliable estimate of time and impact. A test table with a hundred rows doesn't reveal the behavior of a production table with a hundred million.

And before starting, it's worth deciding: can the change be reverted? Can the migration be safely interrupted midway? How does the application behave if the process is aborted halfway through? Without these answers defined in advance, the decision gets made under pressure, in the middle of an incident.

Monitor during and validate afterward

While the migration runs, the checklist lists what to monitor: long-running or blocked queries, replication lag, CPU, I/O, memory, and disk usage, plus errors and warnings in the PostgreSQL log. When the command supports it, tracking progress directly avoids being left in the dark about how much is left.

After the migration finishes, the checklist includes confirming that performance hasn't degraded, running ANALYZE if the planner's statistics became outdated, checking table and index bloat after large data modifications, confirming that replication has caught up with the primary again, and reviewing the logs for errors that went unnoticed during execution.

What changes for those who operate databases in production

Nothing in the checklist is an isolated surprise for anyone who has already read PostgreSQL's documentation on ALTER TABLE and lock modes. Its value lies in gathering these questions into a roadmap applicable before every deploy that touches schema or bulk data, instead of relying on the memory of whoever is on call.

For teams without a dedicated DBA, this kind of list works as a partial substitute for experience: it doesn't prevent every incident, but it reduces the chance of someone running an ALTER TABLE on a production table at 3pm on a Tuesday without knowing it rewrites 40 million rows.

Translated from the Brazilian Portuguese original · Read the original