Re: Pluggable cumulative statistics
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
To: Michael Paquier <michael@paquier.xyz>
Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-06-20T14:27:14Z
Lists: pgsql-hackers
Hi,
On Thu, Jun 20, 2024 at 09:46:30AM +0900, Michael Paquier wrote:
> 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.
I think it makes sense to follow the same "behavior" as the custom
wal resource managers. That, indeed, looks much more simpler than v1.
> 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,
Agree that a backend lifetime memory area is fine for that purpose.
> 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.
+ MemoryContextAllocZero(TopMemoryContext,
+ sizeof(PgStat_KindInfo *) * PGSTAT_KIND_CUSTOM_SIZE);
and that's only 8 * PGSTAT_KIND_CUSTOM_SIZE bytes in total.
I had a quick look at the patches (have in mind to do more):
> 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.
0001 and 0002 look pretty straightforward at a quick look.
> - 0003 adds the backend support to plug-in custom stats.
1 ===
It looks to me that there is a mix of "in core" and "built-in" to name the
non custom stats. Maybe it's worth to just use one?
As I can see (and as you said above) this is mainly inspired by the custom
resource manager and 2 === and 3 === are probably copy/paste consequences.
2 ===
+ if (pgstat_kind_custom_infos[idx] != NULL &&
+ pgstat_kind_custom_infos[idx]->name != NULL)
+ ereport(ERROR,
+ (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
+ errdetail("Custom resource manager \"%s\" already registered with the same ID.",
+ pgstat_kind_custom_infos[idx]->name)));
s/Custom resource manager/Custom cumulative statistics/
3 ===
+ ereport(LOG,
+ (errmsg("registered custom resource manager \"%s\" with ID %u",
+ kind_info->name, kind)));
s/custom resource manager/custom cumulative statistics/
> - 0004 includes documentation.
Did not look yet.
> - 0005 is an example of how to use them, with a TAP test providing
> coverage.
Did not look yet.
As I said, I've in mind to do a more in depth review. I've noted the above while
doing a quick read of the patches so thought it makes sense to share them
now while at it.
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
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