plruby
plruby : Embed MRI Ruby as an untrusted PostgreSQL procedural language
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3160 | plruby
|
plruby
|
2.5 |
LANG
|
MIT
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d--
|
No
|
Yes
|
No
|
Yes
|
no
|
no
|
| Relationships | |
|---|---|
| Schemas | pg_catalog |
| Need By | hstore_plruby
jsonb_plruby
ltree_plruby
|
| See Also | jsonb_plruby
hstore_plruby
ltree_plruby
plperl
plpython3u
pllua
plv8
plrust
|
| Siblings | jsonb_plruby
hstore_plruby
ltree_plruby
|
Extension control default_version is 2.5 while the project and package version is 2.5.0; PL/Ruby embeds MRI Ruby 3.x, is untrusted and superuser-only, and requires no preload. RPM builds also provide an llvmjit subpackage.
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY
|
2.5 |
18
17
16
15
14
|
plruby |
- |
| RPM | PIGSTY
|
2.5.0 |
18
17
16
15
14
|
plruby_$v |
ruby-libs |
| DEB | PIGSTY
|
2.5.0 |
18
17
16
15
14
|
postgresql-$v-plruby |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
el8.aarch64
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
el9.x86_64
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
el9.aarch64
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0
|
PIGSTY 2.5.0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source
pig build pkg plruby; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install plruby; # install via package name, for the active PG version
pig install plruby -v 18; # install for PG 18
pig install plruby -v 17; # install for PG 17
pig install plruby -v 16; # install for PG 16
pig install plruby -v 15; # install for PG 15
pig install plruby -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION plruby;Usage
Sources:
- PL/Ruby v2.5.0 README
- PL/Ruby language reference
- PL/Ruby cookbook
- PL/Ruby v2.5.0 control file
- PL/Ruby changelog
plruby is the maintained Command Prompt procedural-language extension that embeds Ruby 3 in PostgreSQL. Package release 2.5.0 installs SQL extension version 2.5. It supports scalar and set-returning functions, triggers, event triggers, procedures, anonymous DO blocks, SPI queries, cursors, and prepared plans.
Create a Function
CREATE EXTENSION plruby;
CREATE FUNCTION ruby_add(integer, integer)
RETURNS integer
LANGUAGE plruby
AS $$
args[0] + args[1]
$$;
SELECT ruby_add(2, 3);Arguments are exposed through args; Ruby’s final expression becomes the SQL return value. PostgreSQL scalar, array, composite, and record conversion rules are documented in the language reference.
Set-Returning Functions
Use return_next to emit rows from a set-returning function:
CREATE FUNCTION ruby_series(integer)
RETURNS SETOF integer
LANGUAGE plruby
AS $$
1.upto(args[0]) { |n| return_next(n) }
$$;
SELECT * FROM ruby_series(3);SPI and Database Work
PL/Ruby exposes PostgreSQL’s Server Programming Interface for SQL execution, prepared plans, and cursors. Keep SQL values in parameters rather than interpolating them into command text, and release long-lived cursors or prepared state when the session no longer needs them.
Procedures can use the documented transaction-control surface where PostgreSQL permits COMMIT or ROLLBACK. Functions and triggers remain subject to PostgreSQL’s normal transactional restrictions.
Triggers and Session State
Trigger functions receive trigger metadata through $_TD and return the row action documented by PL/Ruby. Event triggers, anonymous DO blocks, backend-local session data, and shared data are also available. These features run inside the database backend, so an exception, blocking call, or memory leak directly affects that backend.
Version 2.5.0
byteanow maps to a raw, NUL-safe RubyStringwithASCII-8BITencoding instead of PostgreSQL hex text. This is a breaking conversion change: audit functions that parse or construct\x...strings and build bytes explicitly, for example withArray#pack.$_SDadds per-function state that persists across calls in one session and resets when the function is recompiled.$_SHAREDremains session-wide across PL/Ruby functions.spi_colnames,spi_coltypes, andspi_coltypmodsexpose result-column metadata, andltree_plrubyadds the opt-inltreetransform.- After installing the 2.5.0 shared library and SQL files, run
ALTER EXTENSION plruby UPDATEin each database that already has the extension.
Security and Requirements
plrubyis an untrusted language. Ruby 3 provides no safe in-process sandbox, so creating PL/Ruby functions is restricted to superusers and code executes with the PostgreSQL server process’s operating-system authority.- Review all PL/Ruby source as privileged server code. Never allow tenants or ordinary application roles to submit arbitrary Ruby.
- Upstream v2.5.0 supports PostgreSQL 11-18 and Ruby 3.x. Current Pigsty packages target PostgreSQL 14-18.
- No
shared_preload_librariessetting is required. Existing sessions must reconnect after server-side library replacement before assuming a new runtime is active. jsonb_plruby,hstore_plruby, andltree_plrubyare companion transforms. A function must explicitly declareTRANSFORM FOR TYPE ...to receive native Ruby structures instead of the normal datum wrapper/conversion path.