Course outline

PostgreSQL / Advanced: evidence and reliable operations / Lesson 21

Stage and validate a new check constraint

Separate enforcement for new writes from validation of existing rows during a controlled migration.

About 18 min read20 worked examples20 practice exercisesContent reviewed
On this page

Lesson goals

  • Explain NOT VALID and later constraint validation.
  • Find and repair known invalid lab data before validation.
  • Plan locks, compatibility, and evidence for a real migration.
  • Stage and validate a PostgreSQL CHECK constraint while protecting subsequent writes.
  • Verify historical repair, NULL policy and catalog validation state with independent evidence.

Define the invariant and rollout

An existing table may contain rows that violate a new rule. PostgreSQL can add certain constraints with NOT VALID so the initial addition skips checking all existing rows while subsequent inserts and updates are subject to the constraint. A later VALIDATE CONSTRAINT checks existing data. This is a staged enforcement mechanism, not permission to leave known violations indefinitely or a guarantee of a lock-free change.

Find a pre-existing violation

The following SQL belongs only in a new disposable PostgreSQL 18 lab. It creates two synthetic inventory rows, one with a deliberately invalid negative quantity, then adds a named check as NOT VALID. The final SELECT identifies the pre-existing violation. A real repair needs an approved business rule; setting a value to zero is only an explicit decision for this training fixture.

sql Install a staged check in the lab
CREATE TABLE lab_inventory (id integer PRIMARY KEY, quantity integer NOT NULL);
INSERT INTO lab_inventory VALUES (1, 4), (2, -1);
ALTER TABLE lab_inventory ADD CONSTRAINT lab_quantity_nonnegative CHECK (quantity >= 0) NOT VALID;
SELECT id, quantity FROM lab_inventory WHERE quantity < 0 ORDER BY id;

The existing row with id 2 remains present and is returned by the diagnostic. New or updated rows must satisfy the check after it is added. The code is illustrative and does not connect to a configured database.

Repair and validate the fixture

Correct the synthetic row using a guarded predicate, then validate the named constraint and inspect its catalog status. The guard prevents this example from overwriting a row that no longer matches the expected old value. In production, first reconcile the source of each violation, back up appropriately, and use a bounded change procedure with verification and recovery.

sql Repair one row and validate
UPDATE lab_inventory SET quantity = 0 WHERE id = 2 AND quantity = -1;
ALTER TABLE lab_inventory VALIDATE CONSTRAINT lab_quantity_nonnegative;
SELECT conname, convalidated FROM pg_constraint
WHERE conrelid = 'lab_inventory'::regclass AND conname = 'lab_quantity_nonnegative';

Starting from the fixture, id 2 becomes zero and validation can succeed if no other violation exists. The catalog query reports the constraint’s validation state; actual output and lock waits depend on the environment.

Plan concurrency and compatibility

Consult the ALTER TABLE documentation for the specific constraint type and version: adding and validating constraints take locks with different effects. Test with representative traffic and long transactions in an isolated environment and define timeouts and a stopping procedure. Ensure old and new application versions can satisfy the new rule before enforcing it. NOT VALID does not mean that new violations are accepted after the constraint is installed.

Verify both data and behavior

Test a valid new row, an invalid new row inside a rolled-back lab transaction, and the original legacy violation. Confirm the validation state and application behavior after the approved migration. Record row counts, lock waits, errors, and recovery evidence. A successful catalog query or a corrected sample alone does not establish that all existing data and concurrent writers meet the full business contract.

Define the rule and existing data

A staged CHECK migration begins with a precise domain rule and a representative inventory of existing violations. In the lesson’s fixture, quantity >= 0 is the proposed rule and row id 2 has quantity -1, so that row violates it. Decide separately whether NULL is allowed: a PostgreSQL CHECK is satisfied when its expression is true or unknown, so a nonnegative check alone does not enforce presence. Document the owner and authorized repair policy before changing data.

A count of invalid rows is useful but does not identify which business records need correction. Record exact keys and original values in a protected audit appropriate to the system. The examples below are synthetic PostgreSQL 18 scenarios and reasoned outcomes; no server, constraint or production data was changed. Use an isolated authorized environment for any rehearsal.

Separate installation from validation

PostgreSQL permits a CHECK constraint to be added NOT VALID so existing rows are not immediately scanned for validation. The constraint is nevertheless enforced for subsequent inserts and updates according to the documented behavior. An existing negative quantity can remain until repaired, but a new quantity of -2 is rejected after the constraint is installed. NOT VALID is a staged migration tool, not a switch that disables the rule for new writes.

Inspect the constraint definition and validation state rather than relying on the command’s success message. PostgreSQL’s catalog records whether a constraint has been validated; use the documented metadata and target version. A failed new write can leave an explicit transaction aborted until rollback, so test rejection inside a disposable transaction with an explicit cleanup path.

Repair historical rows deliberately

Repair existing violations using a business-approved mapping, not an arbitrary value chosen just to make validation pass. Identify the exact rows, verify the source of corrected quantities, and use a guarded UPDATE that targets only those records. Check affected keys and before-and-after values. A count alone can hide a wrong-row update or a new invalid record introduced by another path.

Coordinate concurrent writers and application behavior during the migration. Once the NOT VALID check is installed, subsequent inserts and updates must satisfy it, but the application still needs a useful error response and a defined NULL policy. If repair changes historical business facts, preserve appropriate audit and authorization. Plan rollback or forward repair before applying any real data change.

Validate and interpret the result

VALIDATE CONSTRAINT checks existing rows against the installed rule. If an old violation remains, validation fails and the constraint remains unvalidated; if all relevant rows satisfy it, validation can mark it validated. Verify the catalog state and recheck the business data after the operation. A valid constraint proves the declared predicate for the checked data under PostgreSQL’s rules, not that every quantity came from a correct external source.

Locking and scan costs depend on the exact ALTER TABLE operation and workload. NOT VALID can reduce the immediate validation work but does not mean zero locking or zero impact. Review PostgreSQL 18 documentation, long transactions, resource limits and stop criteria, and rehearse the exact sequence in an isolated environment. Do not turn a training example into production DDL.

Test the migration as a sequence

Build a test matrix: identify id 2 with -1, install the NOT VALID check, show that a new -2 is rejected, repair the historical row under the approved rule, validate and inspect the state. Include zero, a positive quantity and NULL according to the intended contract. Verify that a failed insert is rolled back and that no unrelated row changed.

Record the original fixture, constraint name, PostgreSQL version, error SQLSTATE, affected keys and final validation evidence. Test application behavior while old and new rows coexist, then verify the final invariant. A successful validation message is one part of evidence; compare exact business values and downstream results as well. Keep the source and recovery plan protected and do not access production or learner data for these examples.

20 worked examples

1. Find existing violation

Problem: Which fixture row violates quantity >= 0?

  1. Row two has a negative quantity.
  2. Rows one and three are nonnegative.
sql Illustrative PostgreSQL 18 constraint rehearsal
SELECT id,quantity FROM (VALUES (1,2),(2,-1),(3,0)) AS x(id,quantity) WHERE quantity<0 ORDER BY id;
Expected output
2 | -1

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Identify exact keys before an approved repair.

2. Install not valid

Problem: What happens to the existing negative row at installation?

  1. NOT VALID skips the immediate scan of historical rows.
  2. The invalid row is not automatically repaired or deleted.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- Fixture includes id 2, quantity -1.
ALTER TABLE lab_stock ADD CONSTRAINT quantity_nonnegative CHECK (quantity>=0) NOT VALID;
Expected output
The existing row remains; the constraint is initially unvalidated.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Installation and validation are separate migration stages.

3. New invalid insert

Problem: Can a new negative row be inserted after installation?

  1. The candidate quantity is negative.
  2. The installed constraint applies to subsequent inserts.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- quantity_nonnegative CHECK (quantity>=0) NOT VALID is installed.
INSERT INTO lab_stock (id,quantity) VALUES (4,-2);
Expected output
The insert violates the CHECK.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: NOT VALID does not disable enforcement for new writes.

4. New valid insert

Problem: Can a new positive row satisfy the installed check?

  1. Three makes quantity>=0 true.
  2. The candidate satisfies the stated CHECK, although other table constraints still apply to the insert.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- quantity_nonnegative CHECK (quantity>=0) NOT VALID is installed.
INSERT INTO lab_stock (id,quantity) VALUES (4,3);
Expected output
The insert can succeed, subject to other constraints.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: The unvalidated historical state does not reject all new writes.

5. Zero boundary

Problem: Does the rule include zero quantity?

  1. Zero equals the lower boundary.
  2. The greater-than-or-equal comparison includes equality, so the zero boundary makes the predicate true.
sql Illustrative PostgreSQL 18 constraint rehearsal
SELECT 0>=0 AS passes;
Expected output
true

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: A boundary test confirms the intended inclusive rule.

6. NULL policy

Problem: Does CHECK quantity>=0 alone reject NULL?

  1. The comparison with NULL is unknown.
  2. PostgreSQL CHECK is satisfied when the expression is true or null.
sql Illustrative PostgreSQL 18 constraint rehearsal
SELECT NULL::integer>=0 AS predicate;
Expected output
NULL (unknown); CHECK alone permits it.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Use a separate requiredness rule when NULL is forbidden.

7. Update old invalid row

Problem: Can an unrelated update leave an old invalid row unchanged?

  1. The resulting row still has quantity negative one.
  2. Subsequent updates must satisfy the installed rule.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- id 2 still has quantity=-1 after NOT VALID installation.
UPDATE lab_stock SET label='box' WHERE id=2;
Expected output
The update is rejected by the installed CHECK.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: NOT VALID does not exempt later writes to an old invalid row.

8. Repair one row

Problem: What does the approved repair change?

  1. The guard identifies the expected historical value.
  2. The returned row records the corrected quantity.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- Approved mapping: id 2 quantity -1 -> 1.
UPDATE lab_stock SET quantity=1 WHERE id=2 AND quantity=-1 RETURNING id,quantity;
Expected output
2 | 1

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: A keyed repair limits scope and supplies verification evidence.

9. Wrong repair key

Problem: Why is one affected row not enough?

  1. The count can still be one.
  2. The targeted identifier differs from the approved key.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- Intended repair: id 2.
-- Mistaken UPDATE targets id 3 and changes one row.
Expected output
The wrong business record can change.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Verify affected identifiers and values, not only the count.

10. Validate with violation

Problem: What happens if id 2 remains negative at validation?

  1. The existing row makes the check false.
  2. VALIDATE checks historical rows against the installed rule.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- id 2 quantity=-1 remains.
ALTER TABLE lab_stock VALIDATE CONSTRAINT quantity_nonnegative;
Expected output
Validation fails while the violating row remains.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Repair the historical violation before expecting the constraint validation to succeed.

11. Validate after repair

Problem: What can happen after every existing row satisfies the rule?

  1. The stated existing rows satisfy the CHECK under its NULL rules.
  2. The validation scan finds no violation in the scenario.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- All existing quantities are nonnegative or allowed NULL.
ALTER TABLE lab_stock VALIDATE CONSTRAINT quantity_nonnegative;
Expected output
Validation can succeed and mark the constraint validated.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: The final state must still be verified in the catalog.

12. Catalog state

Problem: How can a named constraint’s validation state be inspected?

  1. pg_constraint stores metadata for constraints.
  2. The predicate targets the intended table and constraint name.
sql Illustrative PostgreSQL 18 constraint rehearsal
SELECT conname,convalidated FROM pg_constraint WHERE conrelid='lab_stock'::regclass AND conname='quantity_nonnegative';
Expected output
Inspect convalidated for the named constraint.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Check actual state rather than inferring it from a migration script.

13. Failed insert cleanup

Problem: What follows a rejected insert inside a transaction?

  1. The insert violates the installed CHECK.
  2. ROLLBACK ends the failed transaction in the rehearsal.
sql Illustrative PostgreSQL 18 constraint rehearsal
BEGIN;
INSERT INTO lab_stock (id,quantity) VALUES (4,-2);
ROLLBACK;
Expected output
The owner rolls back the failed transaction.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Error testing needs an explicit cleanup boundary.

14. Positive existing rows

Problem: Which rows satisfy the nonnegative rule?

  1. Two and zero are both at least zero.
  2. Negative one fails the comparison.
sql Illustrative PostgreSQL 18 constraint rehearsal
SELECT id FROM (VALUES (1,2),(2,-1),(3,0)) AS x(id,quantity) WHERE quantity>=0 ORDER BY id;
Expected output
1; 3

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: The rule includes its zero boundary.

15. Concurrent invalid write

Problem: Can a new -2 slip in merely because old rows remain unvalidated?

  1. NOT VALID skips initial historical validation.
  2. It does not disable the installed check for subsequent writes.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- CHECK quantity>=0 NOT VALID is installed.
-- Another session attempts INSERT quantity=-2.
Expected output
No; the new write is checked and rejected.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Staging protects new data while historical repair proceeds.

16. Approved correction

Problem: Why not replace every negative quantity with zero automatically?

  1. The negative value may represent different business meanings.
  2. The correct replacement is not established by the constraint alone.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- id 2 has -1.
-- Business source indicates correct value may be 1 or a reversal event.
-- No approved mapping exists.
Expected output
Do not apply an arbitrary repair before approval.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Data repair must preserve the intended business facts.

17. Locking impact

Problem: Does NOT VALID mean the ALTER has zero locking impact?

  1. Skipping historical validation reduces one part of the immediate work.
  2. The ALTER still has operational locking and resource behavior.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- ALTER TABLE ADD CHECK ... NOT VALID is planned.
-- Concurrent workload and long transactions exist.
Expected output
No; inspect documented locks and rehearse the actual workload.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: A staged constraint is not an impact-free operation.

18. Unrelated row

Problem: How can a repair avoid changing a valid row?

  1. The predicate names id two and its expected old value.
  2. Id three does not satisfy that key condition.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- id 2 is -1; id 3 is 0.
UPDATE lab_stock SET quantity=1 WHERE id=2 AND quantity=-1 RETURNING id;
Expected output
Only id 2 matches the stated guard.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: A guarded key predicate limits the intended repair scope.

19. Validation is not truth

Problem: Does a validated CHECK prove an external shipment quantity is correct?

  1. The stored quantity is nonnegative.
  2. The constraint does not compare it with an external shipment record.
sql Illustrative PostgreSQL 18 constraint rehearsal
-- CHECK quantity>=0 is validated.
-- Row id 2 stores quantity=1, but external source is not checked.
Expected output
No; it proves only the declared predicate under its rules.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: Database invariants and business-source accuracy require separate evidence.

20. Complete rehearsal

Problem: What evidence supports the staged migration?

  1. New writes were tested while historical data was repaired.
  2. Final validation and catalog state were verified.
sql Illustrative PostgreSQL 18 constraint rehearsal
Found id 2=-1; installed NOT VALID; new -2 rejected.
Approved repair changes id 2 to 1.
VALIDATE succeeds; catalog state checked.
Expected output
The stated sequence supports the rule for this fixture.

The fixture and outcome are reasoned; no PostgreSQL constraint was changed.

Why this result follows: A staged migration needs evidence at each boundary.

Common errors and fixes

Validation fails after adding the constraint.

Why it happens
Existing rows still violate the check.
How to fix it
Identify and repair violations according to the approved data rule, then validate again.
Prevent it next time
Profile representative legacy data before the rollout.

A new invalid write fails unexpectedly.

Why it happens
NOT VALID was mistaken for disabled enforcement on new writes.
How to fix it
Correct the writer or data to satisfy the installed constraint.
Prevent it next time
Test old and new application versions before adding the rule.

A migration waits on a lock.

Why it happens
Staged validation was assumed to require no locks.
How to fix it
Inspect authorized lock evidence and follow the stopping procedure.
Prevent it next time
Rehearse with representative transactions and documented lock requirements.

20 practice exercises

1. Which fixture row violates the new rule before the repair?

Show hint

Evaluate quantity >= 0 for each fixture row.

Show worked solution

Row id 2 has quantity -1, so it violates quantity >= 0. It remains after the NOT VALID addition until the training repair changes it.

Row id 2 has quantity -1, so it violates the proposed check. Row 3 at zero satisfies the inclusive boundary; record the exact invalid key before an approved repair.

Check your solution

  • Id 2 is invalid.
  • Zero satisfies the rule.

2. Can a new row with quantity -2 be inserted after the NOT VALID check is installed?

Show hint

NOT VALID defers historical validation, not new-write checks.

Show worked solution

No. The check applies to subsequent inserts and updates even before existing rows are validated. Test the rejected insert inside a rolled-back disposable transaction.

A new row with quantity -2 is rejected after the check is installed, even while old rows remain unvalidated. Test the rejection in a disposable transaction and roll it back.

Check your solution

  • The new -2 insert is rejected.
  • Rollback clears the test transaction.

3. Fixture rows are (1,2), (2,-1), (3,0). Which violates quantity>=0?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

Row id 2 violates the rule because -1 is negative. Rows 1 and 3 satisfy it, including the zero boundary; record the exact key before an approved repair.

Check your solution

  • Id 2 is invalid
  • Zero satisfies the inclusive rule

4. A CHECK quantity>=0 is added NOT VALID while id 2 remains -1. Is the old row automatically repaired?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

No. NOT VALID skips the immediate validation scan and does not change the historical row. Identify and repair it under an approved business policy before validating.

Check your solution

  • Old row remains -1
  • Constraint is initially unvalidated

5. After that NOT VALID check is installed, can a new row with quantity -2 be inserted?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

No. The installed check applies to subsequent inserts and updates, so the new negative row is rejected. Test this in a disposable transaction and roll it back after the expected error.

Check your solution

  • New -2 is rejected
  • NOT VALID does not disable new-write enforcement

6. Does the PostgreSQL CHECK quantity>=0 alone reject a NULL quantity?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

No. The comparison is unknown for NULL, and PostgreSQL CHECK accepts true or unknown. Add a separate NOT NULL requirement when the business rule requires a quantity.

Check your solution

  • CHECK alone permits NULL
  • Requiredness is separate

7. Does a zero quantity satisfy the proposed nonnegative PostgreSQL CHECK rule?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

Yes. The greater-than-or-equal rule includes zero. Test zero explicitly along with a positive value, a negative value and NULL under the intended requiredness contract.

Check your solution

  • Zero passes
  • The boundary is inclusive

8. An old row still has -1 after NOT VALID installation. Can an unrelated update preserve that invalid quantity?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

The resulting updated row must satisfy the installed check, so an update that leaves -1 is rejected. Repair the quantity under an approved rule before or as part of a valid update.

Check your solution

  • Subsequent update is checked
  • Negative resulting quantity is rejected

9. Why should a repair record exact keys rather than only the count of invalid rows?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

A count does not identify which business records changed and can hide a wrong-row repair. Preserve the approved key and before-and-after value, then verify the returned identifiers.

Check your solution

  • Record exact invalid keys
  • Verify affected values

10. An approved repair changes id 2 from -1 to 1. What guard helps protect scope?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

Use a predicate matching id 2 and the expected old quantity -1, then inspect RETURNING id and quantity. A zero-row result requires investigation rather than silently assuming repair succeeded.

Check your solution

  • Match the intended key and old value
  • Verify returned key and quantity

11. Why not replace every negative quantity with zero just to pass validation?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

The negative value may represent a correction, error or other business event, and the right replacement is not established by the constraint. Obtain an approved mapping and preserve audit evidence.

Check your solution

  • Do not invent the replacement
  • Use a business-approved repair

12. What happens if VALIDATE CONSTRAINT runs while an old -1 remains?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

Validation fails because the existing row violates the check. The constraint remains unvalidated; repair or otherwise resolve the row under the approved process before retrying validation.

Check your solution

  • Validation fails
  • The old violation remains to resolve

13. What can VALIDATE CONSTRAINT do after all historical rows satisfy the rule?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

It can scan the existing data and mark the constraint validated if no violation is found. Inspect the catalog state and verify business values rather than relying only on a command message.

Check your solution

  • Validation can succeed
  • Check the final validation state

14. Does a validated nonnegative CHECK prove an external shipment quantity is correct?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

No. It establishes the declared nonnegative predicate under PostgreSQL’s rules, not agreement with an external source. Reconcile the actual business record separately.

Check your solution

  • The predicate is enforced
  • External accuracy needs independent evidence

15. Does NOT VALID mean ALTER TABLE has no locking or resource impact?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

No. It avoids the immediate historical validation scan but the operation still has documented locking and workload effects. Rehearse the exact PostgreSQL version and concurrent workload with stop criteria.

Check your solution

  • Historical scan is deferred
  • Locking and resources still matter

16. A rejected negative INSERT occurs inside BEGIN. What is needed before normal continuation?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

The transaction is failed and requires rollback according to PostgreSQL’s transaction behavior. Test the rejection in a disposable transaction and verify no partial publication remains.

Check your solution

  • The insert is rejected
  • Rollback clears the failed transaction

17. Why test a new invalid write while historical violations remain?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

It demonstrates that the installed NOT VALID check protects subsequent writes even before old rows are validated. Verify the expected error and rollback without altering production data.

Check your solution

  • New invalid write is rejected
  • Historical row can remain pending repair

18. A concurrent writer tries to insert -2 after the check is installed. Can it bypass the rule because validation is pending?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

No. The installed check applies to subsequent inserts and updates. Coordinate application error handling and retry behavior, but do not treat NOT VALID as disabled enforcement.

Check your solution

  • New write is checked
  • Validation state does not disable enforcement

19. After repair, a query finds no negative rows. What additional migration evidence is needed?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

Run the authorized VALIDATE step, inspect the named constraint’s validation state and compare corrected business values with the approved source. A negative-row count alone does not prove catalog state or source accuracy.

Check your solution

  • Verify constraint validation state
  • Reconcile corrected values

20. What should a staged constraint rehearsal record for review?

Show hint

Trace historical rows, new-write enforcement, repair scope and validation state.

Show worked solution

Record version, original invalid keys, constraint definition, new-write rejection, approved repair, validation result and final keyed values. Preserve a recovery plan and avoid sensitive data in shared evidence.

Check your solution

  • Record installation and rejection
  • Record repair and final validation

Key takeaways

  • NOT VALID stages validation of existing rows while enforcing the rule for subsequent writes.
  • Repair data under an approved business rule before validation.
  • Rehearse locks, compatibility, and recovery.
  • NOT VALID defers historical validation but enforces subsequent writes.
  • CHECK unknown and NOT NULL represent different rules.
  • Validate and inspect the named constraint after an approved repair.

Official references