diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c index f793ac1516..1d3f068a1c 100644 --- a/src/backend/utils/activity/pgstat_relation.c +++ b/src/backend/utils/activity/pgstat_relation.c @@ -474,17 +474,33 @@ pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid) * If no entry found, return NULL, don't create a new one */ PgStat_TableStatus * -find_tabstat_entry(Oid rel_id) +find_tabstat_entry(Oid rel_id, bool add_subtrans) { PgStat_EntryRef *entry_ref; + PgStat_TableXactStatus *trans; + PgStat_TableStatus *tabentry = NULL; + PgStat_TableStatus *tablestatus = NULL; entry_ref = pgstat_fetch_pending_entry(PGSTAT_KIND_RELATION, MyDatabaseId, rel_id); if (!entry_ref) + { entry_ref = pgstat_fetch_pending_entry(PGSTAT_KIND_RELATION, InvalidOid, rel_id); + return tablestatus; + } + + tabentry = (PgStat_TableStatus *) entry_ref->pending; + tablestatus = palloc(sizeof(PgStat_TableStatus)); + *tablestatus = *tabentry; + + /* live subtransactions' counts aren't in t_counts yet */ + for (trans = tabentry->trans; add_subtrans && trans != NULL; trans = trans->upper) + { + tablestatus->t_counts.t_tuples_inserted += trans->tuples_inserted; + tablestatus->t_counts.t_tuples_updated += trans->tuples_updated; + tablestatus->t_counts.t_tuples_deleted += trans->tuples_deleted; + } - if (entry_ref) - return entry_ref->pending; - return NULL; + return tablestatus; } /* diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 924698e6ae..349fbdf967 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -1363,7 +1363,7 @@ pg_stat_get_xact_numscans(PG_FUNCTION_ARGS) int64 result; PgStat_TableStatus *tabentry; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, false)) == NULL) result = 0; else result = (int64) (tabentry->t_counts.t_numscans); @@ -1378,7 +1378,7 @@ pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS) int64 result; PgStat_TableStatus *tabentry; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, false)) == NULL) result = 0; else result = (int64) (tabentry->t_counts.t_tuples_returned); @@ -1393,7 +1393,7 @@ pg_stat_get_xact_tuples_fetched(PG_FUNCTION_ARGS) int64 result; PgStat_TableStatus *tabentry; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, false)) == NULL) result = 0; else result = (int64) (tabentry->t_counts.t_tuples_fetched); @@ -1407,17 +1407,11 @@ pg_stat_get_xact_tuples_inserted(PG_FUNCTION_ARGS) Oid relid = PG_GETARG_OID(0); int64 result; PgStat_TableStatus *tabentry; - PgStat_TableXactStatus *trans; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, true)) == NULL) result = 0; else - { - result = tabentry->t_counts.t_tuples_inserted; - /* live subtransactions' counts aren't in t_tuples_inserted yet */ - for (trans = tabentry->trans; trans != NULL; trans = trans->upper) - result += trans->tuples_inserted; - } + result = (int64) (tabentry->t_counts.t_tuples_inserted); PG_RETURN_INT64(result); } @@ -1428,17 +1422,11 @@ pg_stat_get_xact_tuples_updated(PG_FUNCTION_ARGS) Oid relid = PG_GETARG_OID(0); int64 result; PgStat_TableStatus *tabentry; - PgStat_TableXactStatus *trans; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, true)) == NULL) result = 0; else - { - result = tabentry->t_counts.t_tuples_updated; - /* live subtransactions' counts aren't in t_tuples_updated yet */ - for (trans = tabentry->trans; trans != NULL; trans = trans->upper) - result += trans->tuples_updated; - } + result = (int64) (tabentry->t_counts.t_tuples_updated); PG_RETURN_INT64(result); } @@ -1449,17 +1437,11 @@ pg_stat_get_xact_tuples_deleted(PG_FUNCTION_ARGS) Oid relid = PG_GETARG_OID(0); int64 result; PgStat_TableStatus *tabentry; - PgStat_TableXactStatus *trans; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, true)) == NULL) result = 0; else - { - result = tabentry->t_counts.t_tuples_deleted; - /* live subtransactions' counts aren't in t_tuples_deleted yet */ - for (trans = tabentry->trans; trans != NULL; trans = trans->upper) - result += trans->tuples_deleted; - } + result = (int64) (tabentry->t_counts.t_tuples_deleted); PG_RETURN_INT64(result); } @@ -1471,7 +1453,7 @@ pg_stat_get_xact_tuples_hot_updated(PG_FUNCTION_ARGS) int64 result; PgStat_TableStatus *tabentry; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, false)) == NULL) result = 0; else result = (int64) (tabentry->t_counts.t_tuples_hot_updated); @@ -1486,7 +1468,7 @@ pg_stat_get_xact_blocks_fetched(PG_FUNCTION_ARGS) int64 result; PgStat_TableStatus *tabentry; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, false)) == NULL) result = 0; else result = (int64) (tabentry->t_counts.t_blocks_fetched); @@ -1501,7 +1483,7 @@ pg_stat_get_xact_blocks_hit(PG_FUNCTION_ARGS) int64 result; PgStat_TableStatus *tabentry; - if ((tabentry = find_tabstat_entry(relid)) == NULL) + if ((tabentry = find_tabstat_entry(relid, false)) == NULL) result = 0; else result = (int64) (tabentry->t_counts.t_blocks_hit); diff --git a/src/include/pgstat.h b/src/include/pgstat.h index db9675884f..ca29c7f26e 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -641,7 +641,7 @@ extern void pgstat_twophase_postabort(TransactionId xid, uint16 info, extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid); extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid); -extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id); +extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id, bool add_subtrans); /*