plr
plr : load R interpreter and execute R script from within a database
Overview
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG | 8.4.8.9 |
18 17 16 15 14 | plr |
- |
| RPM | PGDG | 8.4.8.9 |
18 17 16 15 14 | plr_$v |
- |
| DEB | PGDG | 8.4.8.9 |
18 17 16 15 14 | postgresql-$v-plr |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| el8.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| el9.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| el9.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| el10.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| el10.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| d12.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| d12.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| d13.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| d13.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| u22.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| u22.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| u24.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| u24.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| u26.x86_64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
| u26.aarch64 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 | PGDG 8.4.8.9 |
Source
github.com/postgres-plr/plr
Install
Make sure PGDG repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources:
plr enables writing PostgreSQL functions in the R programming language, providing full access to R’s statistical and data analysis capabilities.
Create Functions
With named arguments:
Argument Handling
- Unnamed arguments are available as
arg1,arg2, …; an explicitly named parameter replaces its correspondingargNvariable. - Scalar SQL NULL becomes R NULL; null array elements become R
NA. ASTRICTfunction skips calls with a whole NULL argument, not arrays containing null elements. - Composite types (rows) are passed as R data.frames
- One-dimensional arrays become R vectors; two-dimensional arrays become matrices and three-dimensional arrays become R arrays. Higher dimensions are unsupported.
Database Access via SPI
Initialize the type OID variables in the same connection before calling a function that prepares a parameterized query:
Set-Returning Functions
Return an R vector for a set of scalar values:
Window Functions
Window functions receive farg1..fargN (vectors of values in the window frame), fnumrows (frame size), and prownum (current row position in partition).
Global Variables
Persist data across function calls using R’s global environment:
Useful Support Functions
Trigger Functions
PL/R supports trigger functions with access to pg.tg.name, pg.tg.relname, pg.tg.when, pg.tg.level, pg.tg.op, pg.tg.new, and pg.tg.old.
Runtime and Privileges
This page follows PL/R 8.4.8.7. PL/R is an untrusted language: creating its functions requires a superuser, and R code runs with the PostgreSQL operating-system user’s access to files and processes. Review function bodies and EXECUTE grants accordingly. The R shared library must be available, and upstream requires R_HOME in the PostgreSQL server process environment before startup on Unix systems. Adding it only to an interactive client shell does not configure the server.
SQL scalar NULL converts to R NULL, while null elements within an array convert to R NA; declaring a function STRICT prevents calls with null arguments. R global state belongs to a backend process, not to all sessions or to a durable database table. Upstream revokes PUBLIC execution of environment-changing helpers including plr_set_rhome(text); use an administrator-managed runtime rather than exposing these helpers to application roles. Normal usage does not require shared preload.