Skip to content
plsh

plsh

plsh : PL/sh procedural language

Overview

ID Extension Package Version Category License Language
3080
plsh
plsh
1.20220917
LANG
MIT
C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
See Also
plxslt
pltcl
plperl
pljava
plr
plperlu
plpython3u
plpgsql

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PGDG
1.20220917
18
17
16
15
14
plsh -
RPM
PGDG
1.20220917
18
17
16
15
14
plsh_$v -
DEB
PGDG
1.20220917
18
17
16
15
14
postgresql-$v-plsh -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
el8.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
el9.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
el9.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
el10.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
el10.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
d12.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
d12.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
d13.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
d13.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
u22.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
u22.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
u24.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
u24.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
u26.x86_64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
u26.aarch64
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
PGDG 1.20220917
Package Version OS ORG SIZE File URL
plsh_18 1.20220917 el8.x86_64 pgdg 22.4 KiB plsh_18-1.20220917-7PGDG.rhel8.x86_64.rpm
plsh_18 1.20220917 el8.aarch64 pgdg 22.1 KiB plsh_18-1.20220917-7PGDG.rhel8.aarch64.rpm
plsh_18 1.20220917 el9.x86_64 pgdg 21.3 KiB plsh_18-1.20220917-9PGDG.rhel9.8.x86_64.rpm
plsh_18 1.20220917 el9.x86_64 pgdg 21.5 KiB plsh_18-1.20220917-7PGDG.rhel9.x86_64.rpm
plsh_18 1.20220917 el9.aarch64 pgdg 21.0 KiB plsh_18-1.20220917-9PGDG.rhel9.8.aarch64.rpm
plsh_18 1.20220917 el9.aarch64 pgdg 20.9 KiB plsh_18-1.20220917-7PGDG.rhel9.aarch64.rpm
plsh_18 1.20220917 el10.x86_64 pgdg 21.6 KiB plsh_18-1.20220917-9PGDG.rhel10.2.x86_64.rpm
plsh_18 1.20220917 el10.x86_64 pgdg 21.9 KiB plsh_18-1.20220917-7PGDG.rhel10.x86_64.rpm
plsh_18 1.20220917 el10.aarch64 pgdg 21.4 KiB plsh_18-1.20220917-9PGDG.rhel10.2.aarch64.rpm
plsh_18 1.20220917 el10.aarch64 pgdg 21.7 KiB plsh_18-1.20220917-7PGDG.rhel10.aarch64.rpm
postgresql-18-plsh 1.20220917 d12.x86_64 pgdg 27.6 KiB postgresql-18-plsh_1.20220917-4.pgdg12+1_amd64.deb
postgresql-18-plsh 1.20220917 d12.aarch64 pgdg 27.1 KiB postgresql-18-plsh_1.20220917-4.pgdg12+1_arm64.deb
postgresql-18-plsh 1.20220917 d13.x86_64 pgdg 27.7 KiB postgresql-18-plsh_1.20220917-4.pgdg13+1_amd64.deb
postgresql-18-plsh 1.20220917 d13.aarch64 pgdg 27.2 KiB postgresql-18-plsh_1.20220917-4.pgdg13+1_arm64.deb
postgresql-18-plsh 1.20220917 u22.x86_64 pgdg 29.5 KiB postgresql-18-plsh_1.20220917-4.pgdg22.04+1_amd64.deb
postgresql-18-plsh 1.20220917 u22.aarch64 pgdg 28.7 KiB postgresql-18-plsh_1.20220917-4.pgdg22.04+1_arm64.deb
postgresql-18-plsh 1.20220917 u24.x86_64 pgdg 27.6 KiB postgresql-18-plsh_1.20220917-4.pgdg24.04+1_amd64.deb
postgresql-18-plsh 1.20220917 u24.aarch64 pgdg 27.2 KiB postgresql-18-plsh_1.20220917-4.pgdg24.04+1_arm64.deb
postgresql-18-plsh 1.20220917 u26.x86_64 pgdg 27.8 KiB postgresql-18-plsh_1.20220917-4.pgdg26.04+1_amd64.deb
postgresql-18-plsh 1.20220917 u26.aarch64 pgdg 27.3 KiB postgresql-18-plsh_1.20220917-4.pgdg26.04+1_arm64.deb

Source

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install plsh;		# install via package name, for the active PG version

pig install plsh -v 18;   # install for PG 18
pig install plsh -v 17;   # install for PG 17
pig install plsh -v 16;   # install for PG 16
pig install plsh -v 15;   # install for PG 15
pig install plsh -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION plsh;

Usage

plsh: PL/sh procedural language

plsh allows writing PostgreSQL functions as shell scripts. The function body must start with a shebang line specifying the interpreter.

CREATE EXTENSION plsh;

Create Shell Functions

CREATE FUNCTION concat(text, text) RETURNS text AS '
#!/bin/sh
echo "$1$2"
' LANGUAGE plsh;

SELECT concat('hello ', 'world');  -- 'hello world'

Argument Passing

Function arguments are passed as positional shell variables ($1, $2, etc.):

CREATE FUNCTION file_line_count(filename text) RETURNS int AS '
#!/bin/sh
wc -l < "$1"
' LANGUAGE plsh;

Return Values

  • Standard output becomes the return value (trailing newlines stripped)
  • Empty output returns NULL
  • Standard error output causes the function to abort with that error message
  • Non-zero exit status triggers an error
CREATE FUNCTION system_uptime() RETURNS text AS '
#!/bin/sh
uptime
' LANGUAGE plsh;

Database Access

Direct SPI access is not available, but psql can be used since libpq environment variables are preconfigured:

CREATE FUNCTION query_db(x int) RETURNS text AS $$
#!/bin/sh
psql -At -c "SELECT name FROM users WHERE id = $1"
$$  LANGUAGE plsh;

Trigger Functions

Trigger context is available via environment variables:

Variable Description
PLSH_TG_NAME Trigger name
PLSH_TG_WHEN BEFORE, INSTEAD OF, or AFTER
PLSH_TG_LEVEL ROW or STATEMENT
PLSH_TG_OP DELETE, INSERT, UPDATE, or TRUNCATE
PLSH_TG_TABLE_NAME Target table name
PLSH_TG_TABLE_SCHEMA Target table schema

Event trigger variables: PLSH_TG_EVENT, PLSH_TG_TAG.

Inline Execution

DO E'#!/bin/sh\necho "running shell command"' LANGUAGE plsh;

Security

plsh should not be declared as TRUSTED since shell scripts have full OS-level access under the PostgreSQL user. Only superusers should create plsh functions.

Last updated on