ClickHouse / ClickHouse/clickhouse-js

introduce "sticky session" concept for handling temporary tables

Open
#381 0 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
331
Forks
74
PR merge metrics
No merged PRs in 30d

Description

### Use case

Today, things like temporary tables rely on the same connection to operate. To ensure that today, we need to ensure we start the clickhouse client with `max_open_connections: 1` as per the docs, but that means down our application stack we need to keep 2 versions of clickhouse client:
* one for the default connection pool
* one that is a constructor to starting a new clickhouse client with `max_open_connections = 1` and a fixed sticky session.

### Describe the solution you'd like

This takes inspiration from the [`database/sql` Golang package and how they handle transactions](https://pkg.go.dev/database/sql#DB.BeginTx).

Basically, allow the `clickhouseClient` to hold a "sticky session" which can occupy the same connection pool (i.e. respect the same `max_open_connections`).

The API could look something like:
```js
const chClient = createClickhouseClient(...)

// All handled in the closure.
await chClient.execStickySession(async (
// maybe a different type that inherits the same ClickhouseClient types but indicates it is sticky?
chClient: ClickhouseClient,
) {
// automatically uses the same session id for the next 2 commands
await chClient.command(...);
await chClient.command(...)
})

// Or explicitly.
const stickySession = await chClient.startStickySession();
try {
...
} finally {
stickySession.close();
}
```

### Describe the alternatives you've considered

We're basically passing 2 different clickhouse clients down the stack now.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.