|
20 | 20 | #include <math.h>
|
21 | 21 |
|
22 | 22 | #include "access/skey.h"
|
| 23 | +#include "access/sysattr.h" |
23 | 24 | #include "foreign/fdwapi.h"
|
24 | 25 | #include "miscadmin.h"
|
25 | 26 | #include "nodes/makefuncs.h"
|
@@ -1881,6 +1882,8 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
|
1881 | 1882 | RelOptInfo *rel = best_path->path.parent;
|
1882 | 1883 | Index scan_relid = rel->relid;
|
1883 | 1884 | RangeTblEntry *rte;
|
| 1885 | + Bitmapset *attrs_used = NULL; |
| 1886 | + ListCell *lc; |
1884 | 1887 | int i;
|
1885 | 1888 |
|
1886 | 1889 | /* it should be a base rel... */
|
@@ -1928,17 +1931,34 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
|
1928 | 1931 | * Detect whether any system columns are requested from rel. This is a
|
1929 | 1932 | * bit of a kluge and might go away someday, so we intentionally leave it
|
1930 | 1933 | * out of the API presented to FDWs.
|
| 1934 | + * |
| 1935 | + * First, examine all the attributes needed for joins or final output. |
| 1936 | + * Note: we must look at reltargetlist, not the attr_needed data, because |
| 1937 | + * attr_needed isn't computed for inheritance child rels. |
1931 | 1938 | */
|
| 1939 | + pull_varattnos((Node *) rel->reltargetlist, rel->relid, &attrs_used); |
| 1940 | + |
| 1941 | + /* Add all the attributes used by restriction clauses. */ |
| 1942 | + foreach(lc, rel->baserestrictinfo) |
| 1943 | + { |
| 1944 | + RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc); |
| 1945 | + |
| 1946 | + pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used); |
| 1947 | + } |
| 1948 | + |
| 1949 | + /* Now, are any system columns requested from rel? */ |
1932 | 1950 | scan_plan->fsSystemCol = false;
|
1933 |
| - for (i = rel->min_attr; i < 0; i++) |
| 1951 | + for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++) |
1934 | 1952 | {
|
1935 |
| - if (!bms_is_empty(rel->attr_needed[i - rel->min_attr])) |
| 1953 | + if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used)) |
1936 | 1954 | {
|
1937 | 1955 | scan_plan->fsSystemCol = true;
|
1938 | 1956 | break;
|
1939 | 1957 | }
|
1940 | 1958 | }
|
1941 | 1959 |
|
| 1960 | + bms_free(attrs_used); |
| 1961 | + |
1942 | 1962 | return scan_plan;
|
1943 | 1963 | }
|
1944 | 1964 |
|
|
0 commit comments