Skip to content

Commit ce5c620

Browse files
committed
Add pgstat_drop_matching_entries() to pgstats
This allows users of the cumulative statistics to drop entries in the shared hash stats table, deleting as well local references. Callers of this function can optionally define a callback able to filter which entries to drop, similarly to pgstat_reset_matching_entries() with its callback do_reset(). pgstat_drop_all_entries() is refactored so as it uses this new function. Author: Lukas Fitti Discussion: https://postgr.es/m/CAP53PkwuFbo3NkwZgxwNRMjMfqPEqidD-SggaoQ4ijotBVLJAA@mail.gmail.com
1 parent 1e380fa commit ce5c620

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/backend/utils/activity/pgstat_shmem.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,19 +993,39 @@ pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
993993
return freed;
994994
}
995995

996+
/*
997+
* Scan through the shared hashtable of stats, dropping statistics if
998+
* approved by the optional do_drop() function.
999+
*/
9961000
void
997-
pgstat_drop_all_entries(void)
1001+
pgstat_drop_matching_entries(bool (*do_drop) (PgStatShared_HashEntry *, Datum),
1002+
Datum match_data)
9981003
{
9991004
dshash_seq_status hstat;
10001005
PgStatShared_HashEntry *ps;
10011006
uint64 not_freed_count = 0;
10021007

1008+
/* entries are removed, take an exclusive lock */
10031009
dshash_seq_init(&hstat, pgStatLocal.shared_hash, true);
10041010
while ((ps = dshash_seq_next(&hstat)) != NULL)
10051011
{
10061012
if (ps->dropped)
10071013
continue;
10081014

1015+
if (do_drop != NULL && !do_drop(ps, match_data))
1016+
continue;
1017+
1018+
/* delete local reference */
1019+
if (pgStatEntryRefHash)
1020+
{
1021+
PgStat_EntryRefHashEntry *lohashent =
1022+
pgstat_entry_ref_hash_lookup(pgStatEntryRefHash, ps->key);
1023+
1024+
if (lohashent)
1025+
pgstat_release_entry_ref(lohashent->key, lohashent->entry_ref,
1026+
true);
1027+
}
1028+
10091029
if (!pgstat_drop_entry_internal(ps, &hstat))
10101030
not_freed_count++;
10111031
}
@@ -1015,6 +1035,15 @@ pgstat_drop_all_entries(void)
10151035
pgstat_request_entry_refs_gc();
10161036
}
10171037

1038+
/*
1039+
* Scan through the shared hashtable of stats and drop all entries.
1040+
*/
1041+
void
1042+
pgstat_drop_all_entries(void)
1043+
{
1044+
pgstat_drop_matching_entries(NULL, 0);
1045+
}
1046+
10181047
static void
10191048
shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header,
10201049
TimestampTz ts)

src/include/utils/pgstat_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ extern bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait);
718718
extern void pgstat_unlock_entry(PgStat_EntryRef *entry_ref);
719719
extern bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid);
720720
extern void pgstat_drop_all_entries(void);
721+
extern void pgstat_drop_matching_entries(bool (*do_drop) (PgStatShared_HashEntry *, Datum),
722+
Datum match_data);
721723
extern PgStat_EntryRef *pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
722724
bool nowait);
723725
extern void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy