pgrx lets developers build PostgreSQL extensions in Rust with cargo workflows, schema generation, and testing across multiple server versions.
Overview
pgrx is a framework for writing PostgreSQL extensions in Rust with an emphasis on idiomatic and safe patterns. It supports building against several PostgreSQL major releases from one codebase, using Rust feature gates for version-specific APIs and testing across those versions.
The cargo-pgrx companion provides commands to create extensions, register PostgreSQL installs, run and test in psql, package RPM and DEB installs, run SQL regressions, and optionally build migration scripts. Extensions can be implemented largely in Rust with automatic mapping of many Rust types into PostgreSQL and generated SQL schemas, plus hooks to include custom SQL.
Safety features include translating Rust panics into transaction-aborting errors, memory management aligned with Rust drops around PostgreSQL errors, and procedural macros for guards and extern functions. Developers can expose UDFs, triggers, custom types, enums, composites, and use SPI safely.
Advanced bindings translate PostgreSQL C macros into Rust, offer hooks into planner and transaction lifecycle, and expose logging and internals through pgrx modules. Documented targets include x86_64 and aarch64 Linux, macOS, and Windows with clang and LLVM toolchain requirements.
Key features
- cargo-pgrx development workflow
- Multi-version PostgreSQL support
- Automatic SQL schema generation
- Rust types as Postgres types
- pg_extern functions and triggers
- SPI and memory context helpers
- C macro translation for Postgres
- Extension packaging for Linux
Use cases
- Custom PostgreSQL extensions
- Rust UDFs inside Postgres
- Cross-version extension testing
- Safe in-database logic
- Packaged extension distribution