prestodb/presto · error · SemanticException

SCHEMA_ALREADY_EXISTS

SCHEMA_ALREADY_EXISTS

Error message

Target schema '%s' already exists

What it means

SemanticException thrown by RenameSchemaTask when the target schema name already exists in the catalog. Renaming into an existing schema would merge/collide namespaces, so Presto rejects it upfront with SCHEMA_ALREADY_EXISTS.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/execution/RenameSchemaTask.java:62

    }

    @Override
    public ListenableFuture<?> execute(RenameSchema statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector, String query)
    {
        CatalogSchemaName source = createCatalogSchemaName(session, statement, Optional.of(statement.getSource()), metadata);
        CatalogSchemaName target = new CatalogSchemaName(source.getCatalogName(), statement.getTarget().getValue());
        MetadataResolver metadataResolver = metadata.getMetadataResolver(session);

        if (!metadataResolver.catalogExists(source.getCatalogName())) {
            throw new SemanticException(MISSING_CATALOG, "Catalog '%s' does not exist", source.getCatalogName());
        }

        if (!metadataResolver.schemaExists(source)) {
            throw new SemanticException(MISSING_SCHEMA, statement, "Schema '%s' does not exist", source);
        }

        if (metadataResolver.schemaExists(target)) {
            throw new SemanticException(SCHEMA_ALREADY_EXISTS, statement, "Target schema '%s' already exists", target);
        }

        accessControl.checkCanRenameSchema(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), source, statement.getTarget().getValue());

        metadata.renameSchema(session, source, statement.getTarget().getValue());

        return immediateFuture(null);
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Pick a target schema name that doesn't exist (verify with SHOW SCHEMAS FROM catalog)
  2. Drop or rename the pre-existing target schema if it's stale
  3. Make the migration idempotent: if target exists and source doesn't, treat the rename as already done
  4. Qualify and double-check the catalog — the collision may exist in a different catalog than intended

Example fix

-- before
ALTER SCHEMA sales.web RENAME TO archive; -- sales.archive already exists
-- after
ALTER SCHEMA sales.web RENAME TO archive_2026; -- unique target
Defensive patterns

Strategy: validation

Validate before calling

Set<String> schemas = showSchemas("sales");
if (schemas.contains("archive")) { /* choose another target or drop stale schema */ }

Try / catch

try {
    execute("ALTER SCHEMA sales.web RENAME TO archive");
} catch (SemanticException e) {
    if (e.getCode() == SCHEMA_ALREADY_EXISTS) { /* pick a unique target name or clean up */ }
    else throw e;
}

Prevention

When it happens

Trigger: ALTER SCHEMA catalog.source RENAME TO target where schemaExists(target) is true — the destination schema name is already taken.

Common situations: Re-running a rename migration after a partial success (source renamed, but a same-named target also exists); choosing a target name that collides with an existing schema; two environments merged onto one cluster.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/93f663eda60c865c. Report an issue: GitHub.