$ show PostgREST/postgrest
PostgREST is a production-grade Haskell web server that converts an existing PostgreSQL schema into a RESTful HTTP API. At startup it introspects PostgreSQL system catalogs to build a schema cache of tables, views, relationships, routines, and media handlers. Each request is parsed into an internal AST (ApiRequest), planned against the schema cache (Plan), compiled to parameterized SQL (QueryBuilder), and executed through the vendored Hasql PostgreSQL driver. The server supports full CRUD, RPC calls, resource embedding via foreign keys, filtering, ordering, pagination, JWT authentication, OpenAPI generation, and custom PostgreSQL-defined media types. Authorization is delegated to the database: PostgREST connects as an authenticator role that does SET ROLE to the JWT-specified role, so row-level security, grants, and triggers are enforced by PostgreSQL. The system runs as a single Haskell binary with lifecycle management, SIGUSR2/config reload through LISTEN/NOTIFY, a separate admin server exposing /live, /ready, /metrics, and schema cache dumps, and metrics via Prometheus. Build and packaging is Nix-based with static linking, Docker images, and cabal/stack support. CI runs integration suites against multiple PostgreSQL versions and includes load, memory, observability, and replica tests. Maintenance is active, with frequent releases and broad production use. The codebase demonstrates a clean separation between HTTP parsing, query planning, SQL generation, and database access. It is one of the few Haskell projects at this scale with deep PostgreSQL binary-protocol integration. The vendored Hasql driver and several standalone modulesβmedia type parsing, HTTP Range handling, SIEVE cache, JSON path parsingβare extractable as reusable components for other Haskell services.
The LLM identified 5 files worth highlighting for reuse:
Generic bounded cache implementing the SIEVE eviction policy. Keeps JWT validation results warm with minimal bookkeeping. The type is polymorphic over key and value, making it extractable into any Haskell project needing a simple, high-hit-rate cache with configurable validation and eviction.
RFC-compliant media type parser and matcher. Handles Accept header parsing, wildcards, and parameter variants. PostgREST uses it to negotiate JSON, CSV, OpenAPI, and custom PostgreSQL domains as response types; the module itself is independent of PostgREST internals and reusable in any HTTP framework.
Parses HTTP Range headers and emits Content-Range headers for offset pagination. Pure functions cover limit/offset calculations, unsatisfiable ranges, and max-rows constraints. Reusable by any REST service needing RFC-compliant pagination without framework-specific support.
A self-contained Parsec-based JSON path parser used to extract user-configurable role claim keys from JWTs (e.g. 'a.b[0].c'). Demonstrates clean parser-combinator design and can be reused to implement small nested-path DSLs for configuration or claim extraction.
Part of the vendored Hasql low-level PostgreSQL client. Implements the PostgreSQL extended query pipeline so multiple statements can be sent in one round-trip. Represents non-trivial binary-protocol engineering and is reusable as a standalone database driver component or as a reference for PostgreSQL protocol implementation.