postgresql/src/backend
Alvaro Herrera 50e3ed8e91
Fix replay of create database records on standby
Crash recovery on standby may encounter missing directories when
replaying create database WAL records.  Prior to this patch, the standby
would fail to recover in such a case.  However, the directories could be
legitimately missing.  Consider a sequence of WAL records as follows:

    CREATE DATABASE
    DROP DATABASE
    DROP TABLESPACE

If, after replaying the last WAL record and removing the tablespace
directory, the standby crashes and has to replay the create database
record again, the crash recovery must be able to move on.

This patch adds a mechanism similar to invalid-page tracking, to keep a
tally of missing directories during crash recovery.  If all the missing
directory references are matched with corresponding drop records at the
end of crash recovery, the standby can safely continue following the
primary.

Backpatch to 13, at least for now.  The bug is older, but fixing it in
older branches requires more careful study of the interactions with
commit e6d8069522, which appeared in 13.

A new TAP test file is added to verify the condition.  However, because
it depends on commit d6d317dbf6, it can only be added to branch
master.  I (Álvaro) manually verified that the code behaves as expected
in branch 14.  It's a bit nervous-making to leave the code uncovered by
tests in older branches, but leaving the bug unfixed is even worse.
Also, the main reason this fix took so long is precisely that we
couldn't agree on a good strategy to approach testing for the bug, so
perhaps this is the best we can do.

Diagnosed-by: Paul Guo <paulguo@gmail.com>
Author: Paul Guo <paulguo@gmail.com>
Author: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Author: Asim R Praveen <apraveen@pivotal.io>
Discussion: https://postgr.es/m/CAEET0ZGx9AvioViLf7nbR_8tH9-=27DN5xWJ2P9-ROH16e4JUA@mail.gmail.com
2022-03-25 13:16:21 +01:00
..
access Fix replay of create database records on standby 2022-03-25 13:16:21 +01:00
bootstrap Skip WAL for new relfilenodes, under wal_level=minimal. 2020-04-04 12:25:34 -07:00
catalog Fix possible recovery trouble if TRUNCATE overlaps a checkpoint. 2022-03-24 14:36:06 -04:00
commands Fix replay of create database records on standby 2022-03-25 13:16:21 +01:00
executor Revert applying column aliases to the output of whole-row Vars. 2022-03-17 18:18:05 -04:00
foreign Update copyrights for 2020 2020-01-01 12:21:45 -05:00
jit Back-patch LLVM 14 API changes. 2022-03-16 11:41:13 +13:00
lib Fix typo in comment 2021-04-20 14:36:41 +02:00
libpq Allow root-owned SSL private keys in libpq, not only the backend. 2022-03-02 11:57:02 -05:00
main Add PostgreSQL home page to --help output 2020-02-28 13:12:21 +01:00
nodes Fix assorted missing logic for GroupingFunc nodes. 2022-03-21 17:44:29 -04:00
optimizer Fix assorted missing logic for GroupingFunc nodes. 2022-03-21 17:44:29 -04:00
parser Fix failure to validate the result of select_common_type(). 2022-01-29 11:41:12 -05:00
partitioning Avoid using ambiguous word "non-negative" in error messages. 2021-07-28 01:21:52 +09:00
po Translation updates 2022-02-07 13:36:22 +01:00
port Reject huge_pages=on if shared_memory_type=sysv. 2021-10-26 13:04:40 +13:00
postmaster Fix waiting in RegisterSyncRequest(). 2022-03-16 15:37:15 +13:00
regex Make pg_regexec() robust against out-of-range search_start. 2021-09-11 15:19:49 -04:00
replication Fix publish_as_relid with multiple publications 2022-03-16 18:14:33 +01:00
rewrite Fix rewriter to set hasModifyingCTE correctly on rewritten queries. 2021-09-08 12:05:43 -04:00
snowball Update copyrights for 2020 2020-01-01 12:21:45 -05:00
statistics Build inherited extended stats on partitioned tables 2022-01-15 19:14:00 +01:00
storage Fix possible recovery trouble if TRUNCATE overlaps a checkpoint. 2022-03-24 14:36:06 -04:00
tcop Suppress warning about stack_base_ptr with late-model GCC. 2022-02-17 22:45:34 -05:00
tsearch Don't leak compiled regex(es) when an ispell cache entry is dropped. 2021-03-18 21:44:43 -04:00
utils Don't try to translate NULL in GetConfigOptionByNum(). 2022-03-23 13:18:00 -07:00
.gitignore
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
Makefile Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nls.mk Translation updates 2020-09-14 13:14:53 +02:00