summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndres Freund2022-04-07 07:03:58 +0000
committerAndres Freund2022-04-07 07:21:54 +0000
commitad401664b8012cafb64e8fce33fe40356c5bc686 (patch)
tree96cf785f2e29948d53d5e850dfccea8021d3bf5e
parent0f96965c658147d6d6bad096d2d4a2c9c665f4a9 (diff)
pgstat: add pg_stat_have_stats() test helper.
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 <[email protected]> Discussion: https://postgr.es/m/CAAKRu_aNxL1WegCa45r=VAViCLnpOU7uNC7bTtGw+=QAPyYivw@mail.gmail.com
-rw-r--r--src/backend/catalog/system_functions.sql2
-rw-r--r--src/backend/utils/activity/pgstat.c10
-rw-r--r--src/backend/utils/adt/pgstatfuncs.c18
-rw-r--r--src/include/catalog/catversion.h2
-rw-r--r--src/include/catalog/pg_proc.dat6
-rw-r--r--src/include/pgstat.h2
6 files changed, 39 insertions, 1 deletions
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 6ae4388d3f..73da687d5d 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -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;
diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c
index 111869b211..7df3fda648 100644
--- a/src/backend/utils/activity/pgstat.c
+++ b/src/backend/utils/activity/pgstat.c
@@ -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.
*
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 5f1815727d..2bf8ab8f98 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -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));
+}
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index 19bb3a79b4..e133113543 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 202204072
+#define CATALOG_VERSION_NO 202204073
#endif
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 2b4438bc3d..776e31f3b5 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_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',
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index a0b853f4a6..88c87a0cc5 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -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