From 81776fe1900d6d6a25524b651f2e792f3a3a403e Mon Sep 17 00:00:00 2001 From: Colm McHugh Date: Wed, 24 Sep 2025 08:20:54 +0000 Subject: [PATCH] Fix crash in Range Table identity check. The range table entry array created by the Postgres planner for each SELECT in a query may have NULL entries as of PG18. Add a NULL check to skip over these when looking for matches in rte identities. --- .../planner/relation_restriction_equivalence.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/planner/relation_restriction_equivalence.c b/src/backend/distributed/planner/relation_restriction_equivalence.c index 94c99ef20..c657c7c03 100644 --- a/src/backend/distributed/planner/relation_restriction_equivalence.c +++ b/src/backend/distributed/planner/relation_restriction_equivalence.c @@ -2431,7 +2431,7 @@ FilterJoinRestrictionContext(JoinRestrictionContext *joinRestrictionContext, Rel /* * RangeTableArrayContainsAnyRTEIdentities returns true if any of the range table entries - * int rangeTableEntries array is an range table relation specified in queryRteIdentities. + * in rangeTableEntries array is a range table relation specified in queryRteIdentities. */ static bool RangeTableArrayContainsAnyRTEIdentities(RangeTblEntry **rangeTableEntries, int @@ -2444,6 +2444,18 @@ RangeTableArrayContainsAnyRTEIdentities(RangeTblEntry **rangeTableEntries, int List *rangeTableRelationList = NULL; ListCell *rteRelationCell = NULL; +#if PG_VERSION_NUM >= PG_VERSION_18 + + /* + * In PG18+, planner array simple_rte_array may contain NULL entries + * for "dead relations". See PG commits 5f6f951 and e9a20e4 for details. + */ + if (rangeTableEntry == NULL) + { + continue; + } +#endif + /* * Get list of all RTE_RELATIONs in the given range table entry * (i.e.,rangeTableEntry could be a subquery where we're interested