Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id C7013632416 for ; Tue, 2 Mar 2010 08:08:23 -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 74887-07 for ; Tue, 2 Mar 2010 12:08:06 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 8B9A5632261 for ; Tue, 2 Mar 2010 08:08:12 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 56EC71E0442; Tue, 2 Mar 2010 20:08:09 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Y0XW6WRGRH7S; Tue, 2 Mar 2010 20:08:07 +0800 (WST) Received: from [10.1.1.2] (124-169-24-37.dyn.iinet.net.au [124.169.24.37]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 11C0F1E0441; Tue, 2 Mar 2010 20:08:06 +0800 (WST) Message-ID: <4B8CFF9C.9080305@postnewspapers.com.au> Date: Tue, 02 Mar 2010 20:07:56 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.7) Gecko/20100111 Thunderbird/3.0.1 MIME-Version: 1.0 To: Richard Huxton CC: rawi , pgsql-sql@postgresql.org Subject: Re: kind of RULE on SELECT depending on existence of a WHERE condition References: <27741669.post@talk.nabble.com> <4B8B983B.4080205@archonet.com> <27742718.post@talk.nabble.com> <4B8BC36E.4040007@archonet.com> In-Reply-To: <4B8BC36E.4040007@archonet.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.881 tagged_above=-10 required=5 tests=AWL=-0.882, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201003/10 X-Sequence-Number: 33717 On 1/03/2010 9:38 PM, Richard Huxton wrote: > On 01/03/10 12:16, rawi wrote: >> >>> Not quite the way you suggest. You could build a series of views with >>> the WHERE conditions built in to them, and grant permissions on those >>> though. >> >> Thank you very much for your help. >> >> Unfortunately is this not what I hoped... >> The permissions will be granted dynamic by the application out of the >> user-records and expressed in the WHERE flags. >> I'll need another approach... > > You could write a set-returning function that takes either: > 1. A list of conditions > 2. The text for a WHERE clause SELECT my_priveleged_function('1=1'); You'll probably have to provide different functions for the use of different roles, or have your function check the current role (see INFORMATION_SCHEMA) and prepend something appropriate to the WHERE clause. Even then you'll probably have to pre-filter the results in a subquery, otherwise it's hard to protect against the user appending 'OR 1=1' or the like to your WHERE clause. Personally, I'd avoid any sort of textual query building - instead I'd provide my_function_for_admins(param1, param2), my_function_for_users(param1, param2) etc. Each one would substitute parameters into existing SQL using `EXECUTE USING'. Possibly-null params can be handled using COALESCE or CASE to avoid string-building. -- Craig Ringer