pljs

PL/JS trusted procedural language

Overview

PackageVersionCategoryLicenseLanguage
pljs1.0.5LANGPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
3011pljsNoYesNoYesNoNopg_catalog
Relatedplv8 jsquery pllua pg_tle plpgsql pg_jsonschema plperl plpython3u

with submodules, hot fix with CONFIG_VERSION

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.0.51817161514pljs-
RPMPIGSTY1.0.51817161514pljs_$v-
DEBPIGSTY1.0.51817161514postgresql-$v-pljs-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

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: PL/JavaScript trusted procedural language

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

Last Modified 2026-03-12: add pg extension catalog (95749bf)