pg_pureu
pg_pure · Untrusted Pure procedural language with scalar functions and PostgreSQL SPI access.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 102300 | pg_pureu | pg_pure | 0.1 | LANG | PostgreSQL | C |
PostgreSQL · 17, 18, 19
Usage
Sources:
pg_pureu executes untrusted Pure scalar functions inside PostgreSQL and exposes SPI database access. It belongs to distribution pg_pure 0.12.1, with SQL version 0.1. Only superusers may create functions in this language: Pure can call arbitrary native functions through extern.
Core Workflow
The extension is non-relocatable. It requires PostgreSQL 17–19, the embeddable libpure fork and its prelude. numeric values use exact Pure big integers or rationals; a fractional result is rounded to PostgreSQL’s numeric display scale on return. Machine integer arithmetic can wrap before the final range-checked conversion, so use big integers when intermediate values can grow.
Objects and Database Access
The language installs call, inline and validation handlers. Scalar SQL types include smallint/integer/bigint, floating point, numeric, boolean and text. SQL NULL is represented by sql::null; a text result must be a Pure string.
sql::query and sql::exec access the database; sql::notice and sql::warning report messages. A database error becomes sql::error carrying SQLSTATE and message, and an uncaught exception becomes a SQL error. Distribution 0.6.0 supports dense one- and two-dimensional float8[] arrays as Pure double matrices; NULL elements and higher dimensions are rejected. Parameterized SPI and set-returning functions are implemented in 0.6.0; DML triggers are supported from 0.11.0. Do not interpolate untrusted values into SQL strings.
Configuration and Failure Boundaries
The shared library is pg_pure, including for this sibling. Optional shared_preload_libraries loading needs a restart, but postmaster warmup does not remove the first-call initialization cost in forked backends; connection pooling avoids repeated initialization. pg_pure.using defaults to math. Query cancellation and stack-overflow hooks are implemented; global definitions made with eval are not transactional and survive rollback.
The embeddable libpure fork at revision ab12de13 or later hides its LLVM symbols and supports normal PostgreSQL JIT. The old JIT incompatibility guidance no longer applies. The trusted pg_pure sibling now executes in a separate safe-mode interpreter; this does not restrict the native-code powers of the untrusted language. The compiled gsl::linalg::cholesky_solve solver in 0.6.0 needs the matching Pure module. Untrusted bodies may import installed modules with using; trusted bodies rely on the DBA’s allowlist. Deploy matching libraries and replace sessions that loaded an older version; SQL version 0.1 alone does not identify the distribution release.
Distribution 0.7.0
bytea maps to a Pure integer row matrix, preserving arbitrary binary data including NUL bytes; elements outside 0..255 raise an error. Domain arguments and results use their base types while enforcing domain constraints. The preceding 0.6.1 patch fixes numeric precision, sign and scale conversion. Version 0.6.2 rejects trusted bodies and DO blocks that escape their single-expression wrapper and leak rules into another function in the same backend. Deploy the corrected library and replace old sessions; SQL version remains 0.1.
Distribution 0.8.0
Distribution 0.8.0 retains SQL version 0.1 and adds tested mpfr, dict and rational modules. In the untrusted language a body may use using mpfr;; in the trusted language the DBA must allow the module through pg_pure.using. Modules still require the matching upstream library installation. The release does not promote csv, xml, stllib or glpk to the tested set. Library replacement and session recycling remain necessary because the unchanged SQL version does not identify the loaded implementation.
Distribution 0.9.0
SQL version remains 0.1. Parameterized sql::query and sql::exec now transparently cache up to 128 plans per backend, keyed by SQL text and inferred parameter types. PostgreSQL revalidates plans when referenced objects, search path or role change. The API is unchanged; replace loaded libraries through the existing session/restart procedure.
Call-Scoped Streaming in 0.10.0
sql::stream returns a lazy Pure list backed by an SPI cursor, fetching rows in batches as consumed. It accepts a query or a query with parameters. The stream is valid only inside the function call that opened it; forcing it after the call returns raises sql::error. It is not a holdable cursor for later SQL statements. The SQL extension version remains 0.1.
DML Triggers and Distribution 0.12.0
Distribution 0.12.0 keeps SQL version 0.1. Since 0.11.0, functions returning trigger support row and statement DML triggers. Bodies receive new, old and tg; absent rows use sql::null. A BEFORE-row trigger returns a record to replace named columns, or sql::null to skip the operation. AFTER and statement triggers ignore the return value. Event triggers and procedure transaction control remain unsupported.
Version 0.12.0 fixes a use-after-free while reporting result-conversion errors. Install the matching library and recycle sessions that loaded the old implementation; the unchanged SQL version alone cannot identify the installed distribution.
Distribution 0.12.1
The Nix flake now builds and runs both language regression suites for PostgreSQL 17, 18 and 19 with server JIT enabled. This is build support, not a claim of package availability in every repository. Set the server’s PURELIB to the installed module directory for modules such as gsl::linalg; the prelude alone does not need it. The flake does not run the TAP suite. SQL version remains 0.1.