Commit 6da378db authored by Tom Lane's avatar Tom Lane

Fix whole-row references in postgres_fdw.

The optimization to not retrieve unnecessary columns wasn't smart enough.
Noted by Thom Brown.
parent 211e157a
...@@ -364,6 +364,7 @@ deparseSimpleSql(StringInfo buf, ...@@ -364,6 +364,7 @@ deparseSimpleSql(StringInfo buf,
{ {
RangeTblEntry *rte = root->simple_rte_array[baserel->relid]; RangeTblEntry *rte = root->simple_rte_array[baserel->relid];
Bitmapset *attrs_used = NULL; Bitmapset *attrs_used = NULL;
bool have_wholerow;
bool first; bool first;
AttrNumber attr; AttrNumber attr;
ListCell *lc; ListCell *lc;
...@@ -381,6 +382,10 @@ deparseSimpleSql(StringInfo buf, ...@@ -381,6 +382,10 @@ deparseSimpleSql(StringInfo buf,
&attrs_used); &attrs_used);
} }
/* If there's a whole-row reference, we'll need all the columns. */
have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber,
attrs_used);
/* /*
* Construct SELECT list * Construct SELECT list
* *
...@@ -401,7 +406,8 @@ deparseSimpleSql(StringInfo buf, ...@@ -401,7 +406,8 @@ deparseSimpleSql(StringInfo buf,
appendStringInfo(buf, ", "); appendStringInfo(buf, ", ");
first = false; first = false;
if (bms_is_member(attr - FirstLowInvalidHeapAttributeNumber, if (have_wholerow ||
bms_is_member(attr - FirstLowInvalidHeapAttributeNumber,
attrs_used)) attrs_used))
deparseColumnRef(buf, baserel->relid, attr, root); deparseColumnRef(buf, baserel->relid, attr, root);
else else
......
...@@ -180,6 +180,35 @@ SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10; ...@@ -180,6 +180,35 @@ SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
110 | 0 | 00110 | Sun Jan 11 00:00:00 1970 PST | Sun Jan 11 00:00:00 1970 | 0 | 0 | foo 110 | 0 | 00110 | Sun Jan 11 00:00:00 1970 PST | Sun Jan 11 00:00:00 1970 | 0 | 0 | foo
(10 rows) (10 rows)
-- whole-row reference
EXPLAIN (VERBOSE, COSTS false) SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
QUERY PLAN
-------------------------------------------------------------------------------------
Limit
Output: t1.*, c3, c1
-> Sort
Output: t1.*, c3, c1
Sort Key: t1.c3, t1.c1
-> Foreign Scan on public.ft1 t1
Output: t1.*, c3, c1
Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1"
(8 rows)
SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
t1
--------------------------------------------------------------------------------------------
(101,1,00101,"Fri Jan 02 00:00:00 1970 PST","Fri Jan 02 00:00:00 1970",1,"1 ",foo)
(102,2,00102,"Sat Jan 03 00:00:00 1970 PST","Sat Jan 03 00:00:00 1970",2,"2 ",foo)
(103,3,00103,"Sun Jan 04 00:00:00 1970 PST","Sun Jan 04 00:00:00 1970",3,"3 ",foo)
(104,4,00104,"Mon Jan 05 00:00:00 1970 PST","Mon Jan 05 00:00:00 1970",4,"4 ",foo)
(105,5,00105,"Tue Jan 06 00:00:00 1970 PST","Tue Jan 06 00:00:00 1970",5,"5 ",foo)
(106,6,00106,"Wed Jan 07 00:00:00 1970 PST","Wed Jan 07 00:00:00 1970",6,"6 ",foo)
(107,7,00107,"Thu Jan 08 00:00:00 1970 PST","Thu Jan 08 00:00:00 1970",7,"7 ",foo)
(108,8,00108,"Fri Jan 09 00:00:00 1970 PST","Fri Jan 09 00:00:00 1970",8,"8 ",foo)
(109,9,00109,"Sat Jan 10 00:00:00 1970 PST","Sat Jan 10 00:00:00 1970",9,"9 ",foo)
(110,0,00110,"Sun Jan 11 00:00:00 1970 PST","Sun Jan 11 00:00:00 1970",0,"0 ",foo)
(10 rows)
-- empty result -- empty result
SELECT * FROM ft1 WHERE false; SELECT * FROM ft1 WHERE false;
c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8
......
...@@ -136,6 +136,9 @@ EXPLAIN (COSTS false) SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10; ...@@ -136,6 +136,9 @@ EXPLAIN (COSTS false) SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10;
SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10; SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10;
EXPLAIN (VERBOSE, COSTS false) SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10; EXPLAIN (VERBOSE, COSTS false) SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10; SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
-- whole-row reference
EXPLAIN (VERBOSE, COSTS false) SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
-- empty result -- empty result
SELECT * FROM ft1 WHERE false; SELECT * FROM ft1 WHERE false;
-- with WHERE clause -- with WHERE clause
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment