LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_io.c (source / functions) Coverage Total Hit LBC UBC GNC CBC ECB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 94.7 % 209 198 3 8 19 179 1 2
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 15 15 3 12
Baseline: lcov-20261128-baseline Branches: 89.6 % 259 232 2 25 40 192
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 23 23 19 4
(360..) days: 94.1 % 185 174 3 8 174 1
Function coverage date bins:
(360..) days: 100.0 % 15 15 3 12
Branch coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 96.2 % 52 50 2 40 10
(360..) days: 87.8 % 205 180 2 23 180

 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-2026, 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.
                                 29                 :                :  * Non-zero time with a zero operation count is allowed as there are cases
                                 30                 :                :  * where this may be appropriate -- like when a backend is waiting on IO
                                 31                 :                :  * initiated by another backend.
                                 32                 :                :  *
                                 33                 :                :  * The passed-in PgStat_BktypeIO must contain stats from the BackendType
                                 34                 :                :  * specified by the second parameter. Caller is responsible for locking the
                                 35                 :                :  * passed-in PgStat_BktypeIO, if needed.
                                 36                 :                :  */
                                 37                 :                : bool
 1389 andres@anarazel.de         38                 :CBC       95967 : pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io,
                                 39                 :                :                              BackendType bktype)
                                 40                 :                : {
 1370 tgl@sss.pgh.pa.us          41         [ +  + ]:         383868 :     for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
                                 42                 :                :     {
                                 43         [ +  + ]:        1727406 :         for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
                                 44                 :                :         {
                                 45         [ +  + ]:       12955545 :             for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
                                 46                 :                :             {
                                 47                 :                :                 /* we don't track it, and it is not 0 */
  120 melanieplageman@gmai       48         [ +  + ]:       11516040 :                 if (!pgstat_tracks_io_op(bktype, io_object, io_context, io_op) &&
                                 49         [ +  - ]:        8051696 :                     (backend_io->counts[io_object][io_context][io_op] != 0 ||
                                 50         [ -  + ]:        8051696 :                      backend_io->times[io_object][io_context][io_op] != 0))
 1389 andres@anarazel.de         51                 :UBC           0 :                     return false;
                                 52                 :                :             }
                                 53                 :                :         }
                                 54                 :                :     }
                                 55                 :                : 
 1389 andres@anarazel.de         56                 :CBC       95967 :     return true;
                                 57                 :                : }
                                 58                 :                : 
                                 59                 :                : void
  683 michael@paquier.xyz        60                 :       92291271 : pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op,
                                 61                 :                :                    uint32 cnt, uint64 bytes)
                                 62                 :                : {
 1369 tgl@sss.pgh.pa.us          63         [ -  + ]:       92291271 :     Assert((unsigned int) io_object < IOOBJECT_NUM_TYPES);
                                 64         [ -  + ]:       92291271 :     Assert((unsigned int) io_context < IOCONTEXT_NUM_TYPES);
  683 michael@paquier.xyz        65   [ +  -  +  +  :       92291271 :     Assert(pgstat_is_ioop_tracked_in_bytes(io_op) || bytes == 0);
                                              -  + ]
 1389 andres@anarazel.de         66         [ -  + ]:       92291271 :     Assert(pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op));
                                 67                 :                : 
 1331                            68                 :       92291271 :     PendingIOStats.counts[io_object][io_context][io_op] += cnt;
  683 michael@paquier.xyz        69                 :       92291271 :     PendingIOStats.bytes[io_object][io_context][io_op] += bytes;
                                 70                 :                : 
                                 71                 :                :     /* Add the per-backend counts */
  676                            72                 :       92291271 :     pgstat_count_backend_io_op(io_object, io_context, io_op, cnt, bytes);
                                 73                 :                : 
 1389 andres@anarazel.de         74                 :       92291271 :     have_iostats = true;
  488 michael@paquier.xyz        75                 :       92291271 :     pgstat_report_fixed = true;
 1389 andres@anarazel.de         76                 :       92291271 : }
                                 77                 :                : 
                                 78                 :                : /*
                                 79                 :                :  * Initialize the internal timing for an IO operation, depending on an
                                 80                 :                :  * IO timing GUC.
                                 81                 :                :  */
                                 82                 :                : instr_time
  640 michael@paquier.xyz        83                 :        6923056 : pgstat_prepare_io_time(bool track_io_guc)
                                 84                 :                : {
                                 85                 :                :     instr_time  io_start;
                                 86                 :                : 
                                 87         [ +  + ]:        6923056 :     if (track_io_guc)
 1331 andres@anarazel.de         88                 :              2 :         INSTR_TIME_SET_CURRENT(io_start);
                                 89                 :                :     else
                                 90                 :                :     {
                                 91                 :                :         /*
                                 92                 :                :          * There is no need to set io_start when an IO timing GUC is disabled.
                                 93                 :                :          * Initialize it to zero to avoid compiler warnings and to let
                                 94                 :                :          * pgstat_count_io_op_time() know that timings should be ignored.
                                 95                 :                :          */
                                 96                 :        6923054 :         INSTR_TIME_SET_ZERO(io_start);
                                 97                 :                :     }
                                 98                 :                : 
                                 99                 :        6923056 :     return io_start;
                                100                 :                : }
                                101                 :                : 
                                102                 :                : /*
                                103                 :                :  * Like pgstat_count_io_op() except it also accumulates time.
                                104                 :                :  *
                                105                 :                :  * The calls related to pgstat_count_buffer_*() are for pgstat_database.  As
                                106                 :                :  * pg_stat_database only counts block read and write times, these are done for
                                107                 :                :  * IOOP_READ, IOOP_WRITE and IOOP_EXTEND.
                                108                 :                :  *
                                109                 :                :  * pgBufferUsage is used for EXPLAIN.  pgBufferUsage has write and read stats
                                110                 :                :  * for shared, local and temporary blocks.  pg_stat_io does not track the
                                111                 :                :  * activity of temporary blocks, so these are ignored here.
                                112                 :                :  */
                                113                 :                : void
 1325 pg@bowt.ie                114                 :        6923040 : pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
                                115                 :                :                         instr_time start_time, uint32 cnt, uint64 bytes)
                                116                 :                : {
  640 michael@paquier.xyz       117         [ +  + ]:        6923040 :     if (!INSTR_TIME_IS_ZERO(start_time))
                                118                 :                :     {
                                119                 :                :         instr_time  io_time;
                                120                 :                : 
 1331 andres@anarazel.de        121                 :              2 :         INSTR_TIME_SET_CURRENT(io_time);
                                122                 :              2 :         INSTR_TIME_SUBTRACT(io_time, start_time);
                                123                 :                : 
  662 michael@paquier.xyz       124         [ +  - ]:              2 :         if (io_object != IOOBJECT_WAL)
                                125                 :                :         {
                                126   [ +  -  -  + ]:              2 :             if (io_op == IOOP_WRITE || io_op == IOOP_EXTEND)
                                127                 :                :             {
  662 michael@paquier.xyz       128                 :LBC         (1) :                 pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
                                129         [ #  # ]:            (1) :                 if (io_object == IOOBJECT_RELATION)
                                130                 :            (1) :                     INSTR_TIME_ADD(pgBufferUsage.shared_blk_write_time, io_time);
  662 michael@paquier.xyz       131         [ #  # ]:UBC           0 :                 else if (io_object == IOOBJECT_TEMP_RELATION)
                                132                 :              0 :                     INSTR_TIME_ADD(pgBufferUsage.local_blk_write_time, io_time);
                                133                 :                :             }
  662 michael@paquier.xyz       134         [ +  - ]:CBC           2 :             else if (io_op == IOOP_READ)
                                135                 :                :             {
                                136                 :              2 :                 pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time));
                                137         [ +  - ]:              2 :                 if (io_object == IOOBJECT_RELATION)
                                138                 :              2 :                     INSTR_TIME_ADD(pgBufferUsage.shared_blk_read_time, io_time);
  662 michael@paquier.xyz       139         [ #  # ]:UBC           0 :                 else if (io_object == IOOBJECT_TEMP_RELATION)
                                140                 :              0 :                     INSTR_TIME_ADD(pgBufferUsage.local_blk_read_time, io_time);
                                141                 :                :             }
                                142                 :                :         }
                                143                 :                : 
 1325 pg@bowt.ie                144                 :CBC           2 :         INSTR_TIME_ADD(PendingIOStats.pending_times[io_object][io_context][io_op],
                                145                 :                :                        io_time);
                                146                 :                : 
                                147                 :                :         /* Add the per-backend count */
  676 michael@paquier.xyz       148                 :              2 :         pgstat_count_backend_io_op_time(io_object, io_context, io_op,
                                149                 :                :                                         io_time);
                                150                 :                :     }
                                151                 :                : 
  683                           152                 :        6923040 :     pgstat_count_io_op(io_object, io_context, io_op, cnt, bytes);
 1331 andres@anarazel.de        153                 :        6923040 : }
                                154                 :                : 
                                155                 :                : PgStat_IO *
 1389                           156                 :             94 : pgstat_fetch_stat_io(void)
                                157                 :                : {
                                158                 :             94 :     pgstat_snapshot_fixed(PGSTAT_KIND_IO);
                                159                 :                : 
                                160                 :             94 :     return &pgStatLocal.snapshot.io;
                                161                 :                : }
                                162                 :                : 
                                163                 :                : /*
                                164                 :                :  * Simpler wrapper of pgstat_io_flush_cb()
                                165                 :                :  */
                                166                 :                : void
  810 michael@paquier.xyz       167                 :         105633 : pgstat_flush_io(bool nowait)
                                168                 :                : {
                                169                 :         105633 :     (void) pgstat_io_flush_cb(nowait);
                                170                 :         105633 : }
                                171                 :                : 
                                172                 :                : /*
                                173                 :                :  * Flush out locally pending IO statistics
                                174                 :                :  *
                                175                 :                :  * If no stats have been recorded, this function returns false.
                                176                 :                :  *
                                177                 :                :  * If nowait is true, this function returns true if the lock could not be
                                178                 :                :  * acquired. Otherwise, return false.
                                179                 :                :  */
                                180                 :                : bool
                                181                 :         150587 : pgstat_io_flush_cb(bool nowait)
                                182                 :                : {
                                183                 :                :     LWLock     *bktype_lock;
                                184                 :                :     PgStat_BktypeIO *bktype_shstats;
                                185                 :                : 
 1389 andres@anarazel.de        186         [ +  + ]:         150587 :     if (!have_iostats)
                                187                 :          56518 :         return false;
                                188                 :                : 
                                189                 :          94069 :     bktype_lock = &pgStatLocal.shmem->io.locks[MyBackendType];
                                190                 :          94069 :     bktype_shstats =
                                191                 :          94069 :         &pgStatLocal.shmem->io.stats.stats[MyBackendType];
                                192                 :                : 
                                193         [ +  + ]:          94069 :     if (!nowait)
                                194                 :          65028 :         LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
                                195         [ +  + ]:          29041 :     else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
                                196                 :             12 :         return true;
                                197                 :                : 
 1370 tgl@sss.pgh.pa.us         198         [ +  + ]:         376228 :     for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
                                199                 :                :     {
                                200         [ +  + ]:        1693026 :         for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
                                201                 :                :         {
                                202         [ +  + ]:       12697695 :             for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
                                203                 :                :             {
                                204                 :                :                 instr_time  time;
                                205                 :                : 
 1331 andres@anarazel.de        206                 :       11286840 :                 bktype_shstats->counts[io_object][io_context][io_op] +=
                                207                 :       11286840 :                     PendingIOStats.counts[io_object][io_context][io_op];
                                208                 :                : 
  683 michael@paquier.xyz       209                 :       11286840 :                 bktype_shstats->bytes[io_object][io_context][io_op] +=
                                210                 :       11286840 :                     PendingIOStats.bytes[io_object][io_context][io_op];
                                211                 :                : 
 1331 andres@anarazel.de        212                 :       11286840 :                 time = PendingIOStats.pending_times[io_object][io_context][io_op];
                                213                 :                : 
                                214                 :       11286840 :                 bktype_shstats->times[io_object][io_context][io_op] +=
                                215                 :       11286840 :                     INSTR_TIME_GET_MICROSEC(time);
                                216                 :                :             }
                                217                 :                :         }
                                218                 :                :     }
                                219                 :                : 
 1389                           220         [ -  + ]:          94057 :     Assert(pgstat_bktype_io_stats_valid(bktype_shstats, MyBackendType));
                                221                 :                : 
                                222                 :          94057 :     LWLockRelease(bktype_lock);
                                223                 :                : 
                                224                 :          94057 :     memset(&PendingIOStats, 0, sizeof(PendingIOStats));
                                225                 :                : 
                                226                 :          94057 :     have_iostats = false;
                                227                 :                : 
                                228                 :          94057 :     return false;
                                229                 :                : }
                                230                 :                : 
                                231                 :                : const char *
                                232                 :          23010 : pgstat_get_io_context_name(IOContext io_context)
                                233                 :                : {
                                234   [ +  +  +  +  :          23010 :     switch (io_context)
                                              +  - ]
                                235                 :                :     {
                                236                 :           4602 :         case IOCONTEXT_BULKREAD:
                                237                 :           4602 :             return "bulkread";
                                238                 :           4602 :         case IOCONTEXT_BULKWRITE:
                                239                 :           4602 :             return "bulkwrite";
  662 michael@paquier.xyz       240                 :           4602 :         case IOCONTEXT_INIT:
                                241                 :           4602 :             return "init";
 1389 andres@anarazel.de        242                 :           4602 :         case IOCONTEXT_NORMAL:
                                243                 :           4602 :             return "normal";
                                244                 :           4602 :         case IOCONTEXT_VACUUM:
                                245                 :           4602 :             return "vacuum";
                                246                 :                :     }
                                247                 :                : 
 1389 andres@anarazel.de        248         [ #  # ]:UBC           0 :     elog(ERROR, "unrecognized IOContext value: %d", io_context);
                                249                 :                :     pg_unreachable();
                                250                 :                : }
                                251                 :                : 
                                252                 :                : const char *
 1389 andres@anarazel.de        253                 :CBC        4602 : pgstat_get_io_object_name(IOObject io_object)
                                254                 :                : {
                                255   [ +  +  +  - ]:           4602 :     switch (io_object)
                                256                 :                :     {
                                257                 :           1534 :         case IOOBJECT_RELATION:
                                258                 :           1534 :             return "relation";
                                259                 :           1534 :         case IOOBJECT_TEMP_RELATION:
                                260                 :           1534 :             return "temp relation";
  662 michael@paquier.xyz       261                 :           1534 :         case IOOBJECT_WAL:
                                262                 :           1534 :             return "wal";
                                263                 :                :     }
                                264                 :                : 
 1389 andres@anarazel.de        265         [ #  # ]:UBC           0 :     elog(ERROR, "unrecognized IOObject value: %d", io_object);
                                266                 :                :     pg_unreachable();
                                267                 :                : }
                                268                 :                : 
                                269                 :                : void
  870 michael@paquier.xyz       270                 :CBC        1325 : pgstat_io_init_shmem_cb(void *stats)
                                271                 :                : {
                                272                 :           1325 :     PgStatShared_IO *stat_shmem = (PgStatShared_IO *) stats;
                                273                 :                : 
                                274         [ +  + ]:          27825 :     for (int i = 0; i < BACKEND_NUM_TYPES; i++)
                                275                 :          26500 :         LWLockInitialize(&stat_shmem->locks[i], LWTRANCHE_PGSTATS_DATA);
                                276                 :           1325 : }
                                277                 :                : 
                                278                 :                : void
 1389 andres@anarazel.de        279                 :            285 : pgstat_io_reset_all_cb(TimestampTz ts)
                                280                 :                : {
                                281         [ +  + ]:           5985 :     for (int i = 0; i < BACKEND_NUM_TYPES; i++)
                                282                 :                :     {
                                283                 :           5700 :         LWLock     *bktype_lock = &pgStatLocal.shmem->io.locks[i];
                                284                 :           5700 :         PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
                                285                 :                : 
                                286                 :           5700 :         LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
                                287                 :                : 
                                288                 :                :         /*
                                289                 :                :          * Use the lock in the first BackendType's PgStat_BktypeIO to protect
                                290                 :                :          * the reset timestamp as well.
                                291                 :                :          */
                                292         [ +  + ]:           5700 :         if (i == 0)
                                293                 :            285 :             pgStatLocal.shmem->io.stats.stat_reset_timestamp = ts;
                                294                 :                : 
                                295                 :           5700 :         memset(bktype_shstats, 0, sizeof(*bktype_shstats));
                                296                 :           5700 :         LWLockRelease(bktype_lock);
                                297                 :                :     }
                                298                 :            285 : }
                                299                 :                : 
                                300                 :                : void
                                301                 :            937 : pgstat_io_snapshot_cb(void)
                                302                 :                : {
                                303         [ +  + ]:          19677 :     for (int i = 0; i < BACKEND_NUM_TYPES; i++)
                                304                 :                :     {
                                305                 :          18740 :         LWLock     *bktype_lock = &pgStatLocal.shmem->io.locks[i];
                                306                 :          18740 :         PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
                                307                 :          18740 :         PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io.stats[i];
                                308                 :                : 
                                309                 :          18740 :         LWLockAcquire(bktype_lock, LW_SHARED);
                                310                 :                : 
                                311                 :                :         /*
                                312                 :                :          * Use the lock in the first BackendType's PgStat_BktypeIO to protect
                                313                 :                :          * the reset timestamp as well.
                                314                 :                :          */
                                315         [ +  + ]:          18740 :         if (i == 0)
                                316                 :            937 :             pgStatLocal.snapshot.io.stat_reset_timestamp =
                                317                 :            937 :                 pgStatLocal.shmem->io.stats.stat_reset_timestamp;
                                318                 :                : 
                                319                 :                :         /* using struct assignment due to better type safety */
                                320                 :          18740 :         *bktype_snap = *bktype_shstats;
                                321                 :          18740 :         LWLockRelease(bktype_lock);
                                322                 :                :     }
                                323                 :            937 : }
                                324                 :                : 
                                325                 :                : /*
                                326                 :                :  * IO statistics are not collected for all BackendTypes.
                                327                 :                :  *
                                328                 :                :  * The following BackendTypes do not participate in the cumulative stats
                                329                 :                :  * subsystem or do not perform IO on which we currently track:
                                330                 :                :  * - Dead-end backend because it is not connected to shared memory and
                                331                 :                :  *   doesn't do any IO
                                332                 :                :  * - Syslogger because it is not connected to shared memory
                                333                 :                :  * - Archiver because most relevant archiving IO is delegated to a
                                334                 :                :  *   specialized command or module
                                335                 :                :  *
                                336                 :                :  * Function returns true if BackendType participates in the cumulative stats
                                337                 :                :  * subsystem for IO and false if it does not.
                                338                 :                :  *
                                339                 :                :  * When adding a new BackendType, also consider adding relevant restrictions to
                                340                 :                :  * pgstat_tracks_io_object() and pgstat_tracks_io_op().
                                341                 :                :  */
                                342                 :                : bool
                                343                 :      188128974 : pgstat_tracks_io_bktype(BackendType bktype)
                                344                 :                : {
                                345                 :                :     /*
                                346                 :                :      * List every type so that new backend types trigger a warning about
                                347                 :                :      * needing to adjust this switch.
                                348                 :                :      */
                                349      [ +  +  - ]:      188128974 :     switch (bktype)
                                350                 :                :     {
                                351                 :          45496 :         case B_INVALID:
                                352                 :                :         case B_DEAD_END_BACKEND:
                                353                 :                :         case B_ARCHIVER:
                                354                 :                :         case B_LOGGER:
                                355                 :          45496 :             return false;
                                356                 :                : 
  239 dgustafsson@postgres      357                 :GNC   188083478 :         case B_DATACHECKSUMSWORKER_LAUNCHER:
                                358                 :                :         case B_DATACHECKSUMSWORKER_WORKER:
 1389 andres@anarazel.de        359                 :ECB (171710081) :         case B_AUTOVAC_LAUNCHER:
                                360                 :                :         case B_AUTOVAC_WORKER:
                                361                 :                :         case B_BACKEND:
                                362                 :                :         case B_BG_WORKER:
                                363                 :                :         case B_BG_WRITER:
                                364                 :                :         case B_CHECKPOINTER:
                                365                 :                :         case B_IO_WORKER:
                                366                 :                :         case B_SLOTSYNC_WORKER:
                                367                 :                :         case B_STANDALONE_BACKEND:
                                368                 :                :         case B_STARTUP:
                                369                 :                :         case B_WAL_RECEIVER:
                                370                 :                :         case B_WAL_SENDER:
                                371                 :                :         case B_WAL_SUMMARIZER:
                                372                 :                :         case B_WAL_WRITER:
 1389 andres@anarazel.de        373                 :CBC   188083478 :             return true;
                                374                 :                :     }
                                375                 :                : 
 1389 andres@anarazel.de        376                 :UBC           0 :     return false;
                                377                 :                : }
                                378                 :                : 
                                379                 :                : /*
                                380                 :                :  * Some BackendTypes do not perform IO on certain IOObjects or in certain
                                381                 :                :  * IOContexts. Some IOObjects are never operated on in some IOContexts. Check
                                382                 :                :  * that the given BackendType is expected to do IO in the given IOContext and
                                383                 :                :  * on the given IOObject and that the given IOObject is expected to be operated
                                384                 :                :  * on in the given IOContext.
                                385                 :                :  */
                                386                 :                : bool
 1389 andres@anarazel.de        387                 :CBC   188127094 : pgstat_tracks_io_object(BackendType bktype, IOObject io_object,
                                388                 :                :                         IOContext io_context)
                                389                 :                : {
                                390                 :                :     bool        no_temp_rel;
                                391                 :                : 
                                392                 :                :     /*
                                393                 :                :      * Some BackendTypes should never track IO statistics.
                                394                 :                :      */
                                395         [ +  + ]:      188127094 :     if (!pgstat_tracks_io_bktype(bktype))
                                396                 :          45120 :         return false;
                                397                 :                : 
                                398                 :                :     /*
                                399                 :                :      * Currently, IO on IOOBJECT_WAL objects can only occur in the
                                400                 :                :      * IOCONTEXT_NORMAL and IOCONTEXT_INIT IOContexts.
                                401                 :                :      */
  662 michael@paquier.xyz       402   [ +  +  +  + ]:      188081974 :     if (io_object == IOOBJECT_WAL &&
                                403         [ +  + ]:        3082808 :         (io_context != IOCONTEXT_NORMAL &&
                                404                 :                :          io_context != IOCONTEXT_INIT))
                                405                 :        2298786 :         return false;
                                406                 :                : 
                                407                 :                :     /*
                                408                 :                :      * Currently, IO on temporary relations can only occur in the
                                409                 :                :      * IOCONTEXT_NORMAL IOContext.
                                410                 :                :      */
 1389 andres@anarazel.de        411   [ +  +  +  + ]:      185783188 :     if (io_context != IOCONTEXT_NORMAL &&
                                412                 :                :         io_object == IOOBJECT_TEMP_RELATION)
                                413                 :        3065048 :         return false;
                                414                 :                : 
                                415                 :                :     /*
                                416                 :                :      * In core Postgres, only regular backends, WAL Sender processes executing
                                417                 :                :      * queries and standalone backends (single-user mode) will use local
                                418                 :                :      * buffers and operate on temporary relations. Parallel workers will not
                                419                 :                :      * use local buffers (see InitLocalBuffers()); however, extensions
                                420                 :                :      * leveraging background workers have no such limitation, so track IO on
                                421                 :                :      * IOOBJECT_TEMP_RELATION for BackendType B_BG_WORKER.
                                422                 :                :      */
                                423   [ +  +  +  + ]:      182675473 :     no_temp_rel = bktype == B_AUTOVAC_LAUNCHER || bktype == B_BG_WRITER ||
                                424   [ +  +  +  + ]:      181483972 :         bktype == B_CHECKPOINTER || bktype == B_AUTOVAC_WORKER ||
   10 fujii@postgresql.org      425         [ +  + ]:      172840977 :         bktype == B_STARTUP ||
  633 michael@paquier.xyz       426   [ +  +  +  +  :      365393613 :         bktype == B_WAL_SUMMARIZER || bktype == B_WAL_WRITER ||
                                              +  + ]
                                427                 :                :         bktype == B_WAL_RECEIVER;
                                428                 :                : 
 1389 andres@anarazel.de        429   [ +  +  +  +  :      182718140 :     if (no_temp_rel && io_context == IOCONTEXT_NORMAL &&
                                              +  + ]
                                430                 :                :         io_object == IOOBJECT_TEMP_RELATION)
                                431                 :         201016 :         return false;
                                432                 :                : 
                                433                 :                :     /*
                                434                 :                :      * Some BackendTypes only perform IO under IOOBJECT_WAL, hence exclude all
                                435                 :                :      * rows for all the other objects for these.
                                436                 :                :      */
  633 michael@paquier.xyz       437   [ +  +  +  +  :      182517124 :     if ((bktype == B_WAL_SUMMARIZER || bktype == B_WAL_RECEIVER ||
                                              +  + ]
                                438         [ +  + ]:         728150 :          bktype == B_WAL_WRITER) && io_object != IOOBJECT_WAL)
                                439                 :         335450 :         return false;
                                440                 :                : 
                                441                 :                :     /*
                                442                 :                :      * Some BackendTypes do not currently perform any IO in certain
                                443                 :                :      * IOContexts, and, while it may not be inherently incorrect for them to
                                444                 :                :      * do so, excluding those rows from the view makes the view easier to use.
                                445                 :                :      */
 1389 andres@anarazel.de        446   [ +  +  +  +  :      182181674 :     if ((bktype == B_CHECKPOINTER || bktype == B_BG_WRITER) &&
                                              +  + ]
                                447         [ +  + ]:         982485 :         (io_context == IOCONTEXT_BULKREAD ||
                                448         [ +  + ]:         877977 :          io_context == IOCONTEXT_BULKWRITE ||
                                449                 :                :          io_context == IOCONTEXT_VACUUM))
                                450                 :         313524 :         return false;
                                451                 :                : 
                                452   [ +  +  +  + ]:      181868150 :     if (bktype == B_AUTOVAC_LAUNCHER && io_context == IOCONTEXT_VACUUM)
                                453                 :           4718 :         return false;
                                454                 :                : 
                                455   [ +  +  +  +  :      181863432 :     if ((bktype == B_AUTOVAC_WORKER || bktype == B_AUTOVAC_LAUNCHER) &&
                                              +  + ]
                                456                 :                :         io_context == IOCONTEXT_BULKWRITE)
                                457                 :          16820 :         return false;
                                458                 :                : 
                                459                 :                :     /*
                                460                 :                :      * The data checksums launcher scans catalogs and emits WAL records for
                                461                 :                :      * checksum state changes. Catalog scans can use a bulkread strategy.
                                462                 :                :      */
  134 fujii@postgresql.org      463         [ +  + ]:GNC   181846612 :     if (bktype == B_DATACHECKSUMSWORKER_LAUNCHER)
                                464                 :                :     {
                                465   [ +  +  +  + ]:          11393 :         if (io_object == IOOBJECT_WAL ||
                                466         [ +  + ]:           6721 :             (io_object == IOOBJECT_RELATION &&
                                467         [ +  + ]:           4931 :              (io_context == IOCONTEXT_BULKREAD ||
                                468                 :                :               io_context == IOCONTEXT_NORMAL)))
                                469                 :           7241 :             return true;
                                470                 :                : 
                                471                 :           4152 :         return false;
                                472                 :                :     }
                                473                 :                : 
                                474                 :                :     /*
                                475                 :                :      * The worker also scans catalogs, then processes relations using a vacuum
                                476                 :                :      * access strategy. Catalog scans can use a bulkread strategy.
                                477                 :                :      */
                                478         [ +  + ]:      181835219 :     if (bktype == B_DATACHECKSUMSWORKER_WORKER)
                                479                 :                :     {
                                480   [ +  +  +  + ]:         274418 :         if (io_object == IOOBJECT_WAL ||
                                481         [ +  + ]:         246674 :             (io_object == IOOBJECT_RELATION &&
                                482         [ +  + ]:         242484 :              (io_context == IOCONTEXT_BULKREAD ||
                                483         [ +  + ]:         137091 :               io_context == IOCONTEXT_NORMAL ||
                                484                 :                :               io_context == IOCONTEXT_VACUUM)))
                                485                 :         270848 :             return true;
                                486                 :                : 
                                487                 :           3570 :         return false;
                                488                 :                :     }
                                489                 :                : 
 1389 andres@anarazel.de        490                 :CBC   181560801 :     return true;
                                491                 :                : }
                                492                 :                : 
                                493                 :                : /*
                                494                 :                :  * Some BackendTypes will never do certain IOOps and some IOOps should not
                                495                 :                :  * occur in certain IOContexts or on certain IOObjects. Check that the given
                                496                 :                :  * IOOp is valid for the given BackendType in the given IOContext and on the
                                497                 :                :  * given IOObject. Note that there are currently no cases of an IOOp being
                                498                 :                :  * invalid for a particular BackendType only within a certain IOContext and/or
                                499                 :                :  * only on a certain IOObject.
                                500                 :                :  */
                                501                 :                : bool
                                502                 :      188104084 : pgstat_tracks_io_op(BackendType bktype, IOObject io_object,
                                503                 :                :                     IOContext io_context, IOOp io_op)
                                504                 :                : {
                                505                 :                :     bool        strategy_io_context;
                                506                 :                : 
                                507                 :                :     /* if (io_context, io_object) will never collect stats, we're done */
                                508         [ +  + ]:      188104084 :     if (!pgstat_tracks_io_object(bktype, io_object, io_context))
                                509                 :        6273800 :         return false;
                                510                 :                : 
                                511                 :                :     /*
                                512                 :                :      * Some BackendTypes will not do certain IOOps.
                                513                 :                :      */
  662 michael@paquier.xyz       514   [ +  +  +  + ]:      181830284 :     if (bktype == B_BG_WRITER &&
 1339 andres@anarazel.de        515   [ +  +  +  + ]:         189031 :         (io_op == IOOP_READ || io_op == IOOP_EVICT || io_op == IOOP_HIT))
 1389                           516                 :          56664 :         return false;
                                517                 :                : 
  662 michael@paquier.xyz       518   [ +  +  +  + ]:      181773620 :     if (bktype == B_CHECKPOINTER &&
                                519   [ +  +  +  + ]:         564798 :         ((io_object != IOOBJECT_WAL && io_op == IOOP_READ) ||
                                520         [ +  + ]:         513762 :          (io_op == IOOP_EVICT || io_op == IOOP_HIT)))
                                521                 :          85060 :         return false;
                                522                 :                : 
  157 melanieplageman@gmai      523   [ +  +  +  +  :      181688560 :     if ((bktype == B_BG_WRITER || bktype == B_CHECKPOINTER) &&
                                              +  + ]
                                524                 :                :         io_op == IOOP_EXTEND)
 1389 andres@anarazel.de        525                 :          52912 :         return false;
                                526                 :                : 
                                527                 :                :     /*
                                528                 :                :      * Some BackendTypes do not perform reads with IOOBJECT_WAL, either from a
                                529                 :                :      * WAL file or from the WAL buffers.
                                530                 :                :      */
   43 michael@paquier.xyz       531   [ +  +  +  + ]:GNC   181635648 :     if (io_object == IOOBJECT_WAL &&
                                532   [ +  +  +  + ]:        8534615 :         (io_op == IOOP_READ || io_op == IOOP_HIT) &&
  662 michael@paquier.xyz       533   [ +  -  +  + ]:CBC     2219501 :         (bktype == B_WAL_RECEIVER || bktype == B_BG_WRITER ||
  619                           534   [ +  +  +  + ]:        2216813 :          bktype == B_AUTOVAC_LAUNCHER || bktype == B_AUTOVAC_WORKER ||
  134 fujii@postgresql.org      535         [ +  + ]:GNC     2209585 :          bktype == B_DATACHECKSUMSWORKER_LAUNCHER ||
                                536         [ +  + ]:        2208661 :          bktype == B_DATACHECKSUMSWORKER_WORKER ||
                                537                 :                :          bktype == B_WAL_WRITER))
  662 michael@paquier.xyz       538                 :CBC       44444 :         return false;
                                539                 :                : 
                                540                 :                :     /*
                                541                 :                :      * Temporary tables are not logged and thus do not require fsync'ing.
                                542                 :                :      * Writeback is not requested for temporary tables.
                                543                 :                :      */
 1291 andres@anarazel.de        544   [ +  +  +  + ]:      181591204 :     if (io_object == IOOBJECT_TEMP_RELATION &&
                                545         [ +  + ]:        4198989 :         (io_op == IOOP_FSYNC || io_op == IOOP_WRITEBACK))
                                546                 :         141794 :         return false;
                                547                 :                : 
                                548                 :                :     /*
                                549                 :                :      * Some IOOps are not valid in certain IOContexts and some IOOps are only
                                550                 :                :      * valid in certain contexts.
                                551                 :                :      */
 1389                           552   [ +  +  +  + ]:      181449410 :     if (io_context == IOCONTEXT_BULKREAD && io_op == IOOP_EXTEND)
                                553                 :          75264 :         return false;
                                554                 :                : 
                                555         [ +  + ]:      178415230 :     strategy_io_context = io_context == IOCONTEXT_BULKREAD ||
                                556   [ +  +  +  + ]:      359789376 :         io_context == IOCONTEXT_BULKWRITE || io_context == IOCONTEXT_VACUUM;
                                557                 :                : 
                                558                 :                :     /*
                                559                 :                :      * IOOP_REUSE is only relevant when a BufferAccessStrategy is in use.
                                560                 :                :      */
                                561   [ +  +  +  + ]:      181374146 :     if (!strategy_io_context && io_op == IOOP_REUSE)
                                562                 :         441688 :         return false;
                                563                 :                : 
                                564                 :                :     /*
                                565                 :                :      * IOOBJECT_WAL IOObject will not do certain IOOps depending on IOContext.
                                566                 :                :      */
  662 michael@paquier.xyz       567   [ +  +  +  +  :      180932458 :     if (io_object == IOOBJECT_WAL && io_context == IOCONTEXT_INIT &&
                                              +  + ]
                                568         [ +  + ]:         518866 :         !(io_op == IOOP_WRITE || io_op == IOOP_FSYNC))
                                569                 :         418997 :         return false;
                                570                 :                : 
                                571   [ +  +  +  +  :      180513461 :     if (io_object == IOOBJECT_WAL && io_context == IOCONTEXT_NORMAL &&
                                              +  + ]
   43 michael@paquier.xyz       572   [ +  +  +  +  :GNC     2384423 :         !(io_op == IOOP_WRITE || io_op == IOOP_READ || io_op == IOOP_FSYNC ||
                                              +  + ]
                                573                 :                :           io_op == IOOP_HIT))
  662 michael@paquier.xyz       574                 :CBC      264919 :         return false;
                                575                 :                : 
                                576                 :                :     /*
                                577                 :                :      * IOOP_FSYNC IOOps done by a backend using a BufferAccessStrategy are
                                578                 :                :      * counted in the IOCONTEXT_NORMAL IOContext. See comment in
                                579                 :                :      * register_dirty_segment() for more details.
                                580                 :                :      */
 1389 andres@anarazel.de        581   [ +  +  +  + ]:      180248542 :     if (strategy_io_context && io_op == IOOP_FSYNC)
                                582                 :         222198 :         return false;
                                583                 :                : 
                                584                 :                : 
                                585                 :      180026344 :     return true;
                                586                 :                : }
        

Generated by: LCOV version 2.0-1