Skip to content

Instantly share code, notes, and snippets.

@huaweigu
Forked from bryanrite/safe.md
Last active September 14, 2017 12:18
Show Gist options
  • Save huaweigu/5a7df95b1beb002406578479eaa20feb to your computer and use it in GitHub Desktop.
Save huaweigu/5a7df95b1beb002406578479eaa20feb to your computer and use it in GitHub Desktop.
Safe Postgres Operations on High Volume Tables

Safe Operations For High Volume PostgreSQL

Safe

  • Add a new column
  • Drop a column
  • Rename a column
  • Add an index concurrently (Example), Note: it will still take a long time to run the migration, but it won't write-lock the table.
  • Drop a constraint (for example, non-nullable)
  • Add a default value to an existing column

Unsafe

Operation Work Around
Add an index Add the index using the CONCURRENTLY keyword
Change the type of a column Add a new column, change the code to write to both columns, and backfill the new column
Add a column with a default Add column, add default as a separate command, and backfill the column with the default value
Add a column that is non-nullable Add a default, enforce non-nullable in code. Backport all null columns in a separate process, when fully backported, add NOT NULL constraint. This takes a short amount of time to run as it has to scan the table once, but usually isn't TOO bad.
Add a column with a unique constraint Add column, add unique index concurrently, and then add the constraint onto the table
VACUUM FULL[3] We use pg_repack instead

Originally taken from: Braintree Article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment