SUMMARY: use pg_plan_queries to generate a query plan on first get thereafter get it from cache.
pg_plan_queries
code path,
SPI_plan_get_cached_plan // step in GetCachedPlan BuildCachedPlan pg_plan_queries