8

I am calling a bash script from the trigger function in postgresql. The script is called after update of the value(log_interval) in the table in SQL.

The read the changed value(log_interval) from the table in the script that is called and store in a file (interval.txt). If I open the file I see the old value of log_interval and not the new value in the interval.txt file.

Below is the code snippet that I am using:

Trigger function:

CREATE OR REPLACE FUNCTION update_upload_interval()
RETURNS trigger AS
$BODY$
#!/bin/sh
exec /home/User/ReadInterval.sh &
$BODY$
LANGUAGE plsh VOLATILE

Trigger:

CREATE TRIGGER assign_new_interval
AFTER UPDATE OF log_interval
ON table_interval
FOR EACH ROW
WHEN ((old.log_interval IS DISTINCT FROM new.log_interval))
EXECUTE PROCEDURE update_upload_interval();

Script code:

function readinterval() {
logperiod=$(psql -c "select log_interval from table_interval where id=1;" -t  $database_name)
echo "$logperiod" > /home/User/interval.txt
}

Am new to scripting and using SQL. Let me know the solution. Thanks in advance.

1
  • 1
    It's usually a pretty bad idea to use PL/SH and invoke shell scripts from the server. Wherever possible use LISTEN and NOTIFY to get a helper daemon to do the work instead. Commented Jun 12, 2014 at 2:54

1 Answer 1

1

Unless there's more code that isn't being shown here, just calling ReadInterval.sh won't do anything because it's nothing but a function declaration. In addition, there's a variable $database_name that isn't being set.

CREATE OR REPLACE FUNCTION update_upload_interval()
RETURNS trigger AS
$BODY$
#!/bin/sh
database_name=???
psql -c "select log_interval from table_interval where id=1;" -t  $database_name > /home/User/interval.txt
$BODY$
LANGUAGE plsh VOLATILE
Sign up to request clarification or add additional context in comments.

1 Comment

Probably I'm talking to the void here, but this does seem like a valid question that can't just be closed off. Why it garnered three upvotes is beyond me though.

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.