{"record":{"id":"d6a8dd02cfdb19d2","repo":"prestodb/presto","slug":"already-exists-d6a8dd","errorCode":"ALREADY_EXISTS","errorMessage":"One or more partitions already exist for table '%s.%s'","messagePattern":"One or more partitions already exist for table '(.+?)\\.(.+?)'","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java","lineNumber":1319,"sourceCode":"        if (partitions.isEmpty()) {\n            return;\n        }\n        try {\n            retry()\n                    .stopOn(AlreadyExistsException.class, InvalidObjectException.class, MetaException.class, NoSuchObjectException.class, PrestoException.class)\n                    .stopOnIllegalExceptions()\n                    .run(\"addPartitions\", stats.getAddPartitions().wrap(() ->\n                            getMetastoreClientThenCall(metastoreContext, client -> {\n                                int partitionsAdded = client.addPartitions(partitions);\n                                if (partitionsAdded != partitions.size()) {\n                                    throw new PrestoException(HIVE_METASTORE_ERROR,\n                                            format(\"Hive metastore only added %s of %s partitions\", partitionsAdded, partitions.size()));\n                                }\n                                return null;\n                            })));\n        }\n        catch (AlreadyExistsException e) {\n            throw new PrestoException(ALREADY_EXISTS, format(\"One or more partitions already exist for table '%s.%s'\", databaseName, tableName), e);\n        }\n        catch (NoSuchObjectException e) {\n            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));\n        }\n        catch (TException e) {\n            throw new PrestoException(HIVE_METASTORE_ERROR, e);\n        }\n        catch (Exception e) {\n            throw propagate(e);\n        }\n    }\n\n    private <V> V getMetastoreClientThenCall(MetastoreContext metastoreContext, MetastoreCallable<V> callable)\n            throws Exception\n    {\n        if (!impersonationEnabled) {\n            try (HiveMetastoreClient client = clientProvider.createMetastoreClient(Optional.empty())) {\n                return callable.call(client);","sourceCodeStart":1301,"sourceCodeEnd":1337,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java#L1301-L1337","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"-- before\nALTER TABLE hive.db.tbl ADD PARTITION (dt='2026-09-01');\n-- after\nALTER TABLE hive.db.tbl ADD IF NOT EXISTS PARTITION (dt='2026-09-01');\n","handlingStrategy":"validation","validationCode":"-- check existing partitions before adding\nSHOW PARTITIONS FROM hive.<db>.<table>;\n-- add only if missing, or use IF NOT EXISTS\nALTER TABLE hive.<db>.<table> ADD IF NOT EXISTS PARTITION (dt='2026-09-01');","typeGuard":null,"tryCatchPattern":"try {\n    addPartitions(...);\n} catch (PrestoException e) {\n    if (\"ALREADY_EXISTS\".equals(e.getErrorCode().getName())) {\n        // idempotent path: treat as success or skip existing partitions\n    } else throw e;\n}","preventionTips":["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"],"tags":["hive","partition","already-exists","ddl"],"backgroundTag":"partition-already-exists","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}