diff options
author | Etsuro Fujita <efujita@postgresql.org> | 2021-05-12 14:00:00 +0900 |
---|---|---|
committer | Etsuro Fujita <efujita@postgresql.org> | 2021-05-12 14:00:00 +0900 |
commit | a363bc6da96b14d27e1cae1bae97242eb6ade5e6 (patch) | |
tree | 34b544b6df0502ad2bdc7a8ceb61f69f41de5eb0 /contrib | |
parent | e135743ef07ea59088d09c459f41ee2eaabe95c3 (diff) | |
download | postgresql-a363bc6da96b14d27e1cae1bae97242eb6ade5e6.tar.gz postgresql-a363bc6da96b14d27e1cae1bae97242eb6ade5e6.zip |
Fix EXPLAIN ANALYZE for async-capable nodes.
EXPLAIN ANALYZE for an async-capable ForeignScan node associated with
postgres_fdw is done just by using instrumentation for ExecProcNode()
called from the node's callbacks, causing the following problems:
1) If the remote table to scan is empty, the node is incorrectly
considered as "never executed" by the command even if the node is
executed, as ExecProcNode() isn't called from the node's callbacks at
all in that case.
2) The command fails to collect timings for things other than
ExecProcNode() done in the node, such as creating a cursor for the
node's remote query.
To fix these problems, add instrumentation for async-capable nodes, and
modify postgres_fdw accordingly.
My oversight in commit 27e1f1456.
While at it, update a comment for the AsyncRequest struct in execnodes.h
and the documentation for the ForeignAsyncRequest API in fdwhandler.sgml
to match the code in ExecAsyncAppendResponse() in nodeAppend.c, and fix
typos in comments in nodeAppend.c.
Per report from Andrey Lepikhov, though I didn't use his patch.
Reviewed-by: Andrey Lepikhov
Discussion: https://postgr.es/m/2eb662bb-105d-fc20-7412-2f027cc3ca72%40postgrespro.ru
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/auto_explain/auto_explain.c | 2 | ||||
-rw-r--r-- | contrib/pg_stat_statements/pg_stat_statements.c | 2 | ||||
-rw-r--r-- | contrib/postgres_fdw/expected/postgres_fdw.out | 39 | ||||
-rw-r--r-- | contrib/postgres_fdw/postgres_fdw.c | 9 | ||||
-rw-r--r-- | contrib/postgres_fdw/sql/postgres_fdw.sql | 21 |
5 files changed, 59 insertions, 14 deletions
diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c index 445bb371912..e9092ba359a 100644 --- a/contrib/auto_explain/auto_explain.c +++ b/contrib/auto_explain/auto_explain.c @@ -314,7 +314,7 @@ explain_ExecutorStart(QueryDesc *queryDesc, int eflags) MemoryContext oldcxt; oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt); - queryDesc->totaltime = InstrAlloc(1, INSTRUMENT_ALL); + queryDesc->totaltime = InstrAlloc(1, INSTRUMENT_ALL, false); MemoryContextSwitchTo(oldcxt); } } diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index f42f07622e9..77ca5abcdc8 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -974,7 +974,7 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) MemoryContext oldcxt; oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt); - queryDesc->totaltime = InstrAlloc(1, INSTRUMENT_ALL); + queryDesc->totaltime = InstrAlloc(1, INSTRUMENT_ALL, false); MemoryContextSwitchTo(oldcxt); } } diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 6f533c745d6..0b0c45f0d9a 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10051,6 +10051,21 @@ SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1; Filter: (t1_3.b === 505) (14 rows) +EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1; + QUERY PLAN +------------------------------------------------------------------------- + Limit (actual rows=1 loops=1) + -> Append (actual rows=1 loops=1) + -> Async Foreign Scan on async_p1 t1_1 (actual rows=0 loops=1) + Filter: (b === 505) + -> Async Foreign Scan on async_p2 t1_2 (actual rows=0 loops=1) + Filter: (b === 505) + -> Seq Scan on async_p3 t1_3 (actual rows=1 loops=1) + Filter: (b === 505) + Rows Removed by Filter: 101 +(9 rows) + SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1; a | b | c ------+-----+------ @@ -10132,18 +10147,32 @@ SELECT * FROM join_tbl ORDER BY a1; (3 rows) DELETE FROM join_tbl; +DROP TABLE local_tbl; +DROP FOREIGN TABLE remote_tbl; +DROP FOREIGN TABLE insert_tbl; +DROP TABLE base_tbl3; +DROP TABLE base_tbl4; RESET enable_mergejoin; RESET enable_hashjoin; +-- Check EXPLAIN ANALYZE for a query that scans empty partitions asynchronously +DELETE FROM async_p1; +DELETE FROM async_p2; +DELETE FROM async_p3; +EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +SELECT * FROM async_pt; + QUERY PLAN +------------------------------------------------------------------------- + Append (actual rows=0 loops=1) + -> Async Foreign Scan on async_p1 async_pt_1 (actual rows=0 loops=1) + -> Async Foreign Scan on async_p2 async_pt_2 (actual rows=0 loops=1) + -> Seq Scan on async_p3 async_pt_3 (actual rows=0 loops=1) +(4 rows) + -- Clean up DROP TABLE async_pt; DROP TABLE base_tbl1; DROP TABLE base_tbl2; DROP TABLE result_tbl; -DROP TABLE local_tbl; -DROP FOREIGN TABLE remote_tbl; -DROP FOREIGN TABLE insert_tbl; -DROP TABLE base_tbl3; -DROP TABLE base_tbl4; DROP TABLE join_tbl; ALTER SERVER loopback OPTIONS (DROP async_capable); ALTER SERVER loopback2 OPTIONS (DROP async_capable); diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index 4ff58d9c275..ee93ee07cc4 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -1542,7 +1542,7 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags) &fsstate->param_values); /* Set the async-capable flag */ - fsstate->async_capable = node->ss.ps.plan->async_capable; + fsstate->async_capable = node->ss.ps.async_capable; } /* @@ -6867,7 +6867,7 @@ produce_tuple_asynchronously(AsyncRequest *areq, bool fetch) } /* Get a tuple from the ForeignScan node */ - result = ExecProcNode((PlanState *) node); + result = areq->requestee->ExecProcNodeReal(areq->requestee); if (!TupIsNull(result)) { /* Mark the request as complete */ @@ -6956,6 +6956,11 @@ process_pending_request(AsyncRequest *areq) /* Unlike AsyncNotify, we call ExecAsyncResponse ourselves */ ExecAsyncResponse(areq); + /* Also, we do instrumentation ourselves, if required */ + if (areq->requestee->instrument) + InstrUpdateTupleCount(areq->requestee->instrument, + TupIsNull(areq->result) ? 0.0 : 1.0); + MemoryContextSwitchTo(oldcontext); } diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 000e2534fc8..53adfe2abc8 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3195,6 +3195,8 @@ SELECT * FROM async_pt t1, async_p2 t2 WHERE t1.a = t2.a AND t1.b === 505; EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1; +EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1; SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1; -- Check with foreign modify @@ -3226,19 +3228,28 @@ INSERT INTO join_tbl SELECT * FROM async_pt LEFT JOIN t ON (async_pt.a = t.a AND SELECT * FROM join_tbl ORDER BY a1; DELETE FROM join_tbl; +DROP TABLE local_tbl; +DROP FOREIGN TABLE remote_tbl; +DROP FOREIGN TABLE insert_tbl; +DROP TABLE base_tbl3; +DROP TABLE base_tbl4; + RESET enable_mergejoin; RESET enable_hashjoin; +-- Check EXPLAIN ANALYZE for a query that scans empty partitions asynchronously +DELETE FROM async_p1; +DELETE FROM async_p2; +DELETE FROM async_p3; + +EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF) +SELECT * FROM async_pt; + -- Clean up DROP TABLE async_pt; DROP TABLE base_tbl1; DROP TABLE base_tbl2; DROP TABLE result_tbl; -DROP TABLE local_tbl; -DROP FOREIGN TABLE remote_tbl; -DROP FOREIGN TABLE insert_tbl; -DROP TABLE base_tbl3; -DROP TABLE base_tbl4; DROP TABLE join_tbl; ALTER SERVER loopback OPTIONS (DROP async_capable); |