@@ -235,16 +235,24 @@ static bool have_relation_stats = false;
235
235
static bool have_function_stats = false;
236
236
237
237
/*
238
- * Tuple insertion/deletion counts for an open transaction can't be propagated
239
- * into PgStat_TableStatus counters until we know if it is going to commit
240
- * or abort. Hence, we keep these counts in per-subxact structs that live
241
- * in TopTransactionContext. This data structure is designed on the assumption
242
- * that subxacts won't usually modify very many tables.
238
+ * Some stats changes are transactional. To maintain those, a stack of
239
+ * PgStat_SubXactStatus entries is maintained, which contain data pertaining
240
+ * to the current transaction and its active subtransactions.
243
241
*/
244
242
typedef struct PgStat_SubXactStatus
245
243
{
246
244
int nest_level ; /* subtransaction nest level */
245
+
247
246
struct PgStat_SubXactStatus * prev ; /* higher-level subxact if any */
247
+
248
+ /*
249
+ * Tuple insertion/deletion counts for an open transaction can't be
250
+ * propagated into PgStat_TableStatus counters until we know if it is
251
+ * going to commit or abort. Hence, we keep these counts in per-subxact
252
+ * structs that live in TopTransactionContext. This data structure is
253
+ * designed on the assumption that subxacts won't usually modify very many
254
+ * tables.
255
+ */
248
256
PgStat_TableXactStatus * first ; /* head of list for this subxact */
249
257
} PgStat_SubXactStatus ;
250
258
@@ -2305,10 +2313,11 @@ find_tabstat_entry(Oid rel_id)
2305
2313
}
2306
2314
2307
2315
/*
2308
- * get_tabstat_stack_level - add a new (sub)transaction stack entry if needed
2316
+ * Ensure (sub)transaction stack entry for the given nest_level exists, adding
2317
+ * it if needed.
2309
2318
*/
2310
2319
static PgStat_SubXactStatus *
2311
- get_tabstat_stack_level (int nest_level )
2320
+ pgstat_xact_stack_level_get (int nest_level )
2312
2321
{
2313
2322
PgStat_SubXactStatus * xact_state ;
2314
2323
@@ -2339,7 +2348,7 @@ add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
2339
2348
* If this is the first rel to be modified at the current nest level, we
2340
2349
* first have to push a transaction stack entry.
2341
2350
*/
2342
- xact_state = get_tabstat_stack_level (nest_level );
2351
+ xact_state = pgstat_xact_stack_level_get (nest_level );
2343
2352
2344
2353
/* Now make a per-table stack entry */
2345
2354
trans = (PgStat_TableXactStatus * )
@@ -2353,6 +2362,19 @@ add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
2353
2362
pgstat_info -> trans = trans ;
2354
2363
}
2355
2364
2365
+ /*
2366
+ * Add a new (sub)transaction record if needed.
2367
+ */
2368
+ static void
2369
+ ensure_tabstat_xact_level (PgStat_TableStatus * pgstat_info )
2370
+ {
2371
+ int nest_level = GetCurrentTransactionNestLevel ();
2372
+
2373
+ if (pgstat_info -> trans == NULL ||
2374
+ pgstat_info -> trans -> nest_level != nest_level )
2375
+ add_tabstat_xact_level (pgstat_info , nest_level );
2376
+ }
2377
+
2356
2378
/*
2357
2379
* pgstat_count_heap_insert - count a tuple insertion of n tuples
2358
2380
*/
@@ -2362,13 +2384,8 @@ pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
2362
2384
if (pgstat_relation_should_count (rel ))
2363
2385
{
2364
2386
PgStat_TableStatus * pgstat_info = rel -> pgstat_info ;
2365
- int nest_level = GetCurrentTransactionNestLevel ();
2366
-
2367
- /* We have to log the effect at the proper transactional level */
2368
- if (pgstat_info -> trans == NULL ||
2369
- pgstat_info -> trans -> nest_level != nest_level )
2370
- add_tabstat_xact_level (pgstat_info , nest_level );
2371
2387
2388
+ ensure_tabstat_xact_level (pgstat_info );
2372
2389
pgstat_info -> trans -> tuples_inserted += n ;
2373
2390
}
2374
2391
}
@@ -2382,13 +2399,8 @@ pgstat_count_heap_update(Relation rel, bool hot)
2382
2399
if (pgstat_relation_should_count (rel ))
2383
2400
{
2384
2401
PgStat_TableStatus * pgstat_info = rel -> pgstat_info ;
2385
- int nest_level = GetCurrentTransactionNestLevel ();
2386
-
2387
- /* We have to log the effect at the proper transactional level */
2388
- if (pgstat_info -> trans == NULL ||
2389
- pgstat_info -> trans -> nest_level != nest_level )
2390
- add_tabstat_xact_level (pgstat_info , nest_level );
2391
2402
2403
+ ensure_tabstat_xact_level (pgstat_info );
2392
2404
pgstat_info -> trans -> tuples_updated ++ ;
2393
2405
2394
2406
/* t_tuples_hot_updated is nontransactional, so just advance it */
@@ -2406,13 +2418,8 @@ pgstat_count_heap_delete(Relation rel)
2406
2418
if (pgstat_relation_should_count (rel ))
2407
2419
{
2408
2420
PgStat_TableStatus * pgstat_info = rel -> pgstat_info ;
2409
- int nest_level = GetCurrentTransactionNestLevel ();
2410
-
2411
- /* We have to log the effect at the proper transactional level */
2412
- if (pgstat_info -> trans == NULL ||
2413
- pgstat_info -> trans -> nest_level != nest_level )
2414
- add_tabstat_xact_level (pgstat_info , nest_level );
2415
2421
2422
+ ensure_tabstat_xact_level (pgstat_info );
2416
2423
pgstat_info -> trans -> tuples_deleted ++ ;
2417
2424
}
2418
2425
}
@@ -2463,13 +2470,8 @@ pgstat_count_truncate(Relation rel)
2463
2470
if (pgstat_relation_should_count (rel ))
2464
2471
{
2465
2472
PgStat_TableStatus * pgstat_info = rel -> pgstat_info ;
2466
- int nest_level = GetCurrentTransactionNestLevel ();
2467
-
2468
- /* We have to log the effect at the proper transactional level */
2469
- if (pgstat_info -> trans == NULL ||
2470
- pgstat_info -> trans -> nest_level != nest_level )
2471
- add_tabstat_xact_level (pgstat_info , nest_level );
2472
2473
2474
+ ensure_tabstat_xact_level (pgstat_info );
2473
2475
pgstat_truncdrop_save_counters (pgstat_info -> trans , false);
2474
2476
pgstat_info -> trans -> tuples_inserted = 0 ;
2475
2477
pgstat_info -> trans -> tuples_updated = 0 ;
@@ -2656,7 +2658,7 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in
2656
2658
*/
2657
2659
PgStat_SubXactStatus * upper_xact_state ;
2658
2660
2659
- upper_xact_state = get_tabstat_stack_level (nestDepth - 1 );
2661
+ upper_xact_state = pgstat_xact_stack_level_get (nestDepth - 1 );
2660
2662
trans -> next = upper_xact_state -> first ;
2661
2663
upper_xact_state -> first = trans ;
2662
2664
trans -> nest_level = nestDepth - 1 ;
0 commit comments