Dear Euler,
Thank you for making the patch! I'm also interested in the patch so I want to join the thread.
While testing your patch, I noticed that the 032_apply_delay.pl failed.
PSA logs that generated on my machine. This failure is same as reported by cfbot[1].
It seemed that the apply worker could not exit and starts WaitLatch() again even if the subscription had been disabled.
Followings are cited from attached log.
```
...
2022-09-14 09:44:30.489 UTC [14880] 032_apply_delay.pl LOG: statement: ALTER SUBSCRIPTION tap_sub SET (min_apply_delay
=86460000)
2022-09-14 09:44:30.525 UTC [14777] DEBUG: sending feedback (force 0) to recv 0/1690220, write 0/1690220, flush
0/1690220
2022-09-14 09:44:30.526 UTC [14759] DEBUG: server process (PID 14878) exited with exit code 0
2022-09-14 09:44:30.535 UTC [14777] DEBUG: logical replication apply delay: 86460000 ms
2022-09-14 09:44:30.535 UTC [14777] CONTEXT: processing remote data for replication origin "pg_16393" during "BEGIN"
intransaction 734 finished at 0/16902A8
2022-09-14 09:44:30.576 UTC [14759] DEBUG: forked new backend, pid=14884 socket=6
2022-09-14 09:44:30.578 UTC [14759] DEBUG: server process (PID 14880) exited with exit code 0
2022-09-14 09:44:30.583 UTC [14884] 032_apply_delay.pl LOG: statement: ALTER SUBSCRIPTION tap_sub DISABLE
2022-09-14 09:44:30.589 UTC [14777] DEBUG: logical replication apply delay: 86459945 ms
2022-09-14 09:44:30.589 UTC [14777] CONTEXT: processing remote data for replication origin "pg_16393" during "BEGIN"
intransaction 734 finished at 0/16902A8
2022-09-14 09:44:30.608 UTC [14759] DEBUG: forked new backend, pid=14886 socket=6
2022-09-14 09:44:30.632 UTC [14886] 032_apply_delay.pl LOG: statement: SELECT count(1) = 0 FROM pg_stat_subscription
WHEREsubname = 'tap_sub' AND pid IS NOT NULL;
2022-09-14 09:44:30.665 UTC [14759] DEBUG: server process (PID 14884) exited with exit code 0
...
```
I think this may be caused because the delayed worker will not read the modified catalog even if ALTER SUBSCRIPTION ...
DISABLEDis called.
I also attached the fix patch that can be applied after yours. It seems OK on my env.
[1]: https://cirrus-ci.com/task/4888001967816704
Best Regards,
Hayato Kuroda
FUJITSU LIMITED