LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_xact.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 96.0 % 125 120
Test Date: 2026-07-08 05:15:38 Functions: 100.0 % 12 12
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 88.2 % 76 67

             Branch data     Line data    Source code
       1                 :             : /* -------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pgstat_xact.c
       4                 :             :  *    Transactional integration for the cumulative statistics system.
       5                 :             :  *
       6                 :             :  * Copyright (c) 2001-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  * IDENTIFICATION
       9                 :             :  *    src/backend/utils/activity/pgstat_xact.c
      10                 :             :  * -------------------------------------------------------------------------
      11                 :             :  */
      12                 :             : 
      13                 :             : #include "postgres.h"
      14                 :             : 
      15                 :             : #include "access/xact.h"
      16                 :             : #include "pgstat.h"
      17                 :             : #include "utils/memutils.h"
      18                 :             : #include "utils/pgstat_internal.h"
      19                 :             : 
      20                 :             : 
      21                 :             : typedef struct PgStat_PendingDroppedStatsItem
      22                 :             : {
      23                 :             :     xl_xact_stats_item item;
      24                 :             :     bool        is_create;
      25                 :             :     dlist_node  node;
      26                 :             : } PgStat_PendingDroppedStatsItem;
      27                 :             : 
      28                 :             : 
      29                 :             : static void AtEOXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit);
      30                 :             : static void AtEOSubXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state,
      31                 :             :                                             bool isCommit, int nestDepth);
      32                 :             : 
      33                 :             : static PgStat_SubXactStatus *pgStatXactStack = NULL;
      34                 :             : 
      35                 :             : 
      36                 :             : /*
      37                 :             :  * Called from access/transam/xact.c at top-level transaction commit/abort.
      38                 :             :  */
      39                 :             : void
      40                 :      659096 : AtEOXact_PgStat(bool isCommit, bool parallel)
      41                 :             : {
      42                 :             :     PgStat_SubXactStatus *xact_state;
      43                 :             : 
      44                 :      659096 :     AtEOXact_PgStat_Database(isCommit, parallel);
      45                 :             : 
      46                 :             :     /* handle transactional stats information */
      47                 :      659096 :     xact_state = pgStatXactStack;
      48         [ +  + ]:      659096 :     if (xact_state != NULL)
      49                 :             :     {
      50                 :             :         Assert(xact_state->nest_level == 1);
      51                 :             :         Assert(xact_state->prev == NULL);
      52                 :             : 
      53                 :      160287 :         AtEOXact_PgStat_Relations(xact_state, isCommit);
      54                 :      160287 :         AtEOXact_PgStat_DroppedStats(xact_state, isCommit);
      55                 :             :     }
      56                 :      659096 :     pgStatXactStack = NULL;
      57                 :             : 
      58                 :             :     /* Make sure any stats snapshot is thrown away */
      59                 :      659096 :     pgstat_clear_snapshot();
      60                 :      659096 : }
      61                 :             : 
      62                 :             : /*
      63                 :             :  * When committing, drop stats for objects dropped in the transaction. When
      64                 :             :  * aborting, drop stats for objects created in the transaction.
      65                 :             :  */
      66                 :             : static void
      67                 :      160287 : AtEOXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit)
      68                 :             : {
      69                 :             :     dlist_mutable_iter iter;
      70                 :      160287 :     int         not_freed_count = 0;
      71                 :             : 
      72         [ +  + ]:      160287 :     if (dclist_count(&xact_state->pending_drops) == 0)
      73                 :      100609 :         return;
      74                 :             : 
      75   [ +  -  +  + ]:      217156 :     dclist_foreach_modify(iter, &xact_state->pending_drops)
      76                 :             :     {
      77                 :      157478 :         PgStat_PendingDroppedStatsItem *pending =
      78                 :      157478 :             dclist_container(PgStat_PendingDroppedStatsItem, node, iter.cur);
      79                 :      157478 :         xl_xact_stats_item *it = &pending->item;
      80                 :      157478 :         uint64      objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
      81                 :             : 
      82   [ +  +  +  + ]:      157478 :         if (isCommit && !pending->is_create)
      83                 :             :         {
      84                 :             :             /*
      85                 :             :              * Transaction that dropped an object committed. Drop the stats
      86                 :             :              * too.
      87                 :             :              */
      88         [ +  + ]:       60575 :             if (!pgstat_drop_entry(it->kind, it->dboid, objid, true))
      89                 :        5345 :                 not_freed_count++;
      90                 :             :         }
      91   [ +  +  +  + ]:      102248 :         else if (!isCommit && pending->is_create)
      92                 :             :         {
      93                 :             :             /*
      94                 :             :              * Transaction that created an object aborted. Drop the stats
      95                 :             :              * associated with the object.
      96                 :             :              */
      97         [ -  + ]:        3405 :             if (!pgstat_drop_entry(it->kind, it->dboid, objid, true))
      98                 :           0 :                 not_freed_count++;
      99                 :             :         }
     100                 :             : 
     101                 :      157478 :         dclist_delete_from(&xact_state->pending_drops, &pending->node);
     102                 :      157478 :         pfree(pending);
     103                 :             :     }
     104                 :             : 
     105         [ +  + ]:       59678 :     if (not_freed_count > 0)
     106                 :        2079 :         pgstat_request_entry_refs_gc();
     107                 :             : }
     108                 :             : 
     109                 :             : /*
     110                 :             :  * Called from access/transam/xact.c at subtransaction commit/abort.
     111                 :             :  */
     112                 :             : void
     113                 :       11059 : AtEOSubXact_PgStat(bool isCommit, int nestDepth)
     114                 :             : {
     115                 :             :     PgStat_SubXactStatus *xact_state;
     116                 :             : 
     117                 :             :     /* merge the sub-transaction's transactional stats into the parent */
     118                 :       11059 :     xact_state = pgStatXactStack;
     119         [ +  + ]:       11059 :     if (xact_state != NULL &&
     120         [ +  + ]:        5047 :         xact_state->nest_level >= nestDepth)
     121                 :             :     {
     122                 :             :         /* delink xact_state from stack immediately to simplify reuse case */
     123                 :        4445 :         pgStatXactStack = xact_state->prev;
     124                 :             : 
     125                 :        4445 :         AtEOSubXact_PgStat_Relations(xact_state, isCommit, nestDepth);
     126                 :        4445 :         AtEOSubXact_PgStat_DroppedStats(xact_state, isCommit, nestDepth);
     127                 :             : 
     128                 :        4445 :         pfree(xact_state);
     129                 :             :     }
     130                 :       11059 : }
     131                 :             : 
     132                 :             : /*
     133                 :             :  * Like AtEOXact_PgStat_DroppedStats(), but for subtransactions.
     134                 :             :  */
     135                 :             : static void
     136                 :        4445 : AtEOSubXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state,
     137                 :             :                                 bool isCommit, int nestDepth)
     138                 :             : {
     139                 :             :     PgStat_SubXactStatus *parent_xact_state;
     140                 :             :     dlist_mutable_iter iter;
     141                 :        4445 :     int         not_freed_count = 0;
     142                 :             : 
     143         [ +  + ]:        4445 :     if (dclist_count(&xact_state->pending_drops) == 0)
     144                 :        4336 :         return;
     145                 :             : 
     146                 :         109 :     parent_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
     147                 :             : 
     148   [ +  -  +  + ]:         318 :     dclist_foreach_modify(iter, &xact_state->pending_drops)
     149                 :             :     {
     150                 :         209 :         PgStat_PendingDroppedStatsItem *pending =
     151                 :         209 :             dclist_container(PgStat_PendingDroppedStatsItem, node, iter.cur);
     152                 :         209 :         xl_xact_stats_item *it = &pending->item;
     153                 :         209 :         uint64      objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
     154                 :             : 
     155                 :         209 :         dclist_delete_from(&xact_state->pending_drops, &pending->node);
     156                 :             : 
     157   [ +  +  +  + ]:         209 :         if (!isCommit && pending->is_create)
     158                 :             :         {
     159                 :             :             /*
     160                 :             :              * Subtransaction creating a new stats object aborted. Drop the
     161                 :             :              * stats object.
     162                 :             :              */
     163         [ -  + ]:          98 :             if (!pgstat_drop_entry(it->kind, it->dboid, objid, true))
     164                 :           0 :                 not_freed_count++;
     165                 :          98 :             pfree(pending);
     166                 :             :         }
     167         [ +  + ]:         111 :         else if (isCommit)
     168                 :             :         {
     169                 :             :             /*
     170                 :             :              * Subtransaction dropping a stats object committed. Can't yet
     171                 :             :              * remove the stats object, the surrounding transaction might
     172                 :             :              * still abort. Pass it on to the parent.
     173                 :             :              */
     174                 :          76 :             dclist_push_tail(&parent_xact_state->pending_drops, &pending->node);
     175                 :             :         }
     176                 :             :         else
     177                 :             :         {
     178                 :          35 :             pfree(pending);
     179                 :             :         }
     180                 :             :     }
     181                 :             : 
     182                 :             :     Assert(dclist_count(&xact_state->pending_drops) == 0);
     183         [ -  + ]:         109 :     if (not_freed_count > 0)
     184                 :           0 :         pgstat_request_entry_refs_gc();
     185                 :             : }
     186                 :             : 
     187                 :             : /*
     188                 :             :  * Save the transactional stats state at 2PC transaction prepare.
     189                 :             :  */
     190                 :             : void
     191                 :         316 : AtPrepare_PgStat(void)
     192                 :             : {
     193                 :             :     PgStat_SubXactStatus *xact_state;
     194                 :             : 
     195                 :         316 :     xact_state = pgStatXactStack;
     196         [ +  + ]:         316 :     if (xact_state != NULL)
     197                 :             :     {
     198                 :             :         Assert(xact_state->nest_level == 1);
     199                 :             :         Assert(xact_state->prev == NULL);
     200                 :             : 
     201                 :         305 :         AtPrepare_PgStat_Relations(xact_state);
     202                 :             :     }
     203                 :         316 : }
     204                 :             : 
     205                 :             : /*
     206                 :             :  * Clean up after successful PREPARE.
     207                 :             :  *
     208                 :             :  * Note: AtEOXact_PgStat is not called during PREPARE.
     209                 :             :  */
     210                 :             : void
     211                 :         316 : PostPrepare_PgStat(void)
     212                 :             : {
     213                 :             :     PgStat_SubXactStatus *xact_state;
     214                 :             : 
     215                 :             :     /*
     216                 :             :      * We don't bother to free any of the transactional state, since it's all
     217                 :             :      * in TopTransactionContext and will go away anyway.
     218                 :             :      */
     219                 :         316 :     xact_state = pgStatXactStack;
     220         [ +  + ]:         316 :     if (xact_state != NULL)
     221                 :             :     {
     222                 :             :         Assert(xact_state->nest_level == 1);
     223                 :             :         Assert(xact_state->prev == NULL);
     224                 :             : 
     225                 :         305 :         PostPrepare_PgStat_Relations(xact_state);
     226                 :             :     }
     227                 :         316 :     pgStatXactStack = NULL;
     228                 :             : 
     229                 :             :     /* Make sure any stats snapshot is thrown away */
     230                 :         316 :     pgstat_clear_snapshot();
     231                 :         316 : }
     232                 :             : 
     233                 :             : /*
     234                 :             :  * Ensure (sub)transaction stack entry for the given nest_level exists, adding
     235                 :             :  * it if needed.
     236                 :             :  */
     237                 :             : PgStat_SubXactStatus *
     238                 :      624673 : pgstat_get_xact_stack_level(int nest_level)
     239                 :             : {
     240                 :             :     PgStat_SubXactStatus *xact_state;
     241                 :             : 
     242                 :      624673 :     xact_state = pgStatXactStack;
     243   [ +  +  +  + ]:      624673 :     if (xact_state == NULL || xact_state->nest_level != nest_level)
     244                 :             :     {
     245                 :             :         xact_state = (PgStat_SubXactStatus *)
     246                 :      165037 :             MemoryContextAlloc(TopTransactionContext,
     247                 :             :                                sizeof(PgStat_SubXactStatus));
     248                 :      165037 :         dclist_init(&xact_state->pending_drops);
     249                 :      165037 :         xact_state->nest_level = nest_level;
     250                 :      165037 :         xact_state->prev = pgStatXactStack;
     251                 :      165037 :         xact_state->first = NULL;
     252                 :      165037 :         pgStatXactStack = xact_state;
     253                 :             :     }
     254                 :      624673 :     return xact_state;
     255                 :             : }
     256                 :             : 
     257                 :             : /*
     258                 :             :  * Get stat items that need to be dropped at commit / abort.
     259                 :             :  *
     260                 :             :  * When committing, stats for objects that have been dropped in the
     261                 :             :  * transaction are returned. When aborting, stats for newly created objects are
     262                 :             :  * returned.
     263                 :             :  *
     264                 :             :  * Used by COMMIT / ABORT and 2PC PREPARE processing when building their
     265                 :             :  * respective WAL records, to ensure stats are dropped in case of a crash / on
     266                 :             :  * standbys.
     267                 :             :  *
     268                 :             :  * The list of items is allocated in CurrentMemoryContext and must be freed by
     269                 :             :  * the caller (directly or via memory context reset).
     270                 :             :  */
     271                 :             : int
     272                 :      631011 : pgstat_get_transactional_drops(bool isCommit, xl_xact_stats_item **items)
     273                 :             : {
     274                 :      631011 :     PgStat_SubXactStatus *xact_state = pgStatXactStack;
     275                 :      631011 :     int         nitems = 0;
     276                 :             :     dlist_iter  iter;
     277                 :             : 
     278         [ +  + ]:      631011 :     if (xact_state == NULL)
     279                 :      469255 :         return 0;
     280                 :             : 
     281                 :             :     /*
     282                 :             :      * We expect to be called for subtransaction abort (which logs a WAL
     283                 :             :      * record), but not for subtransaction commit (which doesn't).
     284                 :             :      */
     285                 :             :     Assert(!isCommit || xact_state->nest_level == 1);
     286                 :             :     Assert(!isCommit || xact_state->prev == NULL);
     287                 :             : 
     288                 :      161756 :     *items = palloc(dclist_count(&xact_state->pending_drops)
     289                 :             :                     * sizeof(xl_xact_stats_item));
     290                 :             : 
     291   [ +  -  +  + ]:      320365 :     dclist_foreach(iter, &xact_state->pending_drops)
     292                 :             :     {
     293                 :      158609 :         PgStat_PendingDroppedStatsItem *pending =
     294                 :      158609 :             dclist_container(PgStat_PendingDroppedStatsItem, node, iter.cur);
     295                 :             : 
     296   [ +  +  +  + ]:      158609 :         if (isCommit && pending->is_create)
     297                 :       98236 :             continue;
     298   [ +  +  +  + ]:       60373 :         if (!isCommit && !pending->is_create)
     299                 :         715 :             continue;
     300                 :             : 
     301                 :             :         Assert(nitems < dclist_count(&xact_state->pending_drops));
     302                 :       59658 :         (*items)[nitems++] = pending->item;
     303                 :             :     }
     304                 :             : 
     305                 :      161756 :     return nitems;
     306                 :             : }
     307                 :             : 
     308                 :             : /*
     309                 :             :  * Execute scheduled drops post-commit. Called from xact_redo_commit() /
     310                 :             :  * xact_redo_abort() during recovery, and from FinishPreparedTransaction()
     311                 :             :  * during normal 2PC COMMIT/ABORT PREPARED processing.
     312                 :             :  */
     313                 :             : void
     314                 :        3815 : pgstat_execute_transactional_drops(int ndrops, struct xl_xact_stats_item *items, bool is_redo)
     315                 :             : {
     316                 :        3815 :     int         not_freed_count = 0;
     317                 :             : 
     318         [ +  + ]:        3815 :     if (ndrops == 0)
     319                 :         304 :         return;
     320                 :             : 
     321         [ +  + ]:       14179 :     for (int i = 0; i < ndrops; i++)
     322                 :             :     {
     323                 :       10668 :         xl_xact_stats_item *it = &items[i];
     324                 :       10668 :         uint64      objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
     325                 :             : 
     326         [ +  + ]:       10668 :         if (!pgstat_drop_entry(it->kind, it->dboid, objid, true))
     327                 :           2 :             not_freed_count++;
     328                 :             :     }
     329                 :             : 
     330         [ +  + ]:        3511 :     if (not_freed_count > 0)
     331                 :           2 :         pgstat_request_entry_refs_gc();
     332                 :             : }
     333                 :             : 
     334                 :             : static void
     335                 :      157680 : create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, uint64 objid, bool is_create)
     336                 :             : {
     337                 :      157680 :     int         nest_level = GetCurrentTransactionNestLevel();
     338                 :             :     PgStat_SubXactStatus *xact_state;
     339                 :             :     PgStat_PendingDroppedStatsItem *drop = (PgStat_PendingDroppedStatsItem *)
     340                 :      157680 :         MemoryContextAlloc(TopTransactionContext, sizeof(PgStat_PendingDroppedStatsItem));
     341                 :             : 
     342                 :      157680 :     xact_state = pgstat_get_xact_stack_level(nest_level);
     343                 :             : 
     344                 :      157680 :     drop->is_create = is_create;
     345                 :      157680 :     drop->item.kind = kind;
     346                 :      157680 :     drop->item.dboid = dboid;
     347                 :      157680 :     drop->item.objid_lo = (uint32) objid;
     348                 :      157680 :     drop->item.objid_hi = (uint32) (objid >> 32);
     349                 :             : 
     350                 :      157680 :     dclist_push_tail(&xact_state->pending_drops, &drop->node);
     351                 :      157680 : }
     352                 :             : 
     353                 :             : /*
     354                 :             :  * Create a stats entry for a newly created database object in a transactional
     355                 :             :  * manner.
     356                 :             :  *
     357                 :             :  * I.e. if the current (sub-)transaction aborts, the stats entry will also be
     358                 :             :  * dropped.
     359                 :             :  */
     360                 :             : void
     361                 :      101739 : pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
     362                 :             : {
     363         [ -  + ]:      101739 :     if (pgstat_get_entry_ref(kind, dboid, objid, false, NULL))
     364                 :             :     {
     365         [ #  # ]:           0 :         ereport(WARNING,
     366                 :             :                 errmsg("resetting existing statistics for kind %s, db=%u, oid=%" PRIu64,
     367                 :             :                        (pgstat_get_kind_info(kind))->name, dboid,
     368                 :             :                        objid));
     369                 :             : 
     370                 :           0 :         pgstat_reset(kind, dboid, objid);
     371                 :             :     }
     372                 :             : 
     373                 :      101739 :     create_drop_transactional_internal(kind, dboid, objid, /* create */ true);
     374                 :      101739 : }
     375                 :             : 
     376                 :             : /*
     377                 :             :  * Drop a stats entry for a just dropped database object in a transactional
     378                 :             :  * manner.
     379                 :             :  *
     380                 :             :  * I.e. if the current (sub-)transaction aborts, the stats entry will stay
     381                 :             :  * alive.
     382                 :             :  */
     383                 :             : void
     384                 :       55941 : pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
     385                 :             : {
     386                 :       55941 :     create_drop_transactional_internal(kind, dboid, objid, /* create */ false);
     387                 :       55941 : }
        

Generated by: LCOV version 2.0-1