Skip to content

Commit d1cca94

Browse files
author
Etsuro Fujita
committed
postgres_fdw: Fix handling of a pending asynchronous request in postgresReScanForeignScan().
Commit 27e1f14 failed to process a pending asynchronous request made for a given ForeignScan node in postgresReScanForeignScan() (if any) in cases where we would only reset the next_tuple counter in that function, contradicting the assumption that there should be no pending asynchronous requests that have been made for async-capable subplans for the parent Append node after ReScan. This led to an assert failure in an assert-enabled build. I think this would also lead to mis-rewinding the cursor in that function in the case where we have already fetched one batch for the ForeignScan node and the asynchronous request has been made for the second batch, because even in that case we would just reset the counter when called from that function, so we would fail to execute MOVE BACKWARD ALL. To fix, modify that function to process the asynchronous request before restarting the scan. While at it, add a comment to a function to match other places. Per bug #17344 from Alexander Lakhin. Back-patch to v14 where the aforesaid commit came in. Patch by me. Test case by Alexander Lakhin, adjusted by me. Reviewed and tested by Alexander Lakhin and Dmitry Dolgov. Discussion: https://postgr.es/m/17344-226b78b00de73a7e@postgresql.org
1 parent d94a95c commit d1cca94

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10657,6 +10657,43 @@ DROP TABLE base_tbl1;
1065710657
DROP TABLE base_tbl2;
1065810658
DROP TABLE result_tbl;
1065910659
DROP TABLE join_tbl;
10660+
-- Test that an asynchronous fetch is processed before restarting the scan in
10661+
-- ReScanForeignScan
10662+
CREATE TABLE base_tbl (a int, b int);
10663+
INSERT INTO base_tbl VALUES (1, 11), (2, 22), (3, 33);
10664+
CREATE FOREIGN TABLE foreign_tbl (b int)
10665+
SERVER loopback OPTIONS (table_name 'base_tbl');
10666+
CREATE FOREIGN TABLE foreign_tbl2 () INHERITS (foreign_tbl)
10667+
SERVER loopback OPTIONS (table_name 'base_tbl');
10668+
EXPLAIN (VERBOSE, COSTS OFF)
10669+
SELECT a FROM base_tbl WHERE a IN (SELECT a FROM foreign_tbl);
10670+
QUERY PLAN
10671+
-----------------------------------------------------------------------------
10672+
Seq Scan on public.base_tbl
10673+
Output: base_tbl.a
10674+
Filter: (SubPlan 1)
10675+
SubPlan 1
10676+
-> Result
10677+
Output: base_tbl.a
10678+
-> Append
10679+
-> Async Foreign Scan on public.foreign_tbl foreign_tbl_1
10680+
Remote SQL: SELECT NULL FROM public.base_tbl
10681+
-> Async Foreign Scan on public.foreign_tbl2 foreign_tbl_2
10682+
Remote SQL: SELECT NULL FROM public.base_tbl
10683+
(11 rows)
10684+
10685+
SELECT a FROM base_tbl WHERE a IN (SELECT a FROM foreign_tbl);
10686+
a
10687+
---
10688+
1
10689+
2
10690+
3
10691+
(3 rows)
10692+
10693+
-- Clean up
10694+
DROP FOREIGN TABLE foreign_tbl CASCADE;
10695+
NOTICE: drop cascades to foreign table foreign_tbl2
10696+
DROP TABLE base_tbl;
1066010697
ALTER SERVER loopback OPTIONS (DROP async_capable);
1066110698
ALTER SERVER loopback2 OPTIONS (DROP async_capable);
1066210699
-- ===================================================================

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,18 @@ postgresReScanForeignScan(ForeignScanState *node)
16511651
if (!fsstate->cursor_exists)
16521652
return;
16531653

1654+
/*
1655+
* If the node is async-capable, and an asynchronous fetch for it has been
1656+
* begun, the asynchronous fetch might not have yet completed. Check if
1657+
* the node is async-capable, and an asynchronous fetch for it is still in
1658+
* progress; if so, complete the asynchronous fetch before restarting the
1659+
* scan.
1660+
*/
1661+
if (fsstate->async_capable &&
1662+
fsstate->conn_state->pendingAreq &&
1663+
fsstate->conn_state->pendingAreq->requestee == (PlanState *) node)
1664+
fetch_more_data(node);
1665+
16541666
/*
16551667
* If any internal parameters affecting this node have changed, we'd
16561668
* better destroy and recreate the cursor. Otherwise, rewinding it should
@@ -7003,6 +7015,8 @@ produce_tuple_asynchronously(AsyncRequest *areq, bool fetch)
70037015
ExecAsyncRequestDone(areq, result);
70047016
return;
70057017
}
7018+
7019+
/* We must have run out of tuples */
70067020
Assert(fsstate->next_tuple >= fsstate->num_tuples);
70077021

70087022
/* Fetch some more tuples, if we've not detected EOF yet */

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,6 +3379,23 @@ DROP TABLE base_tbl2;
33793379
DROP TABLE result_tbl;
33803380
DROP TABLE join_tbl;
33813381

3382+
-- Test that an asynchronous fetch is processed before restarting the scan in
3383+
-- ReScanForeignScan
3384+
CREATE TABLE base_tbl (a int, b int);
3385+
INSERT INTO base_tbl VALUES (1, 11), (2, 22), (3, 33);
3386+
CREATE FOREIGN TABLE foreign_tbl (b int)
3387+
SERVER loopback OPTIONS (table_name 'base_tbl');
3388+
CREATE FOREIGN TABLE foreign_tbl2 () INHERITS (foreign_tbl)
3389+
SERVER loopback OPTIONS (table_name 'base_tbl');
3390+
3391+
EXPLAIN (VERBOSE, COSTS OFF)
3392+
SELECT a FROM base_tbl WHERE a IN (SELECT a FROM foreign_tbl);
3393+
SELECT a FROM base_tbl WHERE a IN (SELECT a FROM foreign_tbl);
3394+
3395+
-- Clean up
3396+
DROP FOREIGN TABLE foreign_tbl CASCADE;
3397+
DROP TABLE base_tbl;
3398+
33823399
ALTER SERVER loopback OPTIONS (DROP async_capable);
33833400
ALTER SERVER loopback2 OPTIONS (DROP async_capable);
33843401

0 commit comments

Comments
 (0)