From 8aaa7bd7134ce22acaecdec91aa578e82ba3702c Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Thu, 3 Dec 2026 21:08:12 -0500
Subject: [PATCH 1/2] Don't trust a generic plan built while invalidations
 arrived.

Invalidations for planner-added dependencies were lost during planning.

Reported-by: Xinlei Zhang <zxlmgsps2@gmail.com>
Discussion: https://postgr.es/m/CAL9_+FHP-FaVfqZUVjBWV29zSXx3EraOGFQYkYAo04jR7u9=bw@mail.gmail.com
---
 src/backend/utils/cache/plancache.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c
index a1b406cee29..fffc68cfc1f 100644
--- a/src/backend/utils/cache/plancache.c
+++ b/src/backend/utils/cache/plancache.c
@@ -68,6 +68,7 @@
 #include "parser/analyze.h"
 #include "rewrite/rewriteHandler.h"
 #include "storage/lmgr.h"
+#include "storage/sinval.h"
 #include "tcop/pquery.h"
 #include "tcop/utility.h"
 #include "utils/acl.h"
@@ -1049,6 +1050,7 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
 	List	   *plist;
 	bool		snapshot_set;
 	bool		is_transient;
+	uint64		inval_count;
 	MemoryContext plan_context;
 	MemoryContext oldcxt = CurrentMemoryContext;
 	ListCell   *lc;
@@ -1095,8 +1097,11 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
 	}
 
 	/*
-	 * Generate the plan.
+	 * Generate the plan.  Remember the sinval message count first, so we can
+	 * tell below whether any invalidation messages were processed while
+	 * planning.
 	 */
+	inval_count = SharedInvalidMessageCounter;
 	plist = pg_plan_queries(qlist, plansource->query_string,
 							plansource->cursor_options, boundParams);
 
@@ -1165,7 +1170,15 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
 	plan->context = plan_context;
 	plan->is_oneshot = plansource->is_oneshot;
 	plan->is_saved = false;
-	plan->is_valid = true;
+
+	/*
+	 * If invalidation messages were processed while planning, the callbacks
+	 * could not check this plan's planner-added dependencies, since it is not
+	 * linked into the plansource yet.  We can't tell whether a message
+	 * mattered, so mark the plan invalid.  The caller may still use it once,
+	 * but the next GetCachedPlan call will replan.
+	 */
+	plan->is_valid = (inval_count == SharedInvalidMessageCounter);
 
 	/* assign generation number to new plan */
 	plan->generation = ++(plansource->generation);
@@ -1494,11 +1507,18 @@ CachedPlanAllowsSimpleValidityCheck(CachedPlanSource *plansource,
 	 */
 	Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
 	Assert(plan->magic == CACHEDPLAN_MAGIC);
-	Assert(plan->is_valid);
 	Assert(plan == plansource->gplan);
 	Assert(plansource->search_path != NULL);
 	Assert(SearchPathMatchesCurrentEnvironment(plansource->search_path));
 
+	/*
+	 * BuildCachedPlan can return a plan that is already marked invalid, if
+	 * invalidation messages arrived while it was planning.  Such a plan must
+	 * not be re-used, so decline.
+	 */
+	if (!plan->is_valid)
+		return false;
+
 	/* We don't support oneshot plans here. */
 	if (plansource->is_oneshot)
 		return false;
-- 
2.37.1 (Apple Git-137.1)

