{"record":{"id":"6b358f06bcd5d400","repo":"prestodb/presto","slug":"already-exists-6b358f","errorCode":"ALREADY_EXISTS","errorMessage":"ALREADY_EXISTS: SQLException","messagePattern":"ALREADY_EXISTS: SQLException","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-postgresql/src/main/java/com/facebook/presto/plugin/postgresql/PostgreSqlClient.java","lineNumber":178,"sourceCode":"\n        else if (typeHandle.getJdbcTypeName().equals(\"uuid\")) {\n            return Optional.of(uuidReadMapping());\n        }\n        else if (typeName.equalsIgnoreCase(GEOMETRY) || typeName.equalsIgnoreCase(SPHERICAL_GEOGRAPHY)) {\n            return Optional.of(geometryReadMapping());\n        }\n        return super.toPrestoType(session, typeHandle);\n    }\n\n    @Override\n    public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)\n    {\n        try {\n            createTable(tableMetadata, session, tableMetadata.getTable().getTableName());\n        }\n        catch (SQLException e) {\n            if (DUPLICATE_TABLE_SQLSTATE.equals(e.getSQLState())) {\n                throw new PrestoException(ALREADY_EXISTS, e);\n            }\n            throw new PrestoException(JDBC_ERROR, e);\n        }\n    }\n\n    @Override\n    protected void renameTable(JdbcIdentity identity, String catalogName, SchemaTableName oldTable, SchemaTableName newTable)\n    {\n        // PostgreSQL does not allow qualifying the target of a rename\n        try (Connection connection = connectionFactory.openConnection(identity)) {\n            String sql = format(\n                    \"ALTER TABLE %s RENAME TO %s\",\n                    quoted(catalogName, oldTable.getSchemaName(), oldTable.getTableName()),\n                    quoted(newTable.getTableName()));\n            execute(connection, sql);\n        }\n        catch (SQLException e) {\n            throw new PrestoException(JDBC_ERROR, e);","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-postgresql/src/main/java/com/facebook/presto/plugin/postgresql/PostgreSqlClient.java#L160-L196","documentation":"PostgreSqlClient.createTable wraps SQLExceptions from the underlying CREATE TABLE statement. When PostgreSQL reports SQLSTATE 42P07 (duplicate_table), it is translated into a PrestoException with function code ALREADY_EXISTS so callers can distinguish 'table already exists' from other JDBC failures.","triggerScenarios":"Executing CREATE TABLE against PostgreSQL when a table with the same schema-qualified name already exists in the backing database (DUPLICATE_TABLE_SQLSTATE = 42P07).","commonSituations":"CREATE TABLE IF NOT EXISTS semantics racing with another query/connector writing the same table, retrying a DDL statement that partially succeeded, two Presto workers executing the same metadata DDL concurrently, or a leftover table from a previous failed run.","solutions":["Drop the existing table first if it is stale: DROP TABLE <schema>.<table> (in PostgreSQL or via the connector)","Use CREATE TABLE IF NOT EXISTS at the caller level / check existence via SHOW TABLES before creating","Serialize DDL so concurrent workers don't race to create the same table","If the name collision is unintentional, choose a different target table name"],"exampleFix":"// before\nCREATE TABLE catalog.schema.my_table (...);\n// after\nDROP TABLE IF EXISTS catalog.schema.my_table;\nCREATE TABLE catalog.schema.my_table (...);","handlingStrategy":"try-catch","validationCode":"sql\nSELECT 1 FROM information_schema.tables\nWHERE table_schema = '<schema>' AND table_name = '<table>';","typeGuard":null,"tryCatchPattern":"java\ntry {\n    client.createTable(session, tableMetadata);\n} catch (PrestoException e) {\n    if (e.getErrorCode() == ALREADY_EXISTS.toErrorCode().getCode()) {\n        // table already exists — treat as no-op or drop and recreate\n    } else {\n        throw e;\n    }\n}","preventionTips":["Use CREATE TABLE IF NOT EXISTS or existence checks before DDL","Serialize DDL across workers/clients to avoid races","Clean up leftover tables from failed runs before retrying","Use deterministic, unique table names per job/run"],"tags":["postgresql","jdbc","create-table","duplicate-table"],"backgroundTag":"table-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"}