
PostgreSQL has a rich and extensible type system. Beyond enums and composite types, we can:
- apply data validation rules
- override comparison operators like
=
/+
/-
- create custom aggregations
- define casting rules between types
With a little effort, a user-defined type can feel indistinguishable from a built-in. In this article we focus on validation and ergonomics while quickly touching on a few other concepts.
To illustrate, we’ll create an semver
data type to represent Semantic Versioning values. We’ll then add validation rules to make invalid states unrepresentable.
SemVer
A (very) loose primer on SemVer:
SemVer is a specification for representing software versions that communicate information about backwards compatibility. The type is typically represented as a string with 5 components.
Where pre-release
and metadata
are optional.
The intent of each component is outside the scope of this article but, as an example, incrementing the major version number notifies users that the release includes at least one backwards incompatible change.
For a concise representation of the full spec, check out the grammar.
SQL
For our purposes, we’ll assume that the SemVer type is a critical component of the application that needs to be queried flexibly and efficiently.
Storing Components
To that end, we’ll store each component of the version as a separate field on a composite type.
We can create an instance of this type in SQL by casting a tuple as the semver_components
type.
Unfortunately, our definition is far too permissive.
Our data type has no problem accepting invalid components. To list a few of the SemVer rules we violated:
- Major version must not be null
- Minor version must be ≥ 0
- Patch version must not be null
- Pre-release elements must only include characters [A-z0-9]
- Build metadata elements may not be empty strings
We need to add some validation rules to meet our “make invalid states unrepresentable” goal.
Validation
Domains are Postgres’ solution for optionally layering constraints over a data type. Domains are to types what check constraints are to tables. If you’re not familiar with check constraints, you can think of them as equivalent to zod/pydantic in javascript/python.
Let's codify some SemVer rules, layer them on the semver_components
type, and give the new domain a friendly name.
which references a helper function:
Now, if we repeat our positive and negative test cases using the semver
type (vs semver_components
) we still accept valid states:
while invalid states are rejected with an error:
Testing
Our validation doesn’t have to be called manually. The semver
domain can be used anywhere you’d use the semver_components
type and the validations are automatically applied.
Good stuff!
We’re 48 lines of SQL in and have solved for making invalid states unrepresentable. Now lets think about ergonomics.
Displaying
Now that our data type is well constrained, you might notice that selecting values from a semver
typed column returns a tuple, rather than the SemVer string we’re used to seeing.
For example: (2,0,0,"{rc,1}",)
vs 2.0.0-rc.1
We could work around that problem with some custom casts, but I’d recommend keeping everything explicit with a function call.
Which allows us to query the package_version
table and retrieve a string representation of the data.
Or, better yet, use a generated column
so the text representation is persisted along with the semver
type and incurs no query/filter penalty.
Other Tricks
Postgres provides all the tools you could want to make your data types/domains work with SQL as seamlessly as builtins.
For example, you could:
- add convenience functions to parse a semver type from text
- override the equality operator (
=
) to correctly reflect that versions differing only in build metadata are considered equal - add a
max
function to efficiently query for the newest version of each package from within the database
to name a few.
Aligning the right parts of your business’ logic with the database can dramatically improve throughput, decrease IO, and simplify application code.
Conclusion
Admittedly, building performant and ergonomic custom data types in Postgres involves a lot of ceremony.
That said, in cases where:
- the type’s data integrity is critical
- the type is well specified
- the type’s spec does not change (or changes infrequently)
Teaching Postgres to have first class support for your custom type can be transformative for data integrity and performance.