Dear hackers,

I found that such a statement would get 0 in PL/pgSQL.

PREPARE smt_del(int) AS DELETE FROM t1;
EXECUTE 'EXECUTE smt_del(100)';
GET DIAGNOSTICS j = ROW_COUNT;

In fact, this is a problem with SPI, it does not support getting result of the EXECUTE command. I made a little enhancement. Support for the number of rows processed when executing INSERT/UPDATE/DELETE statements dynamically.

Regards,
Quan Zongliang
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 22dd55c378..b364492e0e 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -2272,6 +2272,27 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
                                        _SPI_current->processed = 
pg_strtouint64(completionTag + 5,
                                                                                
                                         NULL, 10);
                                }
+                               else if (IsA(stmt->utilityStmt, ExecuteStmt))
+                               {
+                                       if (strncmp(completionTag, "INSERT ", 
7) == 0)
+                                       {
+                                               char *p = completionTag + 7;
+                                               /* INSERT: skip oid and space */
+                                               while (*p && *p != ' ')
+                                                       p++;
+                                               if (*p != 0)
+                                               {
+                                                       _SPI_current->processed 
=
+                                                                       
pg_strtouint64(p, NULL, 10);
+                                               }
+                                       }
+                                       else if (strncmp(completionTag, "UPDATE 
", 7) == 0 ||
+                                                       strncmp(completionTag, 
"DELETE ", 7) == 0)
+                                       {
+                                               _SPI_current->processed =
+                                                               
pg_strtouint64(completionTag + 7, NULL, 10);
+                                       }
+                               }
                        }
 
                        /*

Reply via email to