4

I'm not sure if I was specific in the question, but I'm having trouble creating a Postgres function that runs a Linux shell command, with one detail: it's a function in a Trigger after insert and I need to use some NEW columns.

While in MySQL, using the plugin "MySQL UDF" it was pretty simple, trigger worked like this:

BEGIN
 DECLARE result int(10);
 SET result = sys_exec('/usr/bin/php /var/www/html/.../regras.php NEW.uniqueid NEW.linkedid NEW.eventtype');
END

But on PostgreSQL I tried the language PL/sh, which enables running any shell script, so I wrote the following function:

   CREATE FUNCTION tarifador_func2() RETURNS TRIGGER
    LANGUAGE plsh
    AS $$
    #!/bin/sh
    /usr/bin/php /var/www/html/...regras.php NEW.uniqueid NEW.linkedid NEW.eventtype
   $$;

It does execute the .php file in proper way, the problem is the language does not recognize the NEW variables I'm giving as arguments to the PHP, so in the args[] what I got is "NEW.uniqueid", "NEW.linkedid" and "NEW.eventtype".

So, anyone knows how can I properly use the NEW argument in PL/sh? Another possible solution might be to manually set the three values I need via the arguments on crating the trigger, but it's not allowed to use NEW in the arguments.

1

2 Answers 2

4

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 TRIGGER
LANGUAGE plsh
AS $$
#!/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.

Sign up to request clarification or add additional context in comments.

2 Comments

DELETE should also report the OLD record as parameters. (because for all trigger events, it uses the tg_trigtuple property). -- Another side note that if the trigger definition has fixed parameters too, they will come before the column values (these seem to be extremely undocumented).
@Lukasz I have a command that return ankit\nankit1 as value(\n is new line) but i want to use these value to insert into other table. Can you please help me with this?
2

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 os
os.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();

3 Comments

I did try using PL/Python, but had no success on executing "subprocess.call", would you have an example of running the php file?
I have added a simple example. I am no Python expert though.
Thank you @laurenz-albe, your solution is also correct for my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.