pgstat: add pg_stat_have_stats() test helper.
authorAndres Freund <andres@anarazel.de>
Thu, 7 Apr 2022 07:03:58 +0000 (00:03 -0700)
committerAndres Freund <andres@anarazel.de>
Thu, 7 Apr 2022 07:21:54 +0000 (00:21 -0700)
Will be used by tests committed subsequently.

Bumps catversion (this time for real, the one in 0f96965c658 got lost when
rebasing over 5c279a6d350).

Author: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://postgr.es/m/CAAKRu_aNxL1WegCa45r=VAViCLnpOU7uNC7bTtGw+=QAPyYivw@mail.gmail.com

src/backend/catalog/system_functions.sql
src/backend/utils/activity/pgstat.c
src/backend/utils/adt/pgstatfuncs.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.dat
src/include/pgstat.h

index 6ae4388d3f8384d28d02d4e6f99ed79583982909..73da687d5dd2f936417c4ffc599502fcc05f6eda 100644 (file)
@@ -637,6 +637,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_have_stats(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;
index 111869b2115cdbe352f43b5d72e1ecd76956d130..7df3fda64849e1b00a0385806b7e72ee9dd41788 100644 (file)
@@ -872,6 +872,16 @@ pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
    return 0;
 }
 
+bool
+pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
+{
+   /* fixed-numbered stats always exist */
+   if (pgstat_get_kind_info(kind)->fixed_amount)
+       return true;
+
+   return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
+}
+
 /*
  * Ensure snapshot for fixed-numbered 'kind' exists.
  *
index 5f1815727de75032e63893d5968a77d7add63422..2bf8ab8f9863c2723db8e2f3a578cefafa2dbfd4 100644 (file)
@@ -2394,3 +2394,21 @@ 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 tests, but not really anything else. Therefore not
+ * documented.
+ */
+Datum
+pg_stat_have_stats(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_get_kind_from_str(stats_type);
+
+   PG_RETURN_BOOL(pgstat_have_entry(kind, dboid, objoid));
+}
index 19bb3a79b42882c01856816dc9eaa9ad6f157c06..e133113543f2893dbc0bb2719eff8ed51f37cfce 100644 (file)
@@ -53,6 +53,6 @@
  */
 
 /*                         yyyymmddN */
-#define CATALOG_VERSION_NO 202204072
+#define CATALOG_VERSION_NO 202204073
 
 #endif
index 2b4438bc3d56bc27735c02a195a45943a7a2c108..776e31f3b58eb2c03460d4e1b9f7d38fb479f9d2 100644 (file)
   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_have_stats', provolatile => 'v', proparallel => 'r',
+  prorettype => 'bool', proargtypes => 'text oid oid',
+  prosrc => 'pg_stat_have_stats' },
+
 { oid => '8523', descr => 'statistics: information about subscription stats',
   proname => 'pg_stat_get_subscription_stats',
   provolatile => 's', proparallel => 'r',
index a0b853f4a6901ce3fa9b55b478fd2ba2fd2be2fa..88c87a0cc5434198febe9348bd6af331f684c8b2 100644 (file)
@@ -426,6 +426,8 @@ extern TimestampTz pgstat_get_stat_snapshot_timestamp(bool *have_snapshot);
 
 /* helpers */
 extern PgStat_Kind pgstat_get_kind_from_str(char *kind_str);
+extern bool pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid);
+
 
 /*
  * Functions in pgstat_archiver.c