+
+/*****************************************************************************
+ * QUERY DEPENDENCY MANAGEMENT
+ *****************************************************************************/
+
+/*
+ * record_plan_function_dependency
+ * Mark the current plan as depending on a particular function.
+ *
+ * This is exported so that the function-inlining code can record a
+ * dependency on a function that it's removed from the plan tree.
+ */
+void
+record_plan_function_dependency(PlannerGlobal *glob, Oid funcid)
+{
+ /*
+ * For performance reasons, we don't bother to track built-in functions;
+ * we just assume they'll never change (or at least not in ways that'd
+ * invalidate plans using them). For this purpose we can consider a
+ * built-in function to be one with OID less than FirstBootstrapObjectId.
+ * Note that the OID generator guarantees never to generate such an OID
+ * after startup, even at OID wraparound.
+ */
+ if (funcid >= (Oid) FirstBootstrapObjectId)
+ {
+ HeapTuple func_tuple;
+ PlanInvalItem *inval_item;
+
+ func_tuple = SearchSysCache(PROCOID,
+ ObjectIdGetDatum(funcid),
+ 0, 0, 0);
+ if (!HeapTupleIsValid(func_tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcid);
+
+ inval_item = makeNode(PlanInvalItem);
+
+ /*
+ * It would work to use any syscache on pg_proc, but plancache.c
+ * expects us to use PROCOID.
+ */
+ inval_item->cacheId = PROCOID;
+ inval_item->tupleId = func_tuple->t_self;
+
+ glob->invalItems = lappend(glob->invalItems, inval_item);
+
+ ReleaseSysCache(func_tuple);
+ }
+}
+
+/*
+ * extract_query_dependencies
+ * Given a not-yet-planned query or queries (i.e. a Query node or list
+ * of Query nodes), extract dependencies just as set_plan_references
+ * would do.
+ *
+ * This is needed by plancache.c to handle invalidation of cached unplanned
+ * queries.
+ */
+void
+extract_query_dependencies(Node *query,
+ List **relationOids,
+ List **invalItems)
+{
+ PlannerGlobal glob;
+
+ /* Make up a dummy PlannerGlobal so we can use this module's machinery */
+ MemSet(&glob, 0, sizeof(glob));
+ glob.type = T_PlannerGlobal;
+ glob.relationOids = NIL;
+ glob.invalItems = NIL;
+
+ (void) extract_query_dependencies_walker(query, &glob);
+
+ *relationOids = glob.relationOids;
+ *invalItems = glob.invalItems;
+}
+
+static bool
+extract_query_dependencies_walker(Node *node, PlannerGlobal *context)
+{
+ if (node == NULL)
+ return false;
+ Assert(!IsA(node, PlaceHolderVar));
+ /* Extract function dependencies and check for regclass Consts */
+ fix_expr_common(context, node);
+ if (IsA(node, Query))
+ {
+ Query *query = (Query *) node;
+ ListCell *lc;
+
+ if (query->commandType == CMD_UTILITY)
+ {
+ /* Ignore utility statements, except EXPLAIN */
+ if (IsA(query->utilityStmt, ExplainStmt))
+ {
+ query = (Query *) ((ExplainStmt *) query->utilityStmt)->query;
+ Assert(IsA(query, Query));
+ Assert(query->commandType != CMD_UTILITY);
+ }
+ else
+ return false;
+ }
+
+ /* Collect relation OIDs in this Query's rtable */
+ foreach(lc, query->rtable)
+ {
+ RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
+
+ if (rte->rtekind == RTE_RELATION)
+ context->relationOids = lappend_oid(context->relationOids,
+ rte->relid);
+ }
+
+ /* And recurse into the query's subexpressions */
+ return query_tree_walker(query, extract_query_dependencies_walker,
+ (void *) context, 0);
+ }
+ return expression_tree_walker(node, extract_query_dependencies_walker,
+ (void *) context);
+}