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 1mSf7P-0002Od-Rb for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Sep 2021 12:43:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mSf7N-0004Ke-DX for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Sep 2021 12:43:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSf7N-0004KV-5w for pgsql-hackers@lists.postgresql.org; Tue, 21 Sep 2021 12:43:25 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSf7G-00083s-Lo for pgsql-hackers@postgresql.org; Tue, 21 Sep 2021 12:43:24 +0000 Received: from oss.nttdata.com (localhost [127.0.0.1]) by oss.nttdata.com (Postfix) with ESMTPA id 03F5E612D4; Tue, 21 Sep 2021 21:43:15 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.3 at oss.nttdata.com MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 21 Sep 2021 21:43:14 +0900 From: torikoshia To: Tom Lane Cc: Peter Eisentraut , Andrew Dunstan , Pgsql Hackers Subject: Re: EXPLAIN(VERBOSE) to CTE with SEARCH BREADTH FIRST fails In-Reply-To: <1126187.1631749231@sss.pgh.pa.us> References: <5bafa66ad529e11860339565c9e7c166@oss.nttdata.com> <3208199.1631039493@sss.pgh.pa.us> <24ce73f9-c714-9a7c-6cc0-34e724c8f1c9@dunslane.net> <984394.1631719708@sss.pgh.pa.us> <1126187.1631749231@sss.pgh.pa.us> User-Agent: Roundcube Webmail/1.4.11 Message-ID: <7da9d40759bab675e57ab495499720c1@oss.nttdata.com> X-Sender: torikoshia@oss.nttdata.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-09-16 08:40, Tom Lane wrote: > I wrote: >> I do not think that patch is a proper solution, but we do need to do >> something about this. > > I poked into this and decided it's an ancient omission within > ruleutils.c. > The reason we've not seen it before is probably that you can't get to > the > case through the parser. The SEARCH stuff is generating a query > structure > basically equivalent to > > regression=# with recursive cte (x,r) as ( > select 42 as x, row(i, 2.3) as r from generate_series(1,3) i > union all > select x, row((c.r).f1, 4.5) from cte c > ) > select * from cte; > ERROR: record type has not been registered > > and as you can see, expandRecordVariable fails to figure out what > the referent of "c.r" is. I think that could be fixed (by looking > into the non-recursive term), but given the lack of field demand, > I'm not feeling that it's urgent. > > So the omission is pretty obvious from the misleading comment: > actually, Vars referencing RTE_CTE RTEs can also appear in > WorkTableScan > nodes, and we're not doing anything to support that. But we only reach > this code when trying to resolve a field of a Var of RECORD type, which > is a case that it seems like the parser can't produce. > > It doesn't look too hard to fix: we just have to find the > RecursiveUnion > that goes with the WorkTableScan, and drill down into that, much as we > would do in the CteScan case. See attached draft patch. I'm too tired > to beat on this heavily or add a test case, but I have verified that it > passes check-world and handles the example presented in this thread. > > regards, tom lane Thanks for looking into this and fixing it! -- Regards, -- Atsushi Torikoshi NTT DATA CORPORATION