Skip to content

Commit 4856299

Browse files
committed
fix: use extract_actual_clauses to properly handle RestrictInfo nodes
1 parent 315adba commit 4856299

File tree

2 files changed

+86
-32
lines changed

2 files changed

+86
-32
lines changed

fdw/logging.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ char* tagTypeToString(NodeTag type)
5757
"T_JoinExpr",
5858
"T_FromExpr",
5959
"T_OnConflictExpr",
60-
"T_IntoClause"
60+
"T_IntoClause",
61+
"T_RestrictInfo" /* Add RestrictInfo for PostgreSQL v16+ compatibility */
6162
};
6263
int idx = (int)type - (int)T_Alias;
6364
if (idx < sizeof(tagNames) / sizeof(tagNames[0])){
6465
return tagNames[idx];
6566
}
66-
return "";
6767

68-
}
68+
/* DEBUG: Log unknown node types for PostgreSQL v16+ compatibility debugging */
69+
static char unknown_type_buf[64];
70+
snprintf(unknown_type_buf, sizeof(unknown_type_buf), "T_Unknown_%d", (int)type);
71+
return unknown_type_buf;
72+
73+
}

fdw/query.c

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ List *clausesInvolvingAttr(Index relid, AttrNumber attnum,
4141

4242
Expr *fdw_get_em_expr(EquivalenceClass *ec, RelOptInfo *rel);
4343

44+
4445
/*
4546
* The list of needed columns (represented by their respective vars)
4647
* is pulled from:
@@ -59,30 +60,61 @@ extractColumns(List *reltargetlist, List *restrictinfolist)
5960
List *targetcolumns;
6061
Node *node = (Node *)lfirst(lc);
6162

62-
targetcolumns = pull_var_clause(node,
63+
elog(DEBUG1, "DEBUG: Processing node in target list (nodeTag=%d)", nodeTag(node));
64+
65+
/* Check if this is a RestrictInfo node and handle it properly */
66+
if (IsA(node, RestrictInfo))
67+
{
68+
List *actual_clauses = extract_actual_clauses(list_make1(node), false);
69+
ListCell *clause_lc;
70+
71+
foreach (clause_lc, actual_clauses)
72+
{
73+
Node *clause = (Node *)lfirst(clause_lc);
74+
List *clause_vars = pull_var_clause(clause,
6375
#if PG_VERSION_NUM >= 90600
64-
PVC_RECURSE_AGGREGATES |
65-
PVC_RECURSE_PLACEHOLDERS);
76+
PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS);
6677
#else
67-
PVC_RECURSE_AGGREGATES,
68-
PVC_RECURSE_PLACEHOLDERS);
78+
PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS);
6979
#endif
70-
columns = list_union(columns, targetcolumns);
80+
columns = list_union(columns, clause_vars);
81+
}
82+
}
83+
else
84+
{
85+
/* For non-RestrictInfo nodes, call pull_var_clause directly */
86+
targetcolumns = pull_var_clause(node,
87+
#if PG_VERSION_NUM >= 90600
88+
PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS);
89+
#else
90+
PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS);
91+
#endif
92+
columns = list_union(columns, targetcolumns);
93+
}
7194
i++;
7295
}
73-
foreach (lc, restrictinfolist)
96+
/* Use extract_actual_clauses to properly handle RestrictInfo nodes */
97+
if (restrictinfolist != NIL)
7498
{
75-
List *targetcolumns;
76-
RestrictInfo *node = (RestrictInfo *)lfirst(lc);
77-
targetcolumns = pull_var_clause((Node *)node->clause,
99+
List *actual_clauses = extract_actual_clauses(restrictinfolist, false);
100+
ListCell *clause_lc;
101+
102+
elog(DEBUG1, "DEBUG: Processing %d actual clauses from restrictinfo list", list_length(actual_clauses));
103+
104+
foreach (clause_lc, actual_clauses)
105+
{
106+
List *targetcolumns;
107+
Node *clause = (Node *)lfirst(clause_lc);
108+
109+
elog(DEBUG1, "DEBUG: Processing actual clause (nodeTag=%d)", nodeTag(clause));
110+
targetcolumns = pull_var_clause(clause,
78111
#if PG_VERSION_NUM >= 90600
79-
PVC_RECURSE_AGGREGATES |
80-
PVC_RECURSE_PLACEHOLDERS);
112+
PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS);
81113
#else
82-
PVC_RECURSE_AGGREGATES,
83-
PVC_RECURSE_PLACEHOLDERS);
114+
PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS);
84115
#endif
85-
columns = list_union(columns, targetcolumns);
116+
columns = list_union(columns, targetcolumns);
117+
}
86118
}
87119
return columns;
88120
}
@@ -151,6 +183,10 @@ unnestClause(Node *node)
151183
return (Node *)((RelabelType *)node)->arg;
152184
case T_ArrayCoerceExpr:
153185
return (Node *)((ArrayCoerceExpr *)node)->arg;
186+
#if PG_VERSION_NUM >= 160000
187+
case T_RestrictInfo:
188+
return (Node *)((RestrictInfo *)node)->clause;
189+
#endif
154190
default:
155191
return node;
156192
}
@@ -221,8 +257,9 @@ canonicalOpExpr(OpExpr *opExpr, Relids base_relids)
221257
l = unnestClause(list_nth(opExpr->args, 0));
222258
r = unnestClause(list_nth(opExpr->args, 1));
223259

224-
elog(DEBUG5, "l arg: %s", nodeToString(l));
225-
elog(DEBUG5, "r arg: %s", nodeToString(r));
260+
/* Temporarily disable nodeToString calls to avoid RestrictInfo issues */
261+
elog(DEBUG5, "l arg: [nodeToString disabled for debugging]");
262+
elog(DEBUG5, "r arg: [nodeToString disabled for debugging]");
226263

227264
swapOperandsAsNeeded(&l, &r, &operatorid, base_relids);
228265

@@ -341,23 +378,35 @@ colnameFromVar(Var *var, PlannerInfo *root, FdwPlanState *planstate)
341378
*/
342379
bool isAttrInRestrictInfo(Index relid, AttrNumber attno, RestrictInfo *restrictinfo)
343380
{
344-
List *vars = pull_var_clause((Node *)restrictinfo->clause,
345-
#if PG_VERSION_NUM >= 90600
346-
PVC_RECURSE_AGGREGATES |
347-
PVC_RECURSE_PLACEHOLDERS);
348-
#else
349-
PVC_RECURSE_AGGREGATES,
350-
PVC_RECURSE_PLACEHOLDERS);
351-
#endif
381+
List *vars;
352382
ListCell *lc;
383+
List *actual_clauses;
384+
ListCell *clause_lc;
385+
386+
elog(DEBUG1, "DEBUG: isAttrInRestrictInfo using extract_actual_clauses (clause nodeTag=%d)", nodeTag((Node *)restrictinfo->clause));
387+
388+
/* Use extract_actual_clauses to properly handle RestrictInfo */
389+
actual_clauses = extract_actual_clauses(list_make1(restrictinfo), false);
353390

354-
foreach (lc, vars)
391+
foreach (clause_lc, actual_clauses)
355392
{
356-
Var *var = (Var *)lfirst(lc);
393+
Node *clause = (Node *)lfirst(clause_lc);
357394

358-
if (var->varno == relid && var->varattno == attno)
395+
vars = pull_var_clause(clause,
396+
#if PG_VERSION_NUM >= 90600
397+
PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS);
398+
#else
399+
PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS);
400+
#endif
401+
402+
foreach (lc, vars)
359403
{
360-
return true;
404+
Var *var = (Var *)lfirst(lc);
405+
406+
if (var->varno == relid && var->varattno == attno)
407+
{
408+
return true;
409+
}
361410
}
362411
}
363412
return false;

0 commit comments

Comments
 (0)