{"record":{"id":"58a178537cdda5b2","repo":"prestodb/presto","slug":"already-exists-58a178","errorCode":"ALREADY_EXISTS","errorMessage":"Session function %s has already been defined","messagePattern":"Session function (.+?) has already been defined","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/execution/QueryStateMachine.java","lineNumber":744,"sourceCode":"    }\n\n    public void removePreparedStatement(String key)\n    {\n        requireNonNull(key, \"key is null\");\n\n        if (!session.getPreparedStatements().containsKey(key)) {\n            throw new PrestoException(NOT_FOUND, \"Prepared statement not found: \" + key);\n        }\n        deallocatedPreparedStatements.add(key);\n    }\n\n    public void addSessionFunction(SqlFunctionId signature, SqlInvokedFunction function)\n    {\n        requireNonNull(signature, \"signature is null\");\n        requireNonNull(function, \"function is null\");\n\n        if (session.getSessionFunctions().containsKey(signature) || addedSessionFunctions.putIfAbsent(signature, function) != null) {\n            throw new PrestoException(ALREADY_EXISTS, format(\"Session function %s has already been defined\", signature));\n        }\n    }\n\n    public void removeSessionFunction(SqlFunctionId signature, boolean suppressNotFoundException)\n    {\n        requireNonNull(signature, \"signature is null\");\n\n        if (!session.getSessionFunctions().containsKey(signature)) {\n            if (!suppressNotFoundException) {\n                throw new PrestoException(NOT_FOUND, format(\"Session function %s not found\", signature.getFunctionName()));\n            }\n        }\n        else {\n            removedSessionFunctions.add(signature);\n        }\n    }\n\n    public void setStartedTransactionId(TransactionId startedTransactionId)","sourceCodeStart":726,"sourceCodeEnd":762,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/execution/QueryStateMachine.java#L726-L762","documentation":"Thrown by QueryStateMachine.addSessionFunction when a session function with the same SqlFunctionId signature is already registered for the query session. Presto sessions allow only one definition per function signature; re-registering the same signature is treated as a conflict. The check consults both the session's existing functions and the set of functions added during this query.","triggerScenarios":"Calling addSessionFunction with a SqlFunctionId already present in session.getSessionFunctions() or already added via addedSessionFunctions (putIfAbsent returns non-null). Typically two ADD FUNCTION statements for the same signature in one session, or a concurrent registration race.","commonSituations":"Re-running an ADD FUNCTION SQL statement without DROP FUNCTION first; client retry re-sending the same session-function registration; two threads sharing a session registering the same function concurrently.","solutions":["Drop the existing session function first via removeSessionFunction (or DROP FUNCTION in SQL) before re-adding it","Use a different function signature (distinct parameter types) if the intent is an overload","Check session.getSessionFunctions().containsKey(signature) before calling addSessionFunction","Guard against concurrent registration by ensuring a single thread owns session function management"],"exampleFix":"// before\nstateMachine.addSessionFunction(signature, function); // throws if already defined\n// after\nif (!session.getSessionFunctions().containsKey(signature)) {\n    stateMachine.addSessionFunction(signature, function);\n} else {\n    stateMachine.removeSessionFunction(signature, false);\n    stateMachine.addSessionFunction(signature, function);\n}","handlingStrategy":"validation","validationCode":"if (session.getSessionFunctions().containsKey(signature)) {\n    // already defined — skip or drop-then-add\n    return;\n}\nstateMachine.addSessionFunction(signature, function);","typeGuard":null,"tryCatchPattern":"try {\n    stateMachine.addSessionFunction(signature, function);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getCode() == StandardErrorCode.ALREADY_EXISTS.getCode()) {\n        stateMachine.removeSessionFunction(signature, true);\n        stateMachine.addSessionFunction(signature, function);\n    } else { throw e; }\n}","preventionTips":["Always pair ADD FUNCTION with a prior DROP FUNCTION in session scripts","Track registered signatures in client code to avoid re-registration on retries","Never share one session across threads that both register functions"],"tags":["presto","session-function","duplicate-definition"],"backgroundTag":"duplicate-resource-definition","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"}