• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

BUILDING.mdH A D29-May-20215.5 KiB200141

BUILTINS.mdH A D29-May-20217.9 KiB255176

CONFIGURATION.mdH A D29-May-2021561 129

FUNCTIONS.mdH A D29-May-20215.4 KiB199159

PGXN.mdH A D29-May-2021607 2112

README.mdH A D29-May-20214.2 KiB12187

README.md

1# PLV8
2
3PLV8 is a _trusted_ Javascript language extension for PostgreSQL.  It can be
4used for _stored procedures_, _triggers_, etc.
5
6PLV8 works with most versions of Postgres, but works best with `9.1` and above,
7including `10.0`, `11`, `12`, and `13`.
8
9## Installing PLV8
10
11If the PLV8 extension has been installed to your system, the PLV8 extension can
12be installed into your PostgreSQL database by running:
13
14```
15=# CREATE EXTENSION plv8;
16```
17
18### Verifying Your Installation
19
20You can verify the installation in two ways.  As of PLV8 `2.0.0`, you can
21execute a stored procedure:
22
23```
24=# SELECT plv8_version();
25```
26
27Alternately, you can run the following on all versions of PLV8:
28
29```
30=# DO $$ plv8.elog(NOTICE, plv8.version); $$ LANGUAGE plv8;
31```
32
33## Updating PLV8
34
35As of PLV8 version `2.3.3`, you can use upgrade scripts to upgrade your
36installation from any version higher than `1.5.0`:
37
38```
39=# ALTER EXTENSION plv8 UPDATE TO `3.0.0`;
40```
41
42Note that until the database has been restarted, the old version of PLV8 will
43still be loaded, though `SELECT plv8_version()` will return the new version.
44This is an artifact of how Postgres manages extensions.
45
46### Updating Older PLV8 Installs
47
48Updating PL/v8 is usually straightforward as it is a small and stable extension
49- it only contains a handful of objects that need to be added to PostgreSQL
50when installing the extension.
51
52The procedure that is responsible for invoking this installation script
53(generated during compile time based on plv8.sql.common), is controlled by
54PostgreSQL and runs when CREATE EXTENSION is executed only. After building, it
55takes the form of plv8--<version>.sql and is usually located under
56`/usr/share/postgresql/<PG_MAJOR>/extension`, depending on the OS.
57
58When this command is executed, PostgreSQL tracks which objects belong to the
59extension and conversely removes them upon uninstallation, i.e., whenever
60`DROP EXTENSION` is called.
61
62You can explore some of the objects that PL/v8 stores under PostgreSQL:
63
64```
65=# SELECT lanname FROM pg_catalog.pg_language WHERE lanname = 'plv8';
66=# SELECT proname FROM pg_proc p WHERE p.proname LIKE 'plv8%';
67=# SELECT typname FROM pg_catalog.pg_type WHERE typname LIKE 'plv8%';
68```
69
70To update PostgreSQL, you can `DROP` the existing extension:
71
72```
73=# DROP EXTENSION plv8;
74```
75
76Install the new version, and `CREATE` the extension:
77
78```
79=# CREATE EXTENSION plv8;
80```
81
82Alternately, you can backup and restore your database.
83
84## Runtime Environment Separation
85
86In PLV8, each session has one global JS runtime context. This enables function
87invocations at low cost, and sharing common object among the functions. However,
88for the security reasons, if the user switches to another with SET ROLE command,
89a new JS runtime context is initialized and used separately. This prevents the
90risk of unexpected information leaking.
91
92Each `plv8` function is invoked as if the function is the property of other
93object. This means this in each function is a Javascript `object` that is created
94every time the function is executed in a query. In other words, the life time and
95the visibility of this object in a function is only a series of function calls in
96a query. If you need to share some value among different functions, keep it in the
97global `plv8` object because each function invocation has a different this object.
98
99## Start-up Procedure
100
101PLV8 provides a start up facility, which allows you to call a `plv8` runtime
102environment initialization function specified in the GUC variable.  This can
103only be set by someone with administrator access to the database you are
104accessing.
105
106```
107SET plv8.start_proc = 'plv8_init';
108SELECT plv8_test(10);
109```
110
111If this variable is set when the runtime is initialized, before the function
112call of `plv8_test()` another `plv8` function `plv8_init()` is invoked. In such
113initialization function, you can add any properties to `plv8` object to expose
114common values or assign them to the this property. In the initialization function,
115the receiver this is specially pointing to the global object, so the variables
116that are assigned to the this property in this initialization are visible from
117any subsequent function as global variables.
118
119Remember `CREATE FUNCTION` also starts the `plv8` runtime environment, so make
120sure to `SET` this GUC before any `plv8` actions including `CREATE FUNCTION`.
121