Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1faH0F-0005zd-0S for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Jul 2018 08:49:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1faH0D-000674-F4 for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Jul 2018 08:49:37 +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.89) (envelope-from ) id 1faH0D-00066x-4g for pgsql-hackers@lists.postgresql.org; Tue, 03 Jul 2018 08:49:37 +0000 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1faH09-00038G-0l for pgsql-hackers@postgresql.org; Tue, 03 Jul 2018 08:49:35 +0000 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w638nMVb027313; Tue, 3 Jul 2018 17:49:22 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 92515638F36; Tue, 3 Jul 2018 17:49:22 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 7C70C638B5E; Tue, 3 Jul 2018 17:49:22 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id 703CE400244; Tue, 3 Jul 2018 17:49:22 +0900 (JST) Subject: Re: pgsql: Clarify use of temporary tables within partition trees From: Amit Langote References: <20180702221650.GA2159@paquier.xyz> <20180703045502.GA26236@paquier.xyz> <20180703061119.GA11732@paquier.xyz> <5db5fe6e-4a73-6a24-09e4-060b9f132ea9@lab.ntt.co.jp> <20180703070551.GC11732@paquier.xyz> Message-ID: <528b14b6-3e08-c8c0-16bc-a4366dbfaf1e@lab.ntt.co.jp> Date: Tue, 3 Jul 2018 17:49:11 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CC-Mail-RelayStamp: 1 To: Michael Paquier Cc: David Rowley , Robert Haas , Ashutosh Bapat , Pg Hackers X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2018/07/03 17:31, Amit Langote wrote: > On 2018/07/03 16:05, Michael Paquier wrote: >> On Tue, Jul 03, 2018 at 03:49:44PM +0900, Amit Langote wrote: >>> I forgot that expand_partitioned_rtentry() will recursively call itself if >>> a partition is itself a partitioned table, in which case the above >>> code helps. >> >> Actually look at the coverage reports: >> https://coverage.postgresql.org/src/backend/optimizer/prep/prepunion.c.gcov.html >> 1742 : /* >> 1743 : * If the partitioned table has no partitions or all the partitions are >> 1744 : * temporary tables from other backends, treat this as non-inheritance >> 1745 : * case. >> 1746 : */ >> 1747 4920 : if (!has_child) >> 1748 0 : parentrte->inh = false; >> 1749 4920 : } >> >> expand_partitioned_rtentry() never disables this flag on recursive calls >> with a multi-level tree. Could it be possible to get a test which >> closes the gap? > > I guess that it will be hard as you mentioned before on the thread that > led to this commit. We can't write regression tests which require using > temporary partitions from other sessions. > > Anyway, I just wanted to say that I was wrong when I said that the block > added by the patch is unreachable. It *is* reachable for multi-level > partitioning. For example, it will execute in the following case: > > create table p (a int, b int) partition by list (a); > create table pd partition of p default partition by list (b); > select * from p; > > expand_partitioned_rtentry will get called twice and the newly added code > would result in early return from the function in the 2nd invocation which > is for 'pd'. > > But, > > 1. I still insist that it's better for the newly added code to be near the > top of the function body than in the middle, which brings me to... > > 2. While we're at fixing the code around here, I think we should think > about trying to get rid of the *non-dead* code that produces a structure > that isn't used anywhere, which I was under the impression, 0a480502b09 > [1] already did (cc'ing Ashutosh). To clarify, we still unnecessarily > create a "duplicate" RTE for partitioned tables in a partition tree > (non-leaf tables) in its role as a child. So, for the above query, there > end up being created 4 entries in the query's range table (2 for 'p' and 2 > for 'pd'). That makes sense for plain inheritance, because even the root > parent table in a plain inheritance tree is a regular table containing > data. That's not true for partition inheritance trees, where non-leaf > tables contain no data, so we don't create a plan to scan them (see > d3cc37f1d80 [2]), which in turn means we don't need to create the > redundant "duplicate" child RTEs for them either. > > See attached my delta patch to address both 1 and 2. > > Thanks, > Amit > > [1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=0a480502b09 > > [2] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=d3cc37f1d80 For some reason, ML address got removed from the list of address when sending the above message. Thanks, Amit