Sometimes it is useful to have some global status data that is held between two calls to a procedure or is shared between different procedures. Equally useful is the ability to create functions that your PL/R functions can share. This is easily done since all PL/R procedures executed in one backend share the same R interpreter. So, any global R variable is accessible to all PL/R procedure calls, and will persist for the duration of the SQL client connection. An example of using a global object appears in the pg.spi.execp example, in Chapter 6.
A globally available, user named, R function (the R function name of PL/R functions is not the same as its PostgreSQL function name; see: Chapter 10) can be created dynamically using the provided PostgreSQL function install_rcmd(text). Here is an example:
select install_rcmd('pg.test.install <-function(msg) {print(msg)}');
install_rcmd
--------------
OK
(1 row)
create or replace function pg_test_install(text) returns text as '
pg.test.install(arg1)
' language 'plr';
select pg_test_install('hello world');
pg_test_install
-----------------
hello world
(1 row)
A globally available, user named, R function can also be automatically created and installed in the R interpreter. See: Chapter 9