Skip to content

Commit 6da378d

Browse files
committed
Fix whole-row references in postgres_fdw.
The optimization to not retrieve unnecessary columns wasn't smart enough. Noted by Thom Brown.
1 parent 211e157 commit 6da378d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ deparseSimpleSql(StringInfo buf,
364364
{
365365
RangeTblEntry *rte = root->simple_rte_array[baserel->relid];
366366
Bitmapset *attrs_used = NULL;
367+
bool have_wholerow;
367368
bool first;
368369
AttrNumber attr;
369370
ListCell *lc;
@@ -381,6 +382,10 @@ deparseSimpleSql(StringInfo buf,
381382
&attrs_used);
382383
}
383384

385+
/* If there's a whole-row reference, we'll need all the columns. */
386+
have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber,
387+
attrs_used);
388+
384389
/*
385390
* Construct SELECT list
386391
*
@@ -401,7 +406,8 @@ deparseSimpleSql(StringInfo buf,
401406
appendStringInfo(buf, ", ");
402407
first = false;
403408

404-
if (bms_is_member(attr - FirstLowInvalidHeapAttributeNumber,
409+
if (have_wholerow ||
410+
bms_is_member(attr - FirstLowInvalidHeapAttributeNumber,
405411
attrs_used))
406412
deparseColumnRef(buf, baserel->relid, attr, root);
407413
else

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
180180
110 | 0 | 00110 | Sun Jan 11 00:00:00 1970 PST | Sun Jan 11 00:00:00 1970 | 0 | 0 | foo
181181
(10 rows)
182182

183+
-- whole-row reference
184+
EXPLAIN (VERBOSE, COSTS false) SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
185+
QUERY PLAN
186+
-------------------------------------------------------------------------------------
187+
Limit
188+
Output: t1.*, c3, c1
189+
-> Sort
190+
Output: t1.*, c3, c1
191+
Sort Key: t1.c3, t1.c1
192+
-> Foreign Scan on public.ft1 t1
193+
Output: t1.*, c3, c1
194+
Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1"
195+
(8 rows)
196+
197+
SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
198+
t1
199+
--------------------------------------------------------------------------------------------
200+
(101,1,00101,"Fri Jan 02 00:00:00 1970 PST","Fri Jan 02 00:00:00 1970",1,"1 ",foo)
201+
(102,2,00102,"Sat Jan 03 00:00:00 1970 PST","Sat Jan 03 00:00:00 1970",2,"2 ",foo)
202+
(103,3,00103,"Sun Jan 04 00:00:00 1970 PST","Sun Jan 04 00:00:00 1970",3,"3 ",foo)
203+
(104,4,00104,"Mon Jan 05 00:00:00 1970 PST","Mon Jan 05 00:00:00 1970",4,"4 ",foo)
204+
(105,5,00105,"Tue Jan 06 00:00:00 1970 PST","Tue Jan 06 00:00:00 1970",5,"5 ",foo)
205+
(106,6,00106,"Wed Jan 07 00:00:00 1970 PST","Wed Jan 07 00:00:00 1970",6,"6 ",foo)
206+
(107,7,00107,"Thu Jan 08 00:00:00 1970 PST","Thu Jan 08 00:00:00 1970",7,"7 ",foo)
207+
(108,8,00108,"Fri Jan 09 00:00:00 1970 PST","Fri Jan 09 00:00:00 1970",8,"8 ",foo)
208+
(109,9,00109,"Sat Jan 10 00:00:00 1970 PST","Sat Jan 10 00:00:00 1970",9,"9 ",foo)
209+
(110,0,00110,"Sun Jan 11 00:00:00 1970 PST","Sun Jan 11 00:00:00 1970",0,"0 ",foo)
210+
(10 rows)
211+
183212
-- empty result
184213
SELECT * FROM ft1 WHERE false;
185214
c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ EXPLAIN (COSTS false) SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10;
136136
SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10;
137137
EXPLAIN (VERBOSE, COSTS false) SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
138138
SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
139+
-- whole-row reference
140+
EXPLAIN (VERBOSE, COSTS false) SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
141+
SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
139142
-- empty result
140143
SELECT * FROM ft1 WHERE false;
141144
-- with WHERE clause

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy