LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_io.c (source / functions) Coverage Total Hit UBC GBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 95.4 % 197 188 9 3 185
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 15 15 15
Baseline: lcov-20250906-005545-baseline Branches: 89.4 % 235 210 25 2 208
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 92.3 % 52 48 4 3 45
(360..) days: 96.6 % 145 140 5 140
Function coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 100.0 % 13 13 13
Branch coverage date bins:
(30,360] days: 87.5 % 96 84 12 2 82
(360..) days: 90.6 % 139 126 13 126

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /* -------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgstat_io.c
                                  4                 :                :  *    Implementation of IO statistics.
                                  5                 :                :  *
                                  6                 :                :  * This file contains the implementation of IO statistics. It is kept separate
                                  7                 :                :  * from pgstat.c to enforce the line between the statistics access / storage
                                  8                 :                :  * implementation and the details about individual types of statistics.
                                  9                 :                :  *
                                 10                 :                :  * Copyright (c) 2021-2025, PostgreSQL Global Development Group
                                 11                 :                :  *
                                 12                 :                :  * IDENTIFICATION
                                 13                 :                :  *    src/backend/utils/activity/pgstat_io.c
                                 14                 :                :  * -------------------------------------------------------------------------
                                 15                 :                :  */
                                 16                 :                : 
                                 17                 :                : #include "postgres.h"
                                 18                 :                : 
                                 19                 :                : #include "executor/instrument.h"
                                 20                 :                : #include "storage/bufmgr.h"
                                 21                 :                : #include "utils/pgstat_internal.h"
                                 22                 :                : 
                                 23                 :                : static PgStat_PendingIO PendingIOStats;
                                 24                 :                : static bool have_iostats = false;
                                 25                 :                : 
                                 26                 :                : /*
                                 27                 :                :  * Check that stats have not been counted for any combination of IOObject,
                                 28                 :                :  * IOContext, and IOOp which are not tracked for the passed-in BackendType. If
                                 29                 :                :  * stats are tracked for this combination and IO times are non-zero, counts
                                 30                 :                :  * should be non-zero.
                                 31                 :                :  *
                                 32                 :                :  * The passed-in PgStat_BktypeIO must contain stats from the BackendType
                                 33                 :                :  * specified by the second parameter. Caller is responsible for locking the
                                 34                 :                :  * passed-in PgStat_BktypeIO, if needed.
                                 35                 :                :  */
                                 36                 :                : bool
  941 andres@anarazel.de         37                 :CBC       64169 : pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io,
                                 38                 :                :                              BackendType bktype)
                                 39                 :                : {
  922 tgl@sss.pgh.pa.us          40         [ +  + ]:         256676 :     for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
                                 41                 :                :     {
                                 42         [ +  + ]:        1155042 :         for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
                                 43                 :                :         {
                                 44         [ +  + ]:        8662815 :             for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
                                 45                 :                :             {
                                 46                 :                :                 /* we do track it */
  883 andres@anarazel.de         47         [ +  + ]:        7700280 :                 if (pgstat_tracks_io_op(bktype, io_object, io_context, io_op))
                                 48                 :                :                 {
                                 49                 :                :                     /* ensure that if IO times are non-zero, counts are > 0 */
                                 50         [ +  + ]:        2369325 :                     if (backend_io->times[io_object][io_context][io_op] != 0 &&
                                 51         [ -  + ]:            360 :                         backend_io->counts[io_object][io_context][io_op] <= 0)
  883 andres@anarazel.de         52                 :UBC           0 :                         return false;
                                 53                 :                : 
  941 andres@anarazel.de         54                 :CBC     2369325 :                     continue;
                                 55                 :                :                 }
                                 56                 :                : 
                                 57                 :                :                 /* we don't track it, and it is not 0 */
  883                            58         [ -  + ]:        5330955 :                 if (backend_io->counts[io_object][io_context][io_op] != 0)
  941 andres@anarazel.de         59                 :UBC           0 :                     return false;
                                 60                 :                :             }
                                 61                 :                :         }
                                 62                 :                :     }
                                 63                 :                : 
  941 andres@anarazel.de         64                 :CBC       64169 :     return true;
                                 65                 :                : }
                                 66                 :                : 
                                 67                 :                : void
  235 michael@paquier.xyz        68                 :       62329852 : pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op,
                                 69                 :                :                    uint32 cnt, uint64 bytes)
                                 70                 :                : {
  921 tgl@sss.pgh.pa.us          71         [ -  + ]:       62329852 :     Assert((unsigned int) io_object < IOOBJECT_NUM_TYPES);
                                 72         [ -  + ]:       62329852 :     Assert((unsigned int) io_context < IOCONTEXT_NUM_TYPES);
  235 michael@paquier.xyz        73   [ +  -  +  +  :       62329852 :     Assert(pgstat_is_ioop_tracked_in_bytes(io_op) || bytes == 0);
                                              -  + ]
  941 andres@anarazel.de         74         [ -  + ]:       62329852 :     Assert(pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op));
                                 75                 :                : 
  883                            76                 :       62329852 :     PendingIOStats.counts[io_object][io_context][io_op] += cnt;
  235 michael@paquier.xyz        77                 :       62329852 :     PendingIOStats.bytes[io_object][io_context][io_op] += bytes;
                                 78                 :                : 
                                 79                 :                :     /* Add the per-backend counts */
  228                            80                 :       62329852 :     pgstat_count_backend_io_op(io_object, io_context, io_op, cnt, bytes);
                                 81                 :                : 
  941 andres@anarazel.de         82                 :       62329852 :     have_iostats = true;
   40 michael@paquier.xyz        83                 :       62329852 :     pgstat_report_fixed = true;
  941 andres@anarazel.de         84                 :       62329852 : }
                                 85                 :                : 
                                 86                 :                : /*
                                 87                 :                :  * Initialize the internal timing for an IO operation, depending on an
                                 88                 :                :  * IO timing GUC.
                                 89                 :                :  */
                                 90                 :                : instr_time
  192 michael@paquier.xyz        91                 :        5944302 : pgstat_prepare_io_time(bool track_io_guc)
                                 92                 :                : {
                                 93                 :                :     instr_time  io_start;
                                 94                 :                : 
                                 95         [ +  + ]:        5944302 :     if (track_io_guc)
  883 andres@anarazel.de         96                 :              3 :         INSTR_TIME_SET_CURRENT(io_start);
                                 97                 :                :     else
                                 98                 :                :     {
                                 99                 :                :         /*
                                100                 :                :          * There is no need to set io_start when an IO timing GUC is disabled.
                                101                 :                :          * Initialize it to zero to avoid compiler warnings and to let
                                102                 :                :          * pgstat_count_io_op_time() know that timings should be ignored.
                                103                 :                :          */
                                104                 :        5944299 :         INSTR_TIME_SET_ZERO(io_start);
                                105                 :                :     }
                                106                 :                : 
                                107                 :        5944302 :     return io_start;
                                108                 :                : }
                                109                 :                : 
                                110                 :                : /*
                                111                 :                :  * Like pgstat_count_io_op() except it also accumulates time.
                                112                 :                :  *
                                113                 :                :  * The calls related to pgstat_count_buffer_*() are for pgstat_database.  As
                                114                 :                :  * pg_stat_database only counts block read and write times, these are done for
                                115                 :                :  * IOOP_READ, IOOP_WRITE and IOOP_EXTEND.
                                116                 :                :  *
                                117                 :                :  * pgBufferUsage is used for EXPLAIN.  pgBufferUsage has write and read stats
                                118                 :                :  * for shared, local and temporary blocks.  pg_stat_io does not track the
                                119                 :                :  * activity of temporary blocks, so these are ignored here.
                                120                 :                :  */
                                121                 :                : void
  877 pg@bowt.ie                122                 :        5944286 : pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
                                123                 :                :                         instr_time start_time, uint32 cnt, uint64 bytes)
                                124                 :                : {
  192 michael@paquier.xyz       125         [ +  + ]:        5944286 :     if (!INSTR_TIME_IS_ZERO(start_time))
                                126                 :                :     {
                                127                 :                :         instr_time  io_time;
                                128                 :                : 
  883 andres@anarazel.de        129                 :              3 :         INSTR_TIME_SET_CURRENT(io_time);
                                130                 :              3 :         INSTR_TIME_SUBTRACT(io_time, start_time);
                                131                 :                : 
  214 michael@paquier.xyz       132         [ +  - ]:              3 :         if (io_object != IOOBJECT_WAL)
                                133                 :                :         {
                                134   [ +  +  -  + ]:              3 :             if (io_op == IOOP_WRITE || io_op == IOOP_EXTEND)
                                135                 :                :             {
  214 michael@paquier.xyz       136                 :GBC           1 :                 pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
                                137         [ +  - ]:              1 :                 if (io_object == IOOBJECT_RELATION)
                                138                 :              1 :                     INSTR_TIME_ADD(pgBufferUsage.shared_blk_write_time, io_time);
  214 michael@paquier.xyz       139         [ #  # ]:UBC           0 :                 else if (io_object == IOOBJECT_TEMP_RELATION)
                                140                 :              0 :                     INSTR_TIME_ADD(pgBufferUsage.local_blk_write_time, io_time);
                                141                 :                :             }
  214 michael@paquier.xyz       142         [ +  - ]:CBC           2 :             else if (io_op == IOOP_READ)
                                143                 :                :             {
                                144                 :              2 :                 pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
                                145         [ +  - ]:              2 :                 if (io_object == IOOBJECT_RELATION)
                                146                 :              2 :                     INSTR_TIME_ADD(pgBufferUsage.shared_blk_read_time, io_time);
  214 michael@paquier.xyz       147         [ #  # ]:UBC           0 :                 else if (io_object == IOOBJECT_TEMP_RELATION)
                                148                 :              0 :                     INSTR_TIME_ADD(pgBufferUsage.local_blk_read_time, io_time);
                                149                 :                :             }
                                150                 :                :         }
                                151                 :                : 
  877 pg@bowt.ie                152                 :CBC           3 :         INSTR_TIME_ADD(PendingIOStats.pending_times[io_object][io_context][io_op],
                                153                 :                :                        io_time);
                                154                 :                : 
                                155                 :                :         /* Add the per-backend count */
  228 michael@paquier.xyz       156                 :              3 :         pgstat_count_backend_io_op_time(io_object, io_context, io_op,
                                157                 :                :                                         io_time);
                                158                 :                :     }
                                159                 :                : 
  235                           160                 :        5944286 :     pgstat_count_io_op(io_object, io_context, io_op, cnt, bytes);
  883 andres@anarazel.de        161                 :        5944286 : }
                                162                 :                : 
                                163                 :                : PgStat_IO *
  941                           164                 :             73 : pgstat_fetch_stat_io(void)
                                165                 :                : {
                                166                 :             73 :     pgstat_snapshot_fixed(PGSTAT_KIND_IO);
                                167                 :                : 
                                168                 :             73 :     return &pgStatLocal.snapshot.io;
                                169                 :                : }
                                170                 :                : 
                                171                 :                : /*
                                172                 :                :  * Simpler wrapper of pgstat_io_flush_cb()
                                173                 :                :  */
                                174                 :                : void
  362 michael@paquier.xyz       175                 :          60965 : pgstat_flush_io(bool nowait)
                                176                 :                : {
                                177                 :          60965 :     (void) pgstat_io_flush_cb(nowait);
                                178                 :          60965 : }
                                179                 :                : 
                                180                 :                : /*
                                181                 :                :  * Flush out locally pending IO statistics
                                182                 :                :  *
                                183                 :                :  * If no stats have been recorded, this function returns false.
                                184                 :                :  *
                                185                 :                :  * If nowait is true, this function returns true if the lock could not be
                                186                 :                :  * acquired. Otherwise, return false.
                                187                 :                :  */
                                188                 :                : bool
                                189                 :          91962 : pgstat_io_flush_cb(bool nowait)
                                190                 :                : {
                                191                 :                :     LWLock     *bktype_lock;
                                192                 :                :     PgStat_BktypeIO *bktype_shstats;
                                193                 :                : 
  941 andres@anarazel.de        194         [ +  + ]:          91962 :     if (!have_iostats)
                                195                 :          29126 :         return false;
                                196                 :                : 
                                197                 :          62836 :     bktype_lock = &pgStatLocal.shmem->io.locks[MyBackendType];
                                198                 :          62836 :     bktype_shstats =
                                199                 :          62836 :         &pgStatLocal.shmem->io.stats.stats[MyBackendType];
                                200                 :                : 
                                201         [ +  + ]:          62836 :     if (!nowait)
                                202                 :          46154 :         LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
                                203         [ +  + ]:          16682 :     else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
                                204                 :              2 :         return true;
                                205                 :                : 
  922 tgl@sss.pgh.pa.us         206         [ +  + ]:         251336 :     for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
                                207                 :                :     {
                                208         [ +  + ]:        1131012 :         for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
                                209                 :                :         {
                                210         [ +  + ]:        8482590 :             for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
                                211                 :                :             {
                                212                 :                :                 instr_time  time;
                                213                 :                : 
  883 andres@anarazel.de        214                 :        7540080 :                 bktype_shstats->counts[io_object][io_context][io_op] +=
                                215                 :        7540080 :                     PendingIOStats.counts[io_object][io_context][io_op];
                                216                 :                : 
  235 michael@paquier.xyz       217                 :        7540080 :                 bktype_shstats->bytes[io_object][io_context][io_op] +=
                                218                 :        7540080 :                     PendingIOStats.bytes[io_object][io_context][io_op];
                                219                 :                : 
  883 andres@anarazel.de        220                 :        7540080 :                 time = PendingIOStats.pending_times[io_object][io_context][io_op];
                                221                 :                : 
                                222                 :        7540080 :                 bktype_shstats->times[io_object][io_context][io_op] +=
                                223                 :        7540080 :                     INSTR_TIME_GET_MICROSEC(time);
                                224                 :                :             }
                                225                 :                :         }
                                226                 :                :     }
                                227                 :                : 
  941                           228         [ -  + ]:          62834 :     Assert(pgstat_bktype_io_stats_valid(bktype_shstats, MyBackendType));
                                229                 :                : 
                                230                 :          62834 :     LWLockRelease(bktype_lock);
                                231                 :                : 
                                232                 :          62834 :     memset(&PendingIOStats, 0, sizeof(PendingIOStats));
                                233                 :                : 
                                234                 :          62834 :     have_iostats = false;
                                235                 :                : 
                                236                 :          62834 :     return false;
                                237                 :                : }
                                238                 :                : 
                                239                 :                : const char *
                                240                 :          15645 : pgstat_get_io_context_name(IOContext io_context)
                                241                 :                : {
                                242   [ +  +  +  +  :          15645 :     switch (io_context)
                                              +  - ]
                                243                 :                :     {
                                244                 :           3129 :         case IOCONTEXT_BULKREAD:
                                245                 :           3129 :             return "bulkread";
                                246                 :           3129 :         case IOCONTEXT_BULKWRITE:
                                247                 :           3129 :             return "bulkwrite";
  214 michael@paquier.xyz       248                 :           3129 :         case IOCONTEXT_INIT:
                                249                 :           3129 :             return "init";
  941 andres@anarazel.de        250                 :           3129 :         case IOCONTEXT_NORMAL:
                                251                 :           3129 :             return "normal";
                                252                 :           3129 :         case IOCONTEXT_VACUUM:
                                253                 :           3129 :             return "vacuum";
                                254                 :                :     }
                                255                 :                : 
  941 andres@anarazel.de        256         [ #  # ]:UBC           0 :     elog(ERROR, "unrecognized IOContext value: %d", io_context);
                                257                 :                :     pg_unreachable();
                                258                 :                : }
                                259                 :                : 
                                260                 :                : const char *
  941 andres@anarazel.de        261                 :CBC        3129 : pgstat_get_io_object_name(IOObject io_object)
                                262                 :                : {
                                263   [ +  +  +  - ]:           3129 :     switch (io_object)
                                264                 :                :     {
                                265                 :           1043 :         case IOOBJECT_RELATION:
                                266                 :           1043 :             return "relation";
                                267                 :           1043 :         case IOOBJECT_TEMP_RELATION:
                                268                 :           1043 :             return "temp relation";
  214 michael@paquier.xyz       269                 :           1043 :         case IOOBJECT_WAL:
                                270                 :           1043 :             return "wal";
                                271                 :                :     }
                                272                 :                : 
  941 andres@anarazel.de        273         [ #  # ]:UBC           0 :     elog(ERROR, "unrecognized IOObject value: %d", io_object);
                                274                 :                :     pg_unreachable();
                                275                 :                : }
                                276                 :                : 
                                277                 :                : void
  422 michael@paquier.xyz       278                 :CBC        1029 : pgstat_io_init_shmem_cb(void *stats)
                                279                 :                : {
                                280                 :           1029 :     PgStatShared_IO *stat_shmem = (PgStatShared_IO *) stats;
                                281                 :                : 
                                282         [ +  + ]:          19551 :     for (int i = 0; i < BACKEND_NUM_TYPES; i++)
                                283                 :          18522 :         LWLockInitialize(&stat_shmem->locks[i], LWTRANCHE_PGSTATS_DATA);
                                284                 :           1029 : }
                                285                 :                : 
                                286                 :                : void
  941 andres@anarazel.de        287                 :            227 : pgstat_io_reset_all_cb(TimestampTz ts)
                                288                 :                : {
                                289         [ +  + ]:           4313 :     for (int i = 0; i < BACKEND_NUM_TYPES; i++)
                                290                 :                :     {
                                291                 :           4086 :         LWLock     *bktype_lock = &pgStatLocal.shmem->io.locks[i];
                                292                 :           4086 :         PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
                                293                 :                : 
                                294                 :           4086 :         LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
                                295                 :                : 
                                296                 :                :         /*
                                297                 :                :          * Use the lock in the first BackendType's PgStat_BktypeIO to protect
                                298                 :                :          * the reset timestamp as well.
                                299                 :                :          */
                                300         [ +  + ]:           4086 :         if (i == 0)
                                301                 :            227 :             pgStatLocal.shmem->io.stats.stat_reset_timestamp = ts;
                                302                 :                : 
                                303                 :           4086 :         memset(bktype_shstats, 0, sizeof(*bktype_shstats));
                                304                 :           4086 :         LWLockRelease(bktype_lock);
                                305                 :                :     }
                                306                 :            227 : }
                                307                 :                : 
                                308                 :                : void
                                309                 :            704 : pgstat_io_snapshot_cb(void)
                                310                 :                : {
                                311         [ +  + ]:          13376 :     for (int i = 0; i < BACKEND_NUM_TYPES; i++)
                                312                 :                :     {
                                313                 :          12672 :         LWLock     *bktype_lock = &pgStatLocal.shmem->io.locks[i];
                                314                 :          12672 :         PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
                                315                 :          12672 :         PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io.stats[i];
                                316                 :                : 
                                317                 :          12672 :         LWLockAcquire(bktype_lock, LW_SHARED);
                                318                 :                : 
                                319                 :                :         /*
                                320                 :                :          * Use the lock in the first BackendType's PgStat_BktypeIO to protect
                                321                 :                :          * the reset timestamp as well.
                                322                 :                :          */
                                323         [ +  + ]:          12672 :         if (i == 0)
                                324                 :            704 :             pgStatLocal.snapshot.io.stat_reset_timestamp =
                                325                 :            704 :                 pgStatLocal.shmem->io.stats.stat_reset_timestamp;
                                326                 :                : 
                                327                 :                :         /* using struct assignment due to better type safety */
                                328                 :          12672 :         *bktype_snap = *bktype_shstats;
                                329                 :          12672 :         LWLockRelease(bktype_lock);
                                330                 :                :     }
                                331                 :            704 : }
                                332                 :                : 
                                333                 :                : /*
                                334                 :                : * IO statistics are not collected for all BackendTypes.
                                335                 :                : *
                                336                 :                : * The following BackendTypes do not participate in the cumulative stats
                                337                 :                : * subsystem or do not perform IO on which we currently track:
                                338                 :                : * - Dead-end backend because it is not connected to shared memory and
                                339                 :                : *   doesn't do any IO
                                340                 :                : * - Syslogger because it is not connected to shared memory
                                341                 :                : * - Archiver because most relevant archiving IO is delegated to a
                                342                 :                : *   specialized command or module
                                343                 :                : *
                                344                 :                : * Function returns true if BackendType participates in the cumulative stats
                                345                 :                : * subsystem for IO and false if it does not.
                                346                 :                : *
                                347                 :                : * When adding a new BackendType, also consider adding relevant restrictions to
                                348                 :                : * pgstat_tracks_io_object() and pgstat_tracks_io_op().
                                349                 :                : */
                                350                 :                : bool
                                351                 :      125168433 : pgstat_tracks_io_bktype(BackendType bktype)
                                352                 :                : {
                                353                 :                :     /*
                                354                 :                :      * List every type so that new backend types trigger a warning about
                                355                 :                :      * needing to adjust this switch.
                                356                 :                :      */
                                357      [ +  +  - ]:      125168433 :     switch (bktype)
                                358                 :                :     {
                                359                 :          35332 :         case B_INVALID:
                                360                 :                :         case B_DEAD_END_BACKEND:
                                361                 :                :         case B_ARCHIVER:
                                362                 :                :         case B_LOGGER:
                                363                 :          35332 :             return false;
                                364                 :                : 
                                365                 :      125133101 :         case B_AUTOVAC_LAUNCHER:
                                366                 :                :         case B_AUTOVAC_WORKER:
                                367                 :                :         case B_BACKEND:
                                368                 :                :         case B_BG_WORKER:
                                369                 :                :         case B_BG_WRITER:
                                370                 :                :         case B_CHECKPOINTER:
                                371                 :                :         case B_IO_WORKER:
                                372                 :                :         case B_SLOTSYNC_WORKER:
                                373                 :                :         case B_STANDALONE_BACKEND:
                                374                 :                :         case B_STARTUP:
                                375                 :                :         case B_WAL_RECEIVER:
                                376                 :                :         case B_WAL_SENDER:
                                377                 :                :         case B_WAL_SUMMARIZER:
                                378                 :                :         case B_WAL_WRITER:
                                379                 :      125133101 :             return true;
                                380                 :                :     }
                                381                 :                : 
  941 andres@anarazel.de        382                 :UBC           0 :     return false;
                                383                 :                : }
                                384                 :                : 
                                385                 :                : /*
                                386                 :                :  * Some BackendTypes do not perform IO on certain IOObjects or in certain
                                387                 :                :  * IOContexts. Some IOObjects are never operated on in some IOContexts. Check
                                388                 :                :  * that the given BackendType is expected to do IO in the given IOContext and
                                389                 :                :  * on the given IOObject and that the given IOObject is expected to be operated
                                390                 :                :  * on in the given IOContext.
                                391                 :                :  */
                                392                 :                : bool
  941 andres@anarazel.de        393                 :CBC   125167119 : pgstat_tracks_io_object(BackendType bktype, IOObject io_object,
                                394                 :                :                         IOContext io_context)
                                395                 :                : {
                                396                 :                :     bool        no_temp_rel;
                                397                 :                : 
                                398                 :                :     /*
                                399                 :                :      * Some BackendTypes should never track IO statistics.
                                400                 :                :      */
                                401         [ +  + ]:      125167119 :     if (!pgstat_tracks_io_bktype(bktype))
                                402                 :          35040 :         return false;
                                403                 :                : 
                                404                 :                :     /*
                                405                 :                :      * Currently, IO on IOOBJECT_WAL objects can only occur in the
                                406                 :                :      * IOCONTEXT_NORMAL and IOCONTEXT_INIT IOContexts.
                                407                 :                :      */
  214 michael@paquier.xyz       408   [ +  +  +  + ]:      125132079 :     if (io_object == IOOBJECT_WAL &&
                                409         [ +  + ]:        2061468 :         (io_context != IOCONTEXT_NORMAL &&
                                410                 :                :          io_context != IOCONTEXT_INIT))
                                411                 :        1536177 :         return false;
                                412                 :                : 
                                413                 :                :     /*
                                414                 :                :      * Currently, IO on temporary relations can only occur in the
                                415                 :                :      * IOCONTEXT_NORMAL IOContext.
                                416                 :                :      */
  941 andres@anarazel.de        417   [ +  +  +  + ]:      123595902 :     if (io_context != IOCONTEXT_NORMAL &&
                                418                 :                :         io_object == IOOBJECT_TEMP_RELATION)
                                419                 :        2048236 :         return false;
                                420                 :                : 
                                421                 :                :     /*
                                422                 :                :      * In core Postgres, only regular backends and WAL Sender processes
                                423                 :                :      * executing queries will use local buffers and operate on temporary
                                424                 :                :      * relations. Parallel workers will not use local buffers (see
                                425                 :                :      * InitLocalBuffers()); however, extensions leveraging background workers
                                426                 :                :      * have no such limitation, so track IO on IOOBJECT_TEMP_RELATION for
                                427                 :                :      * BackendType B_BG_WORKER.
                                428                 :                :      */
                                429   [ +  +  +  + ]:      121515941 :     no_temp_rel = bktype == B_AUTOVAC_LAUNCHER || bktype == B_BG_WRITER ||
                                430   [ +  +  +  + ]:      120794481 :         bktype == B_CHECKPOINTER || bktype == B_AUTOVAC_WORKER ||
  185 michael@paquier.xyz       431   [ +  +  +  + ]:      109754055 :         bktype == B_STANDALONE_BACKEND || bktype == B_STARTUP ||
                                432   [ +  +  +  +  :      243063607 :         bktype == B_WAL_SUMMARIZER || bktype == B_WAL_WRITER ||
                                              +  + ]
                                433                 :                :         bktype == B_WAL_RECEIVER;
                                434                 :                : 
  941 andres@anarazel.de        435   [ +  +  +  +  :      121547666 :     if (no_temp_rel && io_context == IOCONTEXT_NORMAL &&
                                              +  + ]
                                436                 :                :         io_object == IOOBJECT_TEMP_RELATION)
                                437                 :         172977 :         return false;
                                438                 :                : 
                                439                 :                :     /*
                                440                 :                :      * Some BackendTypes only perform IO under IOOBJECT_WAL, hence exclude all
                                441                 :                :      * rows for all the other objects for these.
                                442                 :                :      */
  185 michael@paquier.xyz       443   [ +  +  +  +  :      121374689 :     if ((bktype == B_WAL_SUMMARIZER || bktype == B_WAL_RECEIVER ||
                                              +  + ]
                                444         [ +  + ]:         402801 :          bktype == B_WAL_WRITER) && io_object != IOOBJECT_WAL)
                                445                 :         135975 :         return false;
                                446                 :                : 
                                447                 :                :     /*
                                448                 :                :      * Some BackendTypes do not currently perform any IO in certain
                                449                 :                :      * IOContexts, and, while it may not be inherently incorrect for them to
                                450                 :                :      * do so, excluding those rows from the view makes the view easier to use.
                                451                 :                :      */
  941 andres@anarazel.de        452   [ +  +  +  +  :      121238714 :     if ((bktype == B_CHECKPOINTER || bktype == B_BG_WRITER) &&
                                              +  + ]
                                453         [ +  + ]:         606032 :         (io_context == IOCONTEXT_BULKREAD ||
                                454         [ +  + ]:         548318 :          io_context == IOCONTEXT_BULKWRITE ||
                                455                 :                :          io_context == IOCONTEXT_VACUUM))
                                456                 :         173142 :         return false;
                                457                 :                : 
                                458   [ +  +  +  + ]:      121065572 :     if (bktype == B_AUTOVAC_LAUNCHER && io_context == IOCONTEXT_VACUUM)
                                459                 :           3505 :         return false;
                                460                 :                : 
                                461   [ +  +  +  +  :      121062067 :     if ((bktype == B_AUTOVAC_WORKER || bktype == B_AUTOVAC_LAUNCHER) &&
                                              +  + ]
                                462                 :                :         io_context == IOCONTEXT_BULKWRITE)
                                463                 :           8026 :         return false;
                                464                 :                : 
                                465                 :      121054041 :     return true;
                                466                 :                : }
                                467                 :                : 
                                468                 :                : /*
                                469                 :                :  * Some BackendTypes will never do certain IOOps and some IOOps should not
                                470                 :                :  * occur in certain IOContexts or on certain IOObjects. Check that the given
                                471                 :                :  * IOOp is valid for the given BackendType in the given IOContext and on the
                                472                 :                :  * given IOObject. Note that there are currently no cases of an IOOp being
                                473                 :                :  * invalid for a particular BackendType only within a certain IOContext and/or
                                474                 :                :  * only on a certain IOObject.
                                475                 :                :  */
                                476                 :                : bool
                                477                 :      125151474 : pgstat_tracks_io_op(BackendType bktype, IOObject io_object,
                                478                 :                :                     IOContext io_context, IOOp io_op)
                                479                 :                : {
                                480                 :                :     bool        strategy_io_context;
                                481                 :                : 
                                482                 :                :     /* if (io_context, io_object) will never collect stats, we're done */
                                483         [ +  + ]:      125151474 :     if (!pgstat_tracks_io_object(bktype, io_object, io_context))
                                484                 :        4103368 :         return false;
                                485                 :                : 
                                486                 :                :     /*
                                487                 :                :      * Some BackendTypes will not do certain IOOps.
                                488                 :                :      */
  214 michael@paquier.xyz       489   [ +  +  +  + ]:      121048106 :     if (bktype == B_BG_WRITER &&
  891 andres@anarazel.de        490   [ +  +  +  + ]:          94811 :         (io_op == IOOP_READ || io_op == IOOP_EVICT || io_op == IOOP_HIT))
  941                           491                 :          29556 :         return false;
                                492                 :                : 
  214 michael@paquier.xyz       493   [ +  +  +  + ]:      121018550 :     if (bktype == B_CHECKPOINTER &&
                                494   [ +  +  +  + ]:         385357 :         ((io_object != IOOBJECT_WAL && io_op == IOOP_READ) ||
                                495         [ +  + ]:         356083 :          (io_op == IOOP_EVICT || io_op == IOOP_HIT)))
                                496                 :          48790 :         return false;
                                497                 :                : 
  941 andres@anarazel.de        498   [ +  +  +  +  :      120969760 :     if ((bktype == B_AUTOVAC_LAUNCHER || bktype == B_BG_WRITER ||
                                              +  + ]
                                499         [ +  + ]:         432519 :          bktype == B_CHECKPOINTER) && io_op == IOOP_EXTEND)
                                500                 :          31878 :         return false;
                                501                 :                : 
                                502                 :                :     /*
                                503                 :                :      * Some BackendTypes do not perform reads with IOOBJECT_WAL.
                                504                 :                :      */
  214 michael@paquier.xyz       505   [ +  +  +  +  :      120937882 :     if (io_object == IOOBJECT_WAL && io_op == IOOP_READ &&
                                              +  + ]
                                506   [ +  -  +  + ]:        1802389 :         (bktype == B_WAL_RECEIVER || bktype == B_BG_WRITER ||
  171                           507   [ +  +  +  + ]:        1801385 :          bktype == B_AUTOVAC_LAUNCHER || bktype == B_AUTOVAC_WORKER ||
                                508                 :                :          bktype == B_WAL_WRITER))
  214                           509                 :           9146 :         return false;
                                510                 :                : 
                                511                 :                :     /*
                                512                 :                :      * Temporary tables are not logged and thus do not require fsync'ing.
                                513                 :                :      * Writeback is not requested for temporary tables.
                                514                 :                :      */
  843 andres@anarazel.de        515   [ +  +  +  + ]:      120928736 :     if (io_object == IOOBJECT_TEMP_RELATION &&
                                516         [ +  + ]:        3402917 :         (io_op == IOOP_FSYNC || io_op == IOOP_WRITEBACK))
                                517                 :          85446 :         return false;
                                518                 :                : 
                                519                 :                :     /*
                                520                 :                :      * Some IOOps are not valid in certain IOContexts and some IOOps are only
                                521                 :                :      * valid in certain contexts.
                                522                 :                :      */
  941                           523   [ +  +  +  + ]:      120843290 :     if (io_context == IOCONTEXT_BULKREAD && io_op == IOOP_EXTEND)
                                524                 :          53485 :         return false;
                                525                 :                : 
                                526         [ +  + ]:      117923450 :     strategy_io_context = io_context == IOCONTEXT_BULKREAD ||
                                527   [ +  +  +  + ]:      238713255 :         io_context == IOCONTEXT_BULKWRITE || io_context == IOCONTEXT_VACUUM;
                                528                 :                : 
                                529                 :                :     /*
                                530                 :                :      * IOOP_REUSE is only relevant when a BufferAccessStrategy is in use.
                                531                 :                :      */
                                532   [ +  +  +  + ]:      120789805 :     if (!strategy_io_context && io_op == IOOP_REUSE)
                                533                 :         295221 :         return false;
                                534                 :                : 
                                535                 :                :     /*
                                536                 :                :      * IOOBJECT_WAL IOObject will not do certain IOOps depending on IOContext.
                                537                 :                :      */
  214 michael@paquier.xyz       538   [ +  +  +  +  :      120494584 :     if (io_object == IOOBJECT_WAL && io_context == IOCONTEXT_INIT &&
                                              +  + ]
                                539         [ +  + ]:         362400 :         !(io_op == IOOP_WRITE || io_op == IOOP_FSYNC))
                                540                 :         295036 :         return false;
                                541                 :                : 
                                542   [ +  +  +  +  :      120199548 :     if (io_object == IOOBJECT_WAL && io_context == IOCONTEXT_NORMAL &&
                                              +  + ]
                                543   [ +  +  +  + ]:        2037957 :         !(io_op == IOOP_WRITE || io_op == IOOP_READ || io_op == IOOP_FSYNC))
                                544                 :         237152 :         return false;
                                545                 :                : 
                                546                 :                :     /*
                                547                 :                :      * IOOP_FSYNC IOOps done by a backend using a BufferAccessStrategy are
                                548                 :                :      * counted in the IOCONTEXT_NORMAL IOContext. See comment in
                                549                 :                :      * register_dirty_segment() for more details.
                                550                 :                :      */
  941 andres@anarazel.de        551   [ +  +  +  + ]:      119962396 :     if (strategy_io_context && io_op == IOOP_FSYNC)
                                552                 :         160328 :         return false;
                                553                 :                : 
                                554                 :                : 
                                555                 :      119802068 :     return true;
                                556                 :                : }
        

Generated by: LCOV version 2.4-beta