From ff6d6e5e531e29a359e9d505cd17a2bc65b0c132 Mon Sep 17 00:00:00 2001 From: Lakshmi Narayanan Sreethar Date: Mon, 28 Aug 2023 23:19:22 +0530 Subject: [PATCH] Fix incorrect row count in EXPLAIN ANALYZE INSERT .. ON CONFLICT output INSERT ... ON CONFLICT statements record few metrics in the ModifyTable node's instrument but they get overwritten by hypertable_modify_explain causing wrong output in EXPLAIN ANALYZE statments. Fix it by saving the metrics into HypertableModify node before replacing them. Fixes #6014 (cherry picked from commit 8e941b80ae1b0e0b6affe5431454cdc637628d99) --- .unreleased/bugfix_6015 | 1 + src/nodes/hypertable_modify.c | 11 ++++++++++- test/expected/upsert.out | 19 +++++++++++++++++++ test/sql/upsert.sql | 7 +++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 .unreleased/bugfix_6015 diff --git a/.unreleased/bugfix_6015 b/.unreleased/bugfix_6015 new file mode 100644 index 00000000000..d8d7f0aabb9 --- /dev/null +++ b/.unreleased/bugfix_6015 @@ -0,0 +1 @@ +Fixes: #6015 Correct row count in EXPLAIN ANALYZE INSERT .. ON CONFLICT output diff --git a/src/nodes/hypertable_modify.c b/src/nodes/hypertable_modify.c index b0872f46ee7..979c27c6799 100644 --- a/src/nodes/hypertable_modify.c +++ b/src/nodes/hypertable_modify.c @@ -212,9 +212,18 @@ hypertable_modify_explain(CustomScanState *node, List *ancestors, ExplainState * } /* - * Since we hijack the ModifyTable node instrumentation on ModifyTable will + * Since we hijack the ModifyTable node, instrumentation on ModifyTable will * be missing so we set it to instrumentation of HypertableModify node. */ + if (mtstate->ps.instrument) + { + /* + * INSERT .. ON CONFLICT statements record few metrics in the ModifyTable node. + * So, copy them into HypertableModify node before replacing them. + */ + node->ss.ps.instrument->ntuples2 = mtstate->ps.instrument->ntuples2; + node->ss.ps.instrument->nfiltered1 = mtstate->ps.instrument->nfiltered1; + } mtstate->ps.instrument = node->ss.ps.instrument; #endif diff --git a/test/expected/upsert.out b/test/expected/upsert.out index e1bc8339b68..6b37ea026e3 100644 --- a/test/expected/upsert.out +++ b/test/expected/upsert.out @@ -29,6 +29,25 @@ SELECT * FROM upsert_test; Fri Jan 20 09:00:01 2017 | 23.8 | yellow (1 row) +-- Test 'Tuples Inserted' and 'Conflicting Tuples' values in EXPLAIN ANALYZE +EXPLAIN (VERBOSE, ANALYZE, COSTS FALSE, TIMING FALSE, SUMMARY FALSE) + INSERT INTO upsert_test VALUES + ('2017-01-20T09:00:01', 28.5, 'blue'), + ('2017-01-20T09:00:01', 21.9, 'red'), + ('2017-01-20T10:00:01', 2.4, 'pink') ON CONFLICT DO NOTHING; + QUERY PLAN +---------------------------------------------------------------------------------------- + Custom Scan (HypertableModify) (actual rows=0 loops=1) + -> Insert on public.upsert_test (actual rows=0 loops=1) + Conflict Resolution: NOTHING + Tuples Inserted: 1 + Conflicting Tuples: 2 + -> Custom Scan (ChunkDispatch) (actual rows=3 loops=1) + Output: "*VALUES*".column1, "*VALUES*".column2, "*VALUES*".column3 + -> Values Scan on "*VALUES*" (actual rows=3 loops=1) + Output: "*VALUES*".column1, "*VALUES*".column2, "*VALUES*".column3 +(9 rows) + -- Test ON CONFLICT ON CONSTRAINT INSERT INTO upsert_test VALUES ('2017-01-20T09:00:01', 12.3, 'yellow') ON CONFLICT ON CONSTRAINT upsert_test_pkey DO UPDATE SET temp = 12.3 RETURNING time, temp, color; diff --git a/test/sql/upsert.sql b/test/sql/upsert.sql index 9e6b42630f2..6e761c49318 100644 --- a/test/sql/upsert.sql +++ b/test/sql/upsert.sql @@ -10,6 +10,13 @@ DO UPDATE SET temp = 23.8 RETURNING *; INSERT INTO upsert_test VALUES ('2017-01-20T09:00:01', 78.4, 'yellow') ON CONFLICT DO NOTHING; SELECT * FROM upsert_test; +-- Test 'Tuples Inserted' and 'Conflicting Tuples' values in EXPLAIN ANALYZE +EXPLAIN (VERBOSE, ANALYZE, COSTS FALSE, TIMING FALSE, SUMMARY FALSE) + INSERT INTO upsert_test VALUES + ('2017-01-20T09:00:01', 28.5, 'blue'), + ('2017-01-20T09:00:01', 21.9, 'red'), + ('2017-01-20T10:00:01', 2.4, 'pink') ON CONFLICT DO NOTHING; + -- Test ON CONFLICT ON CONSTRAINT INSERT INTO upsert_test VALUES ('2017-01-20T09:00:01', 12.3, 'yellow') ON CONFLICT ON CONSTRAINT upsert_test_pkey DO UPDATE SET temp = 12.3 RETURNING time, temp, color;