Suppress compiler warnings in new pgstats code.

Some clang versions whine about comparing an enum variable to
a value outside the range of the enum, on the grounds that the
result must be constant.  In the cases we fix here, the loops
will terminate only if the enum variable can in fact hold a
value one beyond its declared range.  While that's very likely
to always be true for these enum types, it still seems like a
poor coding practice to assume it; so use "int" loop variables
instead to silence the warnings.  (This matches what we've done
in other places, for example loops over the range of ForkNumber.)

While at it, let's drop the XXX_FIRST macros for these enums and just
write zeroes for the loop start values.  The apparent flexibility
seems rather illusory given that iterating up to one-less-than-
the-number-of-values is only correct for a zero-based range.

Melanie Plageman

Discussion: https://postgr.es/m/20520.1677435600@sss.pgh.pa.us
This commit is contained in:
Tom Lane 2023-02-27 17:21:31 -05:00
parent 53fe7e6cb8
commit 728560db7d
3 changed files with 10 additions and 20 deletions

View File

@ -36,18 +36,16 @@ pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io,
{
bool bktype_tracked = pgstat_tracks_io_bktype(bktype);
for (IOObject io_object = IOOBJECT_FIRST;
io_object < IOOBJECT_NUM_TYPES; io_object++)
for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
{
for (IOContext io_context = IOCONTEXT_FIRST;
io_context < IOCONTEXT_NUM_TYPES; io_context++)
for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
{
/*
* Don't bother trying to skip to the next loop iteration if
* pgstat_tracks_io_object() would return false here. We still
* need to validate that each counter is zero anyway.
*/
for (IOOp io_op = IOOP_FIRST; io_op < IOOP_NUM_TYPES; io_op++)
for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
{
/* No stats, so nothing to validate */
if (backend_io->data[io_object][io_context][io_op] == 0)
@ -111,14 +109,11 @@ pgstat_flush_io(bool nowait)
else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
return true;
for (IOObject io_object = IOOBJECT_FIRST;
io_object < IOOBJECT_NUM_TYPES; io_object++)
for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
{
for (IOContext io_context = IOCONTEXT_FIRST;
io_context < IOCONTEXT_NUM_TYPES; io_context++)
for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
{
for (IOOp io_op = IOOP_FIRST;
io_op < IOOP_NUM_TYPES; io_op++)
for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
bktype_shstats->data[io_object][io_context][io_op] +=
PendingIOStats.data[io_object][io_context][io_op];
}

View File

@ -1306,7 +1306,7 @@ pg_stat_get_io(PG_FUNCTION_ARGS)
reset_time = TimestampTzGetDatum(backends_io_stats->stat_reset_timestamp);
for (BackendType bktype = B_INVALID; bktype < BACKEND_NUM_TYPES; bktype++)
for (int bktype = 0; bktype < BACKEND_NUM_TYPES; bktype++)
{
Datum bktype_desc = CStringGetTextDatum(GetBackendTypeDesc(bktype));
PgStat_BktypeIO *bktype_stats = &backends_io_stats->stats[bktype];
@ -1325,13 +1325,11 @@ pg_stat_get_io(PG_FUNCTION_ARGS)
if (!pgstat_tracks_io_bktype(bktype))
continue;
for (IOObject io_obj = IOOBJECT_FIRST;
io_obj < IOOBJECT_NUM_TYPES; io_obj++)
for (int io_obj = 0; io_obj < IOOBJECT_NUM_TYPES; io_obj++)
{
const char *obj_name = pgstat_get_io_object_name(io_obj);
for (IOContext io_context = IOCONTEXT_FIRST;
io_context < IOCONTEXT_NUM_TYPES; io_context++)
for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
{
const char *context_name = pgstat_get_io_context_name(io_context);
@ -1359,7 +1357,7 @@ pg_stat_get_io(PG_FUNCTION_ARGS)
*/
values[IO_COL_CONVERSION] = Int64GetDatum(BLCKSZ);
for (IOOp io_op = IOOP_FIRST; io_op < IOOP_NUM_TYPES; io_op++)
for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
{
int col_idx = pgstat_get_io_op_index(io_op);

View File

@ -287,7 +287,6 @@ typedef enum IOObject
IOOBJECT_TEMP_RELATION,
} IOObject;
#define IOOBJECT_FIRST IOOBJECT_RELATION
#define IOOBJECT_NUM_TYPES (IOOBJECT_TEMP_RELATION + 1)
typedef enum IOContext
@ -298,7 +297,6 @@ typedef enum IOContext
IOCONTEXT_VACUUM,
} IOContext;
#define IOCONTEXT_FIRST IOCONTEXT_BULKREAD
#define IOCONTEXT_NUM_TYPES (IOCONTEXT_VACUUM + 1)
typedef enum IOOp
@ -311,7 +309,6 @@ typedef enum IOOp
IOOP_WRITE,
} IOOp;
#define IOOP_FIRST IOOP_EVICT
#define IOOP_NUM_TYPES (IOOP_WRITE + 1)
typedef struct PgStat_BktypeIO