RE: prevent immature WAL streaming
Jakub Wartak <jakub.wartak@tomtom.com>
Hi Álvaro, -hackers,
> I attach the patch with the change you suggested.
I've gave a shot to to the v02 patch on top of REL_12_STABLE (already including 5065aeafb0b7593c04d3bc5bc2a86037f32143fc). Previously(yesterday) without the v02 patch I was getting standby corruption always via simulation by having separate /pg_xlog dedicated fs, and archive_mode=on, wal_keep_segments=120, archive_command set to rsync to different dir on same fs, wal_init_zero at default(true).
Today (with v02) I've got corruption in only initial 2 runs out of ~ >30 tries on standby. Probably the 2 failures were somehow my fault (?) or some rare condition (and in 1 of those 2 cases simply restarting standby did help). To be honest I've tried to force this error, but with v02 I simply cannot force this error anymore, so that's good! :)
> I didn't have a lot of luck with a reliable reproducer script. I was able to
> reproduce the problem starting with Ryo Matsumura's script and attaching
> a replica; most of the time the replica would recover by restarting from a
> streaming position earlier than where the problem occurred; but a few
> times it would just get stuck with a WAL segment containing a bogus
> record.
In order to get reliable reproducer and get proper the fault injection instead of playing with really filling up fs, apparently one could substitute fd with fd of /dev/full using e.g. dup2() so that every write is going to throw this error too:
root@hive:~# ./t & # simple while(1) { fprintf() flush () } testcase
root@hive:~# ls -l /proc/27296/fd/3
lrwx------ 1 root root 64 Aug 25 06:22 /proc/27296/fd/3 -> /tmp/testwrite
root@hive:~# gdb -q -p 27296
-- 1089 is bitmask O_WRONLY|..
(gdb) p dup2(open("/dev/full", 1089, 0777), 3)
$1 = 3
(gdb) c
Continuing.
==>
fflush/write(): : No space left on device
So I've also tried to be malicious while writing to the DB and inject ENOSPCE near places like:
a) XLogWrite()->XLogFileInit() near line 3322 // assuming: if (wal_init_zero) is true, one gets classic "PANIC: could not write to file "pg_wal/xlogtemp.90670": No space left on device"
b) XLogWrite() near line 2547 just after pg_pwrite // one can get "PANIC: could not write to log file 000000010000003B000000A8 at offset 0, length 15466496: No space left on device" (that would be possible with wal_init_zero=false?)
c) XLogWrite() near line 2592 // just before issue_xlog_fsync to get "PANIC: could not fdatasync file "000000010000004300000004": Invalid argument" that would pretty much mean same as above but with last possible offset near end of WAL?
This was done with gdb voodoo:
handle SIGUSR1 noprint nostop
break xlog.c:<LINE> // https://github.com/postgres/postgres/blob/REL_12_STABLE/src/backend/access/transam/xlog.c#L3311
c
print fd or openLogFile -- to verify it is 3
p dup2(open("/dev/full", 1089, 0777), 3) -- during most of walwriter runtime it has current log as fd=3
After restarting master and inspecting standby - in all of those above 3 cases - the standby didn't inhibit the "invalid contrecord length" at least here, while without corruption this v02 patch it is notorious. So if it passes the worst-case code review assumptions I would be wondering if it shouldn't even be committed as it stands right now.
-J.
Commits
-
Fix determination of broken LSN in OVERWRITTEN_CONTRECORD
- ef41c3fd6c86 13.6 landed
- d24dac954906 14.2 landed
- 9c6ab1ef3382 12.10 landed
- 72cf39d51ade 10.20 landed
- 44bd3ed332d6 15.0 landed
- 2c3fddcbbd8a 11.15 landed
-
Fix instability in 026_overwrite_contrecord.pl test.
- b66767b56b1c 15.0 landed
- 9959a078f2d7 12.10 landed
- 5a3240cf6b2e 13.6 landed
- 4dc2cb74d00e 11.15 landed
- 3aa858c8932e 14.2 landed
- 18ce21768a80 10.20 landed
-
Fix typo
- f914b8badc21 12.9 landed
- c3bda112ebe2 10.19 landed
- 7c0a78f08998 13.5 landed
- 3aa572b8b296 11.14 landed
- 9b6194eb0aa5 9.6.24 landed
-
Change recently added test code for stability
- 79c7fe1af82d 14.1 landed
- 6287b8e195f3 11.14 landed
- 5ec87619bc5d 9.6.24 landed
- 4b7abbe48a11 12.9 landed
- 41cce2326135 10.19 landed
- 2cdf97fd1ec8 13.5 landed
- 010e5233733a 15.0 landed
-
Remove unstable, unnecessary test; fix typo
- d186d233dfde 15.0 landed
- 0ce67bce0071 14.1 landed
-
Repair two portability oversights of new test
- e3731bac52cf 14.1 landed
- d03bca4d70c2 15.0 landed
-
Fix WAL replay in presence of an incomplete record
- 64a8687a6891 14.1 landed
- ff9f111bce24 15.0 landed
- d9fe2cc7dd3e 10.19 landed
- cfedb279a6b8 11.14 landed
- 1df0a914d58f 12.9 landed
- 1d97d3d0867f 13.5 landed
- 148c6ee3be39 9.6.24 landed
-
Document XLOG_INCLUDE_XID a little better
- c1d1ae1db237 14.0 landed
- ade24dab97a2 15.0 landed
-
Revert "Avoid creating archive status ".ready" files too early"
- e456167a8af4 12.9 landed
- c801c3a1ff73 11.14 landed
- aa8bd0890bb2 14.0 landed
- 9aaf7dbff36f 10.19 landed
- 96b665083eb7 15.0 landed
- 518621c40b23 13.5 landed
- 121cdaff1068 9.6.24 landed
-
Avoid creating archive status ".ready" files too early
- 5065aeafb0b7 12.9 cited
-
Fix scenario where streaming standby gets stuck at a continuation record.
- 066871980183 11.0 cited
-
Teach xlogreader to follow timeline switches
- 1148e22a82ed 10.0 cited
-
Forbid using pg_xlogfile_name() and pg_xlogfile_name_offset() during
- 370f770c15a4 9.0.0 cited