Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to 2.11.x: #6015: Fix incorrect row count in EXPLAIN ANALYZE INSERT .. ON CONFLICT output #6033

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading