• Stars
    star
    154
  • Rank 237,453 (Top 5 %)
  • Language
    C
  • License
    Other
  • Created over 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PL/sh is a procedural language handler for PostgreSQL that allows you to write stored procedures in a shell of your choice.

PL/sh Procedural Language Handler for PostgreSQL

PL/sh is a procedural language handler for PostgreSQL that allows you to write stored procedures in a shell of your choice. For example,

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

The first line must be a #!-style line that indicates the shell to use. The rest of the function body will be executed by that shell in a separate process. The arguments are available as $1, $2, etc., as usual. (This is the shell's syntax. If your shell uses something different then that's what you need to use.) The return value will become what is printed to the standard output, with a newline stripped. If nothing is printed, a null value is returned. If anything is printed to the standard error, then the function aborts with an error and the message is printed. If the script does not exit with status 0 then an error is raised as well.

The shell script can do anything you want, but you can't access the database directly. Trigger functions are also possible, but they can't change the rows. Needless to say, this language should not be declared as TRUSTED.

The distribution also contains a test suite in the directory test/, which contains a simplistic demonstration of the functionality.

I'm interested if anyone is using this.

Peter Eisentraut [email protected]

Database Access

You can't access the database directly from PL/sh through something like SPI, but PL/sh sets up libpq environment variables so that you can easily call psql back into the same database, for example

CREATE FUNCTION query (x int) RETURNS text
LANGUAGE plsh
AS $$
#!/bin/sh
psql -At -c "select b from pbar where a = $1"
$$;

Note: The "bin" directory is prepended to the path, but only if the PATH environment variable is already set.

Triggers

In a trigger procedure, trigger data is available to the script through environment variables (analogous to PL/pgSQL):

  • 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: name of the table the trigger is acting on
  • PLSH_TG_TABLE_SCHEMA: schema name of the table the trigger is acting on

Event Triggers

In an event trigger procedure, the event trigger data is available to the script through the following environment variables:

  • PLSH_TG_EVENT: event name
  • PLSH_TG_TAG: command tag

Inline Handler

PL/sh supports the DO command. For example:

DO E'#!/bin/sh\nrm -f /tmp/file' LANGUAGE plsh;

Installation

You need to have PostgreSQL 8.4 or later, and you need to have the server include files installed.

To build and install PL/sh, use this procedure:

make
make install

The include files are found using the pg_config program that is included in the PostgreSQL installation. To use a different PostgreSQL installation, point configure to a different pg_config like so:

make PG_CONFIG=/else/where/pg_config
make install PG_CONFIG=/else/where/pg_config

Note that generally server-side modules such as this one have to be recompiled for every major PostgreSQL version (that is, 8.4, 9.0, ...).

To declare the language in a database, use the extension system with PostgreSQL version 9.1 or later. Run

CREATE EXTENSION plsh;

inside the database of choice. To upgrade from a previous installation that doesn't use the extension system, use

CREATE EXTENSION plsh FROM unpackaged;

Use DROP EXTENSION to remove it.

With versions prior to PostgreSQL 9.1, use

psql -d DBNAME -f .../share/contrib/plsh.sql

with a server running. To drop it, use droplang plsh, or DROP FUNCTION plsh_handler(); DROP LANGUAGE plsh; if you want to do it manually.

Test suite

Build Status

To run the test suite, execute

make installcheck

after installation.

More Repositories

1

pguri

uri type for PostgreSQL
C
297
star
2

homebrew-postgresql

๐Ÿ˜ PostgreSQL formulae for the Homebrew package manager
Ruby
288
star
3

pguint

unsigned integer types extension for PostgreSQL
Python
231
star
4

pex

light-weight package manager for PostgreSQL
Shell
95
star
5

postgresqlfs

FUSE driver to access PostgreSQL databases as a file system
C
80
star
6

pgemailaddr

email address type for PostgreSQL
C
43
star
7

git-whoami

essential Git command
Shell
24
star
8

pgtrashcan

PostgreSQL trash can
C
23
star
9

homebrew-auto-update

automatically runs brew update
Shell
22
star
10

plxslt

XSLT procedural language for PostgreSQL
C
18
star
11

pgpcre

PCRE functions for PostgreSQL
C
17
star
12

getent-osx

simplistic getent tool emulation for OS X
Perl
14
star
13

pex-packages

package library for pex (https://github.com/petere/pex)
13
star
14

veung

visual explain รผber-next generation (for PostgreSQL)
Python
12
star
15

postgresql-common

PostgreSQL database-cluster manager โ€” You might be interested in https://github.com/petere/homebrew-postgresql as well.
Perl
12
star
16

homebrew-sgml

SGML tools for Homebrew
Ruby
10
star
17

autopex

PostgreSQL extension installation magic
C
9
star
18

pglockviz

tool to visualize PostgreSQL locks
Python
8
star
19

plpydbapi

Python DB-API interface on top of PL/Python
Python
7
star
20

pgbloomagg

Bloom filter aggregate function for PostgreSQL
PLpgSQL
7
star
21

logging-hooks-presentation

material for presentation about logging hooks in PostgreSQL
Puppet
7
star
22

adventofcode-2020

https://adventofcode.com/2020
Raku
6
star
23

pgci

continuous integration service for PostgreSQL
Python
4
star
24

markdown-formatter-for-chrome

extension for Google Chrome that formats Markdown text as HTML
JavaScript
4
star
25

pgvihash

version-independent hash functions for PostgreSQL
C
4
star
26

emacs-save-packages

save and restore installed ELPA packages
Emacs Lisp
4
star
27

pglogjsonsrvgo

pg_logforward JSON consumer written in Go
Go
2
star
28

emacs-ssh-file-modes

Emacs major modes for ssh authorized_keys and known_hosts files
Emacs Lisp
2
star
29

peter.eisentraut.org

Peter Eisentraut's blog
Ruby
2
star
30

debnorepo

find installed Debian packages without repository
2
star
31

pglibuuid

PostgreSQL wrapper for libuuid (obsolete, use PostgreSQL's configure --with-uuid=e2fs for equivalent functionality)
C
2
star
32

sgml-spell-checker

SGML spell-checking package
Shell
2
star
33

pgcatviz

generate schema diagram of PostgreSQL system catalogs using Graphviz
Python
2
star
34

my-gettext-scripts

some small wrapper scripts around gettext tools
Shell
1
star
35

updatable_views

updatable views extension for PostgreSQL
C
1
star
36

samsung-q45-support

support for Samsung Q45 laptops on Debian/Ubuntu
1
star
37

sgml-spell-checker-word-lists

additional word lists to use with sgml-spell-checker
Makefile
1
star
38

plpylint

runs pylint over PostgreSQL PL/Python functions
Python
1
star
39

emacs-eruby-mode

Emacs minor mode for eRuby template (.erb) files
Emacs Lisp
1
star
40

nodes-cookbook

Chef cookbook to store node attributes in data bag items
Ruby
1
star
41

adventofcode-2021

๐ŸŽ„
Python
1
star
42

commitfest-tools

tools for messing with PostgreSQL commit fests
Python
1
star
43

homebrew-icu

ICU formulae for the Homebrew package manager
Ruby
1
star