Chapter 15 · Semi-Structured and Polymorphic Data
Schema-on-Write Versus Flexible Attributes
Compare schema-on-write with flexible attribute models, understand validation ownership, discoverability, evolution, and when flexibility helps or harms maintainability.
Learning outcomes
Schema-on-write validates structure before or during persistence. Flexible attribute designs delay some structural decisions and allow records of the same broad type to carry different attributes. Neither approach is universally superior. The right choice depends on stability, governance, queryability, and the cost of invalid data.
Compare strict and flexible schema strategies.
Understand where validation ownership moves when schemas become flexible.
Design metadata-driven attributes without losing discoverability.
Choose flexibility based on domain volatility rather than fashion.
Schema-on-write
Part( part_id BIGINT PRIMARY KEY, sku TEXT NOT NULL UNIQUE, description TEXT NOT NULL, manufacturer_id BIGINT NOT NULL REFERENCES Manufacturer)Data must satisfy the declared structure before it becomes persistent source-of-truth state.
Benefits of strict schemas
- clear domains;
- predictable queries;
- declarative constraints;
- discoverable metadata;
- stable application contracts;
- better optimizer statistics.
Cost of strict schemas
Adding new fields may require:
- migration;
- application changes;
- backfill/default strategy;
- compatibility planning.
Flexible attributes
One pattern:
Part( part_id, sku, description, attributes_json)Different Part families can store different optional properties.
Flexibility moves responsibility
When the table no longer declares every field, someone else must define:
- allowed names;
- types;
- required/optional status;
- units;
- validation;
- display rules.
It often means the schema has moved from DDL into metadata or application code.
Metadata-driven attribute definitions
AttributeDefinition( attribute_code, display_name, data_type, unit_code, required_flag)This can make flexible attributes discoverable and governed.
Per-category attributes
CategoryAttribute( category_id, attribute_code, required_flag, sort_order)Now the system can say pumps require max_pressure while cables require conductor_count.
Where values live
Options include:
- JSON document;
- EAV rows;
- subtype tables;
- wide sparse table;
- separate document store.
Lesson 4 examines EAV in depth.
Discoverability
A database analyst can inspect normal columns easily. Flexible metadata requires documentation and tooling so users know which attributes exist and how to query them.
Query stability
Strict column:
WHERE max_pressure_bar >= 20Flexible JSON:
WHERE (attributes_json->>'max_pressure_bar')::numeric >= 20The latter needs stable path/type assumptions and often specialized indexing.
Unit consistency
Flexible attributes can become dangerous if one record stores:
"pressure": 20 -- bar?and another:
"pressure": 290 -- psi?Attribute metadata must define units or values must carry them explicitly.
Promote maturing attributes
An attribute that starts experimental may later become:
- required for most rows;
- frequently filtered;
- integrity-critical;
- used in joins.
At that point, promote it into a normal column or related entity.
Schema evolution still exists
Flexible storage avoids some DDL migrations but introduces semantic migrations:
"temperature" -> "temperature_c"string -> numericlegacy code -> standardized codeOld documents must remain interpretable.
Validation versions
schema_version = 5lets new validators coexist with historical records.
WorkshopHub strategy
| Field family | Strategy |
|---|---|
| Core WorkOrder identifiers | Strict relational columns |
| Service-type-specific questionnaire | Versioned flexible schema |
| Manufacturer-specific metadata | Flexible attributes initially |
| Common searchable technical rating | Promoted typed column |
Practice: strict or flexible?
Inspection forms
WorkshopHub supports 80 equipment types, each with different inspection questions that change quarterly. Should every answer be a new WorkOrder column?
Review answer
No. A versioned metadata-driven form plus flexible answer payload is more appropriate. However, fields used for hard compliance rules or frequent cross-record analytics may deserve promoted relational columns or dedicated result tables.
Summary and next lesson
Schema-on-write gives strong contracts and queryability; flexible attributes reduce friction when shapes vary. Flexibility shifts rather than removes schema responsibility. The next lesson examines polymorphic associations, another common attempt to make schemas more generic.
References
- Martin Kleppmann, Designing Data-Intensive Applications.
- Bill Karwin, SQL Antipatterns.
- DBMS documentation for JSON and generated columns.