pljs
pljs
pljs : PL/JS trusted procedural language
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3011 | pljs
|
pljs
|
1.0.5 |
LANG
|
PostgreSQL
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d--
|
No
|
Yes
|
No
|
Yes
|
no
|
no
|
| Relationships | |
|---|---|
| Schemas | pg_catalog |
| See Also | plv8
jsquery
pllua
pg_tle
plpgsql
pg_jsonschema
plperl
plpython3u
|
with submodules, hot fix with CONFIG_VERSION
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | MIXED
|
1.0.5 |
18
17
16
15
14
|
pljs |
- |
| RPM | PIGSTY
|
1.0.5 |
18
17
16
15
14
|
pljs_$v |
- |
| DEB | PIGSTY
|
1.0.5 |
18
17
16
15
14
|
postgresql-$v-pljs |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
el8.aarch64
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
el9.x86_64
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
el9.aarch64
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
el10.x86_64
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
el10.aarch64
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
PIGSTY 1.0.5
|
d12.x86_64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
d12.aarch64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
d13.x86_64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
d13.aarch64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
u22.x86_64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
u22.aarch64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
u24.x86_64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
u24.aarch64
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
PGDG 1.0.5
|
Source
pig build pkg pljs; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pljs; # install via package name, for the active PG version
pig install pljs -v 18; # install for PG 18
pig install pljs -v 17; # install for PG 17
pig install pljs -v 16; # install for PG 16
pig install pljs -v 15; # install for PG 15
pig install pljs -v 14; # install for PG 14Create this extension with:
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); -- 3Database 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 versionLast updated on