<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kill connections &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/kill-connections/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 24 May 2017 19:51:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Non Superuser can kill their own running queries</title>
		<link>https://www.dbrnd.com/2017/05/postgresql-non-superuser-can-kill-their-own-running-queries/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 30 May 2017 16:16:29 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Kill connections]]></category>
		<category><![CDATA[Kill idle session]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[super user]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5348</guid>

					<description><![CDATA[In this post, I am sharing a UDF and using this non-super user can kill their queries. As a PostgreSQL DBA, You may find long running queries or IDLE queries of the user. Many times users also disturb to DBA for killing their queries. In my organisation, we created one function and assigned to all [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&amp;linkname=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&amp;linkname=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&amp;linkname=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&amp;linkname=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&amp;linkname=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&amp;linkname=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-non-superuser-can-kill-their-own-running-queries%2F&#038;title=PostgreSQL%3A%20Non%20Superuser%20can%20kill%20their%20own%20running%20queries" data-a2a-url="https://www.dbrnd.com/2017/05/postgresql-non-superuser-can-kill-their-own-running-queries/" data-a2a-title="PostgreSQL: Non Superuser can kill their own running queries"></a></p><p>In this post, I am sharing a UDF and using this non-super user can kill their queries.</p>
<p>As a PostgreSQL DBA, You may find long running queries or IDLE queries of the user. Many times users also disturb to DBA for killing their queries.</p>
<p>In my organisation, we created one function and assigned to all users so that they can kill their queries without disturbing to DBA team. </p>
<p>This function created such a way that, a user can kill their own queries only. They can find process_id of other users, but they can&#8217;t kill other queries.</p>
<p>Below is a full demonstration of this:</p>
<p><strong>Create a schema:</strong>﻿</p><pre class="crayon-plain-tag">CREATE SCHEMA query_admin;</pre><p> </p>
<p><strong>Create a function to kill user query:</strong></p><pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION query_admin.kill_process(userpid integer)
RETURNS boolean AS $body$
DECLARE
    qry boolean;
BEGIN
    qry := (SELECT pg_catalog.pg_cancel_backend(pid) 
            FROM pg_stat_activity 
            WHERE usename=(select session_user) 
            AND pid=userpid);
    RETURN qry;
END;
$body$
LANGUAGE plpgsql
SECURITY DEFINER
VOLATILE
RETURNS NULL ON NULL INPUT;</pre><p> </p>
<p><strong>Grant permission to Public so that all user can access this:</strong></p>
<pre class="crayon-plain-tag">GRANT USAGE ON SCHEMA query_admin TO public;
GRANT EXECUTE ON FUNCTION query_admin.kill_process(pid integer) TO public;</pre> </p>
<p><strong>User can check their running process_id queries using below query:</strong></p>
<pre class="crayon-plain-tag">SELECT * FROM pg_stat_activity 
WHERE usename = 'user_name';</pre> </p>
<p><strong>Sample execution of kill_process():</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM query_admin.kill_process(process_id);
SELECT *FROM query_admin.kill_process(14526);</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
