You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/sql-review-rule-explained-enforce-not-valid-in-check.md
+17-13Lines changed: 17 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,10 @@ tags: Explanation
7
7
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.
8
8
---
9
9
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
+
10
14
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.
11
15
This SQL Review rule ensures that all new CHECK constraints are created using the safe, non-blocking approach.
12
16
@@ -34,16 +38,16 @@ This is where the risk comes from.
34
38
35
39
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:
36
40
37
-
* Reads
38
-
* Writes
39
-
* Other schema changes
41
+
- Reads
42
+
- Writes
43
+
- Other schema changes
40
44
41
45
On large tables or busy production databases, the lock can cause:
42
46
43
-
* Query timeouts
44
-
* Application errors
45
-
* Service degradation
46
-
* Full outages
47
+
- Query timeouts
48
+
- Application errors
49
+
- Service degradation
50
+
- Full outages
47
51
48
52
This SQL Review rule prevents teams from accidentally introducing blocking schema changes during deployments.
49
53
@@ -59,9 +63,9 @@ ALTER TABLE orders
59
63
ADD CONSTRAINT orders_positive CHECK (amount >0) NOT VALID;
60
64
```
61
65
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
65
69
66
70
**Step 2: Validate at a convenient time**
67
71
@@ -103,7 +107,7 @@ This ensures the constraint is applied safely without disrupting user traffic.
103
107
Adding CHECK constraints without `NOT VALID` can block reads and writes and lead to downtime.
104
108
This SQL Review rule enforces the two-step PostgreSQL best practice:
105
109
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
108
112
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