Suppress compiler warnings in new pgstats code.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 27 Feb 2023 22:21:31 +0000 (17:21 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 27 Feb 2023 22:21:31 +0000 (17:21 -0500)
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

src/backend/utils/activity/pgstat_io.c
src/backend/utils/adt/pgstatfuncs.c
src/include/pgstat.h

index 0e07e0848d321f907b5dfff3283cac24c4f00e4b..c4199d18c8a43fd1230deade7bccebe177f21c44 100644 (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];
                }
index 9d707c35216a87f62ca82efd3c08b9bf1f24de56..b61a12382b309e5ce1de1ce642ce855b8c9e2cc4 100644 (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);
 
index db9675884f368063853e5bce573c9fb62be31a19..f43fac09ede65d91e4724d321866a0d78432c942 100644 (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