Skip to content

feat: add Session api for stateful connection and set role support #1597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shivanshuraj1333
Copy link

@shivanshuraj1333 shivanshuraj1333 commented Jul 13, 2025

fixes: #1443

please check the end to end implementation with this toy program https://github.com/shivanshuraj1333/clickhuose-test

make sure to update the go.mod to point to the changes present in this PR

Summary:

  • Problem: ClickHouse Go driver lacks support for SET ROLE functionality due to connection pooling, which doesn't maintain connection state across multiple statements.

  • Solution: Added session management API that provides stateful connections while maintaining backward compatibility.

Key Changes

  • New Session Interface: Added Session interface with SetRole(), UnsetRole(), and Close() methods
  • Connection Enhancement: Added AcquireSession() method to main connection interface

Implementation:

  • session struct for native driver
  • stdSession struct for standard SQL driver
  • Proper resource management and error handling

example usage:

// Acquire a stateful session
session, err := conn.AcquireSession()
if err != nil {
    return err
}
defer session.Close()

// Set role for this session
if err := session.SetRole("admin"); err != nil {
    return err
}

// Execute queries - role persists across operations
rows, err := session.Query("SELECT currentUser()")
// ... more operations

// Unset role when done
session.UnsetRole()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support SET ROLE for clickhouse.Conn
1 participant