ClickHouse raises the THERE_IS_NO_SESSION error when a query tries to perform an operation that requires an active session, but no session context exists. This commonly happens when you attempt to create or reference temporary tables, or use session-level settings through a stateless connection such as a plain HTTP request without a session_id parameter.
Impact
Queries that depend on session state will fail outright. Temporary tables cannot be created or queried, and any session-scoped variables or settings will be unavailable. In applications that rely on temporary tables for intermediate results, this error blocks entire workflows. The error is immediate and deterministic -- it will occur every time session-dependent features are used without a session.
Common Causes
- Using temporary tables over a plain HTTP connection -- The ClickHouse HTTP interface is stateless by default. Without passing a
session_idquery parameter, there is no session context, andCREATE TEMPORARY TABLEwill fail. - CLI or driver misconfiguration -- Some ClickHouse drivers or client libraries do not enable sessions automatically. If the driver opens a new connection for every query without session tracking, session-dependent operations will trigger this error.
- Load balancer breaking session affinity -- When a load balancer sits in front of multiple ClickHouse nodes and does not maintain sticky sessions, requests may land on a node that has no knowledge of the session.
- Using
clickhouse-clientin non-interactive mode with session features -- Running one-off commands viaclickhouse-client -qdoes not maintain a persistent session between invocations. - Attempting session operations inside a
remote()orcluster()call -- Queries executed on remote nodes do not inherit the local session context.
Troubleshooting and Resolution Steps
Add a session_id to HTTP requests: When using the HTTP interface, include the
session_idparameter to establish a session:curl "http://localhost:8123/?session_id=my_session_1" \ --data-binary "CREATE TEMPORARY TABLE tmp (x UInt32) ENGINE = Memory"Enable sessions in your client library: Most ClickHouse drivers support a session mode. For example, in
clickhouse-driverfor Python:from clickhouse_driver import Client client = Client('localhost', settings={'session_id': 'my_session'}) client.execute('CREATE TEMPORARY TABLE tmp (x UInt32) ENGINE = Memory')Use the native TCP protocol when possible: The native protocol (
clickhouse-client) maintains a session automatically in interactive mode. If your workflow relies heavily on temporary tables, prefer the native interface.Ensure load balancer sticky sessions: If you run ClickHouse behind a load balancer, configure session affinity (sticky sessions) based on the
session_idparameter or a client cookie so that all requests from one session reach the same server.Replace temporary tables with CTEs or subqueries: If establishing a session is impractical, consider rewriting the query to use Common Table Expressions (CTEs) instead of temporary tables:
WITH tmp AS ( SELECT number AS x FROM numbers(100) ) SELECT sum(x) FROM tmp;
Best Practices
- Always pass a
session_idwhen working with the HTTP interface and you need session features like temporary tables. - Use unique session IDs per logical workflow to avoid collisions between concurrent users.
- Prefer CTEs over temporary tables for stateless query patterns, especially in microservice architectures.
- Document which parts of your application depend on session state to make debugging easier.
- Set
session_timeoutexplicitly when creating sessions to control how long idle sessions persist.
Frequently Asked Questions
Q: What is the difference between THERE_IS_NO_SESSION and SESSION_NOT_FOUND?
A: THERE_IS_NO_SESSION means no session was requested at all -- the connection is entirely stateless. SESSION_NOT_FOUND means a session ID was provided, but the server cannot find a matching active session (it may have expired or never existed).
Q: Do I need a session for regular SELECT or INSERT queries?
A: No. Sessions are only required for operations that depend on session state, such as creating temporary tables, using session-level settings that persist across queries, or working with transactions. Regular queries work fine without a session.
Q: Can I use sessions with the ClickHouse HTTP interface in a connection pool?
A: Yes, but you must ensure that all requests for a given session ID are routed to the same ClickHouse server. Connection pools that spread requests across multiple nodes will cause session-related errors unless sticky routing is configured.
Q: Does clickhouse-client always have a session?
A: In interactive mode, yes -- clickhouse-client maintains a session for the duration of the interactive session. However, when running individual commands with -q or --query, each invocation is independent and does not share session state with other invocations.