Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id F3AA7632BC8 for ; Sat, 6 Feb 2010 04:17:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 99152-03 for ; Sat, 6 Feb 2010 08:17:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iw0-f197.google.com (mail-iw0-f197.google.com [209.85.223.197]) by mail.postgresql.org (Postfix) with ESMTP id 1FE9B6324A7 for ; Sat, 6 Feb 2010 04:14:23 -0400 (AST) Received: by iwn35 with SMTP id 35so4922991iwn.4 for ; Sat, 06 Feb 2010 00:14:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:from:date:message-id :subject:to:content-type; bh=zbfsZDnU2Z2Zlwez3h+mUQPRRF47dhmHqU9mMKYHyUQ=; b=uLchpt4kh/5UiUFeIZLxg8+kN3LKP+1nS+E5YAeBBrERHLxW0PLQUukewzpbN5E2UF BnAg98au6eICQVi6mHj7fz1gC4QqNAg/qEV8UrgrFGOmWEOqZPyrHOgh3ACXvU/biJ9y HzIFdfP5r6TJH2tdBtRHfcmSLqbdRfr2m1Ars= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=vQGy5URwsvvXiD3xKQBlHLmXGKuallHN5isLlw2aU8hKD2H6lTWWUtRc0TcNMr2NPc AF4n7mozthUM+Jge8nX9Vuyv5tYc9rWb7B3CDIVg0J0Xkq7XtAgTodtM5n6jupcP3LoF P+rh6Wt0YZ32uANt8oWdd1b08h4qlQDRNxP0k= MIME-Version: 1.0 Received: by 10.231.161.138 with SMTP id r10mr3938760ibx.34.1265410553156; Fri, 05 Feb 2010 14:55:53 -0800 (PST) From: Josh Kupershmidt Date: Fri, 5 Feb 2010 17:55:33 -0500 Message-ID: <4ec1cf761002051455i6e702999y7cf4699b4eb48242@mail.gmail.com> Subject: [patch] pg_cancel_backend for unprivileged users To: pgsql-admin@postgresql.org Content-Type: multipart/mixed; boundary=001636c5971b841392047ee25aea X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.417 tagged_above=-10 required=5 tests=AWL=0.181, BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201002/53 X-Sequence-Number: 33656 --001636c5971b841392047ee25aea Content-Type: multipart/alternative; boundary=001636c5971b84138b047ee25ae8 --001636c5971b84138b047ee25ae8 Content-Type: text/plain; charset=ISO-8859-1 I'd like a way to allow non-superusers to cancel their own queries. Wearing a DBA hat sometimes, I get requests from users asking that their long-running or erroneous queries be killed. These users have typically executed their queries through phpPgAdmin or some other method that doesn't allow them to cancel their own query, as you can in a psql session by hitting Control-C. Letting users kill their own queries would be quite useful[1], as it would cut down on work for DBAs. I couldn't find a way to implement such a function in a PL, so I hacked up backend/utils/adt/misc.c so that: * pg_signal_backend() no longer restricts calls to users passing the superuser() check * callers of pg_signal_backend(), which are just pg_cancel_backend() and pg_terminate_backend() right now, must perform their own permissions checks * pg_cancel_backend() lets users kill their own queries, or superusers kill any query Patch should be attached. This was my first attempt at modifying PG, so I suspect there are some problems I'm not seeing in my simple approach, or else someone would have already done this. Some questions: * Is there anyone else who's attempted[2] to implement something similar to what I'm looking for, or are others interested in having something like this? * Is there a fundamental reason why it's unsafe for non-superusers to be sending SIGTERM to their own backends (in other words, is this idea fatally flawed)? Thanks for any help, Josh -- [1] MySQL offers similar functionality with the "KILL " command, which lets unprivileged users kill queries running under their own usernames (actually, it looks like it disconnects the killed session entirely). Oracle also has ALTER SYSTEM KILL SESSION -- I'm not sure if unprivileged users are allowed to use this. [2] I found a thread from 2006 where Craig James was after similar functionality, but he implemented a rather ugly solution, namely a C function which would let any user send a SIGTERM to any backend PID. I'm not sure why he didn't just use a PL/pgSQL wrapper around pg_terminate_backend(). http://archives.postgresql.org/pgsql-performance/2006-07/msg00092.php --001636c5971b84138b047ee25ae8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I'd like a way to allow non-superusers to cancel their own queries.
=
Wearing a DBA hat sometimes, I get requests from users asking that thei= r long-running or erroneous queries be killed. These users have typically e= xecuted their queries through phpPgAdmin or some other method that doesn= 9;t allow them to cancel their own query, as you can in a psql session by h= itting Control-C. Letting users kill their own queries would be quite usefu= l[1], as it would cut down on work for DBAs.

I couldn't find a way to implement such a function in a PL, so I ha= cked up backend/utils/adt/misc.c so that:
=A0* pg_signal_backend() no lo= nger restricts calls to users passing the superuser() check
=A0* callers= of pg_signal_backend(), which are just pg_cancel_backend() and pg_terminat= e_backend() right now, must perform their own permissions checks
=A0* pg_cancel_backend() lets users kill their own queries, or superusers k= ill any query

Patch should be attached. This was my first attempt at= modifying PG, so I suspect there are some problems I'm not seeing in m= y simple approach, or else someone would have already done this. Some quest= ions:
=A0* Is there anyone else who's attempted[2] to implement something sim= ilar to what I'm looking for, or are others interested in having someth= ing like this?
=A0* Is there a fundamental reason why it's unsafe fo= r non-superusers to be sending SIGTERM to their own backends (in other word= s, is this idea fatally flawed)?

Thanks for any help,
Josh

--
[1] MySQL offers similar func= tionality with the "KILL <thread_id>" command, which lets u= nprivileged users kill queries running under their own usernames (actually,= it looks like it disconnects the killed session entirely). Oracle also has= ALTER SYSTEM KILL SESSION -- I'm not sure if unprivileged users are al= lowed to use this.

[2] I found a thread from 2006 where Craig James was after similar func= tionality, but he implemented a rather ugly solution, namely a C function w= hich would let any user send a SIGTERM to any backend PID. I'm not sure= why he didn't just use a PL/pgSQL wrapper around pg_terminate_backend(= ).
=A0=A0=A0 http://archives.postgresql.org/pgsql-performance/2006-07/m= sg00092.php

--001636c5971b84138b047ee25ae8-- --001636c5971b841392047ee25aea Content-Type: application/octet-stream; name="pg_cancel_backend.patch" Content-Disposition: attachment; filename="pg_cancel_backend.patch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