Subject: Injection Points remaining stats

From: Yogesh Sharma <[email-masked]>
Date: Mon, 12 Aug 2024 10:25:13 -0400

Hi all,

Attaching a patch to add remaining cached and loaded stats as mentioned 
in commit f68cd847fa40ead44a786b9c34aff9ccc048004b message. Existing TAP 
tests were updated to handle new stats. This patch has been tested on 
HEAD using "make check-world" after enabling injection points via 
"--enable-injection-points".

-- 
Kind Regards,
Yogesh Sharma

From 52ac2b14ee34d606aae97b2648c6708901c324b4 Mon Sep 17 00:00:00 2001
From: Yogesh Sharma <[email-masked]>
Date: Wed, 7 Aug 2024 15:32:35 -0400
Subject: [PATCH] This patch adds missing cached and loaded stats to
 injection_points_stats_fixed as mentioned in commit #f68cd847fa

---
 .../injection_points--1.0.sql                 |  4 +++-
 .../injection_points/injection_points.c       |  8 ++++---
 .../injection_points/injection_stats.h        |  4 +++-
 .../injection_points/injection_stats_fixed.c  | 24 +++++++++++++++----
 .../modules/injection_points/t/001_stats.pl   |  6 ++---
 5 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/src/test/modules/injection_points/injection_points--1.0.sql b/src/test/modules/injection_points/injection_points--1.0.sql
index 1b2a4938a9..6c81d55e0d 100644
--- a/src/test/modules/injection_points/injection_points--1.0.sql
+++ b/src/test/modules/injection_points/injection_points--1.0.sql
@@ -91,7 +91,9 @@ LANGUAGE C STRICT;
 -- Reports fixed-numbered statistics for injection points.
 CREATE FUNCTION injection_points_stats_fixed(OUT numattach int8,
    OUT numdetach int8,
-   OUT numrun int8)
+   OUT numrun int8,
+   OUT numcached int8,
+   OUT numloaded int8)
 RETURNS record
 AS 'MODULE_PATHNAME', 'injection_points_stats_fixed'
 LANGUAGE C STRICT;
diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c
index dc02be1bbf..221b826d8d 100644
--- a/src/test/modules/injection_points/injection_points.c
+++ b/src/test/modules/injection_points/injection_points.c
@@ -297,7 +297,7 @@ injection_points_attach(PG_FUNCTION_ARGS)
 		condition.pid = MyProcPid;
 	}
 
-	pgstat_report_inj_fixed(1, 0, 0);
+	pgstat_report_inj_fixed(1, 0, 0, 0,  0);
 	InjectionPointAttach(name, "injection_points", function, &condition,
 						 sizeof(InjectionPointCondition));
 
@@ -329,6 +329,7 @@ injection_points_load(PG_FUNCTION_ARGS)
 	if (inj_state == NULL)
 		injection_init_shmem();
 
+	pgstat_report_inj_fixed(0, 0, 0, 0, 1);
 	INJECTION_POINT_LOAD(name);
 
 	PG_RETURN_VOID();
@@ -343,7 +344,7 @@ injection_points_run(PG_FUNCTION_ARGS)
 {
 	char	   *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
-	pgstat_report_inj_fixed(0, 0, 1);
+	pgstat_report_inj_fixed(0, 0, 1, 0, 0);
 	INJECTION_POINT(name);
 
 	PG_RETURN_VOID();
@@ -358,6 +359,7 @@ injection_points_cached(PG_FUNCTION_ARGS)
 {
 	char	   *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
+	pgstat_report_inj_fixed(0, 0, 0, 1, 0);
 	INJECTION_POINT_CACHED(name);
 
 	PG_RETURN_VOID();
@@ -434,7 +436,7 @@ injection_points_detach(PG_FUNCTION_ARGS)
 {
 	char	   *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
-	pgstat_report_inj_fixed(0, 1, 0);
+	pgstat_report_inj_fixed(0, 1, 0, 0, 0);
 	if (!InjectionPointDetach(name))
 		elog(ERROR, "could not detach injection point \"%s\"", name);
 
diff --git a/src/test/modules/injection_points/injection_stats.h b/src/test/modules/injection_points/injection_stats.h
index d519f29f83..126c110169 100644
--- a/src/test/modules/injection_points/injection_stats.h
+++ b/src/test/modules/injection_points/injection_stats.h
@@ -25,6 +25,8 @@ extern void pgstat_report_inj(const char *name);
 extern void pgstat_register_inj_fixed(void);
 extern void pgstat_report_inj_fixed(uint32 numattach,
 									uint32 numdetach,
-									uint32 numrun);
+									uint32 numrun,
+									uint32 numcached,
+									uint32 numloaded);
 
 #endif
diff --git a/src/test/modules/injection_points/injection_stats_fixed.c b/src/test/modules/injection_points/injection_stats_fixed.c
index 72a5f9decb..e01da5c688 100644
--- a/src/test/modules/injection_points/injection_stats_fixed.c
+++ b/src/test/modules/injection_points/injection_stats_fixed.c
@@ -29,6 +29,8 @@ typedef struct PgStat_StatInjFixedEntry
 	PgStat_Counter numattach;	/* number of points attached */
 	PgStat_Counter numdetach;	/* number of points detached */
 	PgStat_Counter numrun;		/* number of points run */
+	PgStat_Counter numcached;		/* number of points cached */
+	PgStat_Counter numloaded;		/* number of points loaded */
 	TimestampTz stat_reset_timestamp;
 } PgStat_StatInjFixedEntry;
 
@@ -114,6 +116,8 @@ injection_stats_fixed_snapshot_cb(void)
 	FIXED_COMP(numattach);
 	FIXED_COMP(numdetach);
 	FIXED_COMP(numrun);
+	FIXED_COMP(numcached);
+	FIXED_COMP(numloaded);
 #undef FIXED_COMP
 }
 
@@ -135,7 +139,9 @@ pgstat_register_inj_fixed(void)
 void
 pgstat_report_inj_fixed(uint32 numattach,
 						uint32 numdetach,
-						uint32 numrun)
+						uint32 numrun,
+						uint32 numcached,
+						uint32 numloaded)
 {
 	PgStatShared_InjectionPointFixed *stats_shmem;
 
@@ -149,6 +155,8 @@ pgstat_report_inj_fixed(uint32 numattach,
 	stats_shmem->stats.numattach += numattach;
 	stats_shmem->stats.numdetach += numdetach;
 	stats_shmem->stats.numrun += numrun;
+	stats_shmem->stats.numcached += numcached;
+	stats_shmem->stats.numloaded += numloaded;
 	pgstat_end_changecount_write(&stats_shmem->changecount);
 }
 
@@ -160,8 +168,8 @@ Datum
 injection_points_stats_fixed(PG_FUNCTION_ARGS)
 {
 	TupleDesc	tupdesc;
-	Datum		values[3] = {0};
-	bool		nulls[3] = {0};
+	Datum		values[5] = {0};
+	bool		nulls[5] = {0};
 	PgStat_StatInjFixedEntry *stats;
 
 	if (!inj_fixed_loaded)
@@ -171,21 +179,29 @@ injection_points_stats_fixed(PG_FUNCTION_ARGS)
 	stats = pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED);
 
 	/* Initialise attributes information in the tuple descriptor */
-	tupdesc = CreateTemplateTupleDesc(3);
+	tupdesc = CreateTemplateTupleDesc(5);
 	TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numattach",
 					   INT8OID, -1, 0);
 	TupleDescInitEntry(tupdesc, (AttrNumber) 2, "numdetach",
 					   INT8OID, -1, 0);
 	TupleDescInitEntry(tupdesc, (AttrNumber) 3, "numrun",
 					   INT8OID, -1, 0);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 4, "numcached",
+					   INT8OID, -1, 0);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 5, "numloaded",
+					   INT8OID, -1, 0);
 	BlessTupleDesc(tupdesc);
 
 	values[0] = Int64GetDatum(stats->numattach);
 	values[1] = Int64GetDatum(stats->numdetach);
 	values[2] = Int64GetDatum(stats->numrun);
+	values[3] = Int64GetDatum(stats->numcached);
+	values[4] = Int64GetDatum(stats->numloaded);
 	nulls[0] = false;
 	nulls[1] = false;
 	nulls[2] = false;
+	nulls[3] = false;
+	nulls[4] = false;
 
 	/* Returns the record as Datum */
 	PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
diff --git a/src/test/modules/injection_points/t/001_stats.pl b/src/test/modules/injection_points/t/001_stats.pl
index 7897691f95..2f7231a9dc 100644
--- a/src/test/modules/injection_points/t/001_stats.pl
+++ b/src/test/modules/injection_points/t/001_stats.pl
@@ -35,7 +35,7 @@ my $numcalls = $node->safe_psql('postgres',
 is($numcalls, '2', 'number of stats calls');
 my $fixedstats = $node->safe_psql('postgres',
 	"SELECT * FROM injection_points_stats_fixed();");
-is($fixedstats, '1|0|2', 'number of fixed stats');
+is($fixedstats, '1|0|2|0|0', 'number of fixed stats');
 
 # Restart the node cleanly, stats should still be around.
 $node->restart;
@@ -44,7 +44,7 @@ $numcalls = $node->safe_psql('postgres',
 is($numcalls, '2', 'number of stats after clean restart');
 $fixedstats = $node->safe_psql('postgres',
 	"SELECT * FROM injection_points_stats_fixed();");
-is($fixedstats, '1|0|2', 'number of fixed stats after clean restart');
+is($fixedstats, '1|0|2|0|0', 'number of fixed stats after clean restart');
 
 # On crash the stats are gone.
 $node->stop('immediate');
@@ -54,6 +54,6 @@ $numcalls = $node->safe_psql('postgres',
 is($numcalls, '', 'number of stats after crash');
 $fixedstats = $node->safe_psql('postgres',
 	"SELECT * FROM injection_points_stats_fixed();");
-is($fixedstats, '0|0|0', 'number of fixed stats after crash');
+is($fixedstats, '0|0|0|0|0', 'number of fixed stats after crash');
 
 done_testing();
-- 
2.45.2