Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1at2dU-00045D-Jf for pgsql-general@arkaria.postgresql.org; Thu, 21 Apr 2016 00:38:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1at2dU-0004oM-6B for pgsql-general@arkaria.postgresql.org; Thu, 21 Apr 2016 00:38:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1at2bC-0002Ej-J9 for pgsql-general@postgresql.org; Thu, 21 Apr 2016 00:36:02 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1at2b9-0004oX-60 for pgsql-general@postgresql.org; Thu, 21 Apr 2016 00:36:02 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id DEADE20997 for ; Wed, 20 Apr 2016 20:35:57 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute5.internal (MEProxy); Wed, 20 Apr 2016 20:35:57 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=VuVFYvWQDHs8njQqq0NbhUhgFvU=; b=Q42mUp TplVR8Uvn/nBemtr7DKQH4wavrQIsOX/hZuSX+lpdAI9AZq3J0SO0xFwmFLCV3IM YxlKg2D1mrUSKCCr3BxbylCyW/dQ8Dq6dElFq9aGsnzBvoxMsJSIcwuUW2GdUbKW 7QSmM6+r4sAgAGjuctxlc9JLCmfwNAJkjMsrk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=VuVFYvWQDHs8njQ qq0NbhUhgFvU=; b=H2elHiwGuD4/W4HnqGVggigGBkZL0U2/t/MZBqP3wa3c55+ aiuUM+5a0mgWmIahlFEOO3kM9OvLkbjE19vCe9yR9PjbuqAL/OeytdIp1xybllMt kIznsl99dXcbbqLkiQjjtNsDASp6jnEcAfM/ushprHPUpArvsH29xaBSD6Dk= X-Sasl-enc: NbFpN++Eu6sGomhUEBXQwuo0qDmyVzRiX2cY8XF6mkzS 1461198957 Received: from [192.168.1.2] (174-24-160-10.tukw.qwest.net [174.24.160.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 62F4E68016B; Wed, 20 Apr 2016 20:35:57 -0400 (EDT) Subject: Re: RLS policy dump/restore failure due to elided type-casts To: Karl Czajkowski , pgsql-general@postgresql.org References: <20160421001832.GB7976@moraine.isi.edu> From: Adrian Klaver Message-ID: <5718206C.5070101@aklaver.com> Date: Wed, 20 Apr 2016 17:35:56 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <20160421001832.GB7976@moraine.isi.edu> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org On 04/20/2016 05:18 PM, Karl Czajkowski wrote: > Our row level policies require very explicit casts in order to be > accepted by the DB, but those casts are discarded in the dumped policy > statements. Thus, an attempt to reload the dump file fails. I'm not > sure if the underlying problem is that the cast shouldn't be required > in the first place, or if the normalization applied to the policy > expression is just incorrect. > > Below is a trivialized example that shows the problem while removing > complexity found in our real policies. We are doing this to implement > differentiated policies in a web application, where the web server > stores authenticated web client context into session parameters and > our policies check against those attributes rather than DB roles. > > To work around this, we have to maintain our policies in a separate > SQL file, manually edit the dumps, and reapply our working policies. > This is obviously undesirable in the long run, where dumps taken as > periodic backups are not directly usable to reconstruct the DB... > > > Karl > > > Create a test database with these contents: > > CREATE FUNCTION current_attributes() RETURNS text[] > STABLE AS $$ > BEGIN > RETURN ( > SELECT array_agg(value) > FROM json_array_elements_text(current_setting('ourapp.attributes')::json) > ); > EXCEPTION WHEN OTHERS THEN > RETURN NULL::text[]; > END; > $$ LANGUAGE plpgsql; > > CREATE TABLE stuff ( > value text PRIMARY KEY > ); > > CREATE POLICY delete_stuff ON stuff > FOR DELETE USING ('example attribute value' = ANY ( ((SELECT current_attributes()))::text[] )); Trying to figure out how this works in the case above. Looks to me like you are comparing a text value to a text[]. Also why the the cast to text[], when the return value from the function is text[]? > > > The output of pg_dump (and similarly the '\d' command in psql) drops > the cast: > > CREATE POLICY delete_stuff ON stuff > FOR DELETE TO PUBLIC USING (('example attribute value'::text = ANY (( SELECT current_attributes() AS current_attributes)))); > > > And this causes an error when executing the dump file on a new database: > > ERROR: operator does not exist: text = text[] > HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. > > > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general