From fd87c2e8e8bf7bdfec3ea2a5585fb2d57b41b3eb Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sat, 2 Apr 2022 14:21:58 -0700
Subject: [PATCH v68 16/31] pgstat: add pg_stat_exists_stat() for easier
 testing.

Useful for tests.

Author: Melanie Plageman <melanieplageman@gmail.com>
---
 src/include/catalog/pg_proc.dat          |  6 ++++++
 src/include/pgstat.h                     |  2 ++
 src/backend/catalog/system_functions.sql |  2 ++
 src/backend/postmaster/pgstat.c          |  9 +++++++++
 src/backend/utils/adt/pgstatfuncs.c      | 17 +++++++++++++++++
 5 files changed, 36 insertions(+)

diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 12526c599e0..948e6504da0 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -5376,6 +5376,12 @@
   proargmodes => '{i,o,o,o,o,o,o,o,o,o,o}',
   proargnames => '{slot_name,slot_name,spill_txns,spill_count,spill_bytes,stream_txns,stream_count,stream_bytes,total_txns,total_bytes,stats_reset}',
   prosrc => 'pg_stat_get_replication_slot' },
+
+{ oid => '8384', descr => 'statistics: check if a stats object exists',
+  proname => 'pg_stat_exists_stat', provolatile => 'v', proparallel => 'r',
+  prorettype => 'bool', proargtypes => 'text oid oid',
+  prosrc => 'pg_stat_exists_stat' },
+
 { oid => '8523', descr => 'statistics: information about subscription stats',
   proname => 'pg_stat_get_subscription_stats',
   provolatile => 's', proparallel => 'r',
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 3481ba5e262..fae3186c8eb 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -438,6 +438,8 @@ extern TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot);
 
 /* helpers */
 extern PgStat_Kind pgstat_kind_from_str(char *kind_str);
+extern bool pgstat_exists_entry(PgStat_Kind kind, Oid dboid, Oid objoid);
+
 
 /*
  * Functions in pgstat_archiver.c
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 81bac6f5812..07386f97f95 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -639,6 +639,8 @@ REVOKE EXECUTE ON FUNCTION pg_stat_reset_single_function_counters(oid) FROM publ
 
 REVOKE EXECUTE ON FUNCTION pg_stat_reset_replication_slot(text) FROM public;
 
+REVOKE EXECUTE ON FUNCTION pg_stat_exists_stat(text, oid, oid) FROM public;
+
 REVOKE EXECUTE ON FUNCTION pg_stat_reset_subscription_stats(oid) FROM public;
 
 REVOKE EXECUTE ON FUNCTION lo_import(text) FROM public;
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 0a0d64be829..8d619d114b2 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -835,6 +835,15 @@ pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
 	return 0;
 }
 
+bool
+pgstat_exists_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
+{
+	if (pgstat_kind_info_for(kind)->fixed_amount)
+		return true;
+
+	return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
+}
+
 /*
  * Ensure snapshot for a kind of global stats exist.
  *
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index caaf0c5a672..2660bd1eb07 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -2395,3 +2395,20 @@ pg_stat_get_subscription_stats(PG_FUNCTION_ARGS)
 	/* Returns the record as Datum */
 	PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
 }
+
+/*
+ * Checks for presence of stats for object with provided kind, database oid,
+ * object oid.
+ *
+ * This is useful for regression tests, but not much more.
+ */
+Datum
+pg_stat_exists_stat(PG_FUNCTION_ARGS)
+{
+	char	   *stats_type = text_to_cstring(PG_GETARG_TEXT_P(0));
+	Oid			dboid = PG_GETARG_OID(1);
+	Oid			objoid = PG_GETARG_OID(2);
+	PgStat_Kind	kind = pgstat_kind_from_str(stats_type);
+
+	PG_RETURN_BOOL(pgstat_exists_entry(kind, dboid, objoid));
+}
-- 
2.35.1.677.gabf474a5dd

