Running system command with argument in a PostgreSQL function Running system command with argument in a PostgreSQL function database database

Running system command with argument in a PostgreSQL function


You can access some values in plsh triggers.

  • UPDATE offers only OLD
  • INSERT offers only NEW (duh)
  • DELETE I didn't test

So you get those values using arguments, like $1, $2

You function would look kinda like this:

CREATE FUNCTION tarifador_func2() RETURNS TRIGGERLANGUAGE plshAS $$#!/bin/sh/usr/bin/php /var/www/html/...regras.php $3 $6 $1

$$;

Notice that I didn't use $1 $2 $3, that is because plsh extension dumps ALL columns into arguments in order they are declared in your table. So you might do something like INSERT INTO table1 (column3) VALUES (6); and it will be under $3 in plsh, assuming this is third column in table.

As a side note, metadata of trigger is available thru env vars.


As far as I know, you cannot access the NEWand OLD tuple in PL/sh.

I would use PL/Perl or PL/Python for this purpose.

Here is an example in PL/Python:

CREATE OR REPLACE FUNCTION pytrig() RETURNS trigger   LANGUAGE plpythonu AS$$import osos.system("/usr/bin/php /home/laurenz/hello.php '" + TD["new"]["val"] + "'")$$;CREATE TABLE test (id integer PRIMARY KEY, val text);CREATE TRIGGER pytrig AFTER INSERT ON test FOR EACH ROW   EXECUTE PROCEDURE pytrig();