Re: Pluggable cumulative statistics
Michael Paquier <michael@paquier.xyz>
From: Michael Paquier <michael@paquier.xyz>
To: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-06-20T00:46:30Z
Lists: pgsql-hackers
Attachments
- v2-0001-Switch-PgStat_Kind-from-enum-to-uint32.patch (text/x-diff) patch v2-0001
- v2-0002-Replace-hardcoded-identifiers-in-pgstats-file-by-.patch (text/x-diff) patch v2-0002
- v2-0003-Introduce-pluggable-APIs-for-Cumulative-Statistic.patch (text/x-diff) patch v2-0003
- v2-0004-doc-Add-section-for-Custom-Cumulative-Statistics-.patch (text/x-diff) patch v2-0004
- v2-0005-injection_points-Add-statistics-for-custom-points.patch (text/x-diff) patch v2-0005
On Thu, Jun 13, 2024 at 04:59:50PM +0900, Michael Paquier wrote: > - How should the persistence of the custom stats be achieved? > Callbacks to give custom stats kinds a way to write/read their data, > push everything into a single file, or support both? > - Should this do like custom RMGRs and assign to each stats kinds ID > that are set in stone rather than dynamic ones? These two questions have been itching me in terms of how it would work for extension developers, after noticing that custom RMGRs are used more than I thought: https://wiki.postgresql.org/wiki/CustomWALResourceManagers The result is proving to be nicer, shorter by 300 lines in total and much simpler when it comes to think about the way stats are flushed because it is possible to achieve the same result as the first patch set without manipulating any of the code paths doing the read and write of the pgstats file. In terms of implementation, pgstat.c's KindInfo data is divided into two parts, for efficiency: - The exiting in-core stats with designated initializers, renamed as built-in stats kinds. - The custom stats kinds are saved in TopMemoryContext, and can only be registered with shared_preload_libraries. The patch reserves a set of 128 harcoded slots for all the custom kinds making the lookups for the KindInfos quite cheap. Upon registration, a custom stats kind needs to assign a unique ID, with uniqueness on the names and IDs checked at registration. The backend code does ID -> information lookups in the hotter paths, meaning that the code only checks if an ID is built-in or custom, then redirects to the correct array where the information is stored. There is one code path that does a name -> information lookup for the undocumented SQL function pg_stat_have_stats() used in the tests, which is a bit less efficient now, but that does not strike me as an issue. modules/injection_points/ works as previously as a template to show how to use these APIs, with tests for the whole. With that in mind, the patch set is more pleasant to the eye, and the attached v2 consists of: - 0001 and 0002 are some cleanups, same as previously to prepare for the backend-side APIs. - 0003 adds the backend support to plug-in custom stats. - 0004 includes documentation. - 0005 is an example of how to use them, with a TAP test providing coverage. Note that the patch I've proposed to make stats persistent at checkpoint so as we don't discard everything after a crash is able to work with the custom stats proposed on this thread: https://commitfest.postgresql.org/48/5047/ -- Michael
Commits
-
injection_points: Add some fixed-numbered statistics
- f68cd847fa40 18.0 landed
-
injection_points: Add some cumulative stats for injection points
- 75534436a477 18.0 landed
-
Add helper routines to retrieve data for custom fixed-numbered pgstats
- 2eff9e678d35 18.0 landed
-
Introduce pluggable APIs for Cumulative Statistics
- 7949d9594582 18.0 landed
-
Switch PgStat_Kind from an enum to a uint32 type
- 3188a4582a8c 18.0 landed
-
Improve comment of pgstat_read_statsfile()
- 72c0b24b2ddd 18.0 landed
-
Add a new 'F' entry type for fixed-numbered stats in pgstats file
- 9e4664d950c1 18.0 landed
-
Add PgStat_KindInfo.init_shmem_cb
- 21471f18e9d9 18.0 landed
-
Use pgstat_kind_infos to write fixed shared statistics
- b68b29bc8fec 18.0 landed
-
Replace hardcoded identifiers of pgstats file by #defines
- 9fd02525793f 18.0 landed