Prevent restore of incremental backup from bloating VM fork.

When I (rhaas) wrote the WAL summarizer code, I incorrectly believed
that XLOG_SMGR_TRUNCATE truncates all forks to the same length.  In
fact, what other parts of the code do is compute the truncation length
for the FSM and VM forks from the truncation length used for the main
fork. But, because I was confused, I coded the WAL summarizer to set the
limit block for the VM fork to the same value as for the main fork.
(Incremental backup always copies FSM forks in full, so there is no
similar issue in that case.)

Doing that doesn't directly cause any data corruption, as far as I can
see. However, it does create a serious risk of consuming a large amount
of extra disk space, because pg_combinebackup's reconstruct.c believes
that the reconstructed file should always be at least as long as the
limit block value. We might want to be smarter about that at some point
in the future, because it's always safe to omit all-zeroes blocks at the
end of the last segment of a relation, and doing so could save disk
space, but the current algorithm will rarely waste enough disk space to
worry about unless we believe that a relation has been truncated to a
length much longer than its actual length on disk, which is exactly what
happens as a result of the problem mentioned in the previous paragraph.

To fix, create a new visibilitymap helper function and use it to include
the right limit block in the summary files. Incremental backups taken
with existing summary files will still have this issue, but this should
improve the situation going forward.

Diagnosed-by: Oleg Tkachenko <[email protected]>
Diagnosed-by: Amul Sul <[email protected]>
Discussion: 
http://postgr.es/m/caaj_b97pqg89hvpnj8cgwmk94gj9kof_plsowuyqgzgjy32...@mail.gmail.com
Discussion: http://postgr.es/m/6897DAF7-B699-41BF-A6FB-B818FCFFD585%40gmail.com
Backpatch-through: 17

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/ffc226ab64d4ebdb089c278396d5df3d0a3f83b9

Modified Files
--------------
src/backend/access/heap/visibilitymap.c         | 17 +++++++++++++++++
src/backend/postmaster/walsummarizer.c          |  4 +++-
src/bin/pg_combinebackup/t/011_ib_truncation.pl | 24 +++++++++++++++++++++---
src/include/access/visibilitymap.h              |  1 +
4 files changed, 42 insertions(+), 4 deletions(-)

Reply via email to