Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lyysN-0004dC-9q for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Jul 2021 15:45:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lyysL-0003EW-VC for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Jul 2021 15:45:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lyysL-0003EL-NE for pgsql-hackers@lists.postgresql.org; Thu, 01 Jul 2021 15:45:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lyysE-0000eM-6C for pgsql-hackers@lists.postgresql.org; Thu, 01 Jul 2021 15:45:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 161Fj3Sc697680; Thu, 1 Jul 2021 11:45:03 -0400 From: Tom Lane To: Amit Langote cc: PostgreSQL Hackers Subject: Re: ExecRTCheckPerms() and many prunable partitions In-reply-to: References: Comments: In-reply-to Amit Langote message dated "Wed, 30 Jun 2021 22:33:44 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <697678.1625154303.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 01 Jul 2021 11:45:03 -0400 Message-ID: <697679.1625154303@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Amit Langote writes: > The problem is that it loops over the entire range table even though > only one or handful of those entries actually need their permissions > checked. Most entries, especially those of partition child tables > have their requiredPerms set to 0, which David pointed out to me in > [2], so what ExecCheckRTPerms() does in their case is pure overhead. > An idea to fix that is to store the RT indexes of the entries that > have non-0 requiredPerms into a separate list or a bitmapset in > PlannedStmt. I think perhaps we ought to be more ambitious than that, and consider separating the list of permissions-to-check from the rtable entirely. Your patch hardly qualifies as non-invasive, plus it seems to invite errors of omission, while if we changed the data structure altogether then the compiler would help find any not-updated code. But the main reason that this strikes me as possibly a good idea is that I was just taking another look at the complaint in [1], where I wrote >> I think it's impossible to avoid less-than-O(N^2) growth on this sort >> of case. For example, the v2 subquery initially has RTEs for v2 itself >> plus v1. When we flatten v1 into v2, v2 acquires the RTEs from v1, >> namely v1 itself plus foo. Similarly, once vK-1 is pulled up into vK, >> there are going to be order-of-K entries in vK's rtable, and that stack= ing >> makes for O(N^2) work overall just in manipulating the rtable. >> = >> We can't get rid of these rtable entries altogether, since all of them >> represent table privilege checks that the executor will need to do. Perhaps, if we separated the rtable from the required-permissions data structure, then we could avoid pulling up otherwise-useless RTEs when flattening a view (or even better, not make the extra RTEs in the first place??), and thus possibly avoid that exponential planning-time growth for nested views. Or maybe not. But I think we should take a hard look at whether separating these data structures could solve both of these problems at once. regards, tom lane [1] https://www.postgresql.org/message-id/flat/797aff54-b49b-4914-9ff9-aa4= 2564a4d7d%40www.fastmail.com