Skip to content

Commit 8363102

Browse files
committed
pgstat: introduce pgstat_relation_should_count().
A later commit will make the check more complicated than the current (rel)->pgstat_info != NULL. It also just seems nicer to have a central copy of the logic, even while still simple. Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20220303021600.hs34ghqcw6zcokdh@alap3.anarazel.de
1 parent 2d655a0 commit 8363102

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/backend/catalog/index.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
17431743
tabentry = pgstat_fetch_stat_tabentry(oldIndexId);
17441744
if (tabentry)
17451745
{
1746-
if (newClassRel->pgstat_info)
1746+
if (pgstat_relation_should_count(newClassRel))
17471747
{
17481748
newClassRel->pgstat_info->t_counts.t_numscans = tabentry->numscans;
17491749
newClassRel->pgstat_info->t_counts.t_tuples_returned = tabentry->tuples_returned;

src/backend/postmaster/pgstat.c

+17-19
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ pgstat_report_analyze(Relation rel,
17091709
*
17101710
* Waste no time on partitioned tables, though.
17111711
*/
1712-
if (rel->pgstat_info != NULL &&
1712+
if (pgstat_relation_should_count(rel) &&
17131713
rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
17141714
{
17151715
PgStat_TableXactStatus *trans;
@@ -2359,13 +2359,12 @@ add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
23592359
void
23602360
pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
23612361
{
2362-
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2363-
2364-
if (pgstat_info != NULL)
2362+
if (pgstat_relation_should_count(rel))
23652363
{
2366-
/* We have to log the effect at the proper transactional level */
2364+
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
23672365
int nest_level = GetCurrentTransactionNestLevel();
23682366

2367+
/* We have to log the effect at the proper transactional level */
23692368
if (pgstat_info->trans == NULL ||
23702369
pgstat_info->trans->nest_level != nest_level)
23712370
add_tabstat_xact_level(pgstat_info, nest_level);
@@ -2380,13 +2379,12 @@ pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
23802379
void
23812380
pgstat_count_heap_update(Relation rel, bool hot)
23822381
{
2383-
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2384-
2385-
if (pgstat_info != NULL)
2382+
if (pgstat_relation_should_count(rel))
23862383
{
2387-
/* We have to log the effect at the proper transactional level */
2384+
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
23882385
int nest_level = GetCurrentTransactionNestLevel();
23892386

2387+
/* We have to log the effect at the proper transactional level */
23902388
if (pgstat_info->trans == NULL ||
23912389
pgstat_info->trans->nest_level != nest_level)
23922390
add_tabstat_xact_level(pgstat_info, nest_level);
@@ -2405,13 +2403,12 @@ pgstat_count_heap_update(Relation rel, bool hot)
24052403
void
24062404
pgstat_count_heap_delete(Relation rel)
24072405
{
2408-
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2409-
2410-
if (pgstat_info != NULL)
2406+
if (pgstat_relation_should_count(rel))
24112407
{
2412-
/* We have to log the effect at the proper transactional level */
2408+
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
24132409
int nest_level = GetCurrentTransactionNestLevel();
24142410

2411+
/* We have to log the effect at the proper transactional level */
24152412
if (pgstat_info->trans == NULL ||
24162413
pgstat_info->trans->nest_level != nest_level)
24172414
add_tabstat_xact_level(pgstat_info, nest_level);
@@ -2463,13 +2460,12 @@ pgstat_truncdrop_restore_counters(PgStat_TableXactStatus *trans)
24632460
void
24642461
pgstat_count_truncate(Relation rel)
24652462
{
2466-
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2467-
2468-
if (pgstat_info != NULL)
2463+
if (pgstat_relation_should_count(rel))
24692464
{
2470-
/* We have to log the effect at the proper transactional level */
2465+
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
24712466
int nest_level = GetCurrentTransactionNestLevel();
24722467

2468+
/* We have to log the effect at the proper transactional level */
24732469
if (pgstat_info->trans == NULL ||
24742470
pgstat_info->trans->nest_level != nest_level)
24752471
add_tabstat_xact_level(pgstat_info, nest_level);
@@ -2492,10 +2488,12 @@ pgstat_count_truncate(Relation rel)
24922488
void
24932489
pgstat_update_heap_dead_tuples(Relation rel, int delta)
24942490
{
2495-
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2491+
if (pgstat_relation_should_count(rel))
2492+
{
2493+
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
24962494

2497-
if (pgstat_info != NULL)
24982495
pgstat_info->t_counts.t_delta_dead_tuples -= delta;
2496+
}
24992497
}
25002498

25012499
/*

src/include/pgstat.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -1104,41 +1104,44 @@ extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
11041104

11051105
extern void pgstat_initstats(Relation rel);
11061106

1107+
#define pgstat_relation_should_count(rel) \
1108+
(likely((rel)->pgstat_info != NULL))
1109+
11071110
/* nontransactional event counts are simple enough to inline */
11081111

11091112
#define pgstat_count_heap_scan(rel) \
11101113
do { \
1111-
if ((rel)->pgstat_info != NULL) \
1114+
if (pgstat_relation_should_count(rel)) \
11121115
(rel)->pgstat_info->t_counts.t_numscans++; \
11131116
} while (0)
11141117
#define pgstat_count_heap_getnext(rel) \
11151118
do { \
1116-
if ((rel)->pgstat_info != NULL) \
1119+
if (pgstat_relation_should_count(rel)) \
11171120
(rel)->pgstat_info->t_counts.t_tuples_returned++; \
11181121
} while (0)
11191122
#define pgstat_count_heap_fetch(rel) \
11201123
do { \
1121-
if ((rel)->pgstat_info != NULL) \
1124+
if (pgstat_relation_should_count(rel)) \
11221125
(rel)->pgstat_info->t_counts.t_tuples_fetched++; \
11231126
} while (0)
11241127
#define pgstat_count_index_scan(rel) \
11251128
do { \
1126-
if ((rel)->pgstat_info != NULL) \
1129+
if (pgstat_relation_should_count(rel)) \
11271130
(rel)->pgstat_info->t_counts.t_numscans++; \
11281131
} while (0)
11291132
#define pgstat_count_index_tuples(rel, n) \
11301133
do { \
1131-
if ((rel)->pgstat_info != NULL) \
1134+
if (pgstat_relation_should_count(rel)) \
11321135
(rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
11331136
} while (0)
11341137
#define pgstat_count_buffer_read(rel) \
11351138
do { \
1136-
if ((rel)->pgstat_info != NULL) \
1139+
if (pgstat_relation_should_count(rel)) \
11371140
(rel)->pgstat_info->t_counts.t_blocks_fetched++; \
11381141
} while (0)
11391142
#define pgstat_count_buffer_hit(rel) \
11401143
do { \
1141-
if ((rel)->pgstat_info != NULL) \
1144+
if (pgstat_relation_should_count(rel)) \
11421145
(rel)->pgstat_info->t_counts.t_blocks_hit++; \
11431146
} while (0)
11441147
#define pgstat_count_buffer_read_time(n) \

0 commit comments

Comments
 (0)