Fix memory lifetime issues of replication slot stats.
authorAndres Freund <andres@anarazel.de>
Wed, 17 Mar 2021 23:18:37 +0000 (16:18 -0700)
committerAndres Freund <andres@anarazel.de>
Wed, 17 Mar 2021 23:21:46 +0000 (16:21 -0700)
commit5f79580ad69f6e696365bdc63bc265f45bd77211
treee00826de1ff309ad8bddbdec330131058209ff4d
parent70945649d734d16be22c3d1d90dd8c3d3c1e9d89
Fix memory lifetime issues of replication slot stats.

When accessing replication slot stats, introduced in 98681675002d,
pgstat_read_statsfiles() reads the data into newly allocated
memory. Unfortunately the current memory context at that point is the
callers, leading to leaks and use-after-free dangers.

The fix is trivial, explicitly use pgStatLocalContext. There's some
potential for further improvements, but that's outside of the scope of
this bugfix.

No backpatch necessary, feature is only in HEAD.

Author: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i@alap3.anarazel.de
contrib/test_decoding/expected/stats.out
contrib/test_decoding/sql/stats.sql
src/backend/postmaster/pgstat.c