Add pg_stat_kind_info system view

Поиск
Список
Период
Сортировка
Искать

Add pg_stat_kind_info system view

От:
"Tristan Partin" <tristan@partin.io>
Дата:
Hey hackers,

In 7949d959458[0], we introduced pluggable cumulative statistics. 
Extensions could now register their own custom statistics. However, 
there was no way for an extension author to introspect their custom 
statistics through SQL, and I don't believe there is currently a way to 
introspect builtin statistics either.

Additionally, there was no way to know how much shared memory each 
statistics kind was using. We could only know the total shared memory 
used by the statistics subsystem.

	dbltap@postgres=# SELECT * FROM pg_shmem_allocations WHERE name = 'Shared Memory Stats';
	        name         |    off    |  size  | allocated_size
	---------------------+-----------+--------+----------------
	 Shared Memory Stats | 153456640 | 321976 |         321976

In this patch, I have added a new system view: pg_stat_kind_info built 
on a new function pg_stat_get_kind_info(). The view has the following 
columns:

- id: id of the statistics kind
- name: name of the statistics kind
- count: number of entries for the statistics kind
- builtin: whether the statistics kind of builtin or not
- shared_size: shared memory size of each entry

	dbltap@postgres=# SELECT * FROM pg_stat_kind_info;
	 id |          name           | count  | builtin | shared_size
	----+-------------------------+--------+---------+-------------
	  1 | database                | (null) | t       |         288
	  2 | relation                | (null) | t       |         248
	  3 | function                | (null) | t       |          56
	  4 | replslot                | (null) | t       |         120
	  5 | subscription            | (null) | t       |         120
	  6 | backend                 | (null) | t       |        2952
	  7 | archiver                |      1 | t       |           0
	  8 | bgwriter                |      1 | t       |           0
	  9 | checkpointer            |      1 | t       |           0
	 10 | io                      |      1 | t       |           0
	 11 | lock                    |      1 | t       |           0
	 12 | slru                    |      1 | t       |           0
	 13 | wal                     |      1 | t       |           0
	 25 | test_custom_var_stats   |      0 | f       |          40
	 26 | test_custom_fixed_stats |      1 | f       |          56
	(15 rows)

I am not sure that shared_size is a good column name, and I needed to 
include pgstat_internal.h in pgstatfuncs.c to get everything working. 
I'm also curious to hear if anyone thinks there is other valuable 
information to expose.

[0]: https://github.com/postgres/postgres/commit/7949d9594582ab49dee221e1db1aa5401ace49d4

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)


Re: Add pg_stat_kind_info system view

От:
"Tristan Partin" <tristan@partin.io>
Дата:
Вложения:
Of course I forgot to attach the patch :D.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Re: Add pg_stat_kind_info system view

От:
"Tristan Partin" <tristan@partin.io>
Дата:
On Thu May 14, 2026 at 5:30 AM UTC, Michael Paquier wrote:
> On Thu, Apr 30, 2026 at 07:38:57PM +0000, Tristan Partin wrote:
>> Of course I forgot to attach the patch :D.
>
> Thanks for the patch.  That sounds like a good idea particularly for
> track_entry_count, because we have no real way to provide this
> information, which would be more valuable if an in-core stats kind has
> the idea to switch this flag to true in the future, and I don't really
> want all extensions to re-implement the same thing to access this
> information.
>
> +            if (info->track_entry_count)
> +            {
> +                values[2] = Int64GetDatum(pgstat_get_entry_count(kind));
> +            }
> +            else
> +            {
> +                nulls[2] = true;
> +            }
>
> Returning NULL if the flag is not set sounds sensible.  For
> fixed-numbered, fine by me for 1, so as it is possible to aggregate
> the full size counting with the fixed shmem size of each stats kind.

Awesome.

> +SELECT name, builtin FROM pg_stat_kind_info
> +  ORDER BY name COLLATE "C";
>
> This is not going to be stable if running installcheck on an instance
> where a custom kind is loaded, so let's restrict the query to check
> for built-in kinds.

I'll fix this in v2, which I will send after resolving more discussion. 
I had not considered this as a potential problem. I'll remove the 
builtin column from the SELECT and filter on builtin as suggested.

> I'd suggest to expand the data published to more fields and not only
> what's presented here, so as it becomes possible to do more SQL sanity
> checks with the stats kind info (same attribute name as the fields):
> - fixed_amount, where shared_size > 0 does not make sense.
> - snapshot_ctl_off and shared_ctl_off, that only makes sense under
> fixed_amount.  These should not be set for !fixed_amount.
> - pending_size, that should not be set for fixed_amount.
> - existence of flush_pending_cb, delete_pending_cb,
> reset_timestamp_cb, to_serialized_name, from_serialized_name,
> to_serialized_data, from_serialized_data (should be booleans), fine
> for !fixed_amount, never for fixed_amount.
> - existence of init_shmem_cb, reset_all_cb, snapshot_cb (should be
> booleans), fine for fixed_amount, never for !fixed_amount.

Can you share how someone might use this additional information? I can 
see some inherent value for additionally exposing:

- fixed_amount
- accessed_across_databases
- write_to_file
- snapshot_ctl_off
- shared_ctl_off
- shared_data_off
- shared_data_len
- pending_size

> I may be missing one or two things.  pgstat_internal.h documents all
> these requirements, the idea is to translate these requirements at SQL
> level.  We should definitely apply these checks for both custom and
> built-in stats kinds, which should save time for developers of pgstats
> in core and outside of core by detecting inconsistent patterns
> beforehand.

Not sure I understand why we would want to expose the existence of the 
callbacks to make assertions at the SQL level. I see that in 
pgstat_register_kind(), we have the following code:

	/*
	 * Check some data for fixed-numbered stats.
	 */
	if (kind_info->fixed_amount)
	{
		if (kind_info->shared_size == 0)
			ereport(ERROR,
					(errmsg("custom cumulative statistics property is invalid"),
					 errhint("Custom cumulative statistics require a shared memory size for fixed-numbered objects.")));
		if (kind_info->track_entry_count)
			ereport(ERROR,
					(errmsg("custom cumulative statistics property is invalid"),
					 errhint("Custom cumulative statistics cannot use entry count tracking for fixed-numbered objects.")));
	}

We could extend these invariant checks to make sure that the callbacks 
are only set when fixed_amount is true for instance. I am very much open 
to having my mind changed.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)


Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, May 28, 2026 at 05:11:42PM +0000, Tristan Partin wrote:
> Can you share how someone might use this additional information?
>
> Not sure I understand why we would want to expose the existence of the 
> callbacks to make assertions at the SQL level. I see that in 
> pgstat_register_kind(), we have the following code:
> 
> We could extend these invariant checks to make sure that the callbacks 
> are only set when fixed_amount is true for instance. I am very much open 
> to having my mind changed.

There is currently no internal mechanism to make sure that the
built-in stats kinds have a consistent setup in terms of flags and
callbacks set, so for developers we could immediately complain when
generating patches that add new stats kinds.  For custom stats kinds,
loaded libraries could have more cross-checks.

Perhaps it is not worth bothering at the end, and I'd be fine to keep
the data published as minimal as you see fit.  Still, fixed_amount,
write_to_file and accessed_across_databases feel like useful
additions.

If we keep shared_size, it may make sense to set it to NULL if we
don't know about it?  That's the case of the built-in fixed-sized
stats kinds.  We set the value for custom fixed-sized stats kinds.
--
Michael

Re: Add pg_stat_kind_info system view

От:
"Tristan Partin" <tristan@partin.io>
Дата:
On Mon Jun 1, 2026 at 5:41 AM UTC, Michael Paquier wrote:
> On Thu, May 28, 2026 at 05:11:42PM +0000, Tristan Partin wrote:
>> Can you share how someone might use this additional information?
>>
>> Not sure I understand why we would want to expose the existence of the 
>> callbacks to make assertions at the SQL level. I see that in 
>> pgstat_register_kind(), we have the following code:
>> 
>> We could extend these invariant checks to make sure that the callbacks 
>> are only set when fixed_amount is true for instance. I am very much open 
>> to having my mind changed.
>
> There is currently no internal mechanism to make sure that the
> built-in stats kinds have a consistent setup in terms of flags and
> callbacks set, so for developers we could immediately complain when
> generating patches that add new stats kinds.  For custom stats kinds,
> loaded libraries could have more cross-checks.

I think there is still some confusion on my end about this line of 
discussion.

> Perhaps it is not worth bothering at the end, and I'd be fine to keep
> the data published as minimal as you see fit.  Still, fixed_amount,
> write_to_file and accessed_across_databases feel like useful
> additions.

The additional columns are now added in v2. Note that I named 
write_to_file as written_to_file for the column name. I wonder if 
persisted would be a better name for the column and if persisted would 
be a better name for PgStat_KindInfo::write_to_file.

> If we keep shared_size, it may make sense to set it to NULL if we
> don't know about it?  That's the case of the built-in fixed-sized
> stats kinds.  We set the value for custom fixed-sized stats kinds.

I believe that I also captured this correctly in v2.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)


Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, Jun 04, 2026 at 04:59:00PM +0000, Tristan Partin wrote:
> I believe that I also captured this correctly in v2.

No v2 of the patch has been attached.  :D
--
Michael

Re: Add pg_stat_kind_info system view

От:
"Tristan Partin" <tristan@partin.io>
Дата:
Вложения:
On Mon Jun 8, 2026 at 12:16 AM UTC, Michael Paquier wrote:
> On Thu, Jun 04, 2026 at 04:59:00PM +0000, Tristan Partin wrote:
>> I believe that I also captured this correctly in v2.
>
> No v2 of the patch has been attached.  :D

I am not a very smart person! :facepalm:

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, Jun 18, 2026 at 04:54:36PM +0000, Tristan Partin wrote:
> On Mon Jun 8, 2026 at 12:16 AM UTC, Michael Paquier wrote:
>> On Thu, Jun 04, 2026 at 04:59:00PM +0000, Tristan Partin wrote:
>>> I believe that I also captured this correctly in v2.
>>
>> No v2 of the patch has been attached.  :D
> 
> I am not a very smart person! :facepalm:

Thanks for the patch.

A small comment that I have is related to the use of the unaligned
output of in stats.sql, where I doubt that we'll have a stats kind
named with more characters than the existing ones.  And there are a
bunch of bools, which would be easier to read if aligned.

"written_to_file" as attribute name of the SQL function is
inconsistent.  "write_to_file" would be.

The rest looks pretty much OK, seen from here.
--
Michael

Re: Add pg_stat_kind_info system view

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Wed, Jun 24, 2026 at 01:12:09PM +0900, Michael Paquier wrote:
> On Thu, Jun 18, 2026 at 04:54:36PM +0000, Tristan Partin wrote:
> > On Mon Jun 8, 2026 at 12:16 AM UTC, Michael Paquier wrote:
> >> On Thu, Jun 04, 2026 at 04:59:00PM +0000, Tristan Partin wrote:
> >>> I believe that I also captured this correctly in v2.
> >>
> >> No v2 of the patch has been attached.  :D

Thanks for the patch, that looks like an useful addition to me.

A few comments:

=== 1

+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -30,6 +30,8 @@
 #include "storage/procarray.h"
 #include "utils/acl.h"
 #include "utils/builtins.h"
+#include "utils/pgstat_internal.h"

But header in pgstat_internal.h mentions:

"
 * Definitions for the PostgreSQL cumulative statistics system that should
 * only be needed by files implementing statistics support (rather than ones
 * reporting / querying stats).

"

So it looks like that we are breaking this convention here. Maybe add helper
function(s)?

=== 2

+  /* For fixed-amount kinds, count is always 1. The entry is stored in
+   * PgStat_ShmemControl. If it is not a fixed-amount, then report the
+   * count of entries if tracked, or NULL if not tracked.
+   */

comment style is not right.

=== 3

+  /* For fixed-amount kinds, count is always 1.

Why not report NULL for fixed-amount kinds too? The track_entry_count field is
documented as being for variable-numbered stats only, so the count would only be
reported for custom variable-numbered kinds that set it to true.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Jun 24, 2026 at 05:31:12AM +0000, Bertrand Drouvot wrote:
> But header in pgstat_internal.h mentions:
> 
> "
>  * Definitions for the PostgreSQL cumulative statistics system that should
>  * only be needed by files implementing statistics support (rather than ones
>  * reporting / querying stats).
> 
> "
> 
> So it looks like that we are breaking this convention here. Maybe add helper
> function(s)?

Yeah.  Pulling pgstat_internal.h in pgstatfuncs.c is not great.  The
code acts as a barrier as the internal part and the fields data
retrieved by the functions.  A couple of alternatives I can think of:
- Put this new function in a new file under activity, named blankly
pgstat_kind.c.
- Move the necessary stuff out of pgstat_internal.h into a new header,
or just pgstat_kind.h with a !FRONTEND block.
- Use a new wrapper function that copies the data we need into an
intermediate structure.  This is just a new pgstat_get_kind_info().

Among these two, putting pg_stat_get_kind_info() into a new file feels
much better than splitting the contents of pgstat_internal.h, which is
kind of the central in-core shmem-side facility.  Using a wrapper as
of option 3 looks weird knowing the existence of the kind_info()
function.

Bertrand?  Tristan?
--
Michael

Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Tue, Jun 30, 2026 at 07:59:04AM +0000, Bertrand Drouvot wrote:
> I do agree. That said, creating a new file only for one function looks a bit
> weird (but I can not think of a better option though).

FWIW, I've done that in the past, when it was necessary.
--
Michael

Re: Add pg_stat_kind_info system view

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Hi,

Thanks for working on this. I like this overall, but have some comments.

1/

+       One row for each loaded statistics kind, showing metadata
about the kind.

The description says "showing metadata about the kind" but
the count  column returns live runtime data (via pgstat_get_entry_count()),
not just metadata. The description should acknowledge both.

2/

+        shared_size bigint
+       
+       
+        Size in bytes of a shared memory entry for this statistics kind.
+        NULL for built-in fixed-amount kinds, whose
+        single entry lives in a statically-allocated slot rather than a
+        sized shared memory entry.

I think we should just use info->shared_data_len for both fixed and
variable-length kinds. This shows the entry_size which is what an
extension developer will care about. Even info->shared_size for
variable-length kinds does not include the per-entry overhead,
so info->shared_data_len will be useful to understand if the size
of your entry changed after an upgrade, etc. Also, we should
rename it to "entry_size" instead of "shared_size"

3/

+        count bigint
+       
+       
+        Number of tracked entries for this kind. For fixed-amount
kinds, this is
+        always 1. For variable-numbered kinds, this is the number of objects
+        currently tracked. NULL if the kind does not track
+        entry counts.
+       
+      

I think this should be 0 for all kinds by default. Only ever > 0 for
variable-numbered
kinds with entry tracking enabled. No NULLs. The fields should be called
"entry_count" instead of "count". We should update the documentation to
reflect that as well. That seems easier to query than having mixed NULLs and
numbers.


Sami Imseih
Amazon Web Services (AWS)


Re: Add pg_stat_kind_info system view

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Wed, Jul 01, 2026 at 10:35:33AM +0900, Michael Paquier wrote:
> On Tue, Jun 30, 2026 at 05:36:57PM -0500, Sami Imseih wrote:
> > +        count bigint
> > +       
> > +       
> > +        Number of tracked entries for this kind. For fixed-amount
> > kinds, this is
> > +        always 1. For variable-numbered kinds, this is the number of objects
> > +        currently tracked. NULL if the kind does not track
> > +        entry counts.
> > +       
> > +      
> > 
> > I think this should be 0 for all kinds by default. Only ever > 0 for
> > variable-numbered
> > kinds with entry tracking enabled. No NULLs. The fields should be called
> > "entry_count" instead of "count". We should update the documentation to
> > reflect that as well. That seems easier to query than having mixed NULLs and
> > numbers.
> 
> IMO, in this case, NULL should be a synonym of "I don't know", which
> is what entry_count set to false means.  0 means "I know, there is no
> data".  I'd be OK with dropping the part about fixed-sized stats where
> we enforce 1, and use NULL instead, though.

+1, that's also my opinion [1].

[1]: https://postgr.es/m/ajtroNXfBS/ldtNZ%40bdtpg

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Add pg_stat_kind_info system view

От:
Sami Imseih <samimseih@gmail.com>
Дата:
> > > I think this should be 0 for all kinds by default. Only ever > 0 for
> > > variable-numbered
> > > kinds with entry tracking enabled. No NULLs. The fields should be called
> > > "entry_count" instead of "count". We should update the documentation to
> > > reflect that as well. That seems easier to query than having mixed NULLs and
> > > numbers.
> >
> > IMO, in this case, NULL should be a synonym of "I don't know", which
> > is what entry_count set to false means.  0 means "I know, there is no
> > data".  I'd be OK with dropping the part about fixed-sized stats where
> > we enforce 1, and use NULL instead, though.
>
> +1, that's also my opinion [1].
>
> [1]: https://postgr.es/m/ajtroNXfBS/ldtNZ%40bdtpg

I'm ok with that if others feel this is better.

--
Sami


Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Tue, Jun 30, 2026 at 11:17:29PM -0500, Sami Imseih wrote:
>>> IMO, in this case, NULL should be a synonym of "I don't know", which
>>> is what entry_count set to false means.  0 means "I know, there is no
>>> data".  I'd be OK with dropping the part about fixed-sized stats where
>>> we enforce 1, and use NULL instead, though.
>>
>> +1, that's also my opinion [1].
>
> I'm ok with that if others feel this is better.

Okay, thanks.  Let's do so then.  Let's also invent a new
pgstat_kind.c in activity/.

I have been chewing a bit on the comments from Sami, leading to the
following result:
- Switched shared_size to entry_size, for consistency with
entry_count, but I don't agree about the use of shared_data_len.
shared_size is more adapted to me because it has the entry overhead
and the shmem entry header.  That's more precise and one does not need
to guess the header size.
- Moved entry_count after entry_size in the list of attributes.
- written_to_file -> write_to_file, same as pgstat_internal.h.
- Moved the new function to a pgstat_kind.c.
- Some extra changes and tweaks to the comments, the docs, some code.

The attached should do all that, hopefully.  Any thoughts?
--
Michael

Re: Add pg_stat_kind_info system view

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Wed, Jul 01, 2026 at 04:07:06PM +0900, Michael Paquier wrote:
> On Tue, Jun 30, 2026 at 11:17:29PM -0500, Sami Imseih wrote:
> >>> IMO, in this case, NULL should be a synonym of "I don't know", which
> >>> is what entry_count set to false means.  0 means "I know, there is no
> >>> data".  I'd be OK with dropping the part about fixed-sized stats where
> >>> we enforce 1, and use NULL instead, though.
> >>
> >> +1, that's also my opinion [1].
> >
> > I'm ok with that if others feel this is better.
> 
> Okay, thanks.  Let's do so then.  Let's also invent a new
> pgstat_kind.c in activity/.
> 
> I have been chewing a bit on the comments from Sami, leading to the
> following result:
> - Switched shared_size to entry_size, for consistency with
> entry_count, but I don't agree about the use of shared_data_len.
> shared_size is more adapted to me because it has the entry overhead
> and the shmem entry header.  That's more precise and one does not need
> to guess the header size.
> - Moved entry_count after entry_size in the list of attributes.
> - written_to_file -> write_to_file, same as pgstat_internal.h.
> - Moved the new function to a pgstat_kind.c.
> - Some extra changes and tweaks to the comments, the docs, some code.
> 
> The attached should do all that, hopefully.  Any thoughts?

Yeah I also think it does all of that and LGTM.

Nit:

$ git show | grep -i loaded
+       One row for each loaded statistics kind, showing information about
+   loaded statistics kind, including both built-in and custom kinds.
+        shutdown and reloaded on startup, false if they are kept only in
+ * Get information about the statistics kinds loaded into the system.
+  descr => 'statistics: information about loaded statistics kinds',
+-- List of loaded statistics kinds.
+-- There should be at least one statistics kind loaded
+-- List of loaded statistics kinds.
+-- There should be at least one statistics kind loaded

s/loaded/registered/?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Add pg_stat_kind_info system view

От:
Sami Imseih <samimseih@gmail.com>
Дата:
> On Wed, Jul 01, 2026 at 04:07:06PM +0900, Michael Paquier wrote:
> > On Tue, Jun 30, 2026 at 11:17:29PM -0500, Sami Imseih wrote:
> > >>> IMO, in this case, NULL should be a synonym of "I don't know", which
> > >>> is what entry_count set to false means.  0 means "I know, there is no
> > >>> data".  I'd be OK with dropping the part about fixed-sized stats where
> > >>> we enforce 1, and use NULL instead, though.
> > >>
> > >> +1, that's also my opinion [1].
> > >
> > > I'm ok with that if others feel this is better.
> >
> > Okay, thanks.  Let's do so then.  Let's also invent a new
> > pgstat_kind.c in activity/.
> >
> > I have been chewing a bit on the comments from Sami, leading to the
> > following result:
> > - Switched shared_size to entry_size, for consistency with
> > entry_count, but I don't agree about the use of shared_data_len.
> > shared_size is more adapted to me because it has the entry overhead
> > and the shmem entry header.  That's more precise and one does not need
> > to guess the header size.

It still does not include PgStatShared_HashEntry which is 40-bytes per entry,
so it could add up if someone is calculating total consumption by a kind as
number_of_entries * size_of_entry. So that should also be accounted for, right?

But also, because this will be used to calculate consumption, we
should add a note in
the documentation to differentiate between live storage usage vs the
DSA footprint,
which will not be shrunk when entries are deleted. Someone may be confused
that their entries are much lower after deleting entries, but their memory
footprint is still high because the OS does not reclaim the free'd space.
What do you think?

--
Sami


Re: Add pg_stat_kind_info system view

От:
Sami Imseih <samimseih@gmail.com>
Дата:
> > But also, because this will be used to calculate consumption, we
> > should add a note in
> > the documentation to differentiate between live storage usage vs the
> > DSA footprint,
> > which will not be shrunk when entries are deleted. Someone may be confused
> > that their entries are much lower after deleting entries, but their memory
> > footprint is still high because the OS does not reclaim the free'd space.
> > What do you think?
>
> Hmm.  I am not completely sure which way is best here.  So, I think
> that I am just going to drop this field for now (entry_size as of
> latest patch), and keep the rest of the patch.  It's still useful to
> me, and we could always add one or more memory-related field later as
> we feel in this release cycle.

Right. I also think this needs more discussion, and it should not hold
up everything else.

--
Sami


Re: Add pg_stat_kind_info system view

От:
"Tristan Partin" <tristan@partin.io>
Дата:
Вложения:
On Thu Jul 2, 2026 at 12:37 AM UTC, Michael Paquier wrote:
> On Wed, Jul 01, 2026 at 07:26:23PM -0500, Sami Imseih wrote:
>> Right. I also think this needs more discussion, and it should not hold
>> up everything else.
>
> Just done this way now as of 3b066de6c0a1, with more more adjustments,
> and without the sizing parts.

Thanks for committing Michael, and thanks for the reviews Bertrand and 
Sami. Here is a patch that add the aforementioned entry_size column. It 
is definitely needs further discussion. I am not entirely sure that 
I see the value of using PgStat_KindInfo::shared_size for this column, 
so I used PgStat_KindInfo::shared_data_len instead via 
pgstat_get_entry_len(). My reasoning for choosing so is:

- An argument against shared_size is that I think trying to match up
  pg_stat_kind_info with pg_shmem_allocations will not work well because 
  we will miss the additional hash table overhead
- Additionally, a few builtin stats don't even report a shared_size 
  (bgwriter, archiver, checkpointer, etc.)

Looking forward to see what other ideas or reasonings you all might 
propose.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Fri, Jul 17, 2026 at 11:58:40AM -0500, Sami Imseih wrote:
> As I noted earlier in this thread, my concern is that this value will be
> misused to estimate how much memory a kind uses, since it excludes overhead.
> The per-entry comment notes this, but I think the docs should also warn that
> entry_count and entry_size together do not give an accurate measure of a
> kind's total memory use, and that the DSA footprint is not shrunk when entries
> are deleted (the OS does not reclaim the freed space, so someone may be
> confused that their memory footprint stays high after deleting entries).

Would it help the monitoring purpose of the change if we began using
GetNamedDSHash() when setting up the pgstats shared hash table?  That
would give a way to monitor the sizing of the table through
pg_dsm_registry_allocations, at least, tackling your concerns about
the imprecision of the data if we put a memory sizing field in
pg_stat_kind_info?
--
Michael

Re: Add pg_stat_kind_info system view

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Mon, Aug 03, 2026 at 04:51:56PM -0500, Sami Imseih wrote:
> Where this may become questionable is because there is slightly more
> locking overhead if we go with the registry approach. The registry path goes
> through more locking during backend startup (attaching to the
> registry's own DSA,
> looking up the entry, then attaching to the pgstat DSA) compared to the
> current code which does a single dsa_attach_in_place at a known address.
> With a high connection churn benchmark, using -C
> 
> ```
> pgbench -C -c $clients -T 10 -n -f bench.sql
> ```
> 
> 35-40 microseconds, but this is an extreme case of high connection churn.

I am not troubled by these numbers myself; that's barely noticeable.
But I have to admit that some folks around here would likely complain
if we change that, so I would live the optimization in place, and just
give up on the idea.  Sad, but well, I like the concept of a peaceful
life if I can.

Saying that.  Do you think that there could be a path forward where we
could optimize the registry locking, benefiting everybody who uses
this API?  Just trying to think about all the sides of the coin.
--
Michael
FAQ