Skip to content

Commit

Permalink
Fix incorrect row count in EXPLAIN ANALYZE INSERT .. ON CONFLICT output
Browse files Browse the repository at this point in the history
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 8e941b8)
  • Loading branch information
lkshminarayanan authored and timescale-automation committed Aug 31, 2023
1 parent aaa8866 commit c095a43
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions .unreleased/bugfix_6015
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #6015 Correct row count in EXPLAIN ANALYZE INSERT .. ON CONFLICT output
11 changes: 10 additions & 1 deletion src/nodes/hypertable_modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions test/expected/upsert.out
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions test/sql/upsert.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c095a43

Please sign in to comment.