Fix race between DROP TABLESPACE and checkpointing.
authorThomas Munro <[email protected]>
Wed, 16 Mar 2022 04:20:24 +0000 (17:20 +1300)
committerThomas Munro <[email protected]>
Wed, 16 Mar 2022 04:41:31 +0000 (17:41 +1300)
commitfe197872cfe8d29b3134568df06b3fba2cdd4c18
tree25a02f8fa7c609cae7d8e3267833a3cfbb62e45d
parent07e343513b85ea152f4cc2d3afeb2d4c87bbda77
Fix race between DROP TABLESPACE and checkpointing.

Commands like ALTER TABLE SET TABLESPACE may leave files for the next
checkpoint to clean up.  If such files are not removed by the time DROP
TABLESPACE is called, we request a checkpoint so that they are deleted.
However, there is presently a window before checkpoint start where new
unlink requests won't be scheduled until the following checkpoint.  This
means that the checkpoint forced by DROP TABLESPACE might not remove the
files we expect it to remove, and the following ERROR will be emitted:

ERROR:  tablespace "mytblspc" is not empty

To fix, add a call to AbsorbSyncRequests() just before advancing the
unlink cycle counter.  This ensures that any unlink requests forwarded
prior to checkpoint start (i.e., when ckpt_started is incremented) will
be processed by the current checkpoint.  Since AbsorbSyncRequests()
performs memory allocations, it cannot be called within a critical
section, so we also need to move SyncPreCheckpoint() to before
CreateCheckPoint()'s critical section.

This is an old bug, so back-patch to all supported versions.

Author: Nathan Bossart <[email protected]>
Reported-by: Nathan Bossart <[email protected]>
Reviewed-by: Thomas Munro <[email protected]>
Reviewed-by: Andres Freund <[email protected]>
Discussion: https://postgr.es/m/20220215235845.GA2665318%40nathanxps13
src/backend/access/transam/xlog.c
src/backend/storage/smgr/md.c