Skip to content

Commit 906599f

Browse files
committed
Fix mishandling of system columns in FDW queries.
postgres_fdw would send query conditions involving system columns to the remote server, even though it makes no effort to ensure that system columns other than CTID match what the remote side thinks. tableoid, in particular, probably won't match and might have some use in queries. Hence, prevent sending conditions that include non-CTID system columns. Also, create_foreignscan_plan neglected to check local restriction conditions while determining whether to set fsSystemCol for a foreign scan plan node. This again would bollix the results for queries that test a foreign table's tableoid. Back-patch the first fix to 9.3 where postgres_fdw was introduced. Back-patch the second to 9.2. The code is probably broken in 9.1 as well, but the patch doesn't apply cleanly there; given the weak state of support for FDWs in 9.1, it doesn't seem worth fixing. Etsuro Fujita, reviewed by Ashutosh Bapat, and somewhat modified by me
1 parent d767aa5 commit 906599f

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/backend/optimizer/plan/createplan.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <math.h>
2121

2222
#include "access/skey.h"
23+
#include "access/sysattr.h"
2324
#include "foreign/fdwapi.h"
2425
#include "miscadmin.h"
2526
#include "nodes/makefuncs.h"
@@ -1881,6 +1882,8 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
18811882
RelOptInfo *rel = best_path->path.parent;
18821883
Index scan_relid = rel->relid;
18831884
RangeTblEntry *rte;
1885+
Bitmapset *attrs_used = NULL;
1886+
ListCell *lc;
18841887
int i;
18851888

18861889
/* it should be a base rel... */
@@ -1928,17 +1931,34 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
19281931
* Detect whether any system columns are requested from rel. This is a
19291932
* bit of a kluge and might go away someday, so we intentionally leave it
19301933
* 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.
19311938
*/
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? */
19321950
scan_plan->fsSystemCol = false;
1933-
for (i = rel->min_attr; i < 0; i++)
1951+
for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
19341952
{
1935-
if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
1953+
if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
19361954
{
19371955
scan_plan->fsSystemCol = true;
19381956
break;
19391957
}
19401958
}
19411959

1960+
bms_free(attrs_used);
1961+
19421962
return scan_plan;
19431963
}
19441964

0 commit comments

Comments
 (0)