From cd017ee69ce44a88d13728cf8bdda0307daa139d Mon Sep 17 00:00:00 2001 From: Mehmet Yilmaz Date: Thu, 8 May 2025 15:46:04 +0000 Subject: [PATCH] =?UTF-8?q?PG18=20-=20Handle=20PG18=E2=80=99s=20synthetic?= =?UTF-8?q?=20RTE=5FGROUP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add ExprReferencesPartitionColumn function to check partition column references in queries Add ExprMentionsPartitionColumn function to identify partition column references in queries Revert "Add ExprReferencesPartitionColumn function to check partition column references in queries" This reverts commit c507801c54acba596de14125f9d6451bb0c99624. indent Enhance partition column handling in TargetListOnPartitionColumn for PostgreSQL 18 compatibility Add support for synthetic GROUP RTE in FindReferencedTableColumn for PostgreSQL 18 Remove ExprMentionsPartitionColumn function for partition column reference checks Remove unnecessary blank line in InsertPartitionColumnMatchesSelect function Remove ExprMentionsPartitionColumn function and simplify partition column checks for PostgreSQL 18 compatibility add RTE_GROUP to the non-complex whitelist for PG 18. A synthetic RTE_GROUP is harmless; treating it as “complex” flips the flag and prevents a router plan. PG 18’s synthetic RTE_GROUP really is “another” RTE kind that you must treat just like the RTE_SUBQUERY and RTE_JOIN cases if you want your composite‐field logic to see through it. add an explicit RTE_GROUP clause that does nothing (or falls through like VALUES/RESULT) for PG 18 Without it, an UPDATE … FROM (SELECT … GROUP BY …) on PG 18 will raise “Unrecognized range table entry.” Update version check for RTE_GROUP to use PG_VERSION_18 constant some reverts Remove citus-tools subproject reference --- .../planner/multi_logical_optimizer.c | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/backend/distributed/planner/multi_logical_optimizer.c b/src/backend/distributed/planner/multi_logical_optimizer.c index c4c11f4eb..1fecb04c8 100644 --- a/src/backend/distributed/planner/multi_logical_optimizer.c +++ b/src/backend/distributed/planner/multi_logical_optimizer.c @@ -4557,6 +4557,31 @@ FindReferencedTableColumn(Expr *columnExpression, List *parentQueryList, Query * FindReferencedTableColumn(joinColumn, parentQueryList, query, column, rteContainingReferencedColumn, skipOuterVars); } +#if PG_VERSION_NUM >= PG_VERSION_18 + else if (rangeTableEntry->rtekind == RTE_GROUP) + { + /* + * PG 18: synthetic GROUP RTE. Each groupexprs item corresponds to the + * columns produced by the grouping step, in the *same ordinal order* as + * the Vars that reference them. + */ + List *groupexprs = rangeTableEntry->groupexprs; + AttrNumber groupIndex = candidateColumn->varattno - 1; + + /* Safety check */ + if (groupIndex < 0 || groupIndex >= list_length(groupexprs)) + { + return; /* malformed Var */ + } + Expr *groupExpr = (Expr *) list_nth(groupexprs, groupIndex); + + /* Recurse on the underlying expression (stay in the same query) */ + FindReferencedTableColumn(groupExpr, parentQueryList, query, + column, rteContainingReferencedColumn, + skipOuterVars); + } +#endif /* PG_VERSION_NUM >= 180000 */ + else if (rangeTableEntry->rtekind == RTE_CTE) { /*