postgresql/src/backend/utils
Andres Freund 06dbd619bf pgstat: Prevent stats reset from corrupting slotname by removing slotname
Previously PgStat_StatReplSlotEntry contained the slotname, which was mainly
used when writing out the stats during shutdown, to identify the slot in the
serialized data (at runtime the index in ReplicationSlotCtl->replication_slots
is used, but that can change during a restart). Unfortunately the slotname was
overwritten when the slot's stats were reset.

That turned out to only cause "real" problems if the slot was active during
the reset, triggering an assertion failure at the next
pgstat_report_replslot(). In other paths the stats were re-initialized during
pgstat_acquire_replslot().

Fix this by removing slotname from PgStat_StatReplSlotEntry. Instead we can
get the slot's name from the slot itself. Besides fixing a bug, this also is
architecturally cleaner (a name is not really statistics). This is safe
because stats, for a slot removed while shut down, will not be restored at
startup.

In 15 the slotname is not removed, but renamed, to avoid changing the stats
format. In master, bump PGSTAT_FILE_FORMAT_ID.

This commit does not contain a test for the fix. I think this can only be
tested by a tap test starting pg_recvlogical in the background and checking
pg_recvlogical's output. That type of test is notoriously hard to be reliable,
so committing it shortly before the release is wrapped seems like a bad idea.

Reported-by: Jaime Casanova <jcasanov@systemguards.com.ec>
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Discussion: https://postgr.es/m/YxfagaTXUNa9ggLb@ahch-to
Backpatch: 15-, where the bug was introduced in 5891c7a8ed
2022-10-08 09:43:29 -07:00
..
activity pgstat: Prevent stats reset from corrupting slotname by removing slotname 2022-10-08 09:43:29 -07:00
adt Use fabsf() instead of Abs() or fabs() where appropriate 2022-10-08 13:43:26 +02:00
cache Revert 56-bit relfilenode change and follow-up commits. 2022-09-28 09:55:28 -04:00
error meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
fmgr meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
hash meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
init Introduce SYSTEM_USER 2022-09-29 15:05:40 +09:00
mb meson: Add windows resource files 2022-10-05 09:56:05 -07:00
misc Remove unnecessary uses of Abs() 2022-10-07 13:29:33 +02:00
mmgr Improve our ability to detect bogus pointers passed to pfree et al. 2022-10-06 21:24:00 -04:00
resowner Allow callback functions to deregister themselves during a call. 2022-09-28 11:23:27 -04:00
sort meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
time meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
.gitignore Rearrange makefile rules for running Gen_fmgrtab.pl. 2018-05-03 17:54:18 -04:00
errcodes.txt Add another SQL/JSON error code 2022-07-18 14:26:43 +02:00
Gen_dummy_probes.pl Update copyright for 2022 2022-01-07 19:04:57 -05:00
Gen_dummy_probes.pl.prolog Update copyright for 2022 2022-01-07 19:04:57 -05:00
Gen_dummy_probes.sed Update copyright for 2022 2022-01-07 19:04:57 -05:00
Gen_fmgrtab.pl Update copyright for 2022 2022-01-07 19:04:57 -05:00
generate-errcodes.pl Add output file argument to generate-errcodes.pl 2022-07-18 12:24:35 -07:00
Makefile Clean up temp file from refactored dtrace rule 2022-07-19 07:31:58 +02:00
meson.build meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
postprocess_dtrace.sed Refactor dtrace postprocessing make rules 2022-07-18 12:33:02 -07:00
probes.d Update copyright for 2022 2022-01-07 19:04:57 -05:00
README.Gen_dummy_probes Tweak generation of Gen_dummy_probes.pl 2021-05-11 20:02:02 -04:00

# Generating dummy probes

If Postgres isn't configured with dtrace enabled, we need to generate
dummy probes for the entries in probes.d, that do nothing.

This is accomplished in Unix via the sed script `Gen_dummy_probes.sed`. We
used to use this in MSVC builds using the perl utility `psed`, which mimicked
sed. However, that utility disappeared from Windows perl distributions and so
we converted the sed script to a perl script to be used in MSVC builds.

We still keep the sed script as the authoritative source for generating
these dummy probes because except on Windows perl is not a hard requirement
when building from a tarball.

So, if you need to change the way dummy probes are generated, first change
the sed script, and when it's working generate the perl script. This can
be accomplished by using the perl utility s2p.

s2p is no longer part of the perl core, so it might not be on your system,
but it is available on CPAN and also in many package systems. e.g.
on Fedora it can be installed using `cpan App::s2p` or
`dnf install perl-App-s2p`.

The Makefile contains a recipe for regenerating Gen_dummy_probes.pl, so all
you need to do is once you have s2p installed is `make Gen_dummy_probes.pl`
Note that in a VPATH build this will generate the file in the vpath tree,
not the source tree.