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 1pGLJL-0004wF-32 for pgsql-hackers@arkaria.postgresql.org; Fri, 13 Jan 2023 14:45:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pGLJI-0007Gv-U7 for pgsql-hackers@arkaria.postgresql.org; Fri, 13 Jan 2023 14:45:36 +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 1pGLJI-0007Gl-Ku for pgsql-hackers@lists.postgresql.org; Fri, 13 Jan 2023 14:45:36 +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 1pGLJC-0001Mi-2C for pgsql-hackers@postgresql.org; Fri, 13 Jan 2023 14:45:35 +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 30DEjQQO1674577; Fri, 13 Jan 2023 09:45:26 -0500 From: Tom Lane To: Amin cc: pgsql-hackers@postgresql.org Subject: Re: Get relid for a relation In-reply-to: References: Comments: In-reply-to Amin message dated "Thu, 12 Jan 2023 17:48:59 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1674575.1673621126.1@sss.pgh.pa.us> Date: Fri, 13 Jan 2023 09:45:26 -0500 Message-ID: <1674576.1673621126@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Amin writes: > In CustomScan cost estimator, where PlannerInfo and RelOptInfo are passed, > I want to get access to the relation stats (for example pg_stat_all_tables) > by calling pg_stat_fetch_stat_tabentry(). However, I don't have access to > relid to pass to this function. Sure you do. The existing code, eg in selfuncs.c, does it about like this: RangeTblEntry *rte = planner_rt_fetch(rel->relid, root); Assert(rte->rtekind == RTE_RELATION); relid = rte->relid; Assert(relid != InvalidOid); ... vardata.statsTuple = SearchSysCache3(STATRELATTINH, ObjectIdGetDatum(relid), Int16GetDatum(colnum), BoolGetDatum(rte->inh)); This is maybe a bit confusing, in that rel->relid is a range table index but rte->relid is an OID. FWIW, I seriously doubt that the numbers kept by the pg_stat mechanisms are what you want for query planning purposes. regards, tom lane