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, nestedArray, and composite/record types asHash. - Set-returning functions: Support for
RETURNS SETOFandRETURNS TABLEusingreturn_next. - Triggers and event triggers: Row and statement triggers via the
$_TDhash; supportCREATE EVENT TRIGGERwithRETURNS event_trigger. - Database access (SPI): Functions like
spi_execandspi_fetch_roware provided for SQL queries. - Procedures and transactions: Full support for
CREATE PROCEDUREwith 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.