Expose BufferUsageAccumDiff().
authorFujii Masao <fujii@postgresql.org>
Mon, 30 Mar 2020 03:15:26 +0000 (12:15 +0900)
committerFujii Masao <fujii@postgresql.org>
Mon, 30 Mar 2020 03:15:26 +0000 (12:15 +0900)
Previously pg_stat_statements calculated the difference of buffer counters
by its own code even while BufferUsageAccumDiff() had the same code.
This commit expose BufferUsageAccumDiff() and makes pg_stat_statements
use it for the calculation, in order to simply the code.

This change also would be useful for the upcoming patch for the planning
counters in pg_stat_statements because the patch will add one more code
for the calculation of difference of buffer counters and that can easily be
done by using BufferUsageAccumDiff().

Author: Julien Rouhaud
Reviewed-by: Fujii Masao
Discussion: https://postgr.es/m/bdfee4e0-a304-2498-8da5-3cb52c0a193e@oss.nttdata.com

contrib/pg_stat_statements/pg_stat_statements.c
src/backend/executor/instrument.c
src/include/executor/instrument.h

index 20dc8c605bfa788588e0db4c81005b3c0e534bee..50c345378da78c022cdf0d60ca2c664c4288bb10 100644 (file)
@@ -1016,30 +1016,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
        rows = (qc && qc->commandTag == CMDTAG_COPY) ? qc->nprocessed : 0;
 
        /* calc differences of buffer counters. */
-       bufusage.shared_blks_hit =
-           pgBufferUsage.shared_blks_hit - bufusage_start.shared_blks_hit;
-       bufusage.shared_blks_read =
-           pgBufferUsage.shared_blks_read - bufusage_start.shared_blks_read;
-       bufusage.shared_blks_dirtied =
-           pgBufferUsage.shared_blks_dirtied - bufusage_start.shared_blks_dirtied;
-       bufusage.shared_blks_written =
-           pgBufferUsage.shared_blks_written - bufusage_start.shared_blks_written;
-       bufusage.local_blks_hit =
-           pgBufferUsage.local_blks_hit - bufusage_start.local_blks_hit;
-       bufusage.local_blks_read =
-           pgBufferUsage.local_blks_read - bufusage_start.local_blks_read;
-       bufusage.local_blks_dirtied =
-           pgBufferUsage.local_blks_dirtied - bufusage_start.local_blks_dirtied;
-       bufusage.local_blks_written =
-           pgBufferUsage.local_blks_written - bufusage_start.local_blks_written;
-       bufusage.temp_blks_read =
-           pgBufferUsage.temp_blks_read - bufusage_start.temp_blks_read;
-       bufusage.temp_blks_written =
-           pgBufferUsage.temp_blks_written - bufusage_start.temp_blks_written;
-       bufusage.blk_read_time = pgBufferUsage.blk_read_time;
-       INSTR_TIME_SUBTRACT(bufusage.blk_read_time, bufusage_start.blk_read_time);
-       bufusage.blk_write_time = pgBufferUsage.blk_write_time;
-       INSTR_TIME_SUBTRACT(bufusage.blk_write_time, bufusage_start.blk_write_time);
+       memset(&bufusage, 0, sizeof(BufferUsage));
+       BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
 
        pgss_store(queryString,
                   0,           /* signal that it's a utility stmt */
index bc1d42bf649e7d168c218acab2168148b400086d..042e10f96bcd22b5ad6395947c96ee91422540dd 100644 (file)
@@ -21,8 +21,6 @@ BufferUsage pgBufferUsage;
 static BufferUsage save_pgBufferUsage;
 
 static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add);
-static void BufferUsageAccumDiff(BufferUsage *dst,
-                                const BufferUsage *add, const BufferUsage *sub);
 
 
 /* Allocate new instrumentation structure(s) */
@@ -203,7 +201,7 @@ BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
 }
 
 /* dst += add - sub */
-static void
+void
 BufferUsageAccumDiff(BufferUsage *dst,
                     const BufferUsage *add,
                     const BufferUsage *sub)
index f48d46aedef9115a5ac319fc22b1536b789ef99d..3825a5ac1f3a75f6593c720060c21a72b6738351 100644 (file)
@@ -81,5 +81,7 @@ extern void InstrAggNode(Instrumentation *dst, Instrumentation *add);
 extern void InstrStartParallelQuery(void);
 extern void InstrEndParallelQuery(BufferUsage *result);
 extern void InstrAccumParallelQuery(BufferUsage *result);
+extern void BufferUsageAccumDiff(BufferUsage *dst,
+                                const BufferUsage *add, const BufferUsage *sub);
 
 #endif                         /* INSTRUMENT_H */