pljs
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pljs | 1.0.5 | LANG | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3011 | pljs | No | Yes | No | Yes | No | No | pg_catalog |
| Related | plv8 jsquery pllua pg_tle plpgsql pg_jsonschema plperl plpython3u |
|---|
with submodules, hot fix with CONFIG_VERSION
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | MIXED | 1.0.5 | 1817161514 | pljs | - |
| RPM | PIGSTY | 1.0.5 | 1817161514 | pljs_$v | - |
| DEB | PIGSTY | 1.0.5 | 1817161514 | postgresql-$v-pljs | - |
Build
You can build the RPM / DEB packages for pljs using pig build:
pig build pkg pljs # build RPM / DEB packages
Install
You can install pljs directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install pljs; # Install for current active PG version
pig ext install -y pljs -v 18 # PG 18
pig ext install -y pljs -v 17 # PG 17
pig ext install -y pljs -v 16 # PG 16
pig ext install -y pljs -v 15 # PG 15
pig ext install -y pljs -v 14 # PG 14
dnf install -y pljs_18 # PG 18
dnf install -y pljs_17 # PG 17
dnf install -y pljs_16 # PG 16
dnf install -y pljs_15 # PG 15
dnf install -y pljs_14 # PG 14
apt install -y postgresql-18-pljs # PG 18
apt install -y postgresql-17-pljs # PG 17
apt install -y postgresql-16-pljs # PG 16
apt install -y postgresql-15-pljs # PG 15
apt install -y postgresql-14-pljs # PG 14
Create Extension:
CREATE EXTENSION pljs;
Usage
pljs enables writing PostgreSQL functions in JavaScript using the QuickJS engine.
CREATE EXTENSION pljs;
DO $$ pljs.elog(NOTICE, "Hello, World!") $$ LANGUAGE pljs;
Create Functions
CREATE FUNCTION pljs_add(a int, b int) RETURNS int AS $$
return a + b;
$$ LANGUAGE pljs;
SELECT pljs_add(1, 2); -- 3
Database Access
CREATE FUNCTION get_users() RETURNS SETOF json AS $$
var rows = pljs.execute('SELECT * FROM users');
for (var i = 0; i < rows.length; i++) {
pljs.return_next(JSON.stringify(rows[i]));
}
$$ LANGUAGE pljs;
Execute with arguments:
var rows = pljs.execute('SELECT * FROM tbl WHERE id = $1', [42]);
var affected = pljs.execute('DELETE FROM tbl WHERE price > $1', [1000]);
Prepared Statements
var plan = pljs.prepare('SELECT * FROM tbl WHERE col = $1', ['int']);
var rows = plan.execute([1]);
plan.free();
Cursors
var plan = pljs.prepare('SELECT * FROM tbl WHERE col = $1', ['int']);
var cursor = plan.cursor([1]);
var row;
while (row = cursor.fetch()) {
// process row
}
cursor.close();
plan.free();
Subtransactions
try {
pljs.subtransaction(function() {
pljs.execute("INSERT INTO tbl VALUES(1)");
pljs.execute("INSERT INTO tbl VALUES(1/0)"); // error - rolls back
});
} catch(e) {
// handle error
}
Logging
pljs.elog(DEBUG1, 'debug message');
pljs.elog(NOTICE, 'notice message');
pljs.elog(WARNING, 'warning message');
pljs.elog(ERROR, 'error message');
Find Other PLJS Functions
CREATE FUNCTION callee(a int) RETURNS int AS $$ return a * a $$ LANGUAGE pljs;
CREATE FUNCTION caller(a int, t int) RETURNS int AS $$
var func = pljs.find_function("callee");
return func(a);
$$ LANGUAGE pljs;
Window Functions
CREATE FUNCTION my_window_func(val int) RETURNS int AS $$
var winobj = pljs.get_window_object();
var pos = winobj.get_current_position();
var total = winobj.get_partition_row_count();
return winobj.get_func_arg_in_current(0);
$$ LANGUAGE pljs WINDOW;
Window object methods: get_current_position(), get_partition_row_count(), set_mark_position(pos), rows_are_peers(pos1, pos2), get_func_arg_in_partition(argno, relpos, seektype, mark_pos), get_func_arg_in_frame(argno, relpos, seektype, mark_pos), get_func_arg_in_current(argno), get_partition_local(), set_partition_local(obj).
Utility Functions
SELECT pljs_info(); -- memory and stack usage as JSON
SELECT pljs_version(); -- extension version
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.