PostgreSQLers,
I have a need at my $dayjob to let users cancel their own back ends. See any issues with this function to allow them to do that? Any security gotchas or anything?
CREATE OR REPLACE FUNCTION iov_cancel_user_backend(
pid INTEGER
) RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$
DECLARE
username NAME;
BEGIN
SELECT usename INTO username FROM iov_catalog.iov_stat_activity WHERE procpid = pid;
IF username IS NULL THEN RETURN FALSE; END IF;
IF username <> session_user THEN
RAISE EXCEPTION 'You do not own back end %', pid;
END IF;
RETURN iov_catalog.pg_cancel_backend(pid);
END;
$$;
I plan to have one that calls pg_terminate_backend(), as well.
Thanks,
David