Received: from maia.hub.org (unknown [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id DF1C61337B50 for ; Thu, 21 Oct 2010 15:46:57 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 59489-03 for ; Thu, 21 Oct 2010 18:46:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mailroom.gap.ca (mailroom.gap.ca [205.207.123.201]) by mail.postgresql.org (Postfix) with ESMTP id 0610D1337B47 for ; Thu, 21 Oct 2010 15:46:49 -0300 (ADT) Received: from mailroom.gap.ca (127.0.0.1) by mailroom.gap.ca (MlfMTA v3.2r9) id ho240o0171sc for ; Thu, 21 Oct 2010 14:35:51 -0400 (envelope-from ) Received: from gap.ca ([10.100.10.40]) by mailroom.gap.ca (SonicWALL 7.2.1.2841) with ESMTP; Thu, 21 Oct 2010 14:35:51 -0400 Received: from [10.100.50.164] (HELO dhcp164.gap.ca) by gap.ca (CommuniGate Pro SMTP 5.2.14) with ESMTPS id 8605763; Thu, 21 Oct 2010 14:46:45 -0400 Subject: Re: DROP ROLE: how to detect active sessions? Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii From: Ken Lalonde In-Reply-To: <27773.1287685132@sss.pgh.pa.us> Date: Thu, 21 Oct 2010 14:46:45 -0400 Cc: pgsql-admin@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <61A975CD-61CC-40CF-87CA-46BEEC1CEEAF@gap.ca> <27773.1287685132@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.1081) X-Mlf-Version: 7.2.1.2841 X-Mlf-UniqueId: o201010211835510076277 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/163 X-Sequence-Number: 35904 This is a web app, so the username is unknown until the user actually = logs in. It would be ideal if pg_stat_activity contained the current role. Until then, I'll go with your second recommendation. Thanks for such a quick and useful reply. Ken On 2010-10-21, at 2:18 PM, Tom Lane wrote: > Ken Lalonde writes: >> Is there any way to determine if a given role has any active = sessions? >=20 > Not if you're using SET ROLE. pg_stat_activity will show you the > login role names for active sessions. Do you really need SET ROLE > rather than logging in with the appropriate username? >=20 > There are going to be race conditions in any case, of course. > Probably the best solution is to just not try to drop roles > except when the system is idle. >=20 > regards, tom lane