A pgconf EU talk recap covering how the expand-contract pattern and pgroll enable zero-downtime schema changes and rollbacks.
Written by
Andrew Farries
Published on
November 13, 2024
Schema changes have a reputation. They’re the kind of task that strikes fear into even the most seasoned developers. Downtime, breaking changes, and complex rollbacks can turn an otherwise smooth deployment into a high-stakes game of database roulette. But there is a better way. In my recent talk at PostgreSQL Conference Europe, I dive into PostgreSQL schema migrations using the expand-contract pattern, a strategy designed for zero-downtime, reversible schema changes. Xata built pgroll, a tool that automates this process while taking care of the tricky parts—like data backfills and versioned schemas.
This blog recaps the highlights of my talk which is now available to watch.
It boils down to three big challenges:
What does that mean in practice? Painful patterns like:
The expand-contract pattern tackles these challenges by splitting schema changes into two phases:
This approach avoids downtime and allows old and new versions of your application to coexist while the migration is underway.
Imagine a table with a simple shipped
boolean column. Now you want a more flexible status
enum to track states like pending
, shipped
, and delivered
.
A naive approach would involve a big bang migration:
This works but means downtime—and we all know how users love downtime, not to mention how costly this can be to your business.
With expand-contract:
status
column and dual-write to both shipped
and status
.shipped
to status
.status
exclusively, then drop shipped
.The result? Zero downtime, no user disruptions, and a cleaner database.
Xata took this concept and turned it into a tool: pgroll, an open-source PostgreSQL schema changes tool designed for zero-downtime migrations.
A pgroll migration starts with an expand
phase, where the new schema elements are introduced. Triggers ensure dual writes, and versioned views give each application a consistent schema. Once everything is updated, the contract
phase removes old schema elements, leaving your database clean and ready.
From building pgroll, Xata discovered some key insights about modern schema migrations:
Currently at version 0.7.0, pgroll is already in production use at Xata, but the team is focused on scaling it further. Plans for version 1.0 include:
Watch the full video of my talk to see pgroll in action
Check out pgroll on GitHub and give it a try. Whether you’re wrangling a messy legacy schema or preparing for your next big feature, pgroll lets you migrate your schema changes with confidence—and without downtime.
Xata provides the best free plan in the industry. It is production ready by default and doesn't pause or cool-down. Take your time to build your business and upgrade when you're ready to scale.
Copyright © 2024 Xatabase Inc.
All rights reserved.