Skip to content

Commit 6b95016

Browse files
committed
pgstat: Prepare to use mechanism for truncated rels also for droppped rels.
The upcoming shared memory stats patch drops stats for dropped objects in a transactional manner, rather than removing them later as part of vacuum. This means that stats for DROP inside a transaction needs to handle aborted (sub-)transactions similar to TRUNCATE: The stats up to the DROP should be restored. Rename the existing infrastructure in preparation. Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20210405092914.mmxqe7j56lsjfsej@alap3.anarazel.de
1 parent e1f958d commit 6b95016

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,13 @@ typedef struct TwoPhasePgStatRecord
257257
PgStat_Counter tuples_inserted; /* tuples inserted in xact */
258258
PgStat_Counter tuples_updated; /* tuples updated in xact */
259259
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;
263264
Oid t_id; /* table's OID */
264265
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? */
266267
} TwoPhasePgStatRecord;
267268

268269
/*
@@ -2300,36 +2301,39 @@ pgstat_count_heap_delete(Relation rel)
23002301
}
23012302

23022303
/*
2303-
* pgstat_truncate_save_counters
2304+
* pgstat_truncdrop_save_counters
23042305
*
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.
23092313
*/
23102314
static void
2311-
pgstat_truncate_save_counters(PgStat_TableXactStatus *trans)
2315+
pgstat_truncdrop_save_counters(PgStat_TableXactStatus *trans, bool is_drop)
23122316
{
2313-
if (!trans->truncated)
2317+
if (!trans->truncdropped || is_drop)
23142318
{
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;
23192323
}
23202324
}
23212325

23222326
/*
2323-
* pgstat_truncate_restore_counters - restore counters when a truncate aborts
2327+
* pgstat_truncdrop_restore_counters - restore counters when a truncate aborts
23242328
*/
23252329
static void
2326-
pgstat_truncate_restore_counters(PgStat_TableXactStatus *trans)
2330+
pgstat_truncdrop_restore_counters(PgStat_TableXactStatus *trans)
23272331
{
2328-
if (trans->truncated)
2332+
if (trans->truncdropped)
23292333
{
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;
23332337
}
23342338
}
23352339

@@ -2350,7 +2354,7 @@ pgstat_count_truncate(Relation rel)
23502354
pgstat_info->trans->nest_level != nest_level)
23512355
add_tabstat_xact_level(pgstat_info, nest_level);
23522356

2353-
pgstat_truncate_save_counters(pgstat_info->trans);
2357+
pgstat_truncdrop_save_counters(pgstat_info->trans, false);
23542358
pgstat_info->trans->tuples_inserted = 0;
23552359
pgstat_info->trans->tuples_updated = 0;
23562360
pgstat_info->trans->tuples_deleted = 0;
@@ -2395,17 +2399,17 @@ AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
23952399
Assert(trans->upper == NULL);
23962400
tabstat = trans->parent;
23972401
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 */
23992403
if (!isCommit)
2400-
pgstat_truncate_restore_counters(trans);
2404+
pgstat_truncdrop_restore_counters(trans);
24012405
/* count attempted actions regardless of commit/abort */
24022406
tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
24032407
tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
24042408
tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
24052409
if (isCommit)
24062410
{
2407-
tabstat->t_counts.t_truncated = trans->truncated;
2408-
if (trans->truncated)
2411+
tabstat->t_counts.t_truncdropped = trans->truncdropped;
2412+
if (trans->truncdropped)
24092413
{
24102414
/* forget live/dead stats seen by backend thus far */
24112415
tabstat->t_counts.t_delta_live_tuples = 0;
@@ -2504,10 +2508,10 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in
25042508
{
25052509
if (trans->upper && trans->upper->nest_level == nestDepth - 1)
25062510
{
2507-
if (trans->truncated)
2511+
if (trans->truncdropped)
25082512
{
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);
25112515
/* replace upper xact stats with ours */
25122516
trans->upper->tuples_inserted = trans->tuples_inserted;
25132517
trans->upper->tuples_updated = trans->tuples_updated;
@@ -2547,8 +2551,8 @@ AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, in
25472551
* subtransaction
25482552
*/
25492553

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);
25522556
/* count attempted actions regardless of commit/abort */
25532557
tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
25542558
tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
@@ -2609,12 +2613,12 @@ AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
26092613
record.tuples_inserted = trans->tuples_inserted;
26102614
record.tuples_updated = trans->tuples_updated;
26112615
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;
26152619
record.t_id = tabstat->t_id;
26162620
record.t_shared = tabstat->t_shared;
2617-
record.t_truncated = trans->truncated;
2621+
record.t_truncdropped = trans->truncdropped;
26182622

26192623
RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
26202624
&record, sizeof(TwoPhasePgStatRecord));
@@ -2710,8 +2714,8 @@ pgstat_twophase_postcommit(TransactionId xid, uint16 info,
27102714
pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
27112715
pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
27122716
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)
27152719
{
27162720
/* forget live/dead stats seen by backend thus far */
27172721
pgstat_info->t_counts.t_delta_live_tuples = 0;
@@ -2743,11 +2747,11 @@ pgstat_twophase_postabort(TransactionId xid, uint16 info,
27432747
pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
27442748

27452749
/* Same math as in AtEOXact_PgStat, abort case */
2746-
if (rec->t_truncated)
2750+
if (rec->t_truncdropped)
27472751
{
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;
27512755
}
27522756
pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
27532757
pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
@@ -5055,8 +5059,11 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
50555059
tabentry->tuples_updated += tabmsg->t_counts.t_tuples_updated;
50565060
tabentry->tuples_deleted += tabmsg->t_counts.t_tuples_deleted;
50575061
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)
50605067
{
50615068
tabentry->n_live_tuples = 0;
50625069
tabentry->n_dead_tuples = 0;

src/include/pgstat.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ typedef struct PgStat_TableCounts
123123
PgStat_Counter t_tuples_updated;
124124
PgStat_Counter t_tuples_deleted;
125125
PgStat_Counter t_tuples_hot_updated;
126-
bool t_truncated;
126+
bool t_truncdropped;
127127

128128
PgStat_Counter t_delta_live_tuples;
129129
PgStat_Counter t_delta_dead_tuples;
@@ -186,10 +186,12 @@ typedef struct PgStat_TableXactStatus
186186
PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
187187
PgStat_Counter tuples_updated; /* tuples updated in (sub)xact */
188188
PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */
189-
bool truncated; /* relation truncated in this (sub)xact */
190-
PgStat_Counter inserted_pre_trunc; /* tuples inserted prior to truncate */
191-
PgStat_Counter updated_pre_trunc; /* tuples updated prior to truncate */
192-
PgStat_Counter deleted_pre_trunc; /* tuples deleted prior to truncate */
189+
bool truncdropped; /* relation truncated/dropped in this
190+
* (sub)xact */
191+
/* tuples i/u/d prior to truncate/drop */
192+
PgStat_Counter inserted_pre_truncdrop;
193+
PgStat_Counter updated_pre_truncdrop;
194+
PgStat_Counter deleted_pre_truncdrop;
193195
int nest_level; /* subtransaction nest level */
194196
/* links to other structs for same relation: */
195197
struct PgStat_TableXactStatus *upper; /* next higher subxact if any */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy