Skip to content

Commit 7eb7d73

Browse files
committed
chore: minor rule wording tweak
1 parent fe5926a commit 7eb7d73

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

content/blog/sql-review-rule-explained-enforce-not-valid-in-check.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ tags: Explanation
77
description: Learn why enforcing NOT VALID in CHECK constraints is important and how the "Enforce NOT VALID in CHECK" review rule protects your production database.
88
---
99

10+
Bytebase [SQL Review](https://docs.bytebase.com/change-database/review) contains a rule to enforce including "NOT VALID" option when adding "CHECK" constraints:
11+
12+
> Adding a CHECK constraint needs to verify the existing data and requires ACCESS EXCLUSIVE table lock. This blocks read and write, which may cause business interruption. It is recommended to add the "NOT VALID" option to validate new data and manually validate existing data after the change is completed.
13+
1014
PostgreSQL allows you to add `CHECK` constraints to enforce data quality rules on a table. This is useful, but adding a CHECK constraint in the wrong way can block reads and writes and cause unexpected downtime.
1115
This SQL Review rule ensures that all new CHECK constraints are created using the safe, non-blocking approach.
1216

@@ -34,16 +38,16 @@ This is where the risk comes from.
3438

3539
When PostgreSQL validates an existing table, it must scan every row to confirm the constraint is not violated. During this step, it acquires an `ACCESS EXCLUSIVE` lock. This lock is the strongest one in PostgreSQL and can block:
3640

37-
* Reads
38-
* Writes
39-
* Other schema changes
41+
- Reads
42+
- Writes
43+
- Other schema changes
4044

4145
On large tables or busy production databases, the lock can cause:
4246

43-
* Query timeouts
44-
* Application errors
45-
* Service degradation
46-
* Full outages
47+
- Query timeouts
48+
- Application errors
49+
- Service degradation
50+
- Full outages
4751

4852
This SQL Review rule prevents teams from accidentally introducing blocking schema changes during deployments.
4953

@@ -59,9 +63,9 @@ ALTER TABLE orders
5963
ADD CONSTRAINT orders_positive CHECK (amount > 0) NOT VALID;
6064
```
6165

62-
* Only a brief catalog lock is required
63-
* The constraint is enforced for all new inserts and updates
64-
* Existing rows are not scanned yet
66+
- Only a brief catalog lock is required
67+
- The constraint is enforced for all new inserts and updates
68+
- Existing rows are not scanned yet
6569

6670
**Step 2: Validate at a convenient time**
6771

@@ -103,7 +107,7 @@ This ensures the constraint is applied safely without disrupting user traffic.
103107
Adding CHECK constraints without `NOT VALID` can block reads and writes and lead to downtime.
104108
This SQL Review rule enforces the two-step PostgreSQL best practice:
105109

106-
* Add the constraint using `NOT VALID`
107-
* Validate it later using a non-blocking operation
110+
- Add the constraint using `NOT VALID`
111+
- Validate it later using a non-blocking operation
108112

109-
By following this pattern, teams can ensure constraint correctness without risking production stability.
113+
By following this pattern, teams can ensure constraint correctness without risking production stability.

0 commit comments

Comments
 (0)