Documentation
¶
Index ¶
- type ConnectionState
- func (s *ConnectionState) Bucket() uint32
- func (s *ConnectionState) ClearUser()
- func (s *ConnectionState) Clone() *ConnectionState
- func (s *ConnectionState) Close()
- func (s *ConnectionState) DeletePreparedStatement(name string)
- func (s *ConnectionState) GetPreparedStatement(name string) *query.PreparedStatement
- func (s *ConnectionState) GetSettings() *Settings
- func (s *ConnectionState) GetUser() string
- func (s *ConnectionState) HasUser() bool
- func (s *ConnectionState) IsClean() bool
- func (s *ConnectionState) SetSettings(settings *Settings)
- func (s *ConnectionState) SetUser(user string)
- func (s *ConnectionState) StorePreparedStatement(stmt *query.PreparedStatement)
- type Settings
- type SettingsCache
- type SettingsCacheKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnectionState ¶
type ConnectionState struct {
// User is the current role set via SET ROLE.
// This is tracked separately from Settings and is NOT used for pool bucket routing.
// The role affects PostgreSQL's role-based access control (RLS policies, object
// permissions, stored procedure access, etc.).
// Empty string means no role has been set (using connection's default role).
User string
// Settings contains session variables (SET commands).
// This is the key for connection pool bucket assignment.
Settings *Settings
// PreparedStatements stores prepared statements by name.
// The unnamed statement uses the empty string "" as the key.
PreparedStatements map[string]*query.PreparedStatement
// contains filtered or unexported fields
}
ConnectionState represents the cumulative state of a connection. This includes all state modifiers like session settings and prepared statements.
All methods are thread-safe.
func NewConnectionState ¶
func NewConnectionState() *ConnectionState
NewConnectionState creates a new empty ConnectionState with initialized maps.
func NewConnectionStateWithSettings ¶
func NewConnectionStateWithSettings(settings *Settings) *ConnectionState
NewConnectionStateWithSettings creates a new ConnectionState with the given settings.
func (*ConnectionState) Bucket ¶
func (s *ConnectionState) Bucket() uint32
Bucket returns the bucket number for this connection state. This is used by the connection pool to distribute connections across stacks. Returns 0 if there are no settings (clean connection).
func (*ConnectionState) ClearUser ¶
func (s *ConnectionState) ClearUser()
ClearUser clears the current user role. This should be called after executing RESET ROLE on the connection.
func (*ConnectionState) Clone ¶
func (s *ConnectionState) Clone() *ConnectionState
Clone creates a deep copy of this state.
func (*ConnectionState) Close ¶
func (s *ConnectionState) Close()
Close cleans up the connection state.
func (*ConnectionState) DeletePreparedStatement ¶
func (s *ConnectionState) DeletePreparedStatement(name string)
DeletePreparedStatement removes a prepared statement by name.
func (*ConnectionState) GetPreparedStatement ¶
func (s *ConnectionState) GetPreparedStatement(name string) *query.PreparedStatement
GetPreparedStatement retrieves a prepared statement by name.
func (*ConnectionState) GetSettings ¶
func (s *ConnectionState) GetSettings() *Settings
GetSettings returns the current settings. Returns nil if no settings.
func (*ConnectionState) GetUser ¶
func (s *ConnectionState) GetUser() string
GetUser returns the current user role set via SET ROLE. Returns empty string if no role has been set.
func (*ConnectionState) HasUser ¶
func (s *ConnectionState) HasUser() bool
HasUser returns true if a user role has been set.
func (*ConnectionState) IsClean ¶
func (s *ConnectionState) IsClean() bool
IsClean returns true if this state has no settings modifiers applied. Prepared statements and portals are not considered for pool routing.
func (*ConnectionState) SetSettings ¶
func (s *ConnectionState) SetSettings(settings *Settings)
SetSettings sets the settings for this connection state.
func (*ConnectionState) SetUser ¶
func (s *ConnectionState) SetUser(user string)
SetUser sets the current user role. This should be called after executing SET ROLE on the connection.
func (*ConnectionState) StorePreparedStatement ¶
func (s *ConnectionState) StorePreparedStatement(stmt *query.PreparedStatement)
StorePreparedStatement stores a prepared statement.
type Settings ¶
type Settings struct {
// Vars maps variable names to their values.
Vars map[string]string
// contains filtered or unexported fields
}
Settings contains session variables (SET commands) and a bucket number for connection pool distribution.
The bucket is assigned when the Settings is created and is used by the connection pool to distribute connections with the same settings to the same stack, enabling efficient connection reuse.
IMPORTANT: Settings should be created via SettingsCache.GetOrCreate() to ensure proper interning. When settings are interned, pointer equality can be used for fast comparison instead of comparing the full Vars map.
func NewSettings ¶
NewSettings creates a new Settings with the given variables and bucket number.
NOTE: For connection pooling, prefer using SettingsCache.GetOrCreate() instead to ensure settings are properly interned (same settings = same pointer).
func (*Settings) ApplyQuery ¶
ApplyQuery returns the SQL to apply these settings to a connection.
Uses pg_catalog.set_config() instead of SET SQL to correctly handle list-valued GUCs (e.g. search_path, DateStyle). The SET SQL command requires list elements to be individually quoted or unquoted:
SET search_path = 'temp_func_test, public' -- WRONG: one schema "temp_func_test, public" SET search_path = temp_func_test, public -- RIGHT: two schemas
set_config() takes a flat string and PG's GUC machinery internally splits it for GUC_LIST_INPUT variables. This is the same approach pg_dump uses (see pg_dump.c: appendStringLiteralAH for search_path serialization).
Single quotes in variable names and values are escaped by doubling them to prevent SQL injection.
func (*Settings) Bucket ¶
Bucket returns the bucket number for these settings. This is used by the connection pool for stack distribution.
func (*Settings) ResetQuery ¶
ResetQuery returns the SQL to reset these settings on a connection. Includes RESET ROLE and RESET SESSION AUTHORIZATION before RESET ALL because PostgreSQL marks both with GUC_NO_RESET_ALL.
type SettingsCache ¶
type SettingsCache struct {
// contains filtered or unexported fields
}
SettingsCache provides a bounded LRU cache for Settings that ensures the same settings configuration always returns the same *Settings pointer. This enables: 1. Pointer equality for fast settings comparison 2. Consistent bucket assignment for same settings 3. Reduced memory allocation for repeated settings
This follows the Vitess pattern where settings must be "interned" for optimal connection pool performance.
func NewSettingsCache ¶
func NewSettingsCache(maxSize int) *SettingsCache
NewSettingsCache creates a new SettingsCache with the specified max size. The maxSize must be > 0; the caller is responsible for providing a valid size.
func (*SettingsCache) Clear ¶
func (c *SettingsCache) Clear()
Clear removes all cached settings and resets metrics.
func (*SettingsCache) GetOrCreate ¶
func (c *SettingsCache) GetOrCreate(vars map[string]string) *Settings
GetOrCreate returns a cached Settings for the given variables, or creates and caches a new one if it doesn't exist. This ensures that the same settings configuration always returns the same *Settings pointer.
func (*SettingsCache) Hits ¶
func (c *SettingsCache) Hits() int64
Hits returns the number of cache hits.
func (*SettingsCache) MaxSize ¶
func (c *SettingsCache) MaxSize() int
MaxSize returns the maximum number of settings that can be cached.
func (*SettingsCache) Misses ¶
func (c *SettingsCache) Misses() int64
Misses returns the number of cache misses.
func (*SettingsCache) Size ¶
func (c *SettingsCache) Size() int
Size returns the number of cached settings.
type SettingsCacheKey ¶
type SettingsCacheKey string
SettingsCacheKey is the type used for cache keys.