Skip to content

Commit 6d3ea48

Browse files
committed
Optimize check for pending backend IO stats
This commit changes the backend stats code so as we rely on a single boolean rather than a repeated check based on pg_memory_is_all_zeros() in the code, making it cheaper should PgStat_PendingIO get bigger in size. The frequency of backend stats reports is not a bottleneck, but there is no reason to not make that cheaper, and the logic is simple as the only entry points updating backend IO stats are pgstat_count_backend_io_op() and pgstat_count_backend_io_op_time(). Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Reviewed-by: Xuneng Zhou <xunengzhou@gmail.com> Discussion: https://postgr.es/m/Z8WYf1jyy4MwOveQ@ip-10-97-1-34.eu-west-3.compute.internal
1 parent 7fb418f commit 6d3ea48

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/backend/utils/activity/pgstat_backend.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* memory allocation.
3838
*/
3939
static PgStat_BackendPending PendingBackendStats;
40+
static bool backend_has_iostats = false;
4041

4142
/*
4243
* WAL usage counters saved from pgWalUsage at the previous call to
@@ -63,6 +64,8 @@ pgstat_count_backend_io_op_time(IOObject io_object, IOContext io_context,
6364

6465
INSTR_TIME_ADD(PendingBackendStats.pending_io.pending_times[io_object][io_context][io_op],
6566
io_time);
67+
68+
backend_has_iostats = true;
6669
}
6770

6871
void
@@ -76,6 +79,8 @@ pgstat_count_backend_io_op(IOObject io_object, IOContext io_context,
7679

7780
PendingBackendStats.pending_io.counts[io_object][io_context][io_op] += cnt;
7881
PendingBackendStats.pending_io.bytes[io_object][io_context][io_op] += bytes;
82+
83+
backend_has_iostats = true;
7984
}
8085

8186
/*
@@ -158,8 +163,7 @@ pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
158163
* statistics. In this case, avoid unnecessarily modifying the stats
159164
* entry.
160165
*/
161-
if (pg_memory_is_all_zeros(&PendingBackendStats.pending_io,
162-
sizeof(struct PgStat_PendingIO)))
166+
if (!backend_has_iostats)
163167
return;
164168

165169
shbackendent = (PgStatShared_Backend *) entry_ref->shared_stats;
@@ -190,6 +194,8 @@ pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
190194
* Clear out the statistics buffer, so it can be re-used.
191195
*/
192196
MemSet(&PendingBackendStats.pending_io, 0, sizeof(PgStat_PendingIO));
197+
198+
backend_has_iostats = false;
193199
}
194200

195201
/*
@@ -259,9 +265,7 @@ pgstat_flush_backend(bool nowait, bits32 flags)
259265
return false;
260266

261267
/* Some IO data pending? */
262-
if ((flags & PGSTAT_BACKEND_FLUSH_IO) &&
263-
!pg_memory_is_all_zeros(&PendingBackendStats.pending_io,
264-
sizeof(struct PgStat_PendingIO)))
268+
if ((flags & PGSTAT_BACKEND_FLUSH_IO) && backend_has_iostats)
265269
has_pending_data = true;
266270

267271
/* Some WAL data pending? */
@@ -298,9 +302,7 @@ pgstat_backend_have_pending_cb(void)
298302
if (!pgstat_tracks_backend_bktype(MyBackendType))
299303
return false;
300304

301-
return (!pg_memory_is_all_zeros(&PendingBackendStats,
302-
sizeof(struct PgStat_BackendPending)) ||
303-
pgstat_backend_wal_have_pending());
305+
return (backend_has_iostats || pgstat_backend_wal_have_pending());
304306
}
305307

306308
/*
@@ -335,6 +337,7 @@ pgstat_create_backend(ProcNumber procnum)
335337
pgstat_unlock_entry(entry_ref);
336338

337339
MemSet(&PendingBackendStats, 0, sizeof(PgStat_BackendPending));
340+
backend_has_iostats = false;
338341

339342
/*
340343
* Initialize prevBackendWalUsage with pgWalUsage so that

0 commit comments

Comments
 (0)