Re: Get relid for a relation

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Amin <amin(dot)fallahi(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Get relid for a relation
Date: 2023-01-13 14:45:26
Message-ID: 1674576.1673621126@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Amin <amin(dot)fallahi(at)gmail(dot)com> 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Xing Guo 2023-01-13 15:00:14 Re: PL/Python: Fix return in the middle of PG_TRY() block.
Previous Message Zhang Mingli 2023-01-13 14:18:09 Re: [feature]COPY FROM enable FORCE_NULL/FORCE_NOT_NULL on all columns