PL/Ruby Brings Ruby into PostgreSQL as a First-Class Procedural Language

PostgreSQL · 31 Jul 2026 · 2 min read

#postgresql

PL/Ruby is a new procedural language handler for PostgreSQL that lets you write database functions in Ruby, stored and executed natively inside the database. It embeds an MRI Ruby interpreter in the backend and is designed for PostgreSQL 11–18 and Ruby 3.x. The extension installs via CREATE EXTENSION plruby;, making it a first-class citizen. This project mirrors the feature set of PL/php and draws inspiration from PL/Perl and PL/Tcl, but with Ruby's elegant syntax and rich standard library.

  • Native data type mapping: Arguments arrive as native Ruby values: Integer, Float, true/false, String, nested Array, and composite/record types as Hash.
  • Set-returning functions: Support for RETURNS SETOF and RETURNS TABLE using return_next.
  • Triggers and event triggers: Row and statement triggers via the $_TD hash; support CREATE EVENT TRIGGER with RETURNS event_trigger.
  • Database access (SPI): Functions like spi_exec and spi_fetch_row are provided for SQL queries.
  • Procedures and transactions: Full support for CREATE PROCEDURE with transaction control.

A simple example from the announcement shows how clean the integration is:

CREATE EXTENSION plruby;
CREATE FUNCTION hello(text) RETURNS text LANGUAGE plruby AS $$
  "Hello, #{args[0]}!"
$$;
SELECT hello('world'); -- Hello, world!

For developers, PL/Ruby opens the door to using Ruby for complex business logic directly inside the database. This can reduce application‑to‑database round trips and centralize data rules. The ability to use Ruby gems and the standard library in triggers and functions is a major productivity boost. However, embedding a full Ruby interpreter in the backend has performance implications—CPU‑intensive functions will run slower than native C or PL/pgSQL, especially under heavy load. Still, for teams already invested in Ruby, PL/Ruby offers a compelling way to unify application and database code in a single language.

Source: https://www.postgresql.org/about/news/plruby-3349/

Related

auto-curated · source linked above ← all news