Search Postgresql Archives

Re: MERGE output doubt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2022-Oct-20, Luca Ferrari wrote:

> Now, if I use  a do nothing merge:
> 
> estdb=> MERGE INTO average_scores a
> USING ( SELECT name, avg( score ) AS avg_score
>        FROM scores GROUP BY name ) s
> ON s.name = a.name
> WHEN MATCHED  THEN
>    do nothing
> WHEN NOT MATCHED THEN
>    do nothing;
> MERGE 2
> 
> I was expecting an output tag like "MERGE 0" since both branches have
> "do nothing", so no tuples should be updated at all on the target
> table.

Hmm, yeah, it should report 0 tuples, according to the documentation.
AFAICS this patch fixes it, will push shortly.

> Moreover, if I truncate the target table and execute again the merge
> query, I got the result of 0:
> 
> testdb=> truncate average_scores ;
> TRUNCATE TABLE
> testdb=> MERGE INTO average_scores a
> USING ( SELECT name, avg( score ) AS avg_score
>        FROM scores GROUP BY name ) s
> ON s.name = a.name
> WHEN MATCHED  THEN   -- caso di match
>    do nothing
> WHEN NOT MATCHED THEN
>    do nothing;
> MERGE 0
> 
> What am I missing here?

Hmm, is this not what you were expecting?  Or are you saying that it
should have been affected by the same bug?

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
>From 4282eadc0af3061dc53a5bc1ffcdd51b03cc28c4 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@xxxxxxxxxxxxxx>
Date: Thu, 17 Nov 2022 11:58:42 +0100
Subject: [PATCH] fix MERGE tuple count with DO NOTHING

---
 src/backend/executor/nodeModifyTable.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index d8fd3cfdbe..fe2b8e4bac 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -2805,7 +2805,7 @@ lmerge_matched:
 		{
 			case TM_Ok:
 				/* all good; perform final actions */
-				if (canSetTag)
+				if (canSetTag && commandType != CMD_NOTHING)
 					(estate->es_processed)++;
 
 				break;
-- 
2.30.2


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux