ClickHouse创建临时表报错Code:113,无会话问题求助
The Root Cause
ClickHouse temporary tables are session-bound—they only exist within an active, persistent database session and are automatically dropped when the session ends. The error Code: 113 pops up because your query interface isn't maintaining a continuous session context.
If you're using a stateless tool (like a one-off HTTP request without session tracking) or running the query in an environment that doesn't preserve sessions, ClickHouse can't associate your CREATE TEMPORARY TABLE statement with any valid session, hence the error.
Fixes to Try
1. Use a Session-Aware Client (Simplest Approach)
The easiest way is to use ClickHouse's official command-line client, which maintains a session by default:
- Open your terminal and run:
clickhouse-client - Once you're in the interactive prompt, execute your statement:
CREATE TEMPORARY TABLE nn1 AS SELECT 1;
You'll be able to query nn1 for as long as the client session stays open.
2. Maintain Sessions for HTTP/API Calls
If you're using the HTTP interface (common for integrations), you need to explicitly track sessions using a session_id parameter:
- Pick a unique identifier for your session (e.g.,
my_temp_session_123), then include it in all related requests:- Create the temporary table:
curl -X POST 'http://your-clickhouse-host:8123/?session_id=my_temp_session_123' --data-binary 'CREATE TEMPORARY TABLE nn1 AS SELECT 1;' - Query the table in the same session:
curl -X POST 'http://your-clickhouse-host:8123/?session_id=my_temp_session_123' --data-binary 'SELECT * FROM nn1;'
- Create the temporary table:
Just make sure the session_id stays consistent across all requests that need access to the temporary table. You can adjust session timeout settings in ClickHouse's config if needed.
3. Alternative: Use a Memory Table (If Session Isn't Feasible)
If you don't need the auto-cleanup behavior of temporary tables, you can create a memory table instead. It's global (not session-bound) but will stay until you drop it manually:
CREATE TABLE nn1 ENGINE = Memory AS SELECT 1;
Remember to run DROP TABLE nn1; once you're done to free up resources.
内容的提问来源于stack exchange,提问作者Nik

