0

I would like to do something like this, but it doesn't work...

CREATE FUNCTION kill(integer) RETURNS integer AS $$
DECLARE
    pid ALIAS FOR $2;
BEGIN
    -- Do some stuff here...

    return kill TERM, pid;
END;
$$ LANGUAGE plpgsql;

Postgresql 8.3

UPGRADING is not an option, sorry...

1

2 Answers 2

2

What are you trying to kill? A query or a backend? If only a query, pg_cancel_backend() existed in Postgresql 8.3. If you really need to execute a shell command, you'll need to use one of the "untrusted" procedural languages like plperlu or plpythonu etc. See http://www.postgresql.org/docs/8.3/static/plperl-trusted.html for more details.

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

1 Comment

pg_terminate_backend didn't, though, and that's probably what they really want.
1

You can't do it in PL/PgSQL, but you can in most of the other PLs:

CREATE LANGUAGE plperlu;

CREATE OR REPLACE FUNCTION signal(signum integer, pid integer) RETURNS integer AS $$
return kill $_[0], $_[1];
$$ LANGUAGE plperlu;

or use PL/Python if you prefer. Or even PL/TCL.

Alternately, write a simple C extension to expose the kill function to SQL. It'll be quite trivial to adapt the examples in the documentation for the purpose.

2 Comments

Do you need super user access to run the code in your answer ?
@Alex Of course. If there was any built-in way to signal a process without having superuser access that'd be monsterous security hole. If you want to add that in your database you can make the function SECURITY DEFINER, REVOKE access to it from PUBLIC, and GRANT rights to only the user(s) or role(s) you want to be able to send signals.

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.