---
title: "Type Constraints in 65 lines of SQL"
description: "A Postgres composite type plus a domain with check constraints can model Semantic Versioning so invalid versions are rejected on insert or cast."
url: "https://swe.quest/articles/type-constraints-in-65-lines-of-sql-c09c12befbef6b57"
canonical: "https://supabase.com/blog/type-constraints-in-65-lines-of-sql"
source: "Supabase"
published: "2023-02-17"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["postgres","sql","type-systems"]
---

# Type Constraints in 65 lines of SQL

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2023-02-17, 6 min read, How-to. Original: [https://supabase.com/blog/type-constraints-in-65-lines-of-sql](https://supabase.com/blog/type-constraints-in-65-lines-of-sql)

## Summary

A Postgres composite type plus a domain with check constraints can model Semantic Versioning so invalid versions are rejected on insert or cast. A semver\_components composite stores major, minor, patch, pre-release and build metadata as queryable fields, and a semver domain adds non-null and non-negative checks and regex validation of array elements through a helper SQL function. An explicit semver\_to\_text function, optionally persisted as a stored generated column, gives the familiar 2.0.0-rc.1 rendering. The approach suits types whose integrity is critical and whose spec is stable, and it can be extended with custom equality operators, parsers and aggregates.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres check constraints ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, Semantic Versioning, Postgres domains, composite types, generated columns

## Related posts

- [Semantic Versioning UDF in ClickHouse](https://swe.quest/articles/semantic-versioning-udf-in-clickhouse-c402b0d64cb4b011.md): ClickHouse · 2024-10-09 · Medium · 6 min read · SQL, Analytics & OLAP
  A ClickHouse SQL UDF sorts app version strings by semantic version rather than lexicographically, so that 2.1.2 sorts before 2.1.10.
- [Enabling static analysis of SQL queries at Meta](https://swe.quest/articles/enabling-static-analysis-of-sql-queries-at-meta-a9210cb14aebcf3f.md): Meta Engineering · 2022-11-30 · Medium · 6 min read · Static analysis, SQL, Type systems, Data pipelines, Query engines
  UPM (Unified Programming Model) is Meta's standalone library that parses SQL into a semantic tree, a typed intermediate representation decoupled from any single query engine, so infrastructure teams can build linters, query rewriters and lineage tools without reimplementing analysis per dialect.
- [pg\_jsonschema: JSON Schema support for Postgres](https://swe.quest/articles/pg-jsonschema-json-schema-support-for-postgres-489ffe5b7545ddca.md): Supabase · 2022-08-19 · Medium · 4 min read · PostgreSQL, Databases
  pg\_jsonschema is a Postgres extension that validates \`json\` and \`jsonb\` values against a JSON Schema through two functions, \`json\_matches\_schema\` and \`jsonb\_matches\_schema\`.
- [What To Know About Postgres if You’re Coming From SQL Server](https://swe.quest/articles/what-to-know-about-postgres-if-you-re-coming-from-sql-server-2dbf370eb4f4c29d.md): Neon · 2024-12-10 · Medium · 9 min read · PostgreSQL, SQL, Databases
  Postgres and SQL Server share SQL but differ in dialect details that trip up migrating developers.
- [Dynamic Table Partitioning in Postgres](https://swe.quest/articles/dynamic-table-partitioning-in-postgres-13de90b4fe06270e.md): Supabase · 2023-10-03 · Medium · 20 min read · PostgreSQL, Schema migrations, SQL, Scaling
  Postgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
