If you ever have used R, the possibility of using such
a powerful statistics package inside your database
should seem like a natural and powerful combination.
nearly any language to the database. By
default, PostgreSQL lets you write server-side functions in Pl/PgSQL, and there long
has been support for Pl/Perl and Pl/Python.
Since 2003, developer Joe Conway
has maintained PL/R, allowing you to
write server-side functions in PostgreSQL,
using R. If you ever have used R, the
possibility of using such a powerful
statistics package inside your database
should seem like a natural and powerful
combination. Rather than having to read
the data into R outside PostgreSQL, you
suddenly can have R work directly on the
results of queries, without needing to use
a separate client application or process.
In this article, I introduce the basics
of PL/R. This combination isn’t for
everyone, but with the growing (and
welcome) popularity of PostgreSQL
among Web developers, and with
the increasing need for analysis of
information gathered from Web users,
it seems to me that PL/R could be an
important tool for many developers.
Introduction to R
The home page for R is http://r-project.org.
From that site, you can download versions
of R for a variety of operating systems,
including Linux. (I was able to install
R on my server running Ubuntu with
apt-get install r-base-core,
which installed a large number of
dependent packages.) New versions
of R come out every few months and
normally are installed in two versions:
the R language and environment and the
runtime necessary to execute programs
written in R, known as “Rscript”. To use
the language interactively, just type R at
the shell prompt.
> c(1, 5, 9, 3)
 1 5 9 3