Fix recovery conflict canceling the startup progress timeout

Поиск
Список
Период
Сортировка
Искать

Fix recovery conflict canceling the startup progress timeout

От:
Aidar Imamov <imamovaj22@gmail.com>
Дата:
Вложения:
Hi,

This came up while reviewing the startup progress timeout thread from
Fujii [1]. It is a separate, pre-existing thing, so following a
suggestion in that thread I am splitting it out into its own thread and
commitfest entry. The patch is attached.

The problem: a single recovery conflict stops "redo in progress"
messages for the rest of WAL replay, even though replay continues
normally.

ResolveRecoveryConflictWithLock() and
ResolveRecoveryConflictWithBufferPin() both set up their own timeouts
(STANDBY_LOCK_TIMEOUT / STANDBY_TIMEOUT and STANDBY_DEADLOCK_TIMEOUT),
wait, and then clean up with:

    disable_all_timeouts(false);

That clears every timeout the process has, not only the ones these
functions enabled.  The comment above it says the startup process is
assumed not to have any other outstanding timeouts, which was true when
the calls were written.  It stopped being true in 15, when
STARTUP_PROGRESS_TIMEOUT was added by 9ce346eabf3.

begin_startup_progress_phase() is called once, right before the redo
loop.  If a conflict happens later, the progress timeout is canceled
along with the conflict timeouts and nothing ever re-arms it, so
ereport_startup_progress() never fires again.

Where it is visible: only when progress is actually reported and clients
can run queries at the same time. PITR with
recovery.signal and hot_standby=on, after consistency is reached.  That
is exactly the setup where people connect to check the data before
letting recovery finish, and where a long SELECT can block replay.

I reproduced it locally with recovery.signal, hot_standby=on, a slow
restore_command with a read-only query holding
ACCESS SHARE on a table that the WAL stream takes ACCESS EXCLUSIVE on:

  no conflict:  22 "redo in progress" messages, up to "redo done"
  one conflict:  7 messages, then silence for the last time of redo

Redo itself completes and the data is correct; only the log messages
disappear.  But someone watching a long recovery sees the progress stop
and can easily conclude it is stuck.

The patch cancels exactly the timeouts each function enables:

  ResolveRecoveryConflictWithLock():
      disable_timeout(STANDBY_LOCK_TIMEOUT, false);
      disable_timeout(STANDBY_DEADLOCK_TIMEOUT, false);

  ResolveRecoveryConflictWithBufferPin():
      disable_timeout(STANDBY_TIMEOUT, false);
      disable_timeout(STANDBY_DEADLOCK_TIMEOUT, false);

This looks like a backpatch candidate to 15 and later, since the bug is
there from the moment the progress timeout exists.

[1] https://www.postgresql.org/message-id/flat/CAHGQGwFJixYTymPZ6dwOO-G0VdjO1AgZTma6CtJgc0Ptxfz3BA@mail.gmail.com

Regards,
Aidar Imamov

Re: Fix recovery conflict canceling the startup progress timeout

От:
Ritesh Kumar <opensource@rtshkmr.com>
Дата:
Вложения:
Hi Aidar,

Thanks for this patch, I agree with the PITR example and believe that
flow is a common instinct that grounds the importance of this fix.

> This looks like a backpatch candidate to 15 and later, since the bug
> is there from the moment the progress timeout exists.

You’re right:

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 branch, @ commit           path(s) tested  method              

────────────────────────────────────────────────────────────────
 REL_15_STABLE 06428115017  lock            manually (Autoconf) 

 REL_16_STABLE 1e9c734c26a  lock            manually (Meson)    

 REL_17_STABLE 25b55788d89  lock + pin      TAP test (Autoconf) 

 REL_18_STABLE 9d0081c01f9  lock + pin      TAP test (Autoconf) 

 REL_19_STABLE ee189838f94  lock + pin      TAP test (Autoconf) 

 master cea96cdfecb         lock + pin      TAP test (Meson)    

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Table 1: findings across branches

The problem reproduced on all, and fixed by v1 on all. v1 also applies
cleanly to 15-19 with a cherry-pick.

For 17-master, I used a modification of 031_recovery_conflict.pl which
tested both the lock- and pin-paths. Couldn’t get TAP section running on
16, so it was manually tested.

I believe your fix is correct and have left some minor comments that
shouldn’t block this patch.

Attached v2-0001 is your v1, unchanged; v2-0002 adds the test so cfbot
runs it alongside the fix. Without 0001, the two “progress reported
after the conflict” checks fail.

This is my first review, so any feedback on how I may improve would be
great too!


Code Trace
══════════

> The problem: a single recovery conflict stops “redo in progress”
> messages for the rest of WAL replay, even though replay continues
> normally.

> ResolveRecoveryConflictWithLock() and
> ResolveRecoveryConflictWithBufferPin() both set up their own
> timeouts (STANDBY_LOCK_TIMEOUT / STANDBY_TIMEOUT and
> STANDBY_DEADLOCK_TIMEOUT), wait, and then clean up with:

> disable_all_timeouts(false);

> That clears every timeout the process has, not only the ones these
> functions enabled. The comment above it says the startup process is
> assumed not to have any other outstanding timeouts, which was true
> when the calls were written. It stopped being true in 15, when
> STARTUP_PROGRESS_TIMEOUT was added by 9ce346eabf3.

Yes, the source of this bug is an outdated assumption — the
recovery-conflict resolvers (`ResolveRecoveryConflictWithLock',
`ResolveRecoveryConflictWithBufferPin') have always canceled /every/
timeout that the process has. Since 9ce346eabf3 that includes the
progress timer (`STARTUP_PROGRESS_TIMEOUT'), although it’s not a timer
that the resolver function armed. It’s not a problem for other conflict
types (snapshot, db, tablespace…) because they use a different wait-path
that doesn’t use timeouts.

> begin_startup_progress_phase() is called once, right before the redo
> loop. If a conflict happens later, the progress timeout is canceled
> along with the conflict timeouts and nothing ever re-arms it, so
> ereport_startup_progress() never fires again.

This is why *one conflict is enough to silence progress for the rest of
redo*, not just momentarily.


Reproducers
═══════════

I manually tested things and also modified existing TAP tests on
recovery conflicts for this. The manual flow covers only the lock path;
the TAP test additionally covers the pin path, modelled on 031’s
existing buffer pin section.


Manual Testing Approach
───────────────────────

> I reproduced it locally with recovery.signal, hot_standby=on, a slow
> restore_command with a read-only query holding ACCESS SHARE on a
> table that the WAL stream takes ACCESS EXCLUSIVE on:

> no conflict: 22 “redo in progress” messages, up to “redo done” one
> conflict: 7 messages, then silence for the last time of redo

> Redo itself completes and the data is correct; only the log messages
> disappear. But someone watching a long recovery sees the progress
> stop and can easily conclude it is stuck.

I followed your directions for the manual reproduction of the lock-path,
some details on my steps:

1. Spawn two shells: T1, a driver; T2, a log-viewer. Basic env setup on
   both:
   ┌────
   │ export R=$PGDATA/repro # scratch path
   │ export P1=$((PGPORT+200)) P2=$((PGPORT+300))  # e.g primary 5719, recovery node 5819
   └────
   Listing 1: Basic envvar setup for both T1 and T2

2. T1: Setup primary, archive and backup, to be run on T1:
   ┌────
   │ rm -rf "$R" && mkdir -p "$R/archive"
   │ 

   │ initdb -D "$R/primary" --no-sync -E UTF8 --no-locale
   │ 

   │ cat >> "$R/primary/postgresql.conf" </dev/null; done; }
   │ fill 20
   │ psql -X -p $P1 -c 'BEGIN; LOCK TABLE t; COMMIT;'
   │ fill 20
   │ 

   │ pg_ctl -D "$R/primary" -m fast -w stop     # shutdown archives the remaining finished segments
   │ 

   │ ls "$R/archive" | wc -l                    # expect ~40+ segments
   └────
   Listing 3: T1: Seed WAL segments

3. Create the Recovery Node, set `recovery.signal' and other parameters
   ┌────
   │ RUN=base
   │ 

   │ rm -rf "$R/pitr" && cp -a "$R/backup" "$R/pitr"
   │ 

   │ cat >> "$R/pitr/postgresql.conf" < ms: recovery conflict on lock
   │ <20 more lines of "restored log file  from archive">
   │ redo done at ...
   └────
   Listing 9: Remaining shape of the log output (without v1 applied)

   ┌────
   │ recovery finished waiting after 

Re: Fix recovery conflict canceling the startup progress timeout

От:
Ritesh Kumar <opensource@rtshkmr.com>
Дата:
Вложения:
Hi Aidar, hackers,

Attached is v3. 0001 is Aidar’s fix (unchanged) and it applies cleanly
to REL_15_STABLE through master. 0002 replaces my v2-0002 test, which
was slow, Unix-only and timing-dependent.


Test-approach
═════════════

The new test uses a small Perl `restore_command',
`t/restore_on_gate_open', that won’t hand over a WAL segment while a
gate file for it exists. That holds the startup process just before the
conflicting record and holds it again just after it. The test waits on
wait events and the replay LSN instead of using sleeps or filtering
log-lines. Hence, it doesn’t depend on replay speed. On unfixed code
only the two “progress after conflict” checks fail, immediately.

As for test-runtime, v2-0002 took 031 from ~3s to 23s. v3-0002 now takes
it to ~6s. It passed on [my fork’s CI]
(),
including Windows.


Red/green testing
═════════════════

Red/green tested on each branch, with 0001 reverted and applied:

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 branch  commit       v3-0002*                                      

────────────────────────────────────────────────────────────────────
 master  0607aa61090  attached                                      

 REL_19  d60d7f4261d  attached                                      

 REL_18  1da6ddbdc32  nocfbot variant: `BufferPin'                  

 REL_17  e6b2f7c2b41  nocfbot variant: `BufferPin'                  

 REL_16  e9c45974adb  nocfbot variant: `BufferPin', helpers inlined 

 REL_15  21a53865fb5  nocfbot variant: `BufferPin', helpers inlined 

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Table 1: reproduction & fix-verification done; TAP-tested

The buffer-pin wait event was renamed in 19 (from `BufferPin' ->
`BufferCleanup'), and 15/16 don’t have `wait_for_event()' and
`advance_wal()' helpers. All of 031 is `skip_all' on 15/16, so I removed
that line locally to run those two. I’d suggest backpatching 0001 from
master through 15, and test to master through 17.


Related threads
═══════════════

1. CF 7372 reworks the same resolvers, and its v10 still ends them with
   `disable_all_timeouts()'. If 7521 goes in first, 7372 needs to keep
   the narrower cleanup when it rebases; v3-0002 fails if it doesn’t.

2. CF 6876 stops the progress timeout during standby-mode replay. This
   bug and the test are about archive recovery, which 6876 doesn’t
   touch.

Regards, Ritesh
FAQ