Supabaseoli_riceTutorial
Type Constraints in 65 lines of SQL
Summary
PostgreSQL’s extensible type system can encode validation and presentation behavior for a custom Semantic Versioning (SemVer) type. The article stores major, minor, patch, pre-release, and build-metadata components in a composite type, then wraps that type in a domain whose check constraint rejects null or negative numeric components and invalid array elements. A SQL helper validates nullable arrays with regular expressions, and the domain automatically enforces these rules when values are inserted into a table. For ergonomics, a function formats the composite value as a SemVer string, while a stored generated column persists that representation; the article also notes possible parsers, operators, and aggregation functions. It concludes that the ceremony is worthwhile when data integrity is critical and the specification is stable or changes infrequently.
Context
The initial composite type accepts invalid SemVer components, including null required fields, negative numeric values, disallowed pre-release characters, and empty build-metadata elements. The work is motivated by making invalid states unrepresentable while retaining flexible and efficient querying for a critical application type.
Approach / What changed
The implementation stores SemVer components in a PostgreSQL composite type, defines a semver domain over it, and applies a check constraint for numeric and array-element validation. A SQL helper checks nullable arrays against regular expressions. A formatting function produces conventional SemVer text, and a stored generated column persists that representation. The article also describes possible parsing functions, equality operators, and a max aggregation.
Takeaways
A PostgreSQL domain can layer check constraints over a composite type, and its validation is applied automatically when the domain is used in table columns or inserted values.
The semver_elements_match_regex helper validates nullable text arrays by checking elements with bool_and and requiring a non-empty array when the input is not null.
A stored generated column can persist semver_to_text(package_semver), providing a conventional string representation alongside the structured value without a query or filter penalty.
Source and freshness
- Source
- supabase.com
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.