Functions To Let Users Cancel/Terminate own Back Ends - Mailing list pgsql-general

From David E. Wheeler
Subject Functions To Let Users Cancel/Terminate own Back Ends
Date
Msg-id E2F88530-BF6F-4D03-A37E-0C0D347CA6E7@kineticode.com
Whole thread Raw
Responses Re: Functions To Let Users Cancel/Terminate own Back Ends
List pgsql-general
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
dothat? 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

pgsql-general by date:

Previous
From: Christophe Pettus
Date:
Subject: xlog min recovery request ... is past current point ...
Next
From: Magnus Hagander
Date:
Subject: Re: Functions To Let Users Cancel/Terminate own Back Ends