prestodb/presto · error · PrestoException
ALREADY_EXISTS
ALREADY_EXISTS
Error message
One or more partitions already exist for table '%s.%s'
What it means
If the Hive metastore throws AlreadyExistsException during addPartitions, ThriftHiveMetastore maps it to a PrestoException with code ALREADY_EXISTS stating that one or more of the requested partitions already exist for the table. The whole batch add is aborted rather than silently skipping duplicates (unless IF NOT EXISTS semantics are used upstream).
Source
Thrown at presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java:1319
if (partitions.isEmpty()) {
return;
}
try {
retry()
.stopOn(AlreadyExistsException.class, InvalidObjectException.class, MetaException.class, NoSuchObjectException.class, PrestoException.class)
.stopOnIllegalExceptions()
.run("addPartitions", stats.getAddPartitions().wrap(() ->
getMetastoreClientThenCall(metastoreContext, client -> {
int partitionsAdded = client.addPartitions(partitions);
if (partitionsAdded != partitions.size()) {
throw new PrestoException(HIVE_METASTORE_ERROR,
format("Hive metastore only added %s of %s partitions", partitionsAdded, partitions.size()));
}
return null;
})));
}
catch (AlreadyExistsException e) {
throw new PrestoException(ALREADY_EXISTS, format("One or more partitions already exist for table '%s.%s'", databaseName, tableName), e);
}
catch (NoSuchObjectException e) {
throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
}
catch (TException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
catch (Exception e) {
throw propagate(e);
}
}
private <V> V getMetastoreClientThenCall(MetastoreContext metastoreContext, MetastoreCallable<V> callable)
throws Exception
{
if (!impersonationEnabled) {
try (HiveMetastoreClient client = clientProvider.createMetastoreClient(Optional.empty())) {
return callable.call(client);View on GitHub (pinned to 55bb57d202)
Solutions
- Use ADD IF NOT EXISTS or check SHOW PARTITIONS before adding
- Drop the existing partition first if re-creation is intended
- Make the pipeline idempotent: skip partitions already registered
- Coordinate concurrent writers so the same partition isn't added twice
Example fix
-- before ALTER TABLE hive.db.tbl ADD PARTITION (dt='2026-09-01'); -- after ALTER TABLE hive.db.tbl ADD IF NOT EXISTS PARTITION (dt='2026-09-01');
Defensive patterns
Strategy: validation
Validate before calling
-- check existing partitions before adding SHOW PARTITIONS FROM hive.<db>.<table>; -- add only if missing, or use IF NOT EXISTS ALTER TABLE hive.<db>.<table> ADD IF NOT EXISTS PARTITION (dt='2026-09-01');
Try / catch
try {
addPartitions(...);
} catch (PrestoException e) {
if ("ALREADY_EXISTS".equals(e.getErrorCode().getName())) {
// idempotent path: treat as success or skip existing partitions
} else throw e;
} Prevention
- Use ADD IF NOT EXISTS for idempotent pipelines
- Coordinate concurrent writers to the same partitions
- Make retries idempotent after failed inserts
- Check SHOW PARTITIONS before re-running partition DDL
When it happens
Trigger: addPartitions is invoked with a batch containing a partition (or partitions) already registered in the metastore — e.g. re-running an INSERT that created partitions, or ADD PARTITION without IF NOT EXISTS on an existing partition.
Common situations: Idempotent retries of a failed INSERT; two jobs writing the same partitions concurrently; re-running ALTER TABLE ADD PARTITION after a prior partial success; external tools (Hive/Spark) already registered the partition.
Related errors
- View already exists
- Materialized view already exists
- View not found
- Materialized view not found
- HIVE_COLUMN_ORDER_MISMATCH
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/d6a8dd02cfdb19d2.
Report an issue: GitHub.