Functions To Let Users Cancel/Terminate own Back Ends

From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: PostgreSQL General <pgsql-general(at)postgreSQL(dot)org>
Subject: Functions To Let Users Cancel/Terminate own Back Ends
Date: 2012-02-02 22:38:33
Message-ID: E2F88530-BF6F-4D03-A37E-0C0D347CA6E7@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Hagander 2012-02-02 22:51:03 Re: Functions To Let Users Cancel/Terminate own Back Ends
Previous Message Christophe Pettus 2012-02-02 18:59:58 xlog min recovery request ... is past current point ...