pgsql: Fix possible recovery trouble if TRUNCATE overlaps a checkpoint.

Robert Haas <rhaas@postgresql.org>

From: Robert Haas <rhaas@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Date: 2022-03-24T19:32:54Z
Lists: pgsql-hackers
Fix possible recovery trouble if TRUNCATE overlaps a checkpoint.

If TRUNCATE causes some buffers to be invalidated and thus the
checkpoint does not flush them, TRUNCATE must also ensure that the
corresponding files are truncated on disk. Otherwise, a replay
from the checkpoint might find that the buffers exist but have
the wrong contents, which may cause replay to fail.

Report by Teja Mupparti. Patch by Kyotaro Horiguchi, per a design
suggestion from Heikki Linnakangas, with some changes to the
comments by me. Review of this and a prior patch that approached
the issue differently by Heikki Linnakangas, Andres Freund, Álvaro
Herrera, Masahiko Sawada, and Tom Lane.

Discussion: http://postgr.es/m/BYAPR06MB6373BF50B469CA393C614257ABF00@BYAPR06MB6373.namprd06.prod.outlook.com

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/access/transam/multixact.c  |  6 +++---
src/backend/access/transam/twophase.c   | 12 ++++++-----
src/backend/access/transam/xact.c       |  5 +++--
src/backend/access/transam/xlog.c       | 16 ++++++++++++--
src/backend/access/transam/xloginsert.c |  2 +-
src/backend/catalog/storage.c           | 29 +++++++++++++++++++++++++-
src/backend/storage/buffer/bufmgr.c     |  6 ++++--
src/backend/storage/ipc/procarray.c     | 26 ++++++++++++++++-------
src/backend/storage/lmgr/proc.c         |  4 ++--
src/include/storage/proc.h              | 37 ++++++++++++++++++++++++++++++++-
src/include/storage/procarray.h         |  5 +++--
11 files changed, 120 insertions(+), 28 deletions(-)

Commits

  1. Rethink the delay-checkpoint-end mechanism in the back-branches.

  2. Rename delayChkpt to delayChkptFlags.

  3. Fix possible recovery trouble if TRUNCATE overlaps a checkpoint.