diff options
author | Andres Freund | 2022-04-07 00:56:19 +0000 |
---|---|---|
committer | Andres Freund | 2022-04-07 00:56:19 +0000 |
commit | 8fb580a35ce358063dfdd10991d017498283c767 (patch) | |
tree | 425767a0d794844d09fe549f89def74783ba6f05 /src/backend/utils | |
parent | 997afad89d12f314555600feee8189d753e105d1 (diff) |
pgstat: prepare APIs used by pgstatfuncs for shared memory stats.
With the introduction of PgStat_Kind PgStat_Single_Reset_Type,
PgStat_Shared_Reset_Target don't make sense anymore. Replace them with
PgStat_Kind.
Instead of having dedicated reset functions for different kinds of stats, use
two generic helper routines (one to reset all stats of a kind, one to reset
one stats entry).
A number of reset functions were named pgstat_reset_*_counter(), despite
affecting multiple counters. The generic helper routines get rid of
pgstat_reset_single_counter(), pgstat_reset_subscription_counter().
Rename pgstat_reset_slru_counter(), pgstat_reset_replslot_counter() to
pgstat_reset_slru(), pgstat_reset_replslot() respectively, and have them only
deal with a single SLRU/slot. Resetting all SLRUs/slots goes through the
generic pgstat_reset_of_kind().
Previously pg_stat_reset_replication_slot() used SearchNamedReplicationSlot()
to check if a slot exists. API wise it seems better to move that to
pgstat_replslot.c.
This is done separately from the - quite large - shared memory statistics
patch to make review easier.
Reviewed-By: Kyotaro Horiguchi <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/activity/pgstat_replslot.c | 37 | ||||
-rw-r--r-- | src/backend/utils/activity/pgstat_slru.c | 8 | ||||
-rw-r--r-- | src/backend/utils/activity/pgstat_subscription.c | 21 | ||||
-rw-r--r-- | src/backend/utils/adt/pgstatfuncs.c | 68 |
4 files changed, 63 insertions, 71 deletions
diff --git a/src/backend/utils/activity/pgstat_replslot.c b/src/backend/utils/activity/pgstat_replslot.c index 35078ad73c8..cfaf8d546c5 100644 --- a/src/backend/utils/activity/pgstat_replslot.c +++ b/src/backend/utils/activity/pgstat_replslot.c @@ -23,30 +23,45 @@ /* - * Reset counters for a single replication slot, or all replication slots - * (when name is null). + * Reset counters for a single replication slot. * * Permission checking for this function is managed through the normal * GRANT system. */ void -pgstat_reset_replslot_counter(const char *name) +pgstat_reset_replslot(const char *name) { + ReplicationSlot *slot; PgStat_MsgResetreplslotcounter msg; + AssertArg(name != NULL); + if (pgStatSock == PGINVALID_SOCKET) return; - if (name) - { - namestrcpy(&msg.m_slotname, name); - msg.clearall = false; - } - else - msg.clearall = true; + /* + * Check if the slot exists with the given name. It is possible that by + * the time this message is executed the slot is dropped but at least this + * check will ensure that the given name is for a valid slot. + */ + slot = SearchNamedReplicationSlot(name, true); - pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETREPLSLOTCOUNTER); + if (!slot) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("replication slot \"%s\" does not exist", + name))); + /* + * Nothing to do for physical slots as we collect stats only for logical + * slots. + */ + if (SlotIsPhysical(slot)) + return; + + pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETREPLSLOTCOUNTER); + namestrcpy(&msg.m_slotname, name); + msg.clearall = false; pgstat_send(&msg, sizeof(msg)); } diff --git a/src/backend/utils/activity/pgstat_slru.c b/src/backend/utils/activity/pgstat_slru.c index 6dae3a5dc66..1f2d2c3bbb7 100644 --- a/src/backend/utils/activity/pgstat_slru.c +++ b/src/backend/utils/activity/pgstat_slru.c @@ -33,21 +33,23 @@ static PgStat_MsgSLRU SLRUStats[SLRU_NUM_ELEMENTS]; /* - * Reset counters for a single SLRU, or all SLRUs (when name is null). + * Reset counters for a single SLRU. * * Permission checking for this function is managed through the normal * GRANT system. */ void -pgstat_reset_slru_counter(const char *name) +pgstat_reset_slru(const char *name) { PgStat_MsgResetslrucounter msg; + AssertArg(name != NULL); + if (pgStatSock == PGINVALID_SOCKET) return; pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETSLRUCOUNTER); - msg.m_index = (name) ? pgstat_slru_index(name) : -1; + msg.m_index = pgstat_slru_index(name); pgstat_send(&msg, sizeof(msg)); } diff --git a/src/backend/utils/activity/pgstat_subscription.c b/src/backend/utils/activity/pgstat_subscription.c index 2ee23d5ae2c..503dcabd204 100644 --- a/src/backend/utils/activity/pgstat_subscription.c +++ b/src/backend/utils/activity/pgstat_subscription.c @@ -21,27 +21,6 @@ /* - * Reset counters for a single subscription, or all subscriptions (when subid - * is InvalidOid). - * - * Permission checking for this function is managed through the normal - * GRANT system. - */ -void -pgstat_reset_subscription_counter(Oid subid) -{ - PgStat_MsgResetsubcounter msg; - - if (pgStatSock == PGINVALID_SOCKET) - return; - - msg.m_subid = subid; - pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETSUBCOUNTER); - - pgstat_send(&msg, sizeof(msg)); -} - -/* * Report a subscription error. */ void diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index fd4276fbc67..709dd5548ac 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -24,7 +24,6 @@ #include "pgstat.h" #include "postmaster/bgworker_internals.h" #include "postmaster/postmaster.h" -#include "replication/slot.h" #include "storage/proc.h" #include "storage/procarray.h" #include "utils/acl.h" @@ -2075,7 +2074,24 @@ pg_stat_reset_shared(PG_FUNCTION_ARGS) { char *target = text_to_cstring(PG_GETARG_TEXT_PP(0)); - pgstat_reset_shared_counters(target); + if (strcmp(target, "archiver") == 0) + pgstat_reset_of_kind(PGSTAT_KIND_ARCHIVER); + else if (strcmp(target, "bgwriter") == 0) + { + /* + * Historically checkpointer was part of bgwriter, continue to reset + * both for now. + */ + pgstat_reset_of_kind(PGSTAT_KIND_BGWRITER); + pgstat_reset_of_kind(PGSTAT_KIND_CHECKPOINTER); + } + else if (strcmp(target, "wal") == 0) + pgstat_reset_of_kind(PGSTAT_KIND_WAL); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("unrecognized reset target: \"%s\"", target), + errhint("Target must be \"archiver\", \"bgwriter\", or \"wal\"."))); PG_RETURN_VOID(); } @@ -2086,7 +2102,7 @@ pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS) { Oid taboid = PG_GETARG_OID(0); - pgstat_reset_single_counter(taboid, RESET_TABLE); + pgstat_reset(PGSTAT_KIND_RELATION, MyDatabaseId, taboid); PG_RETURN_VOID(); } @@ -2096,7 +2112,7 @@ pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS) { Oid funcoid = PG_GETARG_OID(0); - pgstat_reset_single_counter(funcoid, RESET_FUNCTION); + pgstat_reset(PGSTAT_KIND_FUNCTION, MyDatabaseId, funcoid); PG_RETURN_VOID(); } @@ -2107,10 +2123,13 @@ pg_stat_reset_slru(PG_FUNCTION_ARGS) { char *target = NULL; - if (!PG_ARGISNULL(0)) + if (PG_ARGISNULL(0)) + pgstat_reset_of_kind(PGSTAT_KIND_SLRU); + else + { target = text_to_cstring(PG_GETARG_TEXT_PP(0)); - - pgstat_reset_slru_counter(target); + pgstat_reset_slru(target); + } PG_RETURN_VOID(); } @@ -2121,36 +2140,14 @@ pg_stat_reset_replication_slot(PG_FUNCTION_ARGS) { char *target = NULL; - if (!PG_ARGISNULL(0)) + if (PG_ARGISNULL(0)) + pgstat_reset_of_kind(PGSTAT_KIND_REPLSLOT); + else { - ReplicationSlot *slot; - target = text_to_cstring(PG_GETARG_TEXT_PP(0)); - - /* - * Check if the slot exists with the given name. It is possible that - * by the time this message is executed the slot is dropped but at - * least this check will ensure that the given name is for a valid - * slot. - */ - slot = SearchNamedReplicationSlot(target, true); - - if (!slot) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("replication slot \"%s\" does not exist", - target))); - - /* - * Nothing to do for physical slots as we collect stats only for - * logical slots. - */ - if (SlotIsPhysical(slot)) - PG_RETURN_VOID(); + pgstat_reset_replslot(target); } - pgstat_reset_replslot_counter(target); - PG_RETURN_VOID(); } @@ -2163,7 +2160,7 @@ pg_stat_reset_subscription_stats(PG_FUNCTION_ARGS) if (PG_ARGISNULL(0)) { /* Clear all subscription stats */ - subid = InvalidOid; + pgstat_reset_of_kind(PGSTAT_KIND_SUBSCRIPTION); } else { @@ -2173,10 +2170,9 @@ pg_stat_reset_subscription_stats(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid subscription OID %u", subid))); + pgstat_reset(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid); } - pgstat_reset_subscription_counter(subid); - PG_RETURN_VOID(); } |