From a4127b713de3a655a0136bf53adceebf7b83d319 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 22 Mar 2007 19:51:44 +0000
Subject: [PATCH] Allow DROP TABLESPACE to succeed (with a warning) if the
 pg_tblspc symlink doesn't exist.  This allows DROP to be used to clean out
 the pg_tablespace catalog entry in a situation where a previous DROP attempt
 failed before committing but after having removed the directories and
 symlink.

Per report from William Garrison.  Even though his test case depends on an
unrelated bug in PreventTransactionChain, it's certainly possible for this
situation to arise due to other problems, eg a system crash at just the
right time.
---
 src/backend/commands/tablespace.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 8e3bfbda863..16a9f52bd5f 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.44 2007/03/13 00:33:40 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.45 2007/03/22 19:51:44 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -523,12 +523,25 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
 	 * fresh subdirectories in parallel. It is possible that new files are
 	 * being created within subdirectories, though, so the rmdir call could
 	 * fail.  Worst consequence is a less friendly error message.
+	 *
+	 * If redo is true then ENOENT is a likely outcome here, and we allow it
+	 * to pass without comment.  In normal operation we still allow it, but
+	 * with a warning.  This is because even though ProcessUtility disallows
+	 * DROP TABLESPACE in a transaction block, it's possible that a previous
+	 * DROP failed and rolled back after removing the tablespace directories
+	 * and symlink.  We want to allow a new DROP attempt to succeed at
+	 * removing the catalog entries, so we should not give a hard error here.
 	 */
 	dirdesc = AllocateDir(location);
 	if (dirdesc == NULL)
 	{
-		if (redo && errno == ENOENT)
+		if (errno == ENOENT)
 		{
+			if (!redo)
+				ereport(WARNING,
+						(errcode_for_file_access(),
+						 errmsg("could not open directory \"%s\": %m",
+								location)));
 			pfree(location);
 			return true;
 		}
-- 
GitLab