Add statistics for INSERT/UPDATE/DELETE etc. SQL commands.
authorTatsuo Ishii <ishii@sraoss.co.jp>
Fri, 14 Aug 2020 01:45:54 +0000 (10:45 +0900)
committerTatsuo Ishii <ishii@sraoss.co.jp>
Fri, 14 Aug 2020 01:45:54 +0000 (10:45 +0900)
We already allow to collect stats for SELECT, but it would be useful
to collect stats for other types of SQL commands. APIs to fetch stats
data is also added. No UI is added yet. Currently, any SQL commands
(besides SELECT/INSERT/UPDATE/DELETE) other than:

case(T_CheckPointStmt):
case(T_DeallocateStmt):
case(T_DiscardStmt):
case(T_ExecuteStmt):
case(T_ExplainStmt):
case(T_ListenStmt):
case(T_LoadStmt):
case(T_LockStmt):
case(T_NotifyStmt):
case(T_PrepareStmt):
case(T_TransactionStmt):
case(T_UnlistenStmt):
case(T_VacuumStmt):
case(T_VariableSetStmt):
case(T_VariableShowStmt):

are counted as DDL commands.
Discussion: https://www.pgpool.net/pipermail/pgpool-hackers/2020-July/003754.html

src/include/utils/statistics.h
src/utils/statistics.c

index 4f0a33f42d87beab531f0ec1a0b531f2d1d8d188..d235cc37dadd252ab86e73fbb53c44e17533240a 100644 (file)
@@ -26,5 +26,10 @@ void         stat_set_stat_area(void *address);
 void           stat_init_stat_area(void);
 void           stat_count_up(int backend_node_id, Node *parsetree);
 uint64         stat_get_select_count(int backend_node_id);
+uint64         stat_get_insert_count(int backend_node_id);
+uint64         stat_get_update_count(int backend_node_id);
+uint64         stat_get_delete_count(int backend_node_id);
+uint64         stat_get_ddl_count(int backend_node_id);
+uint64         stat_get_other_count(int backend_node_id);
 
 #endif /* statistics_h */
index db3ac0ba6e02f9e6a13ec21bea7cecf328022873..373ba5e2c938e5736ff3c328040e52212562b311 100644 (file)
@@ -92,6 +92,48 @@ stat_count_up(int backend_node_id, Node *parse_tree)
        {
                per_node_stat[backend_node_id].select_cnt++;
        }
+
+       else if (IsA(parse_tree, InsertStmt))
+       {
+               per_node_stat[backend_node_id].insert_cnt++;
+       }
+
+       else if (IsA(parse_tree, UpdateStmt))
+       {
+               per_node_stat[backend_node_id].update_cnt++;
+       }
+
+       else if (IsA(parse_tree, DeleteStmt))
+       {
+               per_node_stat[backend_node_id].delete_cnt++;
+       }
+
+       else
+       {
+               switch(nodeTag(parse_tree))
+               {
+                       case(T_CheckPointStmt):
+                       case(T_DeallocateStmt):
+                       case(T_DiscardStmt):
+                       case(T_ExecuteStmt):
+                       case(T_ExplainStmt):
+                       case(T_ListenStmt):
+                       case(T_LoadStmt):
+                       case(T_LockStmt):
+                       case(T_NotifyStmt):
+                       case(T_PrepareStmt):
+                       case(T_TransactionStmt):
+                       case(T_UnlistenStmt):
+                       case(T_VacuumStmt):
+                       case(T_VariableSetStmt):
+                       case(T_VariableShowStmt):
+                               per_node_stat[backend_node_id].other_cnt++;
+                               break;
+
+                       default:
+                               per_node_stat[backend_node_id].ddl_cnt++;
+               }
+       }
 }
 
 /*
@@ -102,3 +144,33 @@ stat_get_select_count(int backend_node_id)
 {
        return per_node_stat[backend_node_id].select_cnt;
 }
+
+uint64
+stat_get_insert_count(int backend_node_id)
+{
+       return per_node_stat[backend_node_id].insert_cnt;
+}
+
+uint64
+stat_get_update_count(int backend_node_id)
+{
+       return per_node_stat[backend_node_id].update_cnt;
+}
+
+uint64
+stat_get_delete_count(int backend_node_id)
+{
+       return per_node_stat[backend_node_id].delete_cnt;
+}
+
+uint64
+stat_get_ddl_count(int backend_node_id)
+{
+       return per_node_stat[backend_node_id].ddl_cnt;
+}
+
+uint64
+stat_get_other_count(int backend_node_id)
+{
+       return per_node_stat[backend_node_id].other_cnt;
+}