LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_relation.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.0 % 402 390
Test Date: 2026-12-05 17:16:08 Functions: 100.0 % 31 31
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 89.0 % 172 153

             Branch data     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
      56                 :     1446455 : pgstat_get_relation_kind(char relkind)
      57                 :             : {
      58         [ +  + ]:     1446455 :     if (relkind == RELKIND_INDEX)
      59                 :      795663 :         return PGSTAT_KIND_INDEX;
      60                 :      650792 :     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
      69                 :         340 : pgstat_copy_relation_stats(Relation dst, Relation src)
      70                 :             : {
      71                 :         340 :     PgStat_Kind kind = pgstat_get_relation_kind(src->rd_rel->relkind);
      72                 :             : 
      73         [ +  - ]:         340 :     if (kind == PGSTAT_KIND_INDEX)
      74                 :             :     {
      75                 :             :         PgStat_StatIdxEntry *srcstats;
      76                 :             :         PgStatShared_Index *dstshstats;
      77                 :             :         PgStat_EntryRef *dst_ref;
      78                 :             : 
      79                 :         340 :         srcstats = pgstat_fetch_stat_idxentry_ext(src->rd_rel->relisshared,
      80                 :             :                                                   RelationGetRelid(src),
      81                 :             :                                                   NULL);
      82         [ +  + ]:         340 :         if (!srcstats)
      83                 :         202 :             return;
      84                 :             : 
      85                 :         138 :         dst_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_INDEX,
      86                 :         138 :                                               dst->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
      87         [ -  + ]:         138 :                                               RelationGetRelid(dst),
      88                 :             :                                               false);
      89                 :             : 
      90                 :         138 :         dstshstats = (PgStatShared_Index *) dst_ref->shared_stats;
      91                 :         138 :         dstshstats->stats = *srcstats;
      92                 :             : 
      93                 :         138 :         pgstat_unlock_entry(dst_ref);
      94                 :             :     }
      95                 :             :     else
      96                 :             :     {
      97                 :             :         PgStat_StatTabEntry *srcstats;
      98                 :             :         PgStatShared_Relation *dstshstats;
      99                 :             :         PgStat_EntryRef *dst_ref;
     100                 :             : 
     101                 :           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
     131                 :    29706421 : pgstat_init_relation(Relation rel)
     132                 :             : {
     133                 :    29706421 :     char        relkind = rel->rd_rel->relkind;
     134                 :             : 
     135                 :             :     /*
     136                 :             :      * We only count stats for relations with storage and partitioned tables
     137                 :             :      */
     138   [ +  +  +  +  :    29706421 :     if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
          +  +  +  +  +  
                +  +  + ]
     139                 :             :     {
     140                 :      104091 :         rel->pgstat_enabled = false;
     141                 :      104091 :         rel->pgstat_info = NULL;
     142                 :      104091 :         return;
     143                 :             :     }
     144                 :             : 
     145         [ +  + ]:    29602330 :     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;
     152                 :         197 :         rel->pgstat_info = NULL;
     153                 :         197 :         return;
     154                 :             :     }
     155                 :             : 
     156                 :    29602133 :     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                 :     1302405 : pgstat_assoc_relation(Relation rel)
     172                 :             : {
     173                 :             :     PgStat_Kind kind;
     174                 :             : 
     175                 :             :     Assert(rel->pgstat_enabled);
     176                 :             :     Assert(rel->pgstat_info == NULL);
     177                 :             : 
     178                 :     1302405 :     kind = pgstat_get_relation_kind(rel->rd_rel->relkind);
     179                 :             : 
     180                 :             :     /* find or make the PgStat_RelationStatus entry, and update link */
     181                 :     2604810 :     rel->pgstat_info = pgstat_prep_relation_pending(kind,
     182                 :             :                                                     RelationGetRelid(rel),
     183                 :     1302405 :                                                     rel->rd_rel->relisshared);
     184                 :             : 
     185                 :             :     /* don't allow link a stats to multiple relcache entries */
     186                 :             :     Assert(rel->pgstat_info->relation == NULL);
     187                 :             : 
     188                 :             :     /* mark this relation as the owner */
     189                 :     1302405 :     rel->pgstat_info->relation = rel;
     190                 :     1302405 : }
     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                 :     1993261 : pgstat_unlink_relation(Relation rel)
     198                 :             : {
     199                 :             :     /* remove the link to stats info if any */
     200         [ +  + ]:     1993261 :     if (rel->pgstat_info == NULL)
     201                 :      690856 :         return;
     202                 :             : 
     203                 :             :     /* link sanity check */
     204                 :             :     Assert(rel->pgstat_info->relation == rel);
     205                 :     1302405 :     rel->pgstat_info->relation = NULL;
     206                 :     1302405 :     rel->pgstat_info = NULL;
     207                 :             : }
     208                 :             : 
     209                 :             : /*
     210                 :             :  * Ensure that stats are dropped if transaction aborts.
     211                 :             :  */
     212                 :             : void
     213                 :       92184 : pgstat_create_relation(Relation rel)
     214                 :             : {
     215                 :       92184 :     PgStat_Kind kind = pgstat_get_relation_kind(rel->rd_rel->relkind);
     216                 :             : 
     217                 :       92184 :     pgstat_create_transactional(kind,
     218                 :       92184 :                                 rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
     219         [ +  + ]:       92184 :                                 RelationGetRelid(rel));
     220                 :       92184 : }
     221                 :             : 
     222                 :             : /*
     223                 :             :  * Ensure that stats are dropped if transaction commits.
     224                 :             :  */
     225                 :             : void
     226                 :       51526 : pgstat_drop_relation(Relation rel)
     227                 :             : {
     228                 :       51526 :     int         nest_level = GetCurrentTransactionNestLevel();
     229                 :             :     PgStat_RelationStatus *pgstat_info;
     230                 :       51526 :     PgStat_Kind kind = pgstat_get_relation_kind(rel->rd_rel->relkind);
     231                 :             : 
     232                 :       51526 :     pgstat_drop_transactional(kind,
     233                 :       51526 :                               rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
     234         [ -  + ]:       51526 :                               RelationGetRelid(rel));
     235                 :             : 
     236   [ +  +  +  +  :       51526 :     if (!pgstat_should_count_relation(rel))
                   +  + ]
     237                 :        5454 :         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                 :       46072 :     pgstat_info = rel->pgstat_info;
     246         [ +  + ]:       46072 :     if (pgstat_info->kind == PGSTAT_KIND_INDEX)
     247                 :       15552 :         return;
     248                 :             : 
     249         [ +  + ]:       30520 :     if (pgstat_info->tab.trans &&
     250         [ +  + ]:         955 :         pgstat_info->tab.trans->nest_level == nest_level)
     251                 :             :     {
     252                 :         951 :         save_truncdrop_counters(pgstat_info->tab.trans, true);
     253                 :         951 :         pgstat_info->tab.trans->tuples_inserted = 0;
     254                 :         951 :         pgstat_info->tab.trans->tuples_updated = 0;
     255                 :         951 :         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
     264                 :      127732 : 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         [ +  + ]:      127732 :     Oid         dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
     271                 :             :     TimestampTz ts;
     272                 :             :     PgStat_Counter elapsedtime;
     273                 :             : 
     274         [ -  + ]:      127732 :     if (!pgstat_track_counts)
     275                 :           0 :         return;
     276                 :             : 
     277                 :             :     /* Store the data in the table's hash table entry. */
     278                 :      127732 :     ts = GetCurrentTimestamp();
     279                 :      127732 :     elapsedtime = TimestampDifferenceMilliseconds(starttime, ts);
     280                 :             : 
     281                 :             :     /* block acquiring lock for the same reason as pgstat_report_autovac() */
     282                 :      127732 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, dboid,
     283                 :      127732 :                                             RelationGetRelid(rel), false);
     284                 :             : 
     285                 :      127732 :     shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
     286                 :      127732 :     tabentry = &shtabentry->stats;
     287                 :             : 
     288                 :             :     Assert(livetuples >= -1);
     289                 :             : 
     290         [ +  + ]:      127732 :     if (livetuples >= 0)
     291                 :      127719 :         tabentry->live_tuples = livetuples;
     292                 :      127732 :     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                 :      127732 :     tabentry->ins_since_vacuum = 0;
     305                 :             : 
     306         [ +  + ]:      127732 :     if (AmAutoVacuumWorkerProcess())
     307                 :             :     {
     308                 :      111146 :         tabentry->last_autovacuum_time = ts;
     309                 :      111146 :         tabentry->autovacuum_count++;
     310                 :      111146 :         tabentry->total_autovacuum_time += elapsedtime;
     311                 :             :     }
     312                 :             :     else
     313                 :             :     {
     314                 :       16586 :         tabentry->last_vacuum_time = ts;
     315                 :       16586 :         tabentry->vacuum_count++;
     316                 :       16586 :         tabentry->total_vacuum_time += elapsedtime;
     317                 :             :     }
     318                 :             : 
     319                 :      127732 :     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                 :             :      */
     327                 :      127732 :     pgstat_flush_io(false);
     328                 :      127732 :     (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
     338                 :       10634 : 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;
     345         [ +  + ]:       10634 :     Oid         dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
     346                 :             :     TimestampTz ts;
     347                 :             :     PgStat_Counter elapsedtime;
     348                 :             : 
     349         [ -  + ]:       10634 :     if (!pgstat_track_counts)
     350                 :           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                 :             :      */
     364   [ +  +  +  +  :       10634 :     if (pgstat_should_count_relation(rel) &&
                   +  + ]
     365         [ +  + ]:       10581 :         rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
     366                 :             :     {
     367                 :             :         PgStat_TableXactStatus *trans;
     368                 :             : 
     369                 :             :         Assert(rel->pgstat_info->kind == PGSTAT_KIND_RELATION);
     370         [ +  + ]:       10209 :         for (trans = rel->pgstat_info->tab.trans; trans; trans = trans->upper)
     371                 :             :         {
     372                 :         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 */
     376                 :       10058 :         deadtuples -= rel->pgstat_info->tab.counts_xact.delta_dead_tuples;
     377                 :             :         /* Since ANALYZE's counts are estimates, we could have underflowed */
     378                 :       10058 :         livetuples = Max(livetuples, 0);
     379                 :       10058 :         deadtuples = Max(deadtuples, 0);
     380                 :             :     }
     381                 :             : 
     382                 :             :     /* Store the data in the table's hash table entry. */
     383                 :       10634 :     ts = GetCurrentTimestamp();
     384                 :       10634 :     elapsedtime = TimestampDifferenceMilliseconds(starttime, ts);
     385                 :             : 
     386                 :             :     /* block acquiring lock for the same reason as pgstat_report_autovac() */
     387                 :       10634 :     entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_RELATION, dboid,
     388                 :       10634 :                                             RelationGetRelid(rel),
     389                 :             :                                             false);
     390                 :             :     /* can't get dropped while accessed */
     391                 :             :     Assert(entry_ref != NULL && entry_ref->shared_stats != NULL);
     392                 :             : 
     393                 :       10634 :     shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
     394                 :       10634 :     tabentry = &shtabentry->stats;
     395                 :             : 
     396                 :       10634 :     tabentry->live_tuples = livetuples;
     397                 :       10634 :     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                 :             :      */
     404         [ +  + ]:       10634 :     if (resetcounter)
     405                 :       10594 :         tabentry->mod_since_analyze = 0;
     406                 :             : 
     407         [ +  + ]:       10634 :     if (AmAutoVacuumWorkerProcess())
     408                 :             :     {
     409                 :         414 :         tabentry->last_autoanalyze_time = ts;
     410                 :         414 :         tabentry->autoanalyze_count++;
     411                 :         414 :         tabentry->total_autoanalyze_time += elapsedtime;
     412                 :             :     }
     413                 :             :     else
     414                 :             :     {
     415                 :       10220 :         tabentry->last_analyze_time = ts;
     416                 :       10220 :         tabentry->analyze_count++;
     417                 :       10220 :         tabentry->total_analyze_time += elapsedtime;
     418                 :             :     }
     419                 :             : 
     420                 :       10634 :     pgstat_unlock_entry(entry_ref);
     421                 :             : 
     422                 :             :     /* see pgstat_report_vacuum() */
     423                 :       10634 :     pgstat_flush_io(false);
     424                 :       10634 :     (void) pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO);
     425                 :             : }
     426                 :             : 
     427                 :             : /*
     428                 :             :  * count a tuple insertion of n tuples
     429                 :             :  */
     430                 :             : void
     431                 :    12827760 : pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
     432                 :             : {
     433   [ +  +  +  +  :    12827760 :     if (pgstat_should_count_relation(rel))
                   +  + ]
     434                 :             :     {
     435                 :    12596629 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
     436                 :             : 
     437                 :             :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
     438                 :             : 
     439                 :    12596629 :         ensure_tabstat_xact_level(pgstat_info);
     440                 :    12596629 :         pgstat_info->tab.trans->tuples_inserted += n;
     441                 :             :     }
     442                 :    12827760 : }
     443                 :             : 
     444                 :             : /*
     445                 :             :  * count a tuple update
     446                 :             :  */
     447                 :             : void
     448                 :     2429285 : pgstat_count_heap_update(Relation rel, bool hot, bool newpage)
     449                 :             : {
     450                 :             :     Assert(!(hot && newpage));
     451                 :             : 
     452   [ +  +  -  +  :     2429285 :     if (pgstat_should_count_relation(rel))
                   +  + ]
     453                 :             :     {
     454                 :     2429283 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
     455                 :             : 
     456                 :             :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
     457                 :             : 
     458                 :     2429283 :         ensure_tabstat_xact_level(pgstat_info);
     459                 :     2429283 :         pgstat_info->tab.trans->tuples_updated++;
     460                 :             : 
     461                 :             :         /*
     462                 :             :          * tuples_hot_updated and tuples_newpage_updated counters are
     463                 :             :          * nontransactional, but keep them with tuples_updated until the
     464                 :             :          * (sub)transaction ends, so a flush does not publish them ahead of
     465                 :             :          * it.
     466                 :             :          */
     467         [ +  + ]:     2429283 :         if (hot)
     468                 :      180345 :             pgstat_info->tab.trans->tuples_hot_updated++;
     469         [ +  + ]:     2248938 :         else if (newpage)
     470                 :     2231305 :             pgstat_info->tab.trans->tuples_newpage_updated++;
     471                 :             :     }
     472                 :     2429285 : }
     473                 :             : 
     474                 :             : /*
     475                 :             :  * count a tuple deletion
     476                 :             :  */
     477                 :             : void
     478                 :     1971479 : pgstat_count_heap_delete(Relation rel)
     479                 :             : {
     480   [ -  +  -  -  :     1971479 :     if (pgstat_should_count_relation(rel))
                   +  - ]
     481                 :             :     {
     482                 :     1971479 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
     483                 :             : 
     484                 :             :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
     485                 :             : 
     486                 :     1971479 :         ensure_tabstat_xact_level(pgstat_info);
     487                 :     1971479 :         pgstat_info->tab.trans->tuples_deleted++;
     488                 :             :     }
     489                 :     1971479 : }
     490                 :             : 
     491                 :             : /*
     492                 :             :  * update tuple counters due to truncate
     493                 :             :  */
     494                 :             : void
     495                 :        2368 : pgstat_count_truncate(Relation rel)
     496                 :             : {
     497   [ +  +  +  -  :        2368 :     if (pgstat_should_count_relation(rel))
                   +  - ]
     498                 :             :     {
     499                 :        2368 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
     500                 :             : 
     501                 :             :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
     502                 :             : 
     503                 :        2368 :         ensure_tabstat_xact_level(pgstat_info);
     504                 :        2368 :         save_truncdrop_counters(pgstat_info->tab.trans, false);
     505                 :        2368 :         pgstat_info->tab.trans->tuples_inserted = 0;
     506                 :        2368 :         pgstat_info->tab.trans->tuples_updated = 0;
     507                 :        2368 :         pgstat_info->tab.trans->tuples_deleted = 0;
     508                 :             :     }
     509                 :        2368 : }
     510                 :             : 
     511                 :             : /*
     512                 :             :  * update dead-tuples count
     513                 :             :  *
     514                 :             :  * The semantics of this are that we are reporting the nontransactional
     515                 :             :  * recovery of "delta" dead tuples; so delta_dead_tuples decreases
     516                 :             :  * rather than increasing, and the change goes straight into the per-table
     517                 :             :  * counter, not into transactional state.
     518                 :             :  */
     519                 :             : void
     520                 :       22339 : pgstat_update_heap_dead_tuples(Relation rel, int delta)
     521                 :             : {
     522   [ +  +  -  +  :       22339 :     if (pgstat_should_count_relation(rel))
                   +  + ]
     523                 :             :     {
     524                 :       22338 :         PgStat_RelationStatus *pgstat_info = rel->pgstat_info;
     525                 :             : 
     526                 :             :         Assert(pgstat_info->kind == PGSTAT_KIND_RELATION);
     527                 :             : 
     528                 :       22338 :         pgstat_info->tab.counts_xact.delta_dead_tuples -= delta;
     529                 :             :     }
     530                 :       22339 : }
     531                 :             : 
     532                 :             : /*
     533                 :             :  * Support function for the SQL-callable pgstat* functions. Returns
     534                 :             :  * the collected statistics for one table or NULL. NULL doesn't mean
     535                 :             :  * that the table doesn't exist, just that there are no statistics, so the
     536                 :             :  * caller is better off to report ZERO instead.
     537                 :             :  */
     538                 :             : PgStat_StatTabEntry *
     539                 :        6115 : pgstat_fetch_stat_tabentry(Oid relid)
     540                 :             : {
     541                 :        6115 :     return pgstat_fetch_stat_tabentry_ext(IsSharedRelation(relid), relid, NULL);
     542                 :             : }
     543                 :             : 
     544                 :             : /*
     545                 :             :  * More efficient version of pgstat_fetch_stat_tabentry(), allowing to specify
     546                 :             :  * whether the to-be-accessed table is a shared relation or not.  This version
     547                 :             :  * also returns whether the caller can pfree() the result if desired.
     548                 :             :  */
     549                 :             : PgStat_StatTabEntry *
     550                 :      297566 : pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid, bool *may_free)
     551                 :             : {
     552         [ +  + ]:      297566 :     Oid         dboid = (shared ? InvalidOid : MyDatabaseId);
     553                 :             : 
     554                 :      297566 :     return (PgStat_StatTabEntry *)
     555                 :      297566 :         pgstat_fetch_entry(PGSTAT_KIND_RELATION, dboid, reloid, may_free);
     556                 :             : }
     557                 :             : 
     558                 :             : /*
     559                 :             :  * find any existing PgStat_RelationStatus entry for rel and kind
     560                 :             :  *
     561                 :             :  * Find any existing PgStat_RelationStatus entry for rel_id in the current
     562                 :             :  * database. If not found, try finding from shared tables.
     563                 :             :  *
     564                 :             :  * If an entry is found, copy it.  For a PGSTAT_KIND_INDEX entry, just return
     565                 :             :  * the copy.  For a PGSTAT_KIND_RELATION entry, increment the copy's counters
     566                 :             :  * with their subtransaction counterparts, then return the copy.  The caller
     567                 :             :  * may need to pfree() the copy.
     568                 :             :  *
     569                 :             :  * If no entry found, return NULL, don't create a new one.
     570                 :             :  */
     571                 :             : PgStat_RelationStatus *
     572                 :          33 : find_relstat_entry_kind(PgStat_Kind kind, Oid rel_id)
     573                 :             : {
     574                 :             :     PgStat_EntryRef *entry_ref;
     575                 :             :     PgStat_TableXactStatus *trans;
     576                 :          33 :     PgStat_RelationStatus *relentry = NULL;
     577                 :          33 :     PgStat_RelationStatus *relstatus = NULL;
     578                 :             : 
     579                 :          33 :     entry_ref = pgstat_fetch_pending_entry(kind, MyDatabaseId, rel_id);
     580         [ +  + ]:          33 :     if (!entry_ref)
     581                 :             :     {
     582                 :           8 :         entry_ref = pgstat_fetch_pending_entry(kind, InvalidOid, rel_id);
     583         [ +  - ]:           8 :         if (!entry_ref)
     584                 :           8 :             return relstatus;
     585                 :             :     }
     586                 :             : 
     587                 :          25 :     relentry = (PgStat_RelationStatus *) entry_ref->pending;
     588                 :          25 :     relstatus = palloc_object(PgStat_RelationStatus);
     589                 :          25 :     *relstatus = *relentry;
     590                 :             : 
     591                 :             :     /*
     592                 :             :      * For index entries, just return the copy.  There is no transactional
     593                 :             :      * data.
     594                 :             :      */
     595         [ -  + ]:          25 :     if (kind == PGSTAT_KIND_INDEX)
     596                 :           0 :         return relstatus;
     597                 :             : 
     598                 :             :     /*
     599                 :             :      * Reset relstatus->tab.trans in the copy of PgStat_RelationStatus as it
     600                 :             :      * may point to a shared memory area.  Its data is saved below, so
     601                 :             :      * removing it does not matter.
     602                 :             :      */
     603                 :          25 :     relstatus->tab.trans = NULL;
     604                 :             : 
     605                 :             :     /*
     606                 :             :      * Live subtransaction counts are not included yet.  This is not a hot
     607                 :             :      * code path so reconcile tuples_inserted, tuples_updated and
     608                 :             :      * tuples_deleted even if the caller may not be interested in this data.
     609                 :             :      */
     610         [ +  + ]:          58 :     for (trans = relentry->tab.trans; trans != NULL; trans = trans->upper)
     611                 :             :     {
     612                 :          33 :         relstatus->tab.counts_xact.tuples_inserted += trans->tuples_inserted;
     613                 :          33 :         relstatus->tab.counts_xact.tuples_updated += trans->tuples_updated;
     614                 :          33 :         relstatus->tab.counts_xact.tuples_deleted += trans->tuples_deleted;
     615                 :          33 :         relstatus->tab.counts_xact.tuples_hot_updated +=
     616                 :          33 :             trans->tuples_hot_updated;
     617                 :          33 :         relstatus->tab.counts_xact.tuples_newpage_updated +=
     618                 :          33 :             trans->tuples_newpage_updated;
     619                 :             :     }
     620                 :             : 
     621                 :          25 :     return relstatus;
     622                 :             : }
     623                 :             : 
     624                 :             : /*
     625                 :             :  * Perform relation stats specific end-of-transaction work. Helper for
     626                 :             :  * AtEOXact_PgStat.
     627                 :             :  *
     628                 :             :  * Transfer transactional insert/update counts into the base relstat entries.
     629                 :             :  * We don't bother to free any of the transactional state, since it's all in
     630                 :             :  * TopTransactionContext and will go away anyway.
     631                 :             :  */
     632                 :             : void
     633                 :      164716 : AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
     634                 :             : {
     635                 :             :     PgStat_TableXactStatus *trans;
     636                 :             : 
     637         [ +  + ]:      635681 :     for (trans = xact_state->first; trans != NULL; trans = trans->next)
     638                 :             :     {
     639                 :             :         PgStat_RelationStatus *relstat;
     640                 :             : 
     641                 :             :         Assert(trans->nest_level == 1);
     642                 :             :         Assert(trans->upper == NULL);
     643                 :      470965 :         relstat = trans->parent;
     644                 :             :         Assert(relstat->tab.trans == trans);
     645                 :             :         /* restore pre-truncate/drop stats (if any) in case of aborted xact */
     646         [ +  + ]:      470965 :         if (!isCommit)
     647                 :       18107 :             restore_truncdrop_counters(trans);
     648                 :             :         /* count attempted actions regardless of commit/abort */
     649                 :      470965 :         relstat->tab.counts_xact.tuples_inserted += trans->tuples_inserted;
     650                 :      470965 :         relstat->tab.counts_xact.tuples_updated += trans->tuples_updated;
     651                 :      470965 :         relstat->tab.counts_xact.tuples_deleted += trans->tuples_deleted;
     652                 :      470965 :         relstat->tab.counts_xact.tuples_hot_updated +=
     653                 :      470965 :             trans->tuples_hot_updated;
     654                 :      470965 :         relstat->tab.counts_xact.tuples_newpage_updated +=
     655                 :      470965 :             trans->tuples_newpage_updated;
     656         [ +  + ]:      470965 :         if (isCommit)
     657                 :             :         {
     658                 :      452858 :             relstat->tab.truncdropped = trans->truncdropped;
     659         [ +  + ]:      452858 :             if (trans->truncdropped)
     660                 :             :             {
     661                 :             :                 /* forget live/dead stats seen by backend thus far */
     662                 :        2988 :                 relstat->tab.counts_xact.delta_live_tuples = 0;
     663                 :        2988 :                 relstat->tab.counts_xact.delta_dead_tuples = 0;
     664                 :             :             }
     665                 :             :             /* insert adds a live tuple, delete removes one */
     666                 :      452858 :             relstat->tab.counts_xact.delta_live_tuples +=
     667                 :      452858 :                 trans->tuples_inserted - trans->tuples_deleted;
     668                 :             :             /* update and delete each create a dead tuple */
     669                 :      452858 :             relstat->tab.counts_xact.delta_dead_tuples +=
     670                 :      452858 :                 trans->tuples_updated + trans->tuples_deleted;
     671                 :             :             /* insert, update, delete each count as one change event */
     672                 :      452858 :             relstat->tab.counts_xact.changed_tuples +=
     673                 :      452858 :                 trans->tuples_inserted + trans->tuples_updated +
     674                 :      452858 :                 trans->tuples_deleted;
     675                 :             :         }
     676                 :             :         else
     677                 :             :         {
     678                 :             :             /* inserted tuples are dead, deleted tuples are unaffected */
     679                 :       18107 :             relstat->tab.counts_xact.delta_dead_tuples +=
     680                 :       18107 :                 trans->tuples_inserted + trans->tuples_updated;
     681                 :             :             /* an aborted xact generates no changed_tuple events */
     682                 :             :         }
     683                 :      470965 :         relstat->tab.trans = NULL;
     684                 :             :     }
     685                 :      164716 : }
     686                 :             : 
     687                 :             : /*
     688                 :             :  * Perform relation stats specific end-of-sub-transaction work. Helper for
     689                 :             :  * AtEOSubXact_PgStat.
     690                 :             :  *
     691                 :             :  * Transfer transactional insert/update counts into the next higher
     692                 :             :  * subtransaction state.
     693                 :             :  */
     694                 :             : void
     695                 :       16386 : AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
     696                 :             : {
     697                 :             :     PgStat_TableXactStatus *trans;
     698                 :             :     PgStat_TableXactStatus *next_trans;
     699                 :             : 
     700         [ +  + ]:       33512 :     for (trans = xact_state->first; trans != NULL; trans = next_trans)
     701                 :             :     {
     702                 :             :         PgStat_RelationStatus *relstat;
     703                 :             : 
     704                 :       17126 :         next_trans = trans->next;
     705                 :             :         Assert(trans->nest_level == nestDepth);
     706                 :       17126 :         relstat = trans->parent;
     707                 :             :         Assert(relstat->tab.trans == trans);
     708                 :             : 
     709         [ +  + ]:       17126 :         if (isCommit)
     710                 :             :         {
     711   [ +  +  +  + ]:       15669 :             if (trans->upper && trans->upper->nest_level == nestDepth - 1)
     712                 :             :             {
     713         [ +  + ]:       14434 :                 if (trans->truncdropped)
     714                 :             :                 {
     715                 :             :                     /* propagate the truncate/drop status one level up */
     716                 :          16 :                     save_truncdrop_counters(trans->upper, false);
     717                 :             :                     /* replace upper xact stats with ours */
     718                 :          16 :                     trans->upper->tuples_inserted = trans->tuples_inserted;
     719                 :          16 :                     trans->upper->tuples_updated = trans->tuples_updated;
     720                 :          16 :                     trans->upper->tuples_deleted = trans->tuples_deleted;
     721                 :             :                 }
     722                 :             :                 else
     723                 :             :                 {
     724                 :       14418 :                     trans->upper->tuples_inserted += trans->tuples_inserted;
     725                 :       14418 :                     trans->upper->tuples_updated += trans->tuples_updated;
     726                 :       14418 :                     trans->upper->tuples_deleted += trans->tuples_deleted;
     727                 :             :                 }
     728                 :             :                 /* truncate/drop does not reset these */
     729                 :       14434 :                 trans->upper->tuples_hot_updated += trans->tuples_hot_updated;
     730                 :       14434 :                 trans->upper->tuples_newpage_updated +=
     731                 :       14434 :                     trans->tuples_newpage_updated;
     732                 :       14434 :                 relstat->tab.trans = trans->upper;
     733                 :       14434 :                 pfree(trans);
     734                 :             :             }
     735                 :             :             else
     736                 :             :             {
     737                 :             :                 /*
     738                 :             :                  * When there isn't an immediate parent state, we can just
     739                 :             :                  * reuse the record instead of going through a palloc/pfree
     740                 :             :                  * pushup (this works since it's all in TopTransactionContext
     741                 :             :                  * anyway).  We have to re-link it into the parent level,
     742                 :             :                  * though, and that might mean pushing a new entry into the
     743                 :             :                  * pgStatXactStack.
     744                 :             :                  */
     745                 :             :                 PgStat_SubXactStatus *upper_xact_state;
     746                 :             : 
     747                 :        1235 :                 upper_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
     748                 :        1235 :                 trans->next = upper_xact_state->first;
     749                 :        1235 :                 upper_xact_state->first = trans;
     750                 :        1235 :                 trans->nest_level = nestDepth - 1;
     751                 :             :             }
     752                 :             :         }
     753                 :             :         else
     754                 :             :         {
     755                 :             :             /*
     756                 :             :              * On abort, update top-level relstat counts, then forget the
     757                 :             :              * subtransaction
     758                 :             :              */
     759                 :             : 
     760                 :             :             /* first restore values obliterated by truncate/drop */
     761                 :        1457 :             restore_truncdrop_counters(trans);
     762                 :             :             /* count attempted actions regardless of commit/abort */
     763                 :        1457 :             relstat->tab.counts_xact.tuples_inserted += trans->tuples_inserted;
     764                 :        1457 :             relstat->tab.counts_xact.tuples_updated += trans->tuples_updated;
     765                 :        1457 :             relstat->tab.counts_xact.tuples_deleted += trans->tuples_deleted;
     766                 :        1457 :             relstat->tab.counts_xact.tuples_hot_updated +=
     767                 :        1457 :                 trans->tuples_hot_updated;
     768                 :        1457 :             relstat->tab.counts_xact.tuples_newpage_updated +=
     769                 :        1457 :                 trans->tuples_newpage_updated;
     770                 :             :             /* inserted tuples are dead, deleted tuples are unaffected */
     771                 :        1457 :             relstat->tab.counts_xact.delta_dead_tuples +=
     772                 :        1457 :                 trans->tuples_inserted + trans->tuples_updated;
     773                 :        1457 :             relstat->tab.trans = trans->upper;
     774                 :        1457 :             pfree(trans);
     775                 :             :         }
     776                 :             :     }
     777                 :       16386 : }
     778                 :             : 
     779                 :             : /*
     780                 :             :  * Generate 2PC records for all the pending transaction-dependent relation
     781                 :             :  * stats.
     782                 :             :  */
     783                 :             : void
     784                 :         313 : AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
     785                 :             : {
     786                 :             :     PgStat_TableXactStatus *trans;
     787                 :             : 
     788         [ +  + ]:         776 :     for (trans = xact_state->first; trans != NULL; trans = trans->next)
     789                 :             :     {
     790                 :             :         PgStat_RelationStatus *relstat PG_USED_FOR_ASSERTS_ONLY;
     791                 :             :         TwoPhasePgStatRecord record;
     792                 :             : 
     793                 :             :         Assert(trans->nest_level == 1);
     794                 :             :         Assert(trans->upper == NULL);
     795                 :         463 :         relstat = trans->parent;
     796                 :             :         Assert(relstat->tab.trans == trans);
     797                 :             : 
     798                 :         463 :         record.tuples_inserted = trans->tuples_inserted;
     799                 :         463 :         record.tuples_updated = trans->tuples_updated;
     800                 :         463 :         record.tuples_deleted = trans->tuples_deleted;
     801                 :         463 :         record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
     802                 :         463 :         record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
     803                 :         463 :         record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
     804                 :         463 :         record.id = relstat->tab.id;
     805                 :         463 :         record.shared = relstat->tab.shared;
     806                 :         463 :         record.truncdropped = trans->truncdropped;
     807                 :             : 
     808                 :         463 :         RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
     809                 :             :                                &record, sizeof(TwoPhasePgStatRecord));
     810                 :             :     }
     811                 :         313 : }
     812                 :             : 
     813                 :             : /*
     814                 :             :  * All we need do here is unlink the transaction stats state from the
     815                 :             :  * nontransactional state.  The nontransactional action counts will be
     816                 :             :  * reported to the stats system immediately, while the effects on live and
     817                 :             :  * dead tuple counts are preserved in the 2PC state file.
     818                 :             :  *
     819                 :             :  * HOT and new-page update counts are not saved in the 2PC state file, so
     820                 :             :  * add them to this backend's pending entry, to be reported from there.
     821                 :             :  *
     822                 :             :  * Note: AtEOXact_PgStat_Relations is not called during PREPARE.
     823                 :             :  */
     824                 :             : void
     825                 :         313 : PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
     826                 :             : {
     827                 :             :     PgStat_TableXactStatus *trans;
     828                 :             : 
     829         [ +  + ]:         776 :     for (trans = xact_state->first; trans != NULL; trans = trans->next)
     830                 :             :     {
     831                 :             :         PgStat_RelationStatus *relstat;
     832                 :             : 
     833                 :         463 :         relstat = trans->parent;
     834                 :         463 :         relstat->tab.counts_xact.tuples_hot_updated +=
     835                 :         463 :             trans->tuples_hot_updated;
     836                 :         463 :         relstat->tab.counts_xact.tuples_newpage_updated +=
     837                 :         463 :             trans->tuples_newpage_updated;
     838                 :         463 :         relstat->tab.trans = NULL;
     839                 :             :     }
     840                 :         313 : }
     841                 :             : 
     842                 :             : /*
     843                 :             :  * 2PC processing routine for COMMIT PREPARED case.
     844                 :             :  *
     845                 :             :  * Load the saved counts into our local pgstats state.
     846                 :             :  */
     847                 :             : void
     848                 :         375 : pgstat_twophase_postcommit(FullTransactionId fxid, uint16 info,
     849                 :             :                            void *recdata, uint32 len)
     850                 :             : {
     851                 :         375 :     TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
     852                 :             :     PgStat_RelationStatus *pgstat_info;
     853                 :             : 
     854                 :             :     /* Find or create a relstat entry for the rel */
     855                 :         375 :     pgstat_info = pgstat_prep_relation_pending(PGSTAT_KIND_RELATION, rec->id, rec->shared);
     856                 :             : 
     857                 :             :     /* Same math as in AtEOXact_PgStat, commit case */
     858                 :         375 :     pgstat_info->tab.counts_xact.tuples_inserted += rec->tuples_inserted;
     859                 :         375 :     pgstat_info->tab.counts_xact.tuples_updated += rec->tuples_updated;
     860                 :         375 :     pgstat_info->tab.counts_xact.tuples_deleted += rec->tuples_deleted;
     861                 :         375 :     pgstat_info->tab.truncdropped = rec->truncdropped;
     862         [ +  + ]:         375 :     if (rec->truncdropped)
     863                 :             :     {
     864                 :             :         /* forget live/dead stats seen by backend thus far */
     865                 :          13 :         pgstat_info->tab.counts_xact.delta_live_tuples = 0;
     866                 :          13 :         pgstat_info->tab.counts_xact.delta_dead_tuples = 0;
     867                 :             :     }
     868                 :         375 :     pgstat_info->tab.counts_xact.delta_live_tuples +=
     869                 :         375 :         rec->tuples_inserted - rec->tuples_deleted;
     870                 :         375 :     pgstat_info->tab.counts_xact.delta_dead_tuples +=
     871                 :         375 :         rec->tuples_updated + rec->tuples_deleted;
     872                 :         375 :     pgstat_info->tab.counts_xact.changed_tuples +=
     873                 :         375 :         rec->tuples_inserted + rec->tuples_updated +
     874                 :         375 :         rec->tuples_deleted;
     875                 :         375 : }
     876                 :             : 
     877                 :             : /*
     878                 :             :  * 2PC processing routine for ROLLBACK PREPARED case.
     879                 :             :  *
     880                 :             :  * Load the saved counts into our local pgstats state, but treat them
     881                 :             :  * as aborted.
     882                 :             :  */
     883                 :             : void
     884                 :          86 : pgstat_twophase_postabort(FullTransactionId fxid, uint16 info,
     885                 :             :                           void *recdata, uint32 len)
     886                 :             : {
     887                 :          86 :     TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
     888                 :             :     PgStat_RelationStatus *pgstat_info;
     889                 :             : 
     890                 :             :     /* Find or create a relstat entry for the rel */
     891                 :          86 :     pgstat_info = pgstat_prep_relation_pending(PGSTAT_KIND_RELATION, rec->id, rec->shared);
     892                 :             : 
     893                 :             :     /* Same math as in AtEOXact_PgStat, abort case */
     894         [ +  + ]:          86 :     if (rec->truncdropped)
     895                 :             :     {
     896                 :           8 :         rec->tuples_inserted = rec->inserted_pre_truncdrop;
     897                 :           8 :         rec->tuples_updated = rec->updated_pre_truncdrop;
     898                 :           8 :         rec->tuples_deleted = rec->deleted_pre_truncdrop;
     899                 :             :     }
     900                 :          86 :     pgstat_info->tab.counts_xact.tuples_inserted += rec->tuples_inserted;
     901                 :          86 :     pgstat_info->tab.counts_xact.tuples_updated += rec->tuples_updated;
     902                 :          86 :     pgstat_info->tab.counts_xact.tuples_deleted += rec->tuples_deleted;
     903                 :          86 :     pgstat_info->tab.counts_xact.delta_dead_tuples +=
     904                 :          86 :         rec->tuples_inserted + rec->tuples_updated;
     905                 :          86 : }
     906                 :             : 
     907                 :             : /*
     908                 :             :  * Flush out pending stats for the entry
     909                 :             :  *
     910                 :             :  * If nowait is true and the lock could not be immediately acquired, returns
     911                 :             :  * false without flushing the entry.  Otherwise returns true.
     912                 :             :  *
     913                 :             :  * Some of the stats are copied to the corresponding pending database stats
     914                 :             :  * entry when successfully flushing.
     915                 :             :  */
     916                 :             : bool
     917                 :      471738 : pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
     918                 :             : {
     919                 :             :     Oid         dboid;
     920                 :             :     PgStat_RelationStatus *lstats;  /* pending stats entry  */
     921                 :             :     PgStatShared_Relation *shtabstats;
     922                 :             :     PgStat_StatTabEntry *tabentry;  /* table entry of shared stats */
     923                 :             :     PgStat_StatDBEntry *dbentry;    /* pending database entry */
     924                 :             : 
     925                 :      471738 :     dboid = entry_ref->shared_entry->key.dboid;
     926                 :      471738 :     lstats = (PgStat_RelationStatus *) entry_ref->pending;
     927                 :      471738 :     shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats;
     928                 :             : 
     929                 :             :     /*
     930                 :             :      * Ignore entries that didn't accumulate any actual counts.  If the table
     931                 :             :      * was truncated, we still need to flush the entry to reset the live/dead
     932                 :             :      * counters and ins_since_vacuum even when no other counts were
     933                 :             :      * accumulated.
     934                 :             :      */
     935   [ +  +  +  + ]:      942987 :     if (!lstats->tab.truncdropped &&
     936                 :      471249 :         pg_memory_is_all_zeros(&lstats->tab.counts,
     937         [ +  + ]:        4605 :                                sizeof(struct PgStat_TableCounts)) &&
     938                 :        4605 :         pg_memory_is_all_zeros(&lstats->tab.counts_xact,
     939                 :             :                                sizeof(struct PgStat_TableCountsXact)))
     940                 :        3309 :         return true;
     941                 :             : 
     942         [ +  + ]:      468429 :     if (!pgstat_lock_entry(entry_ref, nowait))
     943                 :           8 :         return false;
     944                 :             : 
     945                 :             :     /* add the values to the shared entry. */
     946                 :      468421 :     tabentry = &shtabstats->stats;
     947                 :             : 
     948                 :      468421 :     tabentry->numscans += lstats->tab.counts.numscans;
     949         [ +  + ]:      468421 :     if (lstats->tab.counts.numscans)
     950                 :             :     {
     951                 :      100848 :         TimestampTz t = GetCurrentTransactionStopTimestamp();
     952                 :             : 
     953         [ +  + ]:      100848 :         if (t > tabentry->lastscan)
     954                 :       99923 :             tabentry->lastscan = t;
     955                 :             :     }
     956                 :      468421 :     tabentry->tuples_returned += lstats->tab.counts.tuples_returned;
     957                 :      468421 :     tabentry->tuples_fetched += lstats->tab.counts.tuples_fetched;
     958                 :      468421 :     tabentry->tuples_inserted += lstats->tab.counts_xact.tuples_inserted;
     959                 :      468421 :     tabentry->tuples_updated += lstats->tab.counts_xact.tuples_updated;
     960                 :      468421 :     tabentry->tuples_deleted += lstats->tab.counts_xact.tuples_deleted;
     961                 :      468421 :     tabentry->tuples_hot_updated += lstats->tab.counts_xact.tuples_hot_updated;
     962                 :      468421 :     tabentry->tuples_newpage_updated += lstats->tab.counts_xact.tuples_newpage_updated;
     963                 :             : 
     964                 :             :     /*
     965                 :             :      * If table was truncated/dropped, first reset the live/dead counters.
     966                 :             :      */
     967         [ +  + ]:      468421 :     if (lstats->tab.truncdropped)
     968                 :             :     {
     969                 :         489 :         tabentry->live_tuples = 0;
     970                 :         489 :         tabentry->dead_tuples = 0;
     971                 :         489 :         tabentry->ins_since_vacuum = 0;
     972                 :             :     }
     973                 :             : 
     974                 :      468421 :     tabentry->live_tuples += lstats->tab.counts_xact.delta_live_tuples;
     975                 :      468421 :     tabentry->dead_tuples += lstats->tab.counts_xact.delta_dead_tuples;
     976                 :      468421 :     tabentry->mod_since_analyze += lstats->tab.counts_xact.changed_tuples;
     977                 :             : 
     978                 :             :     /*
     979                 :             :      * Using tuples_inserted to update ins_since_vacuum does mean that we'll
     980                 :             :      * track aborted inserts too.  This isn't ideal, but otherwise probably
     981                 :             :      * not worth adding an extra field for.  It may just amount to autovacuums
     982                 :             :      * triggering for inserts more often than they maybe should, which is
     983                 :             :      * probably not going to be common enough to be too concerned about here.
     984                 :             :      */
     985                 :      468421 :     tabentry->ins_since_vacuum += lstats->tab.counts_xact.tuples_inserted;
     986                 :             : 
     987                 :      468421 :     tabentry->blocks_fetched += lstats->tab.counts.blocks_fetched;
     988                 :      468421 :     tabentry->blocks_hit += lstats->tab.counts.blocks_hit;
     989                 :             : 
     990                 :             :     /* Clamp live_tuples in case of negative delta_live_tuples */
     991                 :      468421 :     tabentry->live_tuples = Max(tabentry->live_tuples, 0);
     992                 :             :     /* Likewise for dead_tuples */
     993                 :      468421 :     tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
     994                 :             : 
     995                 :      468421 :     pgstat_unlock_entry(entry_ref);
     996                 :             : 
     997                 :             :     /* The entry was successfully flushed, add the same to database stats */
     998                 :      468421 :     dbentry = pgstat_prep_database_pending(dboid);
     999                 :      468421 :     dbentry->tuples_returned += lstats->tab.counts.tuples_returned;
    1000                 :      468421 :     dbentry->tuples_fetched += lstats->tab.counts.tuples_fetched;
    1001                 :      468421 :     dbentry->tuples_inserted += lstats->tab.counts_xact.tuples_inserted;
    1002                 :      468421 :     dbentry->tuples_updated += lstats->tab.counts_xact.tuples_updated;
    1003                 :      468421 :     dbentry->tuples_deleted += lstats->tab.counts_xact.tuples_deleted;
    1004                 :      468421 :     dbentry->blocks_fetched += lstats->tab.counts.blocks_fetched;
    1005                 :      468421 :     dbentry->blocks_hit += lstats->tab.counts.blocks_hit;
    1006                 :             : 
    1007                 :      468421 :     return true;
    1008                 :             : }
    1009                 :             : 
    1010                 :             : void
    1011                 :      503177 : pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
    1012                 :             : {
    1013                 :      503177 :     PgStat_RelationStatus *pending = (PgStat_RelationStatus *) entry_ref->pending;
    1014                 :             : 
    1015         [ +  + ]:      503177 :     if (pending->relation)
    1016                 :      444284 :         pgstat_unlink_relation(pending->relation);
    1017                 :      503177 : }
    1018                 :             : 
    1019                 :             : void
    1020                 :        7830 : pgstat_relation_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
    1021                 :             : {
    1022                 :        7830 :     ((PgStatShared_Relation *) header)->stats.stat_reset_time = ts;
    1023                 :        7830 : }
    1024                 :             : 
    1025                 :             : /*
    1026                 :             :  * Find or create a PgStat_RelationStatus entry for rel. New entry is created and
    1027                 :             :  * initialized if not exists.
    1028                 :             :  */
    1029                 :             : static PgStat_RelationStatus *
    1030                 :     1302866 : pgstat_prep_relation_pending(PgStat_Kind kind, Oid rel_id, bool isshared)
    1031                 :             : {
    1032                 :             :     PgStat_EntryRef *entry_ref;
    1033                 :             :     PgStat_RelationStatus *pending;
    1034                 :             : 
    1035         [ +  + ]:     1302866 :     entry_ref = pgstat_prep_pending_entry(kind,
    1036                 :             :                                           isshared ? InvalidOid : MyDatabaseId,
    1037                 :             :                                           rel_id, NULL);
    1038                 :     1302866 :     pending = entry_ref->pending;
    1039                 :     1302866 :     pending->kind = kind;
    1040         [ +  + ]:     1302866 :     if (kind != PGSTAT_KIND_INDEX)
    1041                 :             :     {
    1042                 :      553444 :         pending->tab.id = rel_id;
    1043                 :      553444 :         pending->tab.shared = isshared;
    1044                 :             :     }
    1045                 :             : 
    1046                 :     1302866 :     return pending;
    1047                 :             : }
    1048                 :             : 
    1049                 :             : /*
    1050                 :             :  * add a new (sub)transaction state record
    1051                 :             :  */
    1052                 :             : static void
    1053                 :      487319 : add_tabstat_xact_level(PgStat_RelationStatus *pgstat_info, int nest_level)
    1054                 :             : {
    1055                 :             :     PgStat_SubXactStatus *xact_state;
    1056                 :             :     PgStat_TableXactStatus *trans;
    1057                 :             : 
    1058                 :             :     /*
    1059                 :             :      * If this is the first rel to be modified at the current nest level, we
    1060                 :             :      * first have to push a transaction stack entry.
    1061                 :             :      */
    1062                 :      487319 :     xact_state = pgstat_get_xact_stack_level(nest_level);
    1063                 :             : 
    1064                 :             :     /* Now make a per-table stack entry */
    1065                 :             :     trans = (PgStat_TableXactStatus *)
    1066                 :      487319 :         MemoryContextAllocZero(TopTransactionContext,
    1067                 :             :                                sizeof(PgStat_TableXactStatus));
    1068                 :      487319 :     trans->nest_level = nest_level;
    1069                 :      487319 :     trans->upper = pgstat_info->tab.trans;
    1070                 :      487319 :     trans->parent = pgstat_info;
    1071                 :      487319 :     trans->next = xact_state->first;
    1072                 :      487319 :     xact_state->first = trans;
    1073                 :      487319 :     pgstat_info->tab.trans = trans;
    1074                 :      487319 : }
    1075                 :             : 
    1076                 :             : /*
    1077                 :             :  * Add a new (sub)transaction record if needed.
    1078                 :             :  */
    1079                 :             : static void
    1080                 :    16999759 : ensure_tabstat_xact_level(PgStat_RelationStatus *pgstat_info)
    1081                 :             : {
    1082                 :    16999759 :     int         nest_level = GetCurrentTransactionNestLevel();
    1083                 :             : 
    1084         [ +  + ]:    16999759 :     if (pgstat_info->tab.trans == NULL ||
    1085         [ +  + ]:    16527846 :         pgstat_info->tab.trans->nest_level != nest_level)
    1086                 :      487319 :         add_tabstat_xact_level(pgstat_info, nest_level);
    1087                 :    16999759 : }
    1088                 :             : 
    1089                 :             : /*
    1090                 :             :  * Whenever a table is truncated/dropped, we save its i/u/d counters so that
    1091                 :             :  * they can be cleared, and if the (sub)xact that executed the truncate/drop
    1092                 :             :  * later aborts, the counters can be restored to the saved (pre-truncate/drop)
    1093                 :             :  * values.
    1094                 :             :  *
    1095                 :             :  * Note that for truncate we do this on the first truncate in any particular
    1096                 :             :  * subxact level only.
    1097                 :             :  */
    1098                 :             : static void
    1099                 :        3335 : save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop)
    1100                 :             : {
    1101   [ +  +  +  + ]:        3335 :     if (!trans->truncdropped || is_drop)
    1102                 :             :     {
    1103                 :        3283 :         trans->inserted_pre_truncdrop = trans->tuples_inserted;
    1104                 :        3283 :         trans->updated_pre_truncdrop = trans->tuples_updated;
    1105                 :        3283 :         trans->deleted_pre_truncdrop = trans->tuples_deleted;
    1106                 :        3283 :         trans->truncdropped = true;
    1107                 :             :     }
    1108                 :        3335 : }
    1109                 :             : 
    1110                 :             : /*
    1111                 :             :  * restore counters when a truncate aborts
    1112                 :             :  */
    1113                 :             : static void
    1114                 :       19564 : restore_truncdrop_counters(PgStat_TableXactStatus *trans)
    1115                 :             : {
    1116         [ +  + ]:       19564 :     if (trans->truncdropped)
    1117                 :             :     {
    1118                 :         246 :         trans->tuples_inserted = trans->inserted_pre_truncdrop;
    1119                 :         246 :         trans->tuples_updated = trans->updated_pre_truncdrop;
    1120                 :         246 :         trans->tuples_deleted = trans->deleted_pre_truncdrop;
    1121                 :             :     }
    1122                 :       19564 : }
        

Generated by: LCOV version 2.0-1