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 1l1ZYC-0006XD-RD for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Jan 2021 18:46:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l1ZYB-00069F-Ph for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Jan 2021 18:46:51 +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 1l1ZYB-000697-Io for pgsql-hackers@lists.postgresql.org; Mon, 18 Jan 2021 18:46:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l1ZY9-0001ZI-I7 for pgsql-hackers@postgresql.org; Mon, 18 Jan 2021 18:46:50 +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 10IIkmUu347101; Mon, 18 Jan 2021 13:46:48 -0500 From: Tom Lane To: David Geier cc: pgsql-hackers@postgresql.org Subject: Re: search_plan_tree(): handling of non-leaf CustomScanState nodes causes segfault In-reply-to: <0253344d-9bdd-11c4-7f0d-d88c02cd7991@swarm64.com> References: <0253344d-9bdd-11c4-7f0d-d88c02cd7991@swarm64.com> Comments: In-reply-to David Geier message dated "Mon, 18 Jan 2021 11:43:30 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <347099.1610995608.1@sss.pgh.pa.us> Date: Mon, 18 Jan 2021 13:46:48 -0500 Message-ID: <347100.1610995608@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Geier writes: > search_plan_tree() assumes that > CustomScanState::ScanState::ss_currentRelation is never NULL. In my > understanding that only holds for CustomScanState nodes which are at the > bottom of the plan and actually read from a relation. CustomScanState > nodes which are not at the bottom don't have ss_currentRelation set. I > believe for such nodes, instead search_plan_tree() should recurse into > CustomScanState::custom_ps. Hm. I agree that we shouldn't simply assume that ss_currentRelation isn't null. However, we cannot make search_plan_tree() descend through non-leaf CustomScan nodes, because we don't know what processing is involved there. We need to find a scan that is guaranteed to return rows that are one-to-one with the cursor output. This is why the function doesn't descend through join or aggregation nodes, and I see no argument by which we should assume we know more about what a customscan node will do than we know about those. So I'm inclined to think a suitable fix is just - if (RelationGetRelid(sstate->ss_currentRelation) == table_oid) + if (sstate->ss_currentRelation && + RelationGetRelid(sstate->ss_currentRelation) == table_oid) result = sstate; regards, tom lane