LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_relation.c (source / functions) Coverage Total Hit UNC LBC UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 96.8 % 404 391 10 1 2 151 240 128
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 31 31 23 8 4
Baseline: lcov-20261128-baseline Branches: 79.4 % 218 173 16 1 28 44 129
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 % 3 3 3
(30,360] days: 94.0 % 168 158 10 151 7
(360..) days: 98.7 % 233 230 1 2 230
Function coverage date bins:
(30,360] days: 100.0 % 7 7 5 2
(360..) days: 100.0 % 24 24 18 6
Branch coverage date bins:
(7,30] days: 75.0 % 4 3 1 3
(30,360] days: 74.2 % 62 46 16 44 2
(360..) days: 81.6 % 152 124 1 27 124

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /* -------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgstat_relation.c
                                  4                 :                :  *    Implementation of relation statistics.
                                  5                 :                :  *
                                  6                 :                :  * This file contains the implementation of relation statistics. It is kept
                                  7                 :                :  * separate from pgstat.c to enforce the line between the statistics access /
                                  8                 :                :  * storage implementation and the details about individual types of
                                  9                 :                :  * statistics.
                                 10                 :                :  *
                                 11                 :                :  * Copyright (c) 2001-2026, PostgreSQL Global Development Group
                                 12                 :                :  *
                                 13                 :                :  * IDENTIFICATION
                                 14                 :                :  *    src/backend/utils/activity/pgstat_relation.c
                                 15                 :                :  * -------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : 
                                 18                 :                : #include "postgres.h"
                                 19                 :                : 
                                 20                 :                : #include "access/twophase_rmgr.h"
                                 21                 :                : #include "access/xact.h"
                                 22                 :                : #include "catalog/catalog.h"
                                 23                 :                : #include "utils/memutils.h"
                                 24                 :                : #include "utils/pgstat_internal.h"
                                 25                 :                : #include "utils/rel.h"
                                 26                 :                : #include "utils/timestamp.h"
                                 27                 :                : 
                                 28                 :                : 
                                 29                 :                : /* Record that's written to 2PC state file when pgstat state is persisted */
                                 30                 :                : typedef struct TwoPhasePgStatRecord
                                 31                 :                : {
                                 32                 :                :     PgStat_Counter tuples_inserted; /* tuples inserted in xact */
                                 33                 :                :     PgStat_Counter tuples_updated;  /* tuples updated in xact */
                                 34                 :                :     PgStat_Counter tuples_deleted;  /* tuples deleted in xact */
                                 35                 :                :     /* tuples i/u/d prior to truncate/drop */
                                 36                 :                :     PgStat_Counter inserted_pre_truncdrop;
                                 37                 :                :     PgStat_Counter updated_pre_truncdrop;
                                 38                 :                :     PgStat_Counter deleted_pre_truncdrop;
                                 39                 :                :     Oid         id;             /* table's OID */
                                 40                 :                :     bool        shared;         /* is it a shared catalog? */
                                 41                 :                :     bool        truncdropped;   /* was the relation truncated/dropped? */
                                 42                 :                : } TwoPhasePgStatRecord;
                                 43                 :                : 
                                 44                 :                : 
                                 45                 :                : static PgStat_RelationStatus *pgstat_prep_relation_pending(PgStat_Kind kind,
                                 46                 :                :                                                            Oid rel_id, bool isshared);
                                 47                 :                : static void add_tabstat_xact_level(PgStat_RelationStatus *pgstat_info, int nest_level);
                                 48                 :                : static void ensure_tabstat_xact_level(PgStat_RelationStatus *pgstat_info);
                                 49                 :                : static void save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop);
                                 50                 :                : static void restore_truncdrop_counters(PgStat_TableXactStatus *trans);
                                 51                 :                : 
                                 52                 :                : /*
                                 53                 :                :  * Determine the stats kind for a relation based on its relkind.
                                 54                 :                :  */
                                 55                 :                : static inline PgStat_Kind
  108 michael@paquier.xyz        56                 :GNC     1367705 : pgstat_get_relation_kind(char relkind)
                                 57                 :                : {
                                 58         [ +  + ]:        1367705 :     if (relkind == RELKIND_INDEX)
                                 59                 :         732474 :         return PGSTAT_KIND_INDEX;
                                 60                 :         635231 :     return PGSTAT_KIND_RELATION;
                                 61                 :                : }
                                 62                 :                : 
                                 63                 :                : 
                                 64                 :                : /*
                                 65                 :                :  * Copy stats between relations. This is used for things like REINDEX
                                 66                 :                :  * CONCURRENTLY.
                                 67                 :                :  */
                                 68                 :                : void
 1697 andres@anarazel.de         69                 :CBC         334 : pgstat_copy_relation_stats(Relation dst, Relation src)
                                 70                 :                : {
  108 michael@paquier.xyz        71                 :GNC         334 :     PgStat_Kind kind = pgstat_get_relation_kind(src->rd_rel->relkind);
                                 72                 :                : 
                                 73         [ +  - ]:            334 :     if (kind == PGSTAT_KIND_INDEX)
                                 74                 :                :     {
                                 75                 :                :         PgStat_StatIdxEntry *srcstats;
                                 76                 :                :         PgStatShared_Index *dstshstats;
                                 77                 :                :         PgStat_EntryRef *dst_ref;
                                 78                 :                : 
                                 79                 :            334 :         srcstats = pgstat_fetch_stat_idxentry_ext(src->rd_rel->relisshared,
                                 80                 :                :                                                   RelationGetRelid(src),
                                 81                 :                :                                                   NULL);
                                 82         [ +  + ]:            334 :         if (!srcstats)
                                 83                 :            202 :             return;
                                 84                 :                : 
                                 85                 :            132 :         dst_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_INDEX,
                                 86                 :            132 :                                               dst->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
                                 87         [ -  + ]:            132 :                                               RelationGetRelid(dst),
                                 88                 :                :                                               false);
                                 89                 :                : 
                                 90                 :            132 :         dstshstats = (PgStatShared_Index *) dst_ref->shared_stats;
                                 91                 :            132 :         dstshstats->stats = *srcstats;
                                 92                 :                : 
                                 93                 :            132 :         pgstat_unlock_entry(dst_ref);
                                 94                 :                :     }
                                 95                 :                :     else
                                 96                 :                :     {
                                 97                 :                :         PgStat_StatTabEntry *srcstats;
                                 98                 :                :         PgStatShared_Relation *dstshstats;
                                 99                 :                :         PgStat_EntryRef *dst_ref;
                                100                 :                : 
  108 michael@paquier.xyz       101                 :UNC           0 :         srcstats = pgstat_fetch_stat_tabentry_ext(src->rd_rel->relisshared,
                                102                 :                :                                                   RelationGetRelid(src),
                                103                 :                :                                                   NULL);
                                104         [ #  # ]:              0 :         if (!srcstats)
                                105                 :              0 :             return;
                                106                 :                : 
                                107                 :              0 :         dst_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION,
                                108                 :              0 :                                               dst->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
                                109         [ #  # ]:              0 :                                               RelationGetRelid(dst),
                                110                 :                :                                               false);
                                111                 :                : 
                                112                 :              0 :         dstshstats = (PgStatShared_Relation *) dst_ref->shared_stats;
                                113                 :              0 :         dstshstats->stats = *srcstats;
                                114                 :                : 
                                115                 :              0 :         pgstat_unlock_entry(dst_ref);
                                116                 :                :     }
                                117                 :                : }
                                118                 :                : 
                                119                 :                : /*
                                120                 :                :  * Initialize a relcache entry to count access statistics.  Called whenever a
                                121                 :                :  * relation is opened.
                                122                 :                :  *
                                123                 :                :  * We assume that a relcache entry's pgstat_info field is zeroed by relcache.c
                                124                 :                :  * when the relcache entry is made; thereafter it is long-lived data.
                                125                 :                :  *
                                126                 :                :  * This does not create a reference to a stats entry in shared memory, nor
                                127                 :                :  * allocate memory for the pending stats. That happens in
                                128                 :                :  * pgstat_assoc_relation().
                                129                 :                :  */
                                130                 :                : void
 1697 andres@anarazel.de        131                 :CBC    27346038 : pgstat_init_relation(Relation rel)
                                132                 :                : {
 1713                           133                 :       27346038 :     char        relkind = rel->rd_rel->relkind;
                                134                 :                : 
                                135                 :                :     /*
                                136                 :                :      * We only count stats for relations with storage and partitioned tables
                                137                 :                :      */
                                138   [ +  +  +  +  :       27346038 :     if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
                                     +  +  +  +  +  
                                           +  +  + ]
                                139                 :                :     {
 1697                           140                 :         104188 :         rel->pgstat_enabled = false;
 1713                           141                 :         104188 :         rel->pgstat_info = NULL;
                                142                 :         104188 :         return;
                                143                 :                :     }
                                144                 :                : 
 1697                           145         [ +  + ]:       27241850 :     if (!pgstat_track_counts)
                                146                 :                :     {
                                147         [ +  + ]:            197 :         if (rel->pgstat_info)
                                148                 :             12 :             pgstat_unlink_relation(rel);
                                149                 :                : 
                                150                 :                :         /* We're not counting at all */
                                151                 :            197 :         rel->pgstat_enabled = false;
 1713                           152                 :            197 :         rel->pgstat_info = NULL;
                                153                 :            197 :         return;
                                154                 :                :     }
                                155                 :                : 
 1697                           156                 :       27241653 :     rel->pgstat_enabled = true;
                                157                 :                : }
                                158                 :                : 
                                159                 :                : /*
                                160                 :                :  * Prepare for statistics for this relation to be collected.
                                161                 :                :  *
                                162                 :                :  * This ensures we have a reference to the stats entry before stats can be
                                163                 :                :  * generated. That is important because a relation drop in another connection
                                164                 :                :  * could otherwise lead to the stats entry being dropped, which then later
                                165                 :                :  * would get recreated when flushing stats.
                                166                 :                :  *
                                167                 :                :  * This is separate from pgstat_init_relation() as it is not uncommon for
                                168                 :                :  * relcache entries to be opened without ever getting stats reported.
                                169                 :                :  */
                                170                 :                : void
                                171                 :        1224578 : pgstat_assoc_relation(Relation rel)
                                172                 :                : {
                                173                 :                :     PgStat_Kind kind;
                                174                 :                : 
                                175         [ -  + ]:        1224578 :     Assert(rel->pgstat_enabled);
                                176         [ -  + ]:        1224578 :     Assert(rel->pgstat_info == NULL);
                                177                 :                : 
  108 michael@paquier.xyz       178                 :GNC     1224578 :     kind = pgstat_get_relation_kind(rel->rd_rel->relkind);
                                179                 :                : 
                                180                 :                :     /* find or make the PgStat_RelationStatus entry, and update link */
                                181                 :        2449156 :     rel->pgstat_info = pgstat_prep_relation_pending(kind,
                                182                 :                :                                                     RelationGetRelid(rel),
 1697 andres@anarazel.de        183                 :CBC     1224578 :                                                     rel->rd_rel->relisshared);
                                184                 :                : 
                                185                 :                :     /* don't allow link a stats to multiple relcache entries */
                                186         [ -  + ]:        1224578 :     Assert(rel->pgstat_info->relation == NULL);
                                187                 :                : 
                                188                 :                :     /* mark this relation as the owner */
                                189                 :        1224578 :     rel->pgstat_info->relation = rel;
                                190                 :        1224578 : }
                                191                 :                : 
                                192                 :                : /*
                                193                 :                :  * Break the mutual link between a relcache entry and pending stats entry.
                                194                 :                :  * This must be called whenever one end of the link is removed.
                                195                 :                :  */
                                196                 :                : void
                                197                 :        1894057 : pgstat_unlink_relation(Relation rel)
                                198                 :                : {
                                199                 :                :     /* remove the link to stats info if any */
                                200         [ +  + ]:        1894057 :     if (rel->pgstat_info == NULL)
                                201                 :         669479 :         return;
                                202                 :                : 
                                203                 :                :     /* link sanity check */
                                204         [ -  + ]:        1224578 :     Assert(rel->pgstat_info->relation == rel);
                                205                 :        1224578 :     rel->pgstat_info->relation = NULL;
                                206                 :        1224578 :     rel->pgstat_info = NULL;
                                207                 :                : }
                                208                 :                : 
                                209                 :                : /*
                                210                 :                :  * Ensure that stats are dropped if transaction aborts.
                                211                 :                :  */
                                212                 :                : void
                                213                 :          91432 : pgstat_create_relation(Relation rel)
                                214                 :                : {
  108 michael@paquier.xyz       215                 :GNC       91432 :     PgStat_Kind kind = pgstat_get_relation_kind(rel->rd_rel->relkind);
                                216                 :                : 
                                217                 :          91432 :     pgstat_create_transactional(kind,
 1697 andres@anarazel.de        218                 :CBC       91432 :                                 rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
                                219         [ +  + ]:          91432 :                                 RelationGetRelid(rel));
                                220                 :          91432 : }
                                221                 :                : 
                                222                 :                : /*
                                223                 :                :  * Ensure that stats are dropped if transaction commits.
                                224                 :                :  */
                                225                 :                : void
                                226                 :          51361 : pgstat_drop_relation(Relation rel)
                                227                 :                : {
                                228                 :          51361 :     int         nest_level = GetCurrentTransactionNestLevel();
                                229                 :                :     PgStat_RelationStatus *pgstat_info;
  108 michael@paquier.xyz       230                 :GNC       51361 :     PgStat_Kind kind = pgstat_get_relation_kind(rel->rd_rel->relkind);
                                231                 :                : 
                                232                 :          51361 :     pgstat_drop_transactional(kind,
 1697 andres@anarazel.de        233                 :CBC       51361 :                               rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
                                234         [ -  + ]:          51361 :                               RelationGetRelid(rel));
                                235                 :                : 
                                236   [ +  +  +  +  :          51361 :     if (!pgstat_should_count_relation(rel))
                                              +  + ]
                                237                 :           5450 :         return;
                                238                 :                : 
                                239                 :                :     /*
                                240                 :                :      * Transactionally set counters to 0. That ensures that accesses to
                                241                 :                :      * pg_stat_xact_all_tables inside the transaction show 0.
                                242                 :                :      *
                                243                 :                :      * Indexes have no transactional counters, so leave.
                                244                 :                :      */
                                245                 :          45911 :     pgstat_info = rel->pgstat_info;
  106 michael@paquier.xyz       246         [ +  + ]:GNC       45911 :     if (pgstat_info->kind == PGSTAT_KIND_INDEX)
                                247                 :          15494 :         return;
                                248                 :                : 
                                249         [ +  + ]:          30417 :     if (pgstat_info->tab.trans &&
                                250         [ +  + ]:            943 :         pgstat_info->tab.trans->nest_level == nest_level)
                                251                 :                :     {
                                252                 :            939 :         save_truncdrop_counters(pgstat_info->tab.trans, true);
                                253                 :            939 :         pgstat_info->tab.trans->tuples_inserted = 0;
                                254                 :            939 :         pgstat_info->tab.trans->tuples_updated = 0;
                                255                 :            939 :         pgstat_info->tab.trans->tuples_deleted = 0;
                                256                 :                :     }
                                257                 :                : }
                                258                 :                : 
                                259                 :                : /*
                                260                 :                :  * Report that the table was just vacuumed and flush IO statistics.
                                261                 :                :  * livetuples is -1 if VACUUM could not produce a new estimate.
                                262                 :                :  */
                                263                 :                : void
  346 michael@paquier.xyz       264                 :CBC       16535 : pgstat_report_vacuum(Relation rel, PgStat_Counter livetuples,
                                265                 :                :                      PgStat_Counter deadtuples, TimestampTz starttime)
                                266                 :                : {
                                267                 :                :     PgStat_EntryRef *entry_ref;
                                268                 :                :     PgStatShared_Relation *shtabentry;
                                269                 :                :     PgStat_StatTabEntry *tabentry;
                                270         [ +  + ]:          16535 :     Oid         dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
                                271                 :                :     TimestampTz ts;
                                272                 :                :     PgStat_Counter elapsedtime;
                                273                 :                : 
 1697 andres@anarazel.de        274         [ -  + ]:          16535 :     if (!pgstat_track_counts)
 1713 andres@anarazel.de        275                 :UBC           0 :         return;
                                276                 :                : 
                                277                 :                :     /* Store the data in the table's hash table entry. */
 1697 andres@anarazel.de        278                 :CBC       16535 :     ts = GetCurrentTimestamp();
  669 michael@paquier.xyz       279                 :          16535 :     elapsedtime = TimestampDifferenceMilliseconds(starttime, ts);
                                280                 :                : 
                                281                 :                :     /* block acquiring lock for the same reason as pgstat_report_autovac() */
  346                           282                 :          16535 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, dboid,
                                283                 :          16535 :                                             RelationGetRelid(rel), false);
                                284                 :                : 
 1697 andres@anarazel.de        285                 :          16535 :     shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
                                286                 :          16535 :     tabentry = &shtabentry->stats;
                                287                 :                : 
   16 melanieplageman@gmai      288         [ -  + ]:          16535 :     Assert(livetuples >= -1);
                                289                 :                : 
                                290         [ +  + ]:          16535 :     if (livetuples >= 0)
                                291                 :          16522 :         tabentry->live_tuples = livetuples;
 1453 michael@paquier.xyz       292                 :          16535 :     tabentry->dead_tuples = deadtuples;
                                293                 :                : 
                                294                 :                :     /*
                                295                 :                :      * It is quite possible that a non-aggressive VACUUM ended up skipping
                                296                 :                :      * various pages, however, we'll zero the insert counter here regardless.
                                297                 :                :      * It's currently used only to track when we need to perform an "insert"
                                298                 :                :      * autovacuum, which are mainly intended to freeze newly inserted tuples.
                                299                 :                :      * Zeroing this may just mean we'll not try to vacuum the table again
                                300                 :                :      * until enough tuples have been inserted to trigger another insert
                                301                 :                :      * autovacuum.  An anti-wraparound autovacuum will catch any persistent
                                302                 :                :      * stragglers.
                                303                 :                :      */
                                304                 :          16535 :     tabentry->ins_since_vacuum = 0;
                                305                 :                : 
  999 heikki.linnakangas@i      306         [ +  + ]:          16535 :     if (AmAutoVacuumWorkerProcess())
                                307                 :                :     {
 1453 michael@paquier.xyz       308                 :            494 :         tabentry->last_autovacuum_time = ts;
                                309                 :            494 :         tabentry->autovacuum_count++;
  669                           310                 :            494 :         tabentry->total_autovacuum_time += elapsedtime;
                                311                 :                :     }
                                312                 :                :     else
                                313                 :                :     {
 1453                           314                 :          16041 :         tabentry->last_vacuum_time = ts;
 1697 andres@anarazel.de        315                 :          16041 :         tabentry->vacuum_count++;
  669 michael@paquier.xyz       316                 :          16041 :         tabentry->total_vacuum_time += elapsedtime;
                                317                 :                :     }
                                318                 :                : 
 1697 andres@anarazel.de        319                 :          16535 :     pgstat_unlock_entry(entry_ref);
                                320                 :                : 
                                321                 :                :     /*
                                322                 :                :      * Flush IO statistics now. pgstat_report_stat() will flush IO stats,
                                323                 :                :      * however this will not be called until after an entire autovacuum cycle
                                324                 :                :      * is done -- which will likely vacuum many relations -- or until the
                                325                 :                :      * VACUUM command has processed all tables and committed.
                                326                 :                :      */
 1389                           327                 :          16535 :     pgstat_flush_io(false);
  676 michael@paquier.xyz       328                 :          16535 :     (void) pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO);
                                329                 :                : }
                                330                 :                : 
                                331                 :                : /*
                                332                 :                :  * Report that the table was just analyzed and flush IO statistics.
                                333                 :                :  *
                                334                 :                :  * Caller must provide new live- and dead-tuples estimates, as well as a
                                335                 :                :  * flag indicating whether to reset the mod_since_analyze counter.
                                336                 :                :  */
                                337                 :                : void
 1713 andres@anarazel.de        338                 :          10814 : pgstat_report_analyze(Relation rel,
                                339                 :                :                       PgStat_Counter livetuples, PgStat_Counter deadtuples,
                                340                 :                :                       bool resetcounter, TimestampTz starttime)
                                341                 :                : {
                                342                 :                :     PgStat_EntryRef *entry_ref;
                                343                 :                :     PgStatShared_Relation *shtabentry;
                                344                 :                :     PgStat_StatTabEntry *tabentry;
 1697                           345         [ +  + ]:          10814 :     Oid         dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
                                346                 :                :     TimestampTz ts;
                                347                 :                :     PgStat_Counter elapsedtime;
                                348                 :                : 
                                349         [ -  + ]:          10814 :     if (!pgstat_track_counts)
 1713 andres@anarazel.de        350                 :UBC           0 :         return;
                                351                 :                : 
                                352                 :                :     /*
                                353                 :                :      * Unlike VACUUM, ANALYZE might be running inside a transaction that has
                                354                 :                :      * already inserted and/or deleted rows in the target table. ANALYZE will
                                355                 :                :      * have counted such rows as live or dead respectively. Because we will
                                356                 :                :      * report our counts of such rows at transaction end, we should subtract
                                357                 :                :      * off these counts from the update we're making now, else they'll be
                                358                 :                :      * double-counted after commit.  (This approach also ensures that the
                                359                 :                :      * shared stats entry ends up with the right numbers if we abort instead
                                360                 :                :      * of committing.)
                                361                 :                :      *
                                362                 :                :      * Waste no time on partitioned tables, though.
                                363                 :                :      */
 1697 andres@anarazel.de        364   [ +  +  +  +  :CBC       10814 :     if (pgstat_should_count_relation(rel) &&
                                              +  + ]
 1713                           365         [ +  + ]:          10761 :         rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
                                366                 :                :     {
                                367                 :                :         PgStat_TableXactStatus *trans;
                                368                 :                : 
  106 michael@paquier.xyz       369         [ -  + ]:GNC       10240 :         Assert(rel->pgstat_info->kind == PGSTAT_KIND_RELATION);
                                370         [ +  + ]:          10391 :         for (trans = rel->pgstat_info->tab.trans; trans; trans = trans->upper)
                                371                 :                :         {
 1713 andres@anarazel.de        372                 :CBC         151 :             livetuples -= trans->tuples_inserted - trans->tuples_deleted;
                                373                 :            151 :             deadtuples -= trans->tuples_updated + trans->tuples_deleted;
                                374                 :                :         }
                                375                 :                :         /* count stuff inserted by already-aborted subxacts, too */
   79 michael@paquier.xyz       376                 :GNC       10240 :         deadtuples -= rel->pgstat_info->tab.counts_xact.delta_dead_tuples;
                                377                 :                :         /* Since ANALYZE's counts are estimates, we could have underflowed */
 1713 andres@anarazel.de        378                 :CBC       10240 :         livetuples = Max(livetuples, 0);
                                379                 :          10240 :         deadtuples = Max(deadtuples, 0);
                                380                 :                :     }
                                381                 :                : 
                                382                 :                :     /* Store the data in the table's hash table entry. */
  669 michael@paquier.xyz       383                 :          10814 :     ts = GetCurrentTimestamp();
                                384                 :          10814 :     elapsedtime = TimestampDifferenceMilliseconds(starttime, ts);
                                385                 :                : 
                                386                 :                :     /* block acquiring lock for the same reason as pgstat_report_autovac() */
 1697 andres@anarazel.de        387                 :          10814 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, dboid,
                                388                 :          10814 :                                             RelationGetRelid(rel),
                                389                 :                :                                             false);
                                390                 :                :     /* can't get dropped while accessed */
                                391   [ +  -  -  + ]:          10814 :     Assert(entry_ref != NULL && entry_ref->shared_stats != NULL);
                                392                 :                : 
                                393                 :          10814 :     shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
                                394                 :          10814 :     tabentry = &shtabentry->stats;
                                395                 :                : 
 1453 michael@paquier.xyz       396                 :          10814 :     tabentry->live_tuples = livetuples;
                                397                 :          10814 :     tabentry->dead_tuples = deadtuples;
                                398                 :                : 
                                399                 :                :     /*
                                400                 :                :      * If commanded, reset mod_since_analyze to zero.  This forgets any
                                401                 :                :      * changes that were committed while the ANALYZE was in progress, but we
                                402                 :                :      * have no good way to estimate how many of those there were.
                                403                 :                :      */
 1697 andres@anarazel.de        404         [ +  + ]:          10814 :     if (resetcounter)
 1453 michael@paquier.xyz       405                 :          10774 :         tabentry->mod_since_analyze = 0;
                                406                 :                : 
  999 heikki.linnakangas@i      407         [ +  + ]:          10814 :     if (AmAutoVacuumWorkerProcess())
                                408                 :                :     {
  669 michael@paquier.xyz       409                 :            744 :         tabentry->last_autoanalyze_time = ts;
 1453                           410                 :            744 :         tabentry->autoanalyze_count++;
  669                           411                 :            744 :         tabentry->total_autoanalyze_time += elapsedtime;
                                412                 :                :     }
                                413                 :                :     else
                                414                 :                :     {
                                415                 :          10070 :         tabentry->last_analyze_time = ts;
 1697 andres@anarazel.de        416                 :          10070 :         tabentry->analyze_count++;
  669 michael@paquier.xyz       417                 :          10070 :         tabentry->total_analyze_time += elapsedtime;
                                418                 :                :     }
                                419                 :                : 
 1697 andres@anarazel.de        420                 :          10814 :     pgstat_unlock_entry(entry_ref);
                                421                 :                : 
                                422                 :                :     /* see pgstat_report_vacuum() */
 1389                           423                 :          10814 :     pgstat_flush_io(false);
  676 michael@paquier.xyz       424                 :          10814 :     (void) pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO);
                                425                 :                : }
                                426                 :                : 
                                427                 :                : /*
                                428                 :                :  * count a tuple insertion of n tuples
                                429                 :                :  */
                                430                 :                : void
 1713 andres@anarazel.de        431                 :       12575214 : pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
                                432                 :                : {
 1697                           433   [ +  +  +  +  :       12575214 :     if (pgstat_should_count_relation(rel))
                                              +  + ]
                                434                 :                :     {
  106 michael@paquier.xyz       435                 :GNC    12347932 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
                                436                 :                : 
                                437         [ -  + ]:       12347932 :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
                                438                 :                : 
 1713 andres@anarazel.de        439                 :CBC    12347932 :         ensure_tabstat_xact_level(pgstat_info);
  106 michael@paquier.xyz       440                 :GNC    12347932 :         pgstat_info->tab.trans->tuples_inserted += n;
                                441                 :                :     }
 1713 andres@anarazel.de        442                 :CBC    12575214 : }
                                443                 :                : 
                                444                 :                : /*
                                445                 :                :  * count a tuple update
                                446                 :                :  */
                                447                 :                : void
 1346 pg@bowt.ie                448                 :        2408767 : pgstat_count_heap_update(Relation rel, bool hot, bool newpage)
                                449                 :                : {
                                450   [ +  +  -  + ]:        2408767 :     Assert(!(hot && newpage));
                                451                 :                : 
 1697 andres@anarazel.de        452   [ +  +  -  +  :        2408767 :     if (pgstat_should_count_relation(rel))
                                              +  + ]
                                453                 :                :     {
  106 michael@paquier.xyz       454                 :GNC     2408765 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
                                455                 :                : 
                                456         [ -  + ]:        2408765 :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
                                457                 :                : 
 1713 andres@anarazel.de        458                 :CBC     2408765 :         ensure_tabstat_xact_level(pgstat_info);
  106 michael@paquier.xyz       459                 :GNC     2408765 :         pgstat_info->tab.trans->tuples_updated++;
                                460                 :                : 
                                461                 :                :         /*
                                462                 :                :          * tuples_hot_updated and tuples_newpage_updated counters are
                                463                 :                :          * nontransactional, so just advance them
                                464                 :                :          */
 1713 andres@anarazel.de        465         [ +  + ]:CBC     2408765 :         if (hot)
   79 michael@paquier.xyz       466                 :GNC      177173 :             pgstat_info->tab.counts_xact.tuples_hot_updated++;
 1346 pg@bowt.ie                467         [ +  + ]:CBC     2231592 :         else if (newpage)
   79 michael@paquier.xyz       468                 :GNC     2214500 :             pgstat_info->tab.counts_xact.tuples_newpage_updated++;
                                469                 :                :     }
 1713 andres@anarazel.de        470                 :CBC     2408767 : }
                                471                 :                : 
                                472                 :                : /*
                                473                 :                :  * count a tuple deletion
                                474                 :                :  */
                                475                 :                : void
                                476                 :        1893036 : pgstat_count_heap_delete(Relation rel)
                                477                 :                : {
 1697                           478   [ -  +  -  -  :        1893036 :     if (pgstat_should_count_relation(rel))
                                              +  - ]
                                479                 :                :     {
  106 michael@paquier.xyz       480                 :GNC     1893036 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
                                481                 :                : 
                                482         [ -  + ]:        1893036 :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
                                483                 :                : 
 1713 andres@anarazel.de        484                 :CBC     1893036 :         ensure_tabstat_xact_level(pgstat_info);
  106 michael@paquier.xyz       485                 :GNC     1893036 :         pgstat_info->tab.trans->tuples_deleted++;
                                486                 :                :     }
 1713 andres@anarazel.de        487                 :CBC     1893036 : }
                                488                 :                : 
                                489                 :                : /*
                                490                 :                :  * update tuple counters due to truncate
                                491                 :                :  */
                                492                 :                : void
                                493                 :           2330 : pgstat_count_truncate(Relation rel)
                                494                 :                : {
 1697                           495   [ +  +  +  -  :           2330 :     if (pgstat_should_count_relation(rel))
                                              +  - ]
                                496                 :                :     {
  106 michael@paquier.xyz       497                 :GNC        2330 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
                                498                 :                : 
                                499         [ -  + ]:           2330 :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
                                500                 :                : 
 1713 andres@anarazel.de        501                 :CBC        2330 :         ensure_tabstat_xact_level(pgstat_info);
  106 michael@paquier.xyz       502                 :GNC        2330 :         save_truncdrop_counters(pgstat_info->tab.trans, false);
                                503                 :           2330 :         pgstat_info->tab.trans->tuples_inserted = 0;
                                504                 :           2330 :         pgstat_info->tab.trans->tuples_updated = 0;
                                505                 :           2330 :         pgstat_info->tab.trans->tuples_deleted = 0;
                                506                 :                :     }
 1713 andres@anarazel.de        507                 :CBC        2330 : }
                                508                 :                : 
                                509                 :                : /*
                                510                 :                :  * update dead-tuples count
                                511                 :                :  *
                                512                 :                :  * The semantics of this are that we are reporting the nontransactional
                                513                 :                :  * recovery of "delta" dead tuples; so delta_dead_tuples decreases
                                514                 :                :  * rather than increasing, and the change goes straight into the per-table
                                515                 :                :  * counter, not into transactional state.
                                516                 :                :  */
                                517                 :                : void
                                518                 :          22207 : pgstat_update_heap_dead_tuples(Relation rel, int delta)
                                519                 :                : {
 1697                           520   [ -  +  -  -  :          22207 :     if (pgstat_should_count_relation(rel))
                                              +  - ]
                                521                 :                :     {
  106 michael@paquier.xyz       522                 :GNC       22207 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
                                523                 :                : 
                                524         [ -  + ]:          22207 :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
                                525                 :                : 
   79                           526                 :          22207 :         pgstat_info->tab.counts_xact.delta_dead_tuples -= delta;
                                527                 :                :     }
 1713 andres@anarazel.de        528                 :CBC       22207 : }
                                529                 :                : 
                                530                 :                : /*
                                531                 :                :  * Support function for the SQL-callable pgstat* functions. Returns
                                532                 :                :  * the collected statistics for one table or NULL. NULL doesn't mean
                                533                 :                :  * that the table doesn't exist, just that there are no statistics, so the
                                534                 :                :  * caller is better off to report ZERO instead.
                                535                 :                :  */
                                536                 :                : PgStat_StatTabEntry *
 1697                           537                 :           6114 : pgstat_fetch_stat_tabentry(Oid relid)
                                538                 :                : {
  233 nathan@postgresql.or      539                 :           6114 :     return pgstat_fetch_stat_tabentry_ext(IsSharedRelation(relid), relid, NULL);
                                540                 :                : }
                                541                 :                : 
                                542                 :                : /*
                                543                 :                :  * More efficient version of pgstat_fetch_stat_tabentry(), allowing to specify
                                544                 :                :  * whether the to-be-accessed table is a shared relation or not.  This version
                                545                 :                :  * also returns whether the caller can pfree() the result if desired.
                                546                 :                :  */
                                547                 :                : PgStat_StatTabEntry *
                                548                 :          38165 : pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid, bool *may_free)
                                549                 :                : {
 1697 andres@anarazel.de        550         [ +  + ]:          38165 :     Oid         dboid = (shared ? InvalidOid : MyDatabaseId);
                                551                 :                : 
                                552                 :          38165 :     return (PgStat_StatTabEntry *)
  233 nathan@postgresql.or      553                 :          38165 :         pgstat_fetch_entry(PGSTAT_KIND_RELATION, dboid, reloid, may_free);
                                554                 :                : }
                                555                 :                : 
                                556                 :                : /*
                                557                 :                :  * find any existing PgStat_RelationStatus entry for rel and kind
                                558                 :                :  *
                                559                 :                :  * Find any existing PgStat_RelationStatus entry for rel_id in the current
                                560                 :                :  * database. If not found, try finding from shared tables.
                                561                 :                :  *
                                562                 :                :  * If an entry is found, copy it.  For a PGSTAT_KIND_INDEX entry, just return
                                563                 :                :  * the copy.  For a PGSTAT_KIND_RELATION entry, increment the copy's counters
                                564                 :                :  * with their subtransaction counterparts, then return the copy.  The caller
                                565                 :                :  * may need to pfree() the copy.
                                566                 :                :  *
                                567                 :                :  * If no entry found, return NULL, don't create a new one.
                                568                 :                :  */
                                569                 :                : PgStat_RelationStatus *
  106 michael@paquier.xyz       570                 :GNC          33 : find_relstat_entry_kind(PgStat_Kind kind, Oid rel_id)
                                571                 :                : {
                                572                 :                :     PgStat_EntryRef *entry_ref;
                                573                 :                :     PgStat_TableXactStatus *trans;
                                574                 :             33 :     PgStat_RelationStatus *relentry = NULL;
                                575                 :             33 :     PgStat_RelationStatus *relstatus = NULL;
                                576                 :                : 
  108                           577                 :             33 :     entry_ref = pgstat_fetch_pending_entry(kind, MyDatabaseId, rel_id);
 1697 andres@anarazel.de        578         [ +  + ]:CBC          33 :     if (!entry_ref)
                                579                 :                :     {
  108 michael@paquier.xyz       580                 :GNC           8 :         entry_ref = pgstat_fetch_pending_entry(kind, InvalidOid, rel_id);
 1125 michael@paquier.xyz       581         [ +  - ]:CBC           8 :         if (!entry_ref)
  106 michael@paquier.xyz       582                 :GNC           8 :             return relstatus;
                                583                 :                :     }
                                584                 :                : 
                                585                 :             25 :     relentry = (PgStat_RelationStatus *) entry_ref->pending;
                                586                 :             25 :     relstatus = palloc_object(PgStat_RelationStatus);
                                587                 :             25 :     *relstatus = *relentry;
                                588                 :                : 
                                589                 :                :     /*
                                590                 :                :      * For index entries, just return the copy.  There is no transactional
                                591                 :                :      * data.
                                592                 :                :      */
                                593         [ -  + ]:             25 :     if (kind == PGSTAT_KIND_INDEX)
  106 michael@paquier.xyz       594                 :UNC           0 :         return relstatus;
                                595                 :                : 
                                596                 :                :     /*
                                597                 :                :      * Reset relstatus->tab.trans in the copy of PgStat_RelationStatus as it
                                598                 :                :      * may point to a shared memory area.  Its data is saved below, so
                                599                 :                :      * removing it does not matter.
                                600                 :                :      */
  106 michael@paquier.xyz       601                 :GNC          25 :     relstatus->tab.trans = NULL;
                                602                 :                : 
                                603                 :                :     /*
                                604                 :                :      * Live subtransaction counts are not included yet.  This is not a hot
                                605                 :                :      * code path so reconcile tuples_inserted, tuples_updated and
                                606                 :                :      * tuples_deleted even if the caller may not be interested in this data.
                                607                 :                :      */
                                608         [ +  + ]:             58 :     for (trans = relentry->tab.trans; trans != NULL; trans = trans->upper)
                                609                 :                :     {
   79                           610                 :             33 :         relstatus->tab.counts_xact.tuples_inserted += trans->tuples_inserted;
                                611                 :             33 :         relstatus->tab.counts_xact.tuples_updated += trans->tuples_updated;
                                612                 :             33 :         relstatus->tab.counts_xact.tuples_deleted += trans->tuples_deleted;
                                613                 :                :     }
                                614                 :                : 
  106                           615                 :             25 :     return relstatus;
                                616                 :                : }
                                617                 :                : 
                                618                 :                : /*
                                619                 :                :  * Perform relation stats specific end-of-transaction work. Helper for
                                620                 :                :  * AtEOXact_PgStat.
                                621                 :                :  *
                                622                 :                :  * Transfer transactional insert/update counts into the base relstat entries.
                                623                 :                :  * We don't bother to free any of the transactional state, since it's all in
                                624                 :                :  * TopTransactionContext and will go away anyway.
                                625                 :                :  */
                                626                 :                : void
 1713 andres@anarazel.de        627                 :CBC      162905 : AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
                                628                 :                : {
                                629                 :                :     PgStat_TableXactStatus *trans;
                                630                 :                : 
                                631         [ +  + ]:         630121 :     for (trans = xact_state->first; trans != NULL; trans = trans->next)
                                632                 :                :     {
                                633                 :                :         PgStat_RelationStatus *relstat;
                                634                 :                : 
                                635         [ -  + ]:         467216 :         Assert(trans->nest_level == 1);
                                636         [ -  + ]:         467216 :         Assert(trans->upper == NULL);
  106 michael@paquier.xyz       637                 :GNC      467216 :         relstat = trans->parent;
                                638         [ -  + ]:         467216 :         Assert(relstat->tab.trans == trans);
                                639                 :                :         /* restore pre-truncate/drop stats (if any) in case of aborted xact */
 1713 andres@anarazel.de        640         [ +  + ]:CBC      467216 :         if (!isCommit)
 1697                           641                 :          17942 :             restore_truncdrop_counters(trans);
                                642                 :                :         /* count attempted actions regardless of commit/abort */
   79 michael@paquier.xyz       643                 :GNC      467216 :         relstat->tab.counts_xact.tuples_inserted += trans->tuples_inserted;
                                644                 :         467216 :         relstat->tab.counts_xact.tuples_updated += trans->tuples_updated;
                                645                 :         467216 :         relstat->tab.counts_xact.tuples_deleted += trans->tuples_deleted;
 1713 andres@anarazel.de        646         [ +  + ]:CBC      467216 :         if (isCommit)
                                647                 :                :         {
   79 michael@paquier.xyz       648                 :GNC      449274 :             relstat->tab.truncdropped = trans->truncdropped;
 1713 andres@anarazel.de        649         [ +  + ]:CBC      449274 :             if (trans->truncdropped)
                                650                 :                :             {
                                651                 :                :                 /* forget live/dead stats seen by backend thus far */
   79 michael@paquier.xyz       652                 :GNC        2966 :                 relstat->tab.counts_xact.delta_live_tuples = 0;
                                653                 :           2966 :                 relstat->tab.counts_xact.delta_dead_tuples = 0;
                                654                 :                :             }
                                655                 :                :             /* insert adds a live tuple, delete removes one */
                                656                 :         449274 :             relstat->tab.counts_xact.delta_live_tuples +=
 1713 andres@anarazel.de        657                 :CBC      449274 :                 trans->tuples_inserted - trans->tuples_deleted;
                                658                 :                :             /* update and delete each create a dead tuple */
   79 michael@paquier.xyz       659                 :GNC      449274 :             relstat->tab.counts_xact.delta_dead_tuples +=
 1713 andres@anarazel.de        660                 :CBC      449274 :                 trans->tuples_updated + trans->tuples_deleted;
                                661                 :                :             /* insert, update, delete each count as one change event */
   79 michael@paquier.xyz       662                 :GNC      449274 :             relstat->tab.counts_xact.changed_tuples +=
 1713 andres@anarazel.de        663                 :CBC      449274 :                 trans->tuples_inserted + trans->tuples_updated +
                                664                 :         449274 :                 trans->tuples_deleted;
                                665                 :                :         }
                                666                 :                :         else
                                667                 :                :         {
                                668                 :                :             /* inserted tuples are dead, deleted tuples are unaffected */
   79 michael@paquier.xyz       669                 :GNC       17942 :             relstat->tab.counts_xact.delta_dead_tuples +=
 1713 andres@anarazel.de        670                 :CBC       17942 :                 trans->tuples_inserted + trans->tuples_updated;
                                671                 :                :             /* an aborted xact generates no changed_tuple events */
                                672                 :                :         }
  106 michael@paquier.xyz       673                 :GNC      467216 :         relstat->tab.trans = NULL;
                                674                 :                :     }
 1713 andres@anarazel.de        675                 :CBC      162905 : }
                                676                 :                : 
                                677                 :                : /*
                                678                 :                :  * Perform relation stats specific end-of-sub-transaction work. Helper for
                                679                 :                :  * AtEOSubXact_PgStat.
                                680                 :                :  *
                                681                 :                :  * Transfer transactional insert/update counts into the next higher
                                682                 :                :  * subtransaction state.
                                683                 :                :  */
                                684                 :                : void
                                685                 :          16355 : AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
                                686                 :                : {
                                687                 :                :     PgStat_TableXactStatus *trans;
                                688                 :                :     PgStat_TableXactStatus *next_trans;
                                689                 :                : 
                                690         [ +  + ]:          33450 :     for (trans = xact_state->first; trans != NULL; trans = next_trans)
                                691                 :                :     {
                                692                 :                :         PgStat_RelationStatus *relstat;
                                693                 :                : 
                                694                 :          17095 :         next_trans = trans->next;
                                695         [ -  + ]:          17095 :         Assert(trans->nest_level == nestDepth);
  106 michael@paquier.xyz       696                 :GNC       17095 :         relstat = trans->parent;
                                697         [ -  + ]:          17095 :         Assert(relstat->tab.trans == trans);
                                698                 :                : 
 1713 andres@anarazel.de        699         [ +  + ]:CBC       17095 :         if (isCommit)
                                700                 :                :         {
                                701   [ +  +  +  + ]:          15647 :             if (trans->upper && trans->upper->nest_level == nestDepth - 1)
                                702                 :                :             {
                                703         [ +  + ]:          14428 :                 if (trans->truncdropped)
                                704                 :                :                 {
                                705                 :                :                     /* propagate the truncate/drop status one level up */
 1697                           706                 :             16 :                     save_truncdrop_counters(trans->upper, false);
                                707                 :                :                     /* replace upper xact stats with ours */
 1713                           708                 :             16 :                     trans->upper->tuples_inserted = trans->tuples_inserted;
                                709                 :             16 :                     trans->upper->tuples_updated = trans->tuples_updated;
                                710                 :             16 :                     trans->upper->tuples_deleted = trans->tuples_deleted;
                                711                 :                :                 }
                                712                 :                :                 else
                                713                 :                :                 {
                                714                 :          14412 :                     trans->upper->tuples_inserted += trans->tuples_inserted;
                                715                 :          14412 :                     trans->upper->tuples_updated += trans->tuples_updated;
                                716                 :          14412 :                     trans->upper->tuples_deleted += trans->tuples_deleted;
                                717                 :                :                 }
  106 michael@paquier.xyz       718                 :GNC       14428 :                 relstat->tab.trans = trans->upper;
 1713 andres@anarazel.de        719                 :CBC       14428 :                 pfree(trans);
                                720                 :                :             }
                                721                 :                :             else
                                722                 :                :             {
                                723                 :                :                 /*
                                724                 :                :                  * When there isn't an immediate parent state, we can just
                                725                 :                :                  * reuse the record instead of going through a palloc/pfree
                                726                 :                :                  * pushup (this works since it's all in TopTransactionContext
                                727                 :                :                  * anyway).  We have to re-link it into the parent level,
                                728                 :                :                  * though, and that might mean pushing a new entry into the
                                729                 :                :                  * pgStatXactStack.
                                730                 :                :                  */
                                731                 :                :                 PgStat_SubXactStatus *upper_xact_state;
                                732                 :                : 
 1697                           733                 :           1219 :                 upper_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
 1713                           734                 :           1219 :                 trans->next = upper_xact_state->first;
                                735                 :           1219 :                 upper_xact_state->first = trans;
                                736                 :           1219 :                 trans->nest_level = nestDepth - 1;
                                737                 :                :             }
                                738                 :                :         }
                                739                 :                :         else
                                740                 :                :         {
                                741                 :                :             /*
                                742                 :                :              * On abort, update top-level relstat counts, then forget the
                                743                 :                :              * subtransaction
                                744                 :                :              */
                                745                 :                : 
                                746                 :                :             /* first restore values obliterated by truncate/drop */
 1697                           747                 :           1448 :             restore_truncdrop_counters(trans);
                                748                 :                :             /* count attempted actions regardless of commit/abort */
   79 michael@paquier.xyz       749                 :GNC        1448 :             relstat->tab.counts_xact.tuples_inserted += trans->tuples_inserted;
                                750                 :           1448 :             relstat->tab.counts_xact.tuples_updated += trans->tuples_updated;
                                751                 :           1448 :             relstat->tab.counts_xact.tuples_deleted += trans->tuples_deleted;
                                752                 :                :             /* inserted tuples are dead, deleted tuples are unaffected */
                                753                 :           1448 :             relstat->tab.counts_xact.delta_dead_tuples +=
 1713 andres@anarazel.de        754                 :CBC        1448 :                 trans->tuples_inserted + trans->tuples_updated;
  106 michael@paquier.xyz       755                 :GNC        1448 :             relstat->tab.trans = trans->upper;
 1713 andres@anarazel.de        756                 :CBC        1448 :             pfree(trans);
                                757                 :                :         }
                                758                 :                :     }
                                759                 :          16355 : }
                                760                 :                : 
                                761                 :                : /*
                                762                 :                :  * Generate 2PC records for all the pending transaction-dependent relation
                                763                 :                :  * stats.
                                764                 :                :  */
                                765                 :                : void
                                766                 :            294 : AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
                                767                 :                : {
                                768                 :                :     PgStat_TableXactStatus *trans;
                                769                 :                : 
                                770         [ +  + ]:            720 :     for (trans = xact_state->first; trans != NULL; trans = trans->next)
                                771                 :                :     {
                                772                 :                :         PgStat_RelationStatus *relstat PG_USED_FOR_ASSERTS_ONLY;
                                773                 :                :         TwoPhasePgStatRecord record;
                                774                 :                : 
                                775         [ -  + ]:            426 :         Assert(trans->nest_level == 1);
                                776         [ -  + ]:            426 :         Assert(trans->upper == NULL);
  106 michael@paquier.xyz       777                 :GNC         426 :         relstat = trans->parent;
                                778         [ -  + ]:            426 :         Assert(relstat->tab.trans == trans);
                                779                 :                : 
 1713 andres@anarazel.de        780                 :CBC         426 :         record.tuples_inserted = trans->tuples_inserted;
                                781                 :            426 :         record.tuples_updated = trans->tuples_updated;
                                782                 :            426 :         record.tuples_deleted = trans->tuples_deleted;
                                783                 :            426 :         record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
                                784                 :            426 :         record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
                                785                 :            426 :         record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
  106 michael@paquier.xyz       786                 :GNC         426 :         record.id = relstat->tab.id;
                                787                 :            426 :         record.shared = relstat->tab.shared;
 1345 michael@paquier.xyz       788                 :CBC         426 :         record.truncdropped = trans->truncdropped;
                                789                 :                : 
 1713 andres@anarazel.de        790                 :            426 :         RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
                                791                 :                :                                &record, sizeof(TwoPhasePgStatRecord));
                                792                 :                :     }
                                793                 :            294 : }
                                794                 :                : 
                                795                 :                : /*
                                796                 :                :  * All we need do here is unlink the transaction stats state from the
                                797                 :                :  * nontransactional state.  The nontransactional action counts will be
                                798                 :                :  * reported to the stats system immediately, while the effects on live and
                                799                 :                :  * dead tuple counts are preserved in the 2PC state file.
                                800                 :                :  *
                                801                 :                :  * Note: AtEOXact_PgStat_Relations is not called during PREPARE.
                                802                 :                :  */
                                803                 :                : void
                                804                 :            294 : PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
                                805                 :                : {
                                806                 :                :     PgStat_TableXactStatus *trans;
                                807                 :                : 
                                808         [ +  + ]:            720 :     for (trans = xact_state->first; trans != NULL; trans = trans->next)
                                809                 :                :     {
                                810                 :                :         PgStat_RelationStatus *relstat;
                                811                 :                : 
  106 michael@paquier.xyz       812                 :GNC         426 :         relstat = trans->parent;
                                813                 :            426 :         relstat->tab.trans = NULL;
                                814                 :                :     }
 1713 andres@anarazel.de        815                 :CBC         294 : }
                                816                 :                : 
                                817                 :                : /*
                                818                 :                :  * 2PC processing routine for COMMIT PREPARED case.
                                819                 :                :  *
                                820                 :                :  * Load the saved counts into our local pgstats state.
                                821                 :                :  */
                                822                 :                : void
  509 michael@paquier.xyz       823                 :            352 : pgstat_twophase_postcommit(FullTransactionId fxid, uint16 info,
                                824                 :                :                            void *recdata, uint32 len)
                                825                 :                : {
 1713 andres@anarazel.de        826                 :            352 :     TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
                                827                 :                :     PgStat_RelationStatus *pgstat_info;
                                828                 :                : 
                                829                 :                :     /* Find or create a relstat entry for the rel */
  108 michael@paquier.xyz       830                 :GNC         352 :     pgstat_info = pgstat_prep_relation_pending(PGSTAT_KIND_RELATION, rec->id, rec->shared);
                                831                 :                : 
                                832                 :                :     /* Same math as in AtEOXact_PgStat, commit case */
   79                           833                 :            352 :     pgstat_info->tab.counts_xact.tuples_inserted += rec->tuples_inserted;
                                834                 :            352 :     pgstat_info->tab.counts_xact.tuples_updated += rec->tuples_updated;
                                835                 :            352 :     pgstat_info->tab.counts_xact.tuples_deleted += rec->tuples_deleted;
                                836                 :            352 :     pgstat_info->tab.truncdropped = rec->truncdropped;
 1345 michael@paquier.xyz       837         [ +  + ]:CBC         352 :     if (rec->truncdropped)
                                838                 :                :     {
                                839                 :                :         /* forget live/dead stats seen by backend thus far */
   79 michael@paquier.xyz       840                 :GNC          12 :         pgstat_info->tab.counts_xact.delta_live_tuples = 0;
                                841                 :             12 :         pgstat_info->tab.counts_xact.delta_dead_tuples = 0;
                                842                 :                :     }
                                843                 :            352 :     pgstat_info->tab.counts_xact.delta_live_tuples +=
 1713 andres@anarazel.de        844                 :CBC         352 :         rec->tuples_inserted - rec->tuples_deleted;
   79 michael@paquier.xyz       845                 :GNC         352 :     pgstat_info->tab.counts_xact.delta_dead_tuples +=
 1713 andres@anarazel.de        846                 :CBC         352 :         rec->tuples_updated + rec->tuples_deleted;
   79 michael@paquier.xyz       847                 :GNC         352 :     pgstat_info->tab.counts_xact.changed_tuples +=
 1713 andres@anarazel.de        848                 :CBC         352 :         rec->tuples_inserted + rec->tuples_updated +
                                849                 :            352 :         rec->tuples_deleted;
                                850                 :            352 : }
                                851                 :                : 
                                852                 :                : /*
                                853                 :                :  * 2PC processing routine for ROLLBACK PREPARED case.
                                854                 :                :  *
                                855                 :                :  * Load the saved counts into our local pgstats state, but treat them
                                856                 :                :  * as aborted.
                                857                 :                :  */
                                858                 :                : void
  509 michael@paquier.xyz       859                 :             85 : pgstat_twophase_postabort(FullTransactionId fxid, uint16 info,
                                860                 :                :                           void *recdata, uint32 len)
                                861                 :                : {
 1713 andres@anarazel.de        862                 :             85 :     TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
                                863                 :                :     PgStat_RelationStatus *pgstat_info;
                                864                 :                : 
                                865                 :                :     /* Find or create a relstat entry for the rel */
  108 michael@paquier.xyz       866                 :GNC          85 :     pgstat_info = pgstat_prep_relation_pending(PGSTAT_KIND_RELATION, rec->id, rec->shared);
                                867                 :                : 
                                868                 :                :     /* Same math as in AtEOXact_PgStat, abort case */
 1345 michael@paquier.xyz       869         [ +  + ]:CBC          85 :     if (rec->truncdropped)
                                870                 :                :     {
 1713 andres@anarazel.de        871                 :              8 :         rec->tuples_inserted = rec->inserted_pre_truncdrop;
                                872                 :              8 :         rec->tuples_updated = rec->updated_pre_truncdrop;
                                873                 :              8 :         rec->tuples_deleted = rec->deleted_pre_truncdrop;
                                874                 :                :     }
   79 michael@paquier.xyz       875                 :GNC          85 :     pgstat_info->tab.counts_xact.tuples_inserted += rec->tuples_inserted;
                                876                 :             85 :     pgstat_info->tab.counts_xact.tuples_updated += rec->tuples_updated;
                                877                 :             85 :     pgstat_info->tab.counts_xact.tuples_deleted += rec->tuples_deleted;
                                878                 :             85 :     pgstat_info->tab.counts_xact.delta_dead_tuples +=
 1713 andres@anarazel.de        879                 :CBC          85 :         rec->tuples_inserted + rec->tuples_updated;
                                880                 :             85 : }
                                881                 :                : 
                                882                 :                : /*
                                883                 :                :  * Flush out pending stats for the entry
                                884                 :                :  *
                                885                 :                :  * If nowait is true and the lock could not be immediately acquired, returns
                                886                 :                :  * false without flushing the entry.  Otherwise returns true.
                                887                 :                :  *
                                888                 :                :  * Some of the stats are copied to the corresponding pending database stats
                                889                 :                :  * entry when successfully flushing.
                                890                 :                :  */
                                891                 :                : bool
 1697                           892                 :         462733 : pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
                                893                 :                : {
                                894                 :                :     Oid         dboid;
                                895                 :                :     PgStat_RelationStatus *lstats;  /* pending stats entry  */
                                896                 :                :     PgStatShared_Relation *shtabstats;
                                897                 :                :     PgStat_StatTabEntry *tabentry;  /* table entry of shared stats */
                                898                 :                :     PgStat_StatDBEntry *dbentry;    /* pending database entry */
                                899                 :                : 
                                900                 :         462733 :     dboid = entry_ref->shared_entry->key.dboid;
  106 michael@paquier.xyz       901                 :GNC      462733 :     lstats = (PgStat_RelationStatus *) entry_ref->pending;
 1697 andres@anarazel.de        902                 :CBC      462733 :     shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats;
                                903                 :                : 
                                904                 :                :     /*
                                905                 :                :      * Ignore entries that didn't accumulate any actual counts.  If the table
                                906                 :                :      * was truncated, we still need to flush the entry to reset the live/dead
                                907                 :                :      * counters and ins_since_vacuum even when no other counts were
                                908                 :                :      * accumulated.
                                909                 :                :      */
   79 michael@paquier.xyz       910   [ +  +  +  + ]:GNC      924859 :     if (!lstats->tab.truncdropped &&
                                911                 :         462126 :         pg_memory_is_all_zeros(&lstats->tab.counts,
                                912         [ +  + ]:           4808 :                                sizeof(struct PgStat_TableCounts)) &&
                                913                 :           4808 :         pg_memory_is_all_zeros(&lstats->tab.counts_xact,
                                914                 :                :                                sizeof(struct PgStat_TableCountsXact)))
 1697 andres@anarazel.de        915                 :CBC        3394 :         return true;
                                916                 :                : 
                                917         [ -  + ]:         459339 :     if (!pgstat_lock_entry(entry_ref, nowait))
 1697 andres@anarazel.de        918                 :LBC        (11) :         return false;
                                919                 :                : 
                                920                 :                :     /* add the values to the shared entry. */
 1697 andres@anarazel.de        921                 :CBC      459339 :     tabentry = &shtabstats->stats;
                                922                 :                : 
  106 michael@paquier.xyz       923                 :GNC      459339 :     tabentry->numscans += lstats->tab.counts.numscans;
                                924         [ +  + ]:         459339 :     if (lstats->tab.counts.numscans)
                                925                 :                :     {
 1506 andres@anarazel.de        926                 :CBC      104466 :         TimestampTz t = GetCurrentTransactionStopTimestamp();
                                927                 :                : 
                                928         [ +  + ]:         104466 :         if (t > tabentry->lastscan)
                                929                 :         103741 :             tabentry->lastscan = t;
                                930                 :                :     }
  106 michael@paquier.xyz       931                 :GNC      459339 :     tabentry->tuples_returned += lstats->tab.counts.tuples_returned;
                                932                 :         459339 :     tabentry->tuples_fetched += lstats->tab.counts.tuples_fetched;
   79                           933                 :         459339 :     tabentry->tuples_inserted += lstats->tab.counts_xact.tuples_inserted;
                                934                 :         459339 :     tabentry->tuples_updated += lstats->tab.counts_xact.tuples_updated;
                                935                 :         459339 :     tabentry->tuples_deleted += lstats->tab.counts_xact.tuples_deleted;
                                936                 :         459339 :     tabentry->tuples_hot_updated += lstats->tab.counts_xact.tuples_hot_updated;
                                937                 :         459339 :     tabentry->tuples_newpage_updated += lstats->tab.counts_xact.tuples_newpage_updated;
                                938                 :                : 
                                939                 :                :     /*
                                940                 :                :      * If table was truncated/dropped, first reset the live/dead counters.
                                941                 :                :      */
                                942         [ +  + ]:         459339 :     if (lstats->tab.truncdropped)
                                943                 :                :     {
 1453 michael@paquier.xyz       944                 :CBC         607 :         tabentry->live_tuples = 0;
                                945                 :            607 :         tabentry->dead_tuples = 0;
                                946                 :            607 :         tabentry->ins_since_vacuum = 0;
                                947                 :                :     }
                                948                 :                : 
   79 michael@paquier.xyz       949                 :GNC      459339 :     tabentry->live_tuples += lstats->tab.counts_xact.delta_live_tuples;
                                950                 :         459339 :     tabentry->dead_tuples += lstats->tab.counts_xact.delta_dead_tuples;
                                951                 :         459339 :     tabentry->mod_since_analyze += lstats->tab.counts_xact.changed_tuples;
                                952                 :                : 
                                953                 :                :     /*
                                954                 :                :      * Using tuples_inserted to update ins_since_vacuum does mean that we'll
                                955                 :                :      * track aborted inserts too.  This isn't ideal, but otherwise probably
                                956                 :                :      * not worth adding an extra field for.  It may just amount to autovacuums
                                957                 :                :      * triggering for inserts more often than they maybe should, which is
                                958                 :                :      * probably not going to be common enough to be too concerned about here.
                                959                 :                :      */
                                960                 :         459339 :     tabentry->ins_since_vacuum += lstats->tab.counts_xact.tuples_inserted;
                                961                 :                : 
  106                           962                 :         459339 :     tabentry->blocks_fetched += lstats->tab.counts.blocks_fetched;
                                963                 :         459339 :     tabentry->blocks_hit += lstats->tab.counts.blocks_hit;
                                964                 :                : 
                                965                 :                :     /* Clamp live_tuples in case of negative delta_live_tuples */
 1453 michael@paquier.xyz       966                 :CBC      459339 :     tabentry->live_tuples = Max(tabentry->live_tuples, 0);
                                967                 :                :     /* Likewise for dead_tuples */
                                968                 :         459339 :     tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
                                969                 :                : 
 1697 andres@anarazel.de        970                 :         459339 :     pgstat_unlock_entry(entry_ref);
                                971                 :                : 
                                972                 :                :     /* The entry was successfully flushed, add the same to database stats */
                                973                 :         459339 :     dbentry = pgstat_prep_database_pending(dboid);
  106 michael@paquier.xyz       974                 :GNC      459339 :     dbentry->tuples_returned += lstats->tab.counts.tuples_returned;
                                975                 :         459339 :     dbentry->tuples_fetched += lstats->tab.counts.tuples_fetched;
   79                           976                 :         459339 :     dbentry->tuples_inserted += lstats->tab.counts_xact.tuples_inserted;
                                977                 :         459339 :     dbentry->tuples_updated += lstats->tab.counts_xact.tuples_updated;
                                978                 :         459339 :     dbentry->tuples_deleted += lstats->tab.counts_xact.tuples_deleted;
  106                           979                 :         459339 :     dbentry->blocks_fetched += lstats->tab.counts.blocks_fetched;
                                980                 :         459339 :     dbentry->blocks_hit += lstats->tab.counts.blocks_hit;
                                981                 :                : 
 1697 andres@anarazel.de        982                 :CBC      459339 :     return true;
                                983                 :                : }
                                984                 :                : 
                                985                 :                : void
                                986                 :         494056 : pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
                                987                 :                : {
  106 michael@paquier.xyz       988                 :GNC      494056 :     PgStat_RelationStatus *pending = (PgStat_RelationStatus *) entry_ref->pending;
                                989                 :                : 
 1697 andres@anarazel.de        990         [ +  + ]:CBC      494056 :     if (pending->relation)
                                991                 :         438635 :         pgstat_unlink_relation(pending->relation);
 1713                           992                 :         494056 : }
                                993                 :                : 
                                994                 :                : void
  418 michael@paquier.xyz       995                 :           7630 : pgstat_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
                                996                 :                : {
                                997                 :           7630 :     ((PgStatShared_Relation *) header)->stats.stat_reset_time = ts;
                                998                 :           7630 : }
                                999                 :                : 
                               1000                 :                : /*
                               1001                 :                :  * Find or create a PgStat_RelationStatus entry for rel. New entry is created and
                               1002                 :                :  * initialized if not exists.
                               1003                 :                :  */
                               1004                 :                : static PgStat_RelationStatus *
  108 michael@paquier.xyz      1005                 :GNC     1225015 : pgstat_prep_relation_pending(PgStat_Kind kind, Oid rel_id, bool isshared)
                               1006                 :                : {
                               1007                 :                :     PgStat_EntryRef *entry_ref;
                               1008                 :                :     PgStat_RelationStatus *pending;
                               1009                 :                : 
                               1010         [ +  + ]:        1225015 :     entry_ref = pgstat_prep_pending_entry(kind,
                               1011                 :                :                                           isshared ? InvalidOid : MyDatabaseId,
                               1012                 :                :                                           rel_id, NULL);
 1697 andres@anarazel.de       1013                 :CBC     1225015 :     pending = entry_ref->pending;
  106 michael@paquier.xyz      1014                 :GNC     1225015 :     pending->kind = kind;
                               1015         [ +  + ]:        1225015 :     if (kind != PGSTAT_KIND_INDEX)
                               1016                 :                :     {
                               1017                 :         538442 :         pending->tab.id = rel_id;
                               1018                 :         538442 :         pending->tab.shared = isshared;
                               1019                 :                :     }
                               1020                 :                : 
 1697 andres@anarazel.de       1021                 :CBC     1225015 :     return pending;
                               1022                 :                : }
                               1023                 :                : 
                               1024                 :                : /*
                               1025                 :                :  * add a new (sub)transaction state record
                               1026                 :                :  */
                               1027                 :                : static void
  106 michael@paquier.xyz      1028                 :GNC      483518 : add_tabstat_xact_level(PgStat_RelationStatus *pgstat_info, int nest_level)
                               1029                 :                : {
                               1030                 :                :     PgStat_SubXactStatus *xact_state;
                               1031                 :                :     PgStat_TableXactStatus *trans;
                               1032                 :                : 
                               1033                 :                :     /*
                               1034                 :                :      * If this is the first rel to be modified at the current nest level, we
                               1035                 :                :      * first have to push a transaction stack entry.
                               1036                 :                :      */
 1697 andres@anarazel.de       1037                 :CBC      483518 :     xact_state = pgstat_get_xact_stack_level(nest_level);
                               1038                 :                : 
                               1039                 :                :     /* Now make a per-table stack entry */
                               1040                 :                :     trans = (PgStat_TableXactStatus *)
 1713                          1041                 :         483518 :         MemoryContextAllocZero(TopTransactionContext,
                               1042                 :                :                                sizeof(PgStat_TableXactStatus));
                               1043                 :         483518 :     trans->nest_level = nest_level;
  106 michael@paquier.xyz      1044                 :GNC      483518 :     trans->upper = pgstat_info->tab.trans;
 1713 andres@anarazel.de       1045                 :CBC      483518 :     trans->parent = pgstat_info;
                               1046                 :         483518 :     trans->next = xact_state->first;
                               1047                 :         483518 :     xact_state->first = trans;
  106 michael@paquier.xyz      1048                 :GNC      483518 :     pgstat_info->tab.trans = trans;
 1713 andres@anarazel.de       1049                 :CBC      483518 : }
                               1050                 :                : 
                               1051                 :                : /*
                               1052                 :                :  * Add a new (sub)transaction record if needed.
                               1053                 :                :  */
                               1054                 :                : static void
  106 michael@paquier.xyz      1055                 :GNC    16652063 : ensure_tabstat_xact_level(PgStat_RelationStatus *pgstat_info)
                               1056                 :                : {
 1713 andres@anarazel.de       1057                 :CBC    16652063 :     int         nest_level = GetCurrentTransactionNestLevel();
                               1058                 :                : 
  106 michael@paquier.xyz      1059         [ +  + ]:GNC    16652063 :     if (pgstat_info->tab.trans == NULL ||
                               1060         [ +  + ]:       16183938 :         pgstat_info->tab.trans->nest_level != nest_level)
 1713 andres@anarazel.de       1061                 :CBC      483518 :         add_tabstat_xact_level(pgstat_info, nest_level);
                               1062                 :       16652063 : }
                               1063                 :                : 
                               1064                 :                : /*
                               1065                 :                :  * Whenever a table is truncated/dropped, we save its i/u/d counters so that
                               1066                 :                :  * they can be cleared, and if the (sub)xact that executed the truncate/drop
                               1067                 :                :  * later aborts, the counters can be restored to the saved (pre-truncate/drop)
                               1068                 :                :  * values.
                               1069                 :                :  *
                               1070                 :                :  * Note that for truncate we do this on the first truncate in any particular
                               1071                 :                :  * subxact level only.
                               1072                 :                :  */
                               1073                 :                : static void
 1697                          1074                 :           3285 : save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop)
                               1075                 :                : {
 1713                          1076   [ +  +  +  + ]:           3285 :     if (!trans->truncdropped || is_drop)
                               1077                 :                :     {
                               1078                 :           3253 :         trans->inserted_pre_truncdrop = trans->tuples_inserted;
                               1079                 :           3253 :         trans->updated_pre_truncdrop = trans->tuples_updated;
                               1080                 :           3253 :         trans->deleted_pre_truncdrop = trans->tuples_deleted;
                               1081                 :           3253 :         trans->truncdropped = true;
                               1082                 :                :     }
                               1083                 :           3285 : }
                               1084                 :                : 
                               1085                 :                : /*
                               1086                 :                :  * restore counters when a truncate aborts
                               1087                 :                :  */
                               1088                 :                : static void
 1697                          1089                 :          19390 : restore_truncdrop_counters(PgStat_TableXactStatus *trans)
                               1090                 :                : {
 1713                          1091         [ +  + ]:          19390 :     if (trans->truncdropped)
                               1092                 :                :     {
                               1093                 :            241 :         trans->tuples_inserted = trans->inserted_pre_truncdrop;
                               1094                 :            241 :         trans->tuples_updated = trans->updated_pre_truncdrop;
                               1095                 :            241 :         trans->tuples_deleted = trans->deleted_pre_truncdrop;
                               1096                 :                :     }
                               1097                 :          19390 : }
        

Generated by: LCOV version 2.0-1