Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fc7b065

Browse files
vigneshwaran-cCommitfest Bot
authored and
Commitfest Bot
committed
Fix issue with file handle retention during CREATE DATABASE in pg_restore
During upgrades, when pg_restore performs CREATE DATABASE, the bgwriter or checkpointer may flush buffers and hold a file handle for the table. This causes issues if the table needs to be re-created later (e.g., after a TRUNCATE command), especially on OSes like older versions of Windows, where unlinked files aren't fully removed until they are no longer open. This commit fixes the issue by checking for STATUS_DELETE_PENDING and calling WaitForProcSignalBarrier, ensuring that all smgr file descriptors are closed across all backends before retrying the file operation.
1 parent a675149 commit fc7b065

File tree

1 file changed

+16
-0
lines changed
  • src/backend/storage/smgr

1 file changed

+16
-0
lines changed

src/backend/storage/smgr/md.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "storage/bufmgr.h"
3636
#include "storage/fd.h"
3737
#include "storage/md.h"
38+
#include "storage/procsignal.h"
3839
#include "storage/relfilelocator.h"
3940
#include "storage/smgr.h"
4041
#include "storage/sync.h"
@@ -214,6 +215,9 @@ mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
214215
MdfdVec *mdfd;
215216
RelPathStr path;
216217
File fd;
218+
#if defined(WIN32) && !defined(__CYGWIN__)
219+
bool retryattempted = false;
220+
#endif
217221

218222
if (isRedo && reln->md_num_open_segs[forknum] > 0)
219223
return; /* created and opened already... */
@@ -235,6 +239,9 @@ mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
235239

236240
path = relpath(reln->smgr_rlocator, forknum);
237241

242+
#if defined(WIN32) && !defined(__CYGWIN__)
243+
retry:
244+
#endif
238245
fd = PathNameOpenFile(path.str, _mdfd_open_flags() | O_CREAT | O_EXCL);
239246

240247
if (fd < 0)
@@ -245,6 +252,15 @@ mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
245252
fd = PathNameOpenFile(path.str, _mdfd_open_flags());
246253
if (fd < 0)
247254
{
255+
#if defined(WIN32) && !defined(__CYGWIN__)
256+
if (!retryattempted && pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
257+
{
258+
retryattempted = true;
259+
WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE));
260+
goto retry;
261+
}
262+
#endif
263+
248264
/* be sure to report the error reported by create, not open */
249265
errno = save_errno;
250266
ereport(ERROR,

0 commit comments

Comments
 (0)