I created a shell script which take an integer argument to send me a mail.
Now I try to create a trigger on my psql database which send the id of the inserted row which fired the trigger to my shell script using the plsh extension.
So I created a function sendMailContact() and a trigger mailContact :
TRIGGER mailContact :
CREATE TRIGGER mailcontact
AFTER INSERT
ON contact
FOR EACH ROW
EXECUTE PROCEDURE sendmailcontact(NEW);
FUNCTION sendMailContact :
CREATE OR REPLACE FUNCTION sendmailcontact()
RETURNS trigger AS '
#!/bin/sh
/home/me/Scripts/sendMailContact.sh NEW.id'
LANGUAGE plsh;
But the "NEW.id" seems to doesn't works properly (Here it's part of the code because I was not able to pass it as a variable). According to this answer : https://stackoverflow.com/a/35654749/5591761 And from the official README file, the NEW.x has to work but here it doesn't.
I also tried the TG_ARGV[0] which is used to pass argument from trigger to function in standard SQL langage but I don't really know how to use it here. Maybe the plsh extension provide us a new table of argument but I didn't find it.
Finally I tried to pass directly the id to the function and use it with $1 on my function but it doesn't work aswell
How can I pass the id (integer) of the inserted row to my function and use it as an argument for my shell script ?
----- EDIT : -----
After I took a look at the answers you provided me, I edited my function like this :
CREATE OR REPLACE FUNCTION sendmailcontact()
RETURNS trigger AS
$BODY$
#!/bin/sh
/home/pi/Scripts/sendMailContact.sh $2
$BODY$
LANGUAGE plsh
I used $2 because the id is the first column of the INSERT (id is a serial auto incremental but I think it's a part of the INSERT aswell ?)
Then the trigger :
CREATE TRIGGER mailcontact
AFTER INSERT
ON contact
FOR EACH ROW
EXECUTE PROCEDURE sendmailcontact(NEW);
It still don't working.
But I noticed that the INSERT doesn't work anymore if the trigger is activated. Nothing is added to my table.
In case you are asking if the script or the trigger work the answer is yes, before this I wrote the almost same trigger and function with the trigger calling the function when I INSERT in the contact. The function was just sending me a mail without any argument and either the mail and the INSERT were working.