Skip to content

Commit 12e423e

Browse files
committed
Fix EXPLAIN MERGE output when no tuples are processed
An 'else' clause was misplaced in commit 598ac10, making zero-rows output look a bit silly. Add a test case for it. Pointed out by Tom Lane. Discussion: https://postgr.es/m/21030.1652893083@sss.pgh.pa.us
1 parent 0fbf011 commit 12e423e

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

src/backend/commands/explain.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,19 +4068,22 @@ show_modifytable_info(ModifyTableState *mtstate, List *ancestors,
40684068
skipped_path = total - insert_path - update_path - delete_path;
40694069
Assert(skipped_path >= 0);
40704070

4071-
if (es->format == EXPLAIN_FORMAT_TEXT && total > 0)
4071+
if (es->format == EXPLAIN_FORMAT_TEXT)
40724072
{
4073-
ExplainIndentText(es);
4074-
appendStringInfoString(es->str, "Tuples:");
4075-
if (insert_path > 0)
4076-
appendStringInfo(es->str, " inserted=%.0f", insert_path);
4077-
if (update_path > 0)
4078-
appendStringInfo(es->str, " updated=%.0f", update_path);
4079-
if (delete_path > 0)
4080-
appendStringInfo(es->str, " deleted=%.0f", delete_path);
4081-
if (skipped_path > 0)
4082-
appendStringInfo(es->str, " skipped=%.0f", skipped_path);
4083-
appendStringInfoChar(es->str, '\n');
4073+
if (total > 0)
4074+
{
4075+
ExplainIndentText(es);
4076+
appendStringInfoString(es->str, "Tuples:");
4077+
if (insert_path > 0)
4078+
appendStringInfo(es->str, " inserted=%.0f", insert_path);
4079+
if (update_path > 0)
4080+
appendStringInfo(es->str, " updated=%.0f", update_path);
4081+
if (delete_path > 0)
4082+
appendStringInfo(es->str, " deleted=%.0f", delete_path);
4083+
if (skipped_path > 0)
4084+
appendStringInfo(es->str, " skipped=%.0f", skipped_path);
4085+
appendStringInfoChar(es->str, '\n');
4086+
}
40844087
}
40854088
else
40864089
{

src/test/regress/expected/merge.out

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ BEGIN
13161316
EXECUTE 'explain (analyze, timing off, summary off, costs off) ' ||
13171317
query
13181318
LOOP
1319-
ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx');
1319+
ln := regexp_replace(ln, '(Memory( Usage)?|Buckets|Batches): \S*', '\1: xxx', 'g');
13201320
RETURN NEXT ln;
13211321
END LOOP;
13221322
END;
@@ -1432,6 +1432,24 @@ WHEN NOT MATCHED AND s.a < 20 THEN
14321432
-> Seq Scan on ex_mtarget t (actual rows=49 loops=1)
14331433
(12 rows)
14341434

1435+
-- nothing
1436+
SELECT explain_merge('
1437+
MERGE INTO ex_mtarget t USING ex_msource s ON t.a = s.a AND t.a < -1000
1438+
WHEN MATCHED AND t.a < 10 THEN
1439+
DO NOTHING');
1440+
explain_merge
1441+
--------------------------------------------------------------------
1442+
Merge on ex_mtarget t (actual rows=0 loops=1)
1443+
-> Hash Join (actual rows=0 loops=1)
1444+
Hash Cond: (s.a = t.a)
1445+
-> Seq Scan on ex_msource s (actual rows=1 loops=1)
1446+
-> Hash (actual rows=0 loops=1)
1447+
Buckets: xxx Batches: xxx Memory Usage: xxx
1448+
-> Seq Scan on ex_mtarget t (actual rows=0 loops=1)
1449+
Filter: (a < '-1000'::integer)
1450+
Rows Removed by Filter: 54
1451+
(9 rows)
1452+
14351453
DROP TABLE ex_msource, ex_mtarget;
14361454
DROP FUNCTION explain_merge(text);
14371455
-- Subqueries

src/test/regress/sql/merge.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ BEGIN
878878
EXECUTE 'explain (analyze, timing off, summary off, costs off) ' ||
879879
query
880880
LOOP
881-
ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx');
881+
ln := regexp_replace(ln, '(Memory( Usage)?|Buckets|Batches): \S*', '\1: xxx', 'g');
882882
RETURN NEXT ln;
883883
END LOOP;
884884
END;
@@ -920,6 +920,12 @@ WHEN MATCHED AND t.a >= 30 AND t.a <= 40 THEN
920920
WHEN NOT MATCHED AND s.a < 20 THEN
921921
INSERT VALUES (a, b)');
922922

923+
-- nothing
924+
SELECT explain_merge('
925+
MERGE INTO ex_mtarget t USING ex_msource s ON t.a = s.a AND t.a < -1000
926+
WHEN MATCHED AND t.a < 10 THEN
927+
DO NOTHING');
928+
923929
DROP TABLE ex_msource, ex_mtarget;
924930
DROP FUNCTION explain_merge(text);
925931

0 commit comments

Comments
 (0)