@@ -257,12 +257,13 @@ typedef struct TwoPhasePgStatRecord
257
257
PgStat_Counter tuples_inserted ; /* tuples inserted in xact */
258
258
PgStat_Counter tuples_updated ; /* tuples updated in xact */
259
259
PgStat_Counter tuples_deleted ; /* tuples deleted in xact */
260
- PgStat_Counter inserted_pre_trunc ; /* tuples inserted prior to truncate */
261
- PgStat_Counter updated_pre_trunc ; /* tuples updated prior to truncate */
262
- PgStat_Counter deleted_pre_trunc ; /* tuples deleted prior to truncate */
260
+ /* tuples i/u/d prior to truncate/drop */
261
+ PgStat_Counter inserted_pre_truncdrop ;
262
+ PgStat_Counter updated_pre_truncdrop ;
263
+ PgStat_Counter deleted_pre_truncdrop ;
263
264
Oid t_id ; /* table's OID */
264
265
bool t_shared ; /* is it a shared catalog? */
265
- bool t_truncated ; /* was the relation truncated? */
266
+ bool t_truncdropped ; /* was the relation truncated/dropped ? */
266
267
} TwoPhasePgStatRecord ;
267
268
268
269
/*
@@ -2300,36 +2301,39 @@ pgstat_count_heap_delete(Relation rel)
2300
2301
}
2301
2302
2302
2303
/*
2303
- * pgstat_truncate_save_counters
2304
+ * pgstat_truncdrop_save_counters
2304
2305
*
2305
- * Whenever a table is truncated, we save its i/u/d counters so that they can
2306
- * be cleared, and if the (sub)xact that executed the truncate later aborts,
2307
- * the counters can be restored to the saved (pre-truncate) values. Note we do
2308
- * this on the first truncate in any particular subxact level only.
2306
+ * Whenever a table is truncated/dropped, we save its i/u/d counters so that
2307
+ * they can be cleared, and if the (sub)xact that executed the truncate/drop
2308
+ * later aborts, the counters can be restored to the saved (pre-truncate/drop)
2309
+ * values.
2310
+ *
2311
+ * Note that for truncate we do this on the first truncate in any particular
2312
+ * subxact level only.
2309
2313
*/
2310
2314
static void
2311
- pgstat_truncate_save_counters (PgStat_TableXactStatus * trans )
2315
+ pgstat_truncdrop_save_counters (PgStat_TableXactStatus * trans , bool is_drop )
2312
2316
{
2313
- if (!trans -> truncated )
2317
+ if (!trans -> truncdropped || is_drop )
2314
2318
{
2315
- trans -> inserted_pre_trunc = trans -> tuples_inserted ;
2316
- trans -> updated_pre_trunc = trans -> tuples_updated ;
2317
- trans -> deleted_pre_trunc = trans -> tuples_deleted ;
2318
- trans -> truncated = true;
2319
+ trans -> inserted_pre_truncdrop = trans -> tuples_inserted ;
2320
+ trans -> updated_pre_truncdrop = trans -> tuples_updated ;
2321
+ trans -> deleted_pre_truncdrop = trans -> tuples_deleted ;
2322
+ trans -> truncdropped = true;
2319
2323
}
2320
2324
}
2321
2325
2322
2326
/*
2323
- * pgstat_truncate_restore_counters - restore counters when a truncate aborts
2327
+ * pgstat_truncdrop_restore_counters - restore counters when a truncate aborts
2324
2328
*/
2325
2329
static void
2326
- pgstat_truncate_restore_counters (PgStat_TableXactStatus * trans )
2330
+ pgstat_truncdrop_restore_counters (PgStat_TableXactStatus * trans )
2327
2331
{
2328
- if (trans -> truncated )
2332
+ if (trans -> truncdropped )
2329
2333
{
2330
- trans -> tuples_inserted = trans -> inserted_pre_trunc ;
2331
- trans -> tuples_updated = trans -> updated_pre_trunc ;
2332
- trans -> tuples_deleted = trans -> deleted_pre_trunc ;
2334
+ trans -> tuples_inserted = trans -> inserted_pre_truncdrop ;
2335
+ trans -> tuples_updated = trans -> updated_pre_truncdrop ;
2336
+ trans -> tuples_deleted = trans -> deleted_pre_truncdrop ;
2333
2337
}
2334
2338
}
2335
2339
@@ -2350,7 +2354,7 @@ pgstat_count_truncate(Relation rel)
2350
2354
pgstat_info -> trans -> nest_level != nest_level )
2351
2355
add_tabstat_xact_level (pgstat_info , nest_level );
2352
2356
2353
- pgstat_truncate_save_counters (pgstat_info -> trans );
2357
+ pgstat_truncdrop_save_counters (pgstat_info -> trans , false );
2354
2358
pgstat_info -> trans -> tuples_inserted = 0 ;
2355
2359
pgstat_info -> trans -> tuples_updated = 0 ;
2356
2360
pgstat_info -> trans -> tuples_deleted = 0 ;
@@ -2395,17 +2399,17 @@ AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
2395
2399
Assert (trans -> upper == NULL );
2396
2400
tabstat = trans -> parent ;
2397
2401
Assert (tabstat -> trans == trans );
2398
- /* restore pre-truncate stats (if any) in case of aborted xact */
2402
+ /* restore pre-truncate/drop stats (if any) in case of aborted xact */
2399
2403
if (!isCommit )
2400
- pgstat_truncate_restore_counters (trans );
2404
+ pgstat_truncdrop_restore_counters (trans );
2401
2405
/* count attempted actions regardless of commit/abort */
2402
2406
tabstat -> t_counts .t_tuples_inserted += trans -> tuples_inserted ;
2403
2407
tabstat -> t_counts .t_tuples_updated += trans -> tuples_updated ;
2404
2408
tabstat -> t_counts .t_tuples_deleted += trans -> tuples_deleted ;
2405
2409
if (isCommit )
2406
2410
{
2407
- tabstat -> t_counts .t_truncated = trans -> truncated ;
2408
- if (trans -> truncated )
2411
+ tabstat -> t_counts .t_truncdropped = trans -> truncdropped ;
2412
+ if (trans -> truncdropped )
2409
2413
{
2410
2414
/* forget live/dead stats seen by backend thus far */
2411
2415
tabstat -> t_counts .t_delta_live_tuples = 0 ;
@@ -2504,10 +2508,10 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in
2504
2508
{
2505
2509
if (trans -> upper && trans -> upper -> nest_level == nestDepth - 1 )
2506
2510
{
2507
- if (trans -> truncated )
2511
+ if (trans -> truncdropped )
2508
2512
{
2509
- /* propagate the truncate status one level up */
2510
- pgstat_truncate_save_counters (trans -> upper );
2513
+ /* propagate the truncate/drop status one level up */
2514
+ pgstat_truncdrop_save_counters (trans -> upper , false );
2511
2515
/* replace upper xact stats with ours */
2512
2516
trans -> upper -> tuples_inserted = trans -> tuples_inserted ;
2513
2517
trans -> upper -> tuples_updated = trans -> tuples_updated ;
@@ -2547,8 +2551,8 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in
2547
2551
* subtransaction
2548
2552
*/
2549
2553
2550
- /* first restore values obliterated by truncate */
2551
- pgstat_truncate_restore_counters (trans );
2554
+ /* first restore values obliterated by truncate/drop */
2555
+ pgstat_truncdrop_restore_counters (trans );
2552
2556
/* count attempted actions regardless of commit/abort */
2553
2557
tabstat -> t_counts .t_tuples_inserted += trans -> tuples_inserted ;
2554
2558
tabstat -> t_counts .t_tuples_updated += trans -> tuples_updated ;
@@ -2609,12 +2613,12 @@ AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
2609
2613
record .tuples_inserted = trans -> tuples_inserted ;
2610
2614
record .tuples_updated = trans -> tuples_updated ;
2611
2615
record .tuples_deleted = trans -> tuples_deleted ;
2612
- record .inserted_pre_trunc = trans -> inserted_pre_trunc ;
2613
- record .updated_pre_trunc = trans -> updated_pre_trunc ;
2614
- record .deleted_pre_trunc = trans -> deleted_pre_trunc ;
2616
+ record .inserted_pre_truncdrop = trans -> inserted_pre_truncdrop ;
2617
+ record .updated_pre_truncdrop = trans -> updated_pre_truncdrop ;
2618
+ record .deleted_pre_truncdrop = trans -> deleted_pre_truncdrop ;
2615
2619
record .t_id = tabstat -> t_id ;
2616
2620
record .t_shared = tabstat -> t_shared ;
2617
- record .t_truncated = trans -> truncated ;
2621
+ record .t_truncdropped = trans -> truncdropped ;
2618
2622
2619
2623
RegisterTwoPhaseRecord (TWOPHASE_RM_PGSTAT_ID , 0 ,
2620
2624
& record , sizeof (TwoPhasePgStatRecord ));
@@ -2710,8 +2714,8 @@ pgstat_twophase_postcommit(TransactionId xid, uint16 info,
2710
2714
pgstat_info -> t_counts .t_tuples_inserted += rec -> tuples_inserted ;
2711
2715
pgstat_info -> t_counts .t_tuples_updated += rec -> tuples_updated ;
2712
2716
pgstat_info -> t_counts .t_tuples_deleted += rec -> tuples_deleted ;
2713
- pgstat_info -> t_counts .t_truncated = rec -> t_truncated ;
2714
- if (rec -> t_truncated )
2717
+ pgstat_info -> t_counts .t_truncdropped = rec -> t_truncdropped ;
2718
+ if (rec -> t_truncdropped )
2715
2719
{
2716
2720
/* forget live/dead stats seen by backend thus far */
2717
2721
pgstat_info -> t_counts .t_delta_live_tuples = 0 ;
@@ -2743,11 +2747,11 @@ pgstat_twophase_postabort(TransactionId xid, uint16 info,
2743
2747
pgstat_info = get_tabstat_entry (rec -> t_id , rec -> t_shared );
2744
2748
2745
2749
/* Same math as in AtEOXact_PgStat, abort case */
2746
- if (rec -> t_truncated )
2750
+ if (rec -> t_truncdropped )
2747
2751
{
2748
- rec -> tuples_inserted = rec -> inserted_pre_trunc ;
2749
- rec -> tuples_updated = rec -> updated_pre_trunc ;
2750
- rec -> tuples_deleted = rec -> deleted_pre_trunc ;
2752
+ rec -> tuples_inserted = rec -> inserted_pre_truncdrop ;
2753
+ rec -> tuples_updated = rec -> updated_pre_truncdrop ;
2754
+ rec -> tuples_deleted = rec -> deleted_pre_truncdrop ;
2751
2755
}
2752
2756
pgstat_info -> t_counts .t_tuples_inserted += rec -> tuples_inserted ;
2753
2757
pgstat_info -> t_counts .t_tuples_updated += rec -> tuples_updated ;
@@ -5055,8 +5059,11 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
5055
5059
tabentry -> tuples_updated += tabmsg -> t_counts .t_tuples_updated ;
5056
5060
tabentry -> tuples_deleted += tabmsg -> t_counts .t_tuples_deleted ;
5057
5061
tabentry -> tuples_hot_updated += tabmsg -> t_counts .t_tuples_hot_updated ;
5058
- /* If table was truncated, first reset the live/dead counters */
5059
- if (tabmsg -> t_counts .t_truncated )
5062
+ /*
5063
+ * If table was truncated/dropped, first reset the live/dead
5064
+ * counters.
5065
+ */
5066
+ if (tabmsg -> t_counts .t_truncdropped )
5060
5067
{
5061
5068
tabentry -> n_live_tuples = 0 ;
5062
5069
tabentry -> n_dead_tuples = 0 ;
0 commit comments