Skip to content

Commit dceb512

Browse files
feat(develop): Clarify adding constraints (#12169)
* feat(develop): Clarify adding constraints * update adding columns
1 parent f78f8c1 commit dceb512

File tree

1 file changed

+9
-7
lines changed
  • develop-docs/backend/application-domains/database-migrations

1 file changed

+9
-7
lines changed

develop-docs/backend/application-domains/database-migrations/index.mdx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,28 +440,30 @@ With postgres 14, columns can be added to tables of all sizes as deploy time
440440
migrations if you follow the guidelines on default values & allowing nulls. When
441441
creating new columns they should either be:
442442

443-
- Not null with a default. https://develop.sentry.dev/database-migrations/#adding-not-null-to-columns
443+
- Not null with a default
444444
- Created as nullable. If no default value can be set on the column, then it's best just to make it nullable.
445445

446+
For nullable columns with a constraint and not null columns with a default, see [Adding Constraints to Columns](#adding-constraints-to-columns-including-not-null). For bigger tables, these constraints can lock the table and cause downtime.
447+
448+
446449
### Adding Columns With a Default
447450

448451
Since we run Postgres >= 14 in production we are able to add columns with a default. To do so, instead of using `default=<your_default>`, use `db_default=<your_default>`. This tells Django to set a default at the database level and manage it there, rather than managing it in application code.
449452

450453
We can't use `default` because Django's default behaviour for creating a new not null column with a default is dangerous. When using default, in the migration Django will add the default to backfill all fields, then immediately remove it so that it can handle them in the app layer. This means that during a deploy, the column is sitting in production without a default until all code rolls out, which means that inserts will fail for this table until the deploy completes.
451454

452-
### Adding Not Null To Columns
455+
### Adding Constraints to Columns (Including Not Null)
453456

454-
It can be dangerous to add not null to columns, even if there is data in every row of the table for that column. This is because Postgres still needs to perform a not null check on all rows before it can add the constraint. On small tables this can be fine since
455-
the check will be quick, but on larger tables this can cause downtime. There are a few options here to make this safe:
457+
It can be dangerous to add constraints to columns, whether they are new or existing columns, even if the data in every row of the table does not violate the constraint. This is because Postgres still needs to perform a constraint check on all rows before it can add the constraint. On small tables this can be fine since the check will be quick, but on larger tables this can cause downtime. There are a few options here to make this safe:
456458

457459
```sql
458-
ALTER TABLE tbl ADD CONSTRAINT cnstr CHECK (col IS NOT NULL) NOT VALID;
460+
ALTER TABLE tbl ADD CONSTRAINT cnstr CHECK (col IS NOT NULL) NOT VALID; -- example with a not null constraint
459461
ALTER TABLE tbl VALIDATE CONSTRAINT cnstr;
460462
```
461463

462-
One approach is to create the constraint as not valid. Then we validate it afterwards. We still need to scan the whole table to validate, but we only need to hold a `SHARE UPDATE EXCLUSIVE` lock, which only blocks other `ALTER TABLE` commands, but will allow reads/writes to continue. This works well, but has a slight performance penalty of 0.5-1%. After Postgres 12 we can extend this method to add a real `NOT NULL` constraint.
464+
One approach is to create the constraint as not valid. Then we validate it afterwards. We still need to scan the whole table to validate, but we only need to hold a `SHARE UPDATE EXCLUSIVE` lock, which only blocks other `ALTER TABLE` commands, but will allow reads/writes to continue. This works well, but has a slight performance penalty of 0.5-1%. After Postgres 12 we can extend this method to add the real constraint.
463465

464-
Alternatively, if the table is small enough and has low enough volume it should be safe to just create a normal `NOT NULL` constraint. Small being a few million rows or less.
466+
Alternatively, if the table is small enough and has low enough volume it should be safe to just create the constraint as is. Small being a few million rows or less.
465467

466468
### Altering Column Types
467469

0 commit comments

Comments
 (0)