Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Supabase 6 min read How-to mediumAdept

Type Constraints in 65 lines of SQL

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.

Related posts

  • Semantic Versioning UDF in ClickHouse

    ClickHouse mediumAdept

    A ClickHouse SQL UDF sorts app version strings by semantic version rather than lexicographically, so that 2.1.2 sorts before 2.1.10. It extracts the dotted numeric portion with a regex, splits it on periods, and maps each part to UInt32 with toUInt32OrZero, producing integer arrays that ClickHouse compares element-wise. Known limitations are that pre-release suffixes like -prod or -stg are dropped, so those versions tie, and that oddly shaped strings such as hash-based snapshots parse incorrectly. Embrace reports this is good enough in practice because customers tend to stick to one versioning scheme.

  • Enabling static analysis of SQL queries at Meta

    Meta Engineering mediumAdept

    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. It acts as a pluggable SQL front end exposing one Presto-superset dialect across Presto, Spark and the in-house XStream, either feeding the tree directly to engines or rendering it back to a target dialect. User-defined semantic column types (e.g. TimestampMilliseconds, UserID) let the typechecker catch unit and ID-mismatch errors in UNIONs and JOINs before execution. A lineage tool consumes semantic trees from all recurring queries to build a warehouse-wide column-level lineage graph.

  • pg_jsonschema: JSON Schema support for Postgres

    Supabase mediumAdept

    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`. Used inside a check constraint, it keeps the flexibility of document columns, such as storing raw webhook payloads instead of normalizing them into many joined tables, while still enforcing type, required-field and no-extra-properties rules. In the example, an unconstrained `json` column handled only 2 of 8 test inputs correctly, and the schema-backed check constraint handled all 8. It is available on Supabase via `create extension pg_jsonschema` or through Docker Compose.

  • What To Know About Postgres if You’re Coming From SQL Server

    Neon mediumAdept

    Postgres and SQL Server share SQL but differ in dialect details that trip up migrating developers. Identifiers are quoted with double quotes instead of square brackets. GETDATE/DATEADD/DATEDIFF become CURRENT_TIMESTAMP, interval arithmetic and date subtraction, and TOP becomes LIMIT/OFFSET. Strings concatenate with || rather than +, CROSS APPLY becomes CROSS JOIN LATERAL, and string comparison is case-sensitive by default. Beyond syntax, Postgres is community-driven, so features like UUID generation or trigram search arrive as extensions, and tooling and hosting choices are open rather than vendor-defined.

  • Dynamic Table Partitioning in Postgres

    Supabase mediumAdept

    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. The walkthrough builds a simulated chat app with pl/pgSQL functions that create partitions dynamically. It also covers why large tables hurt (btree depth, vacuum, archiving), the requirement that the partition key be part of composite primary and foreign keys, and ORM support gaps such as Django's lack of composite keys.

The quest for software engineering knowledge.

Look:
Layout: