Prevent pgstats from getting confused when relkind of a relation changes
authorAndres Freund <[email protected]>
Sat, 3 Dec 2022 01:50:26 +0000 (17:50 -0800)
committerAndres Freund <[email protected]>
Sat, 3 Dec 2022 02:13:40 +0000 (18:13 -0800)
commit7944d2d8c78b190908e99b4dc1719d7dca83091b
treee79b2af7d00c40a5d773f53939909bc7ece5e254
parenta844052b5d97c351ba800e51e37b833f93863c29
Prevent pgstats from getting confused when relkind of a relation changes

When the relkind of a relache entry changes, because a table is converted into
a view, pgstats can get confused in 15+, leading to crashes or assertion
failures.

For HEAD, Tom fixed this in b23cd185fd5, by removing support for converting a
table to a view, removing the source of the inconsistency. This commit just
adds an assertion that a relcache entry's relkind does not change, just in
case we end up with another case of that in the future. As there's no cases of
changing relkind anymore, we can't add a test that that's handled correctly.

For 15, fix the problem by not maintaining the association with the old pgstat
entry when the relkind changes during a relcache invalidation processing. In
that case the pgstat entry needs to be unlinked first, to avoid
PgStat_TableStatus->relation getting out of sync. Also add a test reproducing
the issues.

No known problem exists in 11-14, so just add the test there.

Reported-by: vignesh C <[email protected]>
Author: Andres Freund <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
Discussion: https://postgr.es/m/CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
Backpatch: 15-
src/test/regress/expected/create_view.out
src/test/regress/sql/create_view.sql