Hi,
While working on something nearby, I observed that if a custom
pgstats kind is not registered while the on-disk pgstats.stat file
contains entries for this custom kind, it results in all of the stats,
including the built-in ones, being discarded.
This can happen if you initially register this custom kind, cleanly
shut down the database, then start it up without the kind being
registered (i.e. not including the extension that loads this custom
kind in shared_preload_libraries).
This can be observed in 001_stat.pl in the injection_points tests,
where the last $node->start shows a corrupted pgstat.stat file
message because "injection_points_fixed" and "injection_points"
were removed from shared_preload_libraries after a clean shutdown.
```
2025-10-20 18:38:48.664 UTC startup[93662] LOG: database system was
shut down at 2025-10-20 18:38:48 UTC
2025-10-20 18:38:48.665 UTC startup[93662] WARNING: could not find
information of kind 26 for entry of type F
2025-10-20 18:38:48.665 UTC startup[93662] LOG: corrupted statistics
file "pg_stat/pgstat.stat"
2025-10-20 18:38:48.666 UTC postmaster[93643] LOG: database system is
ready to accept connections
2025-10-20 18:38:48.750 UTC postmaster[93643] LOG: received immediate
shutdown request
2025-10-20 18:38:48.757 UTC postmaster[93643] LOG: database system is shut down
```
While I do not think this is a bug, I also do not think this is not the
ideal behavior. Unlike built-in stats, custom stats are not guaranteed
to be registered, and completely resetting all stats in that edge case
seems too strict.
I think this can be better handled by tracking the lengths of the
kinds in the pgstat.stat file when saving, and re-reading the lengths
on startup. This would allow skipping past any unrecognized custom
data. We need to save the lengths since we have no way to know how
much to skip when re-reading. The data can be saved at the start of
the file, after the FORMAT, so the extra space required is minimal.
With the attached patch, this is the warning message instead.
```
2025-10-20 18:25:51.763 UTC startup[47833] LOG: database system was
shut down at 2025-10-20 18:25:51 UTC
2025-10-20 18:25:51.764 UTC startup[47833] WARNING: found unregistered
custom stats kind 26 for entry of type F, skipping
2025-10-20 18:25:51.765 UTC postmaster[47814] LOG: database system is
ready to accept connections
2025-10-20 18:25:51.848 UTC postmaster[47814] LOG: received immediate
shutdown request
2025-10-20 18:25:51.855 UTC postmaster[47814] LOG: database system is shut down
```
Thoughts?
--
Sami Imseih
Amazon Web Services (AWS)