pgsql: Fix data loss in wal_level=minimal crash recovery of CREATE TABL

From: Noah Misch <noah(at)leadboat(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix data loss in wal_level=minimal crash recovery of CREATE TABL
Date: 2021-08-28 06:51:55
Message-ID: E1mJsC3-0002S2-SM@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix data loss in wal_level=minimal crash recovery of CREATE TABLESPACE.

If the system crashed between CREATE TABLESPACE and the next checkpoint,
the result could be some files in the tablespace unexpectedly containing
no rows. Affected files would be those for which the system did not
write WAL; see the wal_skip_threshold documentation. Before v13, a
different set of conditions governed the writing of WAL; see v12's
<sect2 id="populate-pitr">. (The v12 conditions were broader in some
ways and narrower in others.) Users may want to audit non-default
tablespaces for unexpected short files. The bug could have truncated an
index without affecting the associated table, and reindexing the index
would fix that particular problem.

This fixes the bug by making create_tablespace_directories() more like
TablespaceCreateDbspace(). create_tablespace_directories() was
recursively removing tablespace contents, reasoning that WAL redo would
recreate everything removed that way. That assumption holds for other
wal_level values. Under wal_level=minimal, the old approach could
delete files for which no other copy existed. Back-patch to 9.6 (all
supported versions).

Reviewed by Robert Haas and Prabhat Sahu. Reported by Robert Haas.

Discussion: https://postgr.es/m/CA+TgmoaLO9ncuwvr2nN-J4VEP5XyAcy=zKiHxQzBbFRxxGxm0w@mail.gmail.com

Branch
------
REL_13_STABLE

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

Modified Files
--------------
src/backend/commands/tablespace.c | 42 +++++++++++++++------------------
src/test/recovery/t/018_wal_optimize.pl | 31 ++++++++++++++++++++----
2 files changed, 46 insertions(+), 27 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alvaro Herrera 2021-08-28 15:48:49 pgsql: psql \dP: reference regclass with "pg_catalog." prefix
Previous Message Tom Lane 2021-08-27 23:53:29 pgsql: Count SP-GiST index scans in pg_stat statistics.