@@ -41,6 +41,7 @@ List *clausesInvolvingAttr(Index relid, AttrNumber attnum,
4141
4242Expr * 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 }
@@ -341,23 +377,35 @@ colnameFromVar(Var *var, PlannerInfo *root, FdwPlanState *planstate)
341377 */
342378bool isAttrInRestrictInfo (Index relid , AttrNumber attno , RestrictInfo * restrictinfo )
343379{
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
380+ List * vars ;
352381 ListCell * lc ;
382+ List * actual_clauses ;
383+ ListCell * clause_lc ;
384+
385+ elog (DEBUG1 , "DEBUG: isAttrInRestrictInfo using extract_actual_clauses (clause nodeTag=%d)" , nodeTag ((Node * )restrictinfo -> clause ));
386+
387+ /* Use extract_actual_clauses to properly handle RestrictInfo */
388+ actual_clauses = extract_actual_clauses (list_make1 (restrictinfo ), false);
353389
354- foreach (lc , vars )
390+ foreach (clause_lc , actual_clauses )
355391 {
356- Var * var = (Var * )lfirst (lc );
392+ Node * clause = (Node * )lfirst (clause_lc );
357393
358- if (var -> varno == relid && var -> varattno == attno )
394+ vars = pull_var_clause (clause ,
395+ #if PG_VERSION_NUM >= 90600
396+ PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS );
397+ #else
398+ PVC_RECURSE_AGGREGATES , PVC_RECURSE_PLACEHOLDERS );
399+ #endif
400+
401+ foreach (lc , vars )
359402 {
360- return true;
403+ Var * var = (Var * )lfirst (lc );
404+
405+ if (var -> varno == relid && var -> varattno == attno )
406+ {
407+ return true;
408+ }
361409 }
362410 }
363411 return false;
0 commit comments