executing os commands from a function - Mailing list pgsql-general

From Armand Pirvu (home)
Subject executing os commands from a function
Date
Msg-id 9A826EC5-060A-485F-811E-28D96F1ED068@gmail.com
Whole thread Raw
Responses Re: executing os commands from a function
Re: executing os commands from a function
Re: executing os commands from a function
List pgsql-general
All

I know this may sound like heresy since it involves executing an OS command from a function , but here goes

After an insert in a table, I want to touch a file

I.e

After insert into table test values (100) I want in a dir to have file 100

I used plsh extension but I had to use two functions and a trigger, see code below

CREATE or REPLACE FUNCTION func2 (var1 text) RETURNS text AS '
#!/bin/bash
 touch /home/postgres/$1;
' LANGUAGE plsh;
commit;

CREATE FUNCTION func1() RETURNS trigger AS '
BEGIN
perform   func2(NEW.col1);
RETURN NEW;
END;
' LANGUAGE plpgsql;

CREATE TRIGGER trigf1 BEFORE INSERT on test
    FOR EACH ROW EXECUTE PROCEDURE func1();

testdb=# insert into test3 values (777);
INSERT 0 1
testdb=# select * from test3;
 col1
------
  777

[postgres@edb1 ~]$ ls -ltr
-rw------- 1 postgres postgres     0 Sep 29 16:30 777


It works but can I be simpler ? Any other alternatives ? In Ingres for example I can use dbevent and an esqlc app which
listens 


Thank you

Armand



pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [HACKERS] pg_upgrade from 9.5 to 9.6 fails with "invalid argument"
Next
From: Jay Knight
Date:
Subject: Re: Parallel query only when EXPLAIN ANALYZEd