postgresql/src/backend/storage
Thomas Munro 46d9bfb0a6 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 <nathandbossart@gmail.com>
Reported-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Thomas Munro <thomas.munro@gmail.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20220215235845.GA2665318%40nathanxps13
2022-03-16 17:20:24 +13:00
..
buffer Remove xloginsert.h from xlog.h 2022-01-30 12:25:24 -03:00
file Update copyright for 2022 2022-01-07 19:04:57 -05:00
freespace Remove xloginsert.h from xlog.h 2022-01-30 12:25:24 -03:00
ipc Create routine able to set single-call SRFs for Materialize mode 2022-03-07 10:26:29 +09:00
large_object Update copyright for 2022 2022-01-07 19:04:57 -05:00
lmgr Add further debug info to help debug 019_replslot_limit.pl failures. 2022-02-25 17:04:39 -08:00
page Update copyright for 2022 2022-01-07 19:04:57 -05:00
smgr Fix DROP {DATABASE,TABLESPACE} on Windows. 2022-02-12 10:21:23 +13:00
sync Fix race between DROP TABLESPACE and checkpointing. 2022-03-16 17:20:24 +13:00
Makefile Refactor the fsync queue for wider use. 2019-04-04 23:38:38 +13:00