PL/R User's Guide - R Procedural Language
Prev | Home |  | Next

# Chapter 10. Loading R Modules at Startup

PL/R has support for auto-loading R code during interpreter initialization. It uses a special table, `plr_modules`, which is presumed to contain modules of R code. If this table exists, the modules defined are fetched from the table and loaded into the R interpreter immediately after creation.

The definition of the table `plr_modules` is as follows:

    CREATE TABLE plr_modules (
      modseq int4,
      modsrc text
    );

The field `modseq` is used to control the order of installation. The field `modsrc` contains the full text of the R code to be executed, including assignment if that is desired. Consider, for example, the following statement:

    INSERT INTO plr_modules
      VALUES (0, 'pg.test.module.load <-function(msg) {print(msg)}');

This statement will cause an R function named `pg.test.module.load` to be created in the R interpreter on initialization. A PL/R function may now simply reference the function directly as follows:

    create or replace function pg_test_module_load(text) returns text as '
      pg.test.module.load(arg1)
    ' language 'plr';

    select pg_test_module_load('hello world');
     pg_test_module_load
    ---------------------
     hello world
    (1 row)

The table `plr_modules` must be readable by all, but it is wise to make it owned and writable only by the database administrator.

Prev | Home | Next
Window Functions |  | R Function Names