{"record":{"id":"e49fa377347559f2","repo":"prestodb/presto","slug":"schema-already-exists","errorCode":"SCHEMA_ALREADY_EXISTS","errorMessage":"Schema '%s' already exists or created by others","messagePattern":"Schema '(.+?)' already exists or created by others","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-lark-sheets/src/main/java/com/facebook/presto/lark/sheets/api/InMemorySchemaStore.java","lineNumber":44,"sourceCode":"import static java.lang.String.format;\n\npublic class InMemorySchemaStore\n        implements LarkSheetsSchemaStore\n{\n    private final Map<String, LarkSheetsSchema> schemas = new ConcurrentHashMap<>();\n\n    @Override\n    public Optional<LarkSheetsSchema> get(String name)\n    {\n        return Optional.ofNullable(schemas.get(lower(name)));\n    }\n\n    @Override\n    public synchronized void insert(LarkSheetsSchema schema)\n    {\n        String name = lower(schema.getName());\n        if (schemas.containsKey(name)) {\n            throw new PrestoException(LarkSheetsErrorCode.SCHEMA_ALREADY_EXISTS,\n                    format(\"Schema '%s' already exists or created by others\", name));\n        }\n        schemas.put(name, schema);\n    }\n\n    @Override\n    public synchronized void delete(String schemaName)\n    {\n        String name = lower(schemaName);\n        if (!schemas.containsKey(name)) {\n            throw new PrestoException(LarkSheetsErrorCode.SCHEMA_NOT_EXISTS,\n                    format(\"Schema '%s' does not exist\", name));\n        }\n        schemas.remove(name);\n    }\n\n    @Override\n    public Iterable<LarkSheetsSchema> listForUser(String user)","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-lark-sheets/src/main/java/com/facebook/presto/lark/sheets/api/InMemorySchemaStore.java#L26-L62","documentation":"InMemorySchemaStore.insert throws SCHEMA_ALREADY_EXISTS when a schema with the same (lowercased) name is already registered in the in-memory map. The store is a simple synchronized HashMap keyed by lowercase schema name, so duplicate registration is rejected rather than overwritten.","triggerScenarios":"Calling insert(schema) when schemas already contains lower(schema.getName()). Happens on CREATE SCHEMA for a name that already exists, or two nodes/threads concurrently creating the same schema where the second caller loses the race.","commonSituations":"Re-running an idempotent bootstrap/provisioning script that creates schemas; concurrent CREATE SCHEMA statements racing on different Presto nodes sharing the store; case-insensitive name collision ('Sales' vs 'sales').","solutions":["Check existence first with a get/list call before inserting, or wrap insert in try-catch for SCHEMA_ALREADY_EXISTS and treat it as a no-op.","Lowercase the schema name before creating to avoid case-variant duplicates.","If the connector supports it, use IF NOT EXISTS semantics in CREATE SCHEMA."],"exampleFix":"// before\nstore.insert(new LarkSheetsSchema(\"Sales\"));\n\n// after\ntry {\n    store.insert(new LarkSheetsSchema(\"sales\"));\n} catch (PrestoException e) {\n    if (e.getErrorCode() != LarkSheetsErrorCode.SCHEMA_ALREADY_EXISTS.toErrorCodeCode()) {\n        throw e;\n    }\n}","handlingStrategy":"try-catch","validationCode":"boolean exists = store.listForUser(user) != null && streamOf(store.listForUser(user)).anyMatch(s -> s.getName().equalsIgnoreCase(name));","typeGuard":"boolean schemaExists(InMemorySchemaStore store, String name) { try { store.delete(name); store.insert(new LarkSheetsSchema(name)); return false; } catch (PrestoException e) { return true; } }","tryCatchPattern":"try { store.insert(schema); } catch (PrestoException e) { if (LarkSheetsErrorCode.SCHEMA_ALREADY_EXISTS.toErrorCode().getCode() == e.getErrorCode().getCode()) { /* treat as already-created: continue */ } else { throw e; } }","preventionTips":["Lowercase schema names before insert/delete to avoid case-variant duplicates.","Use IF NOT EXISTS / existence checks in provisioning scripts.","Serialize schema creation through a single coordination point to avoid cross-node races."],"tags":["schema","duplicate","presto"],"backgroundTag":"schema-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"}