diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 917429c2b525b673d72e82ce8f393d3c8ea6384f..a74ad9b2f0720b655c7b976c4efd36fc7a874a78 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.145 2004/10/17 20:47:20 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.146 2004/10/28 00:39:58 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -32,6 +32,7 @@
 #include "commands/tablespace.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "postmaster/bgwriter.h"
 #include "storage/fd.h"
 #include "storage/freespace.h"
 #include "storage/sinval.h"
@@ -624,6 +625,14 @@ dropdb(const char *dbname)
 	 */
 	FreeSpaceMapForgetDatabase(db_id);
 
+	/*
+	 * On Windows, force a checkpoint so that the bgwriter doesn't hold any
+	 * open files, which would cause rmdir() to fail.
+	 */
+#ifdef WIN32
+	RequestCheckpoint(true);
+#endif
+
 	/*
 	 * Remove all tablespace subdirs belonging to the database.
 	 */
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index f0b32b126540e50401030702cf2f67f07759a23b..25b953da2fab0ca6751ddb9403e71a67fa8465e3 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.9 2004/10/12 21:54:40 petere Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.10 2004/10/28 00:39:59 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -346,6 +346,15 @@ BackgroundWriterMain(void)
 
 			CreateCheckPoint(false, force_checkpoint);
 
+			/*
+			 * After any checkpoint, close all smgr files.	This is so we
+			 * won't hang onto smgr references to deleted files
+			 * indefinitely. (It is safe to do this because this process
+			 * does not have a relcache, and so no dangling references
+			 * could remain.)
+			 */
+			smgrcloseall();
+
 			/*
 			 * Indicate checkpoint completion to any waiting backends.
 			 */
@@ -359,15 +368,6 @@ BackgroundWriterMain(void)
 			 */
 			last_checkpoint_time = now;
 
-			/*
-			 * After any checkpoint, close all smgr files.	This is so we
-			 * won't hang onto smgr references to deleted files
-			 * indefinitely. (It is safe to do this because this process
-			 * does not have a relcache, and so no dangling references
-			 * could remain.)
-			 */
-			smgrcloseall();
-
 			/* Nap for configured time before rechecking */
 			n = 1;
 		}