Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dKJ9T-0004vZ-EA for pgsql-committers@arkaria.postgresql.org; Mon, 12 Jun 2017 06:48:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dKJ9T-0005Qc-0i for pgsql-committers@arkaria.postgresql.org; Mon, 12 Jun 2017 06:48:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dKJ9S-0005Lp-2o for pgsql-committers@postgresql.org; Mon, 12 Jun 2017 06:48:38 +0000 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dKJ9O-0002KA-OC for pgsql-committers@postgresql.org; Mon, 12 Jun 2017 06:48:36 +0000 Received: from vc1.ecl.ntt.co.jp (vc1.ecl.ntt.co.jp [129.60.86.153]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id v5C6mVVj026687; Mon, 12 Jun 2017 15:48:31 +0900 Received: from vc1.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 2A4985F680; Mon, 12 Jun 2017 15:48:31 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 1BCFD5F65B; Mon, 12 Jun 2017 15:48:31 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id E28C6400A84; Mon, 12 Jun 2017 15:48:30 +0900 (JST) Subject: Re: pgsql: Apply RLS policies to partitioned tables. References: <10974.1497227727@sss.pgh.pa.us> <0c5b8b18-262d-de09-50e1-6cf4815d74ab@joeconway.com> <25990.1497235885@sss.pgh.pa.us> From: Amit Langote Message-ID: <89e6ae66-96ee-b6eb-7195-d88dbb988f2f@lab.ntt.co.jp> Date: Mon, 12 Jun 2017 15:48:09 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <25990.1497235885@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------451EA74BFB0A7F53EDD1DBCA" Content-Language: en-US To: Tom Lane Cc: Joe Conway , pgsql-committers@postgresql.org X-TM-AS-MML: disable List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-committers Precedence: bulk Sender: pgsql-committers-owner@postgresql.org This is a multi-part message in MIME format. --------------451EA74BFB0A7F53EDD1DBCA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 2017/06/12 11:51, Tom Lane wrote: > Amit Langote writes: >> On 2017/06/12 11:09, Joe Conway wrote: >>> On 06/11/2017 05:35 PM, Tom Lane wrote: >>>> First guess is that map_partition_varattnos has forgotten to handle >>>> WithCheckOption.qual. > >>> Drat. I'll take a look, but it would probably be good if someone >>> generally familiar with the partitioned tables patches have a look as well. > >> I am looking too. Commit 587cda35ca3 which added that code did add a test >> in updatable_views.sql, but tests added by this commit have perhaps >> exposed something not previously covered. > > My first guess above is wrong -- the undefined reference is actually > "mtstate->mt_plans[i]". This is because mtstate->mt_num_partitions is > more than mtstate->mt_nplans in the failing case, so that the blithe > assumption that we can use the partition number to index into > mtstate->mt_plans is certainly wrong. Yes, the code should never access mtstate->mt_plans[i] for i > 0, because in this case (the INSERT case), there is only one plan. Unlike UPDATE/DELETE cases, we don't build plans for every partition in the INSERT case. > Don't know how this should > be corrected, offhand, but do we really have more than one plan > for a partitioned query? There can't be, as mentioned, in the INSERT case. How about something like the attached? The patch makes mtstate->mt_plans[0] to be passed for all invocations of ExecInitQual() on WithCheckOption.qual that is being initialized for each partition. Note that we are passing a PlanState that's based on the root parent table's properties to initialize WITH CHECK OPTION quals to be checked against rows that will be inserted into partitions (that is, after tuple-routing). I tried to come up with a test that would cause the existing (unpatched) code to crash, but couldn't. How would one define a policy or WITH CHECK OPTIONS view or something else, such that ExecInitQual() called on WithCheckOptions.qual would try to access parent planstate and would crash once parent becomes an invalid pointer (mtstate->mt_plans[i] for i > 0)? After looking at the code downstream to ExecInitQual(), it seems that the following cases would cases would cause parent planstate to be accessed: * Qual references a whole row var * Qual references an Aggref or a GroupingFunc or a WindowFunc * Qual references a (Alternative) SubPlan Thanks, Amit --------------451EA74BFB0A7F53EDD1DBCA Content-Type: text/plain; charset=UTF-8; name="0001-Don-t-pass-invalid-PlanState-pointer-to-ExecInitQual.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename*0="0001-Don-t-pass-invalid-PlanState-pointer-to-ExecInitQual.pa"; filename*1="tch" RnJvbSA5MGZkYWQyN2MwNGRiNzBmNzhjMjRlNDk5OWNkM2FlZjdjMDZkMWY3 IE1vbiBTZXAgMTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBhbWl0IDxhbWl0bGFu Z290ZTA5QGdtYWlsLmNvbT4KRGF0ZTogTW9uLCAxMiBKdW4gMjAxNyAxNDoz NTowNSArMDkwMApTdWJqZWN0OiBbUEFUQ0hdIERvbid0IHBhc3MgaW52YWxp ZCBQbGFuU3RhdGUgcG9pbnRlciB0byBFeGVjSW5pdFF1YWwKCkN1cnJlbnQg Y29kZSBpbiBFeGVjSW5pdE1vZGlmeVRhYmxlIHRoYXQgaW5pdGlhbGl6ZXMK V2l0aENoZWNrT3B0aW9uLnF1YWwgZmFpbGVkIHRvIGNvbnNpZGVyIHRoYXQg dGhlcmUgKmlzbid0KiBvbmUKcGxhbiBwZXIgcGFydGl0aW9uIGluIHRoZSBN b2RpZnlUYWJsZVN0YXRlLm10X3BsYW5zW10gYXJyYXkuCi0tLQogc3JjL2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kKHdjb0V4cHJzLCB3Y29FeHByKTsKIAkJ CX0KIAotLSAKMi4xMS4wCgo= --------------451EA74BFB0A7F53EDD1DBCA Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-committers --------------451EA74BFB0A7F53EDD1DBCA--