pg_js
alohadb · AlohaDB JavaScript procedural language and function runtime
Overview
PostgreSQL · 18
Usage
Sources:
pg_js 1.0 adds the JavaScript procedural language js to AlohaDB 21.10.0, based on PostgreSQL 18. This preview is documented for that distribution; compatibility with stock PostgreSQL is not established.
Enable and Execute
A superuser installs the extension. The language itself is trusted, so roles with its USAGE privilege may create functions. No preload or restart is required for ordinary SQL calls.
Function bodies may use async JavaScript or an ES module default export. Set-returning functions return an iterable. Triggers receive NEW, OLD and TG. Promises, timers and microtasks finish before the SQL call returns. Loading external modules is unavailable.
SQL, Secrets and HTTP
db.query(text, params) and the tagged SQL template execute parameterized SQL with the caller’s privileges and RLS. Each statement uses a subtransaction; SQL failures become catchable exceptions. STABLE functions cannot write. Numeric values can lose precision when converted to JavaScript numbers; return strings where exact numeric output matters.
env.get(name) reads pg_js_env with the function owner’s privileges. Administration uses pg_js_env_set, pg_js_env_set_vault and pg_js_env_unset; the table and mutators revoke public access. The vault option requires the separate vault facility.
fetch() needs libcurl and is disabled until a superuser sets pg_js.fetch_allowed_hosts. Redirects are checked against the allowlist. pg_js.fetch_timeout defaults to 10 seconds and pg_js.fetch_max_bytes to 10 MB; bodies are buffered and requests run sequentially underneath. Serving functions over HTTP additionally requires the separate REST API setup.
Resource and Isolation Boundaries
Each function and calling role has a cached context within a backend. Module globals can survive calls by different application users sharing that role. Avoid storing user-specific secrets in globals; pg_js.context_reuse = off creates fresh contexts.
pg_js.max_contexts defaults to 32 per backend, pg_js.memory_limit to 64 MB per context, and pg_js.time_limit to 10 seconds, bounded by pg_js.max_time_limit. Query cancellation and statement timeout also interrupt scripts.
Reset affects the current backend’s cached contexts. Preserve matching extension binaries and SQL, back up the secret table securely, and rehearse upgrades of functions that depend on engine behavior.