@@ -2099,10 +2099,7 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
20992099 RelOptInfo * rel = best_path -> path .parent ;
21002100 Index scan_relid = rel -> relid ;
21012101 Oid rel_oid = InvalidOid ;
2102- Bitmapset * attrs_used = NULL ;
21032102 Plan * outer_plan = NULL ;
2104- ListCell * lc ;
2105- int i ;
21062103
21072104 Assert (rel -> fdwroutine != NULL );
21082105
@@ -2180,36 +2177,48 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
21802177 }
21812178
21822179 /*
2183- * Detect whether any system columns are requested from rel. This is a
2184- * bit of a kluge and might go away someday, so we intentionally leave it
2185- * out of the API presented to FDWs.
2186- *
2187- * First, examine all the attributes needed for joins or final output.
2188- * Note: we must look at reltargetlist, not the attr_needed data, because
2189- * attr_needed isn't computed for inheritance child rels .
2180+ * If rel is a base relation, detect whether any system columns are
2181+ * requested from the rel. (If rel is a join relation, rel->relid will be
2182+ * 0, but there can be no Var with relid 0 in the reltargetlist or the
2183+ * restriction clauses, so we skip this in that case. Note that any such
2184+ * columns in base relations that were joined are assumed to be contained
2185+ * in fdw_scan_tlist.) This is a bit of a kluge and might go away someday,
2186+ * so we intentionally leave it out of the API presented to FDWs .
21902187 */
2191- pull_varattnos ((Node * ) rel -> reltargetlist , rel -> relid , & attrs_used );
2192-
2193- /* Add all the attributes used by restriction clauses. */
2194- foreach (lc , rel -> baserestrictinfo )
2188+ scan_plan -> fsSystemCol = false;
2189+ if (scan_relid > 0 )
21952190 {
2196- RestrictInfo * rinfo = (RestrictInfo * ) lfirst (lc );
2191+ Bitmapset * attrs_used = NULL ;
2192+ ListCell * lc ;
2193+ int i ;
21972194
2198- pull_varattnos ((Node * ) rinfo -> clause , rel -> relid , & attrs_used );
2199- }
2195+ /*
2196+ * First, examine all the attributes needed for joins or final output.
2197+ * Note: we must look at reltargetlist, not the attr_needed data,
2198+ * because attr_needed isn't computed for inheritance child rels.
2199+ */
2200+ pull_varattnos ((Node * ) rel -> reltargetlist , scan_relid , & attrs_used );
22002201
2201- /* Now, are any system columns requested from rel? */
2202- scan_plan -> fsSystemCol = false;
2203- for (i = FirstLowInvalidHeapAttributeNumber + 1 ; i < 0 ; i ++ )
2204- {
2205- if (bms_is_member (i - FirstLowInvalidHeapAttributeNumber , attrs_used ))
2202+ /* Add all the attributes used by restriction clauses. */
2203+ foreach (lc , rel -> baserestrictinfo )
22062204 {
2207- scan_plan -> fsSystemCol = true;
2208- break ;
2205+ RestrictInfo * rinfo = (RestrictInfo * ) lfirst (lc );
2206+
2207+ pull_varattnos ((Node * ) rinfo -> clause , scan_relid , & attrs_used );
22092208 }
2210- }
22112209
2212- bms_free (attrs_used );
2210+ /* Now, are any system columns requested from rel? */
2211+ for (i = FirstLowInvalidHeapAttributeNumber + 1 ; i < 0 ; i ++ )
2212+ {
2213+ if (bms_is_member (i - FirstLowInvalidHeapAttributeNumber , attrs_used ))
2214+ {
2215+ scan_plan -> fsSystemCol = true;
2216+ break ;
2217+ }
2218+ }
2219+
2220+ bms_free (attrs_used );
2221+ }
22132222
22142223 return scan_plan ;
22152224}
0 commit comments