diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmds.c
index be7a40d5d2..13a7c327f3 100644
--- a/src/backend/commands/dropcmds.c
+++ b/src/backend/commands/dropcmds.c
@@ -101,6 +101,20 @@ RemoveObjects(DropStmt *stmt)
 						 errhint("Use DROP AGGREGATE to drop aggregate functions.")));
 		}
 
+		/*
+		 * Prevent drop of a temporary schema as this would mess up with
+		 * the end-of-session callback cleaning up all temporary objects.
+		 * As the in-memory state is not cleaned up either here, upon
+		 * recreation of a temporary schema within the same session the
+		 * temporary object handling would be inconsistent.
+		 */
+		if (stmt->removeType == OBJECT_SCHEMA &&
+			isTempNamespace(address.objectId))
+				ereport(ERROR,
+						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+						 errmsg("cannot drop temporary namespace \"%s\"",
+								get_namespace_name(address.objectId))));
+
 		/* Check permissions. */
 		namespaceId = get_object_namespace(&address);
 		if (!OidIsValid(namespaceId) ||
