-
Notifications
You must be signed in to change notification settings - Fork 0
Server Session
The Server Session represents a connection between the Springtail proxy and a PostgreSQL backend server. It is responsible for managing the lifecycle of the server connection, processing client queries, and ensuring proper state synchronization between the client and the server. The Server Session acts as the intermediary between the Client Session and the PostgreSQL server, handling query execution, batching, dependency resolution, and error recovery.
- Connection Management: Establish and maintain a connection to the PostgreSQL server, including authentication and session setup.
- Query Execution: Process queries received from the Client Session, ensuring proper routing and execution.
- Batch Processing: Handle batches of messages from the Client Session, including dependency resolution and pipelining.
- State Synchronization: Replay session state (e.g., session variables, prepared statements) to ensure consistency when switching servers or recovering from failover.
- Error Handling: Manage errors during query execution, including extended protocol error recovery and transaction rollback.
- Response Coordination: Collect responses from the PostgreSQL server and forward them to the Client Session.
The Server Session operates as a state machine, transitioning between states based on the current operation and the responses from the PostgreSQL server.
- STARTUP: The initial state where the server session is being established and authenticated.
- READY: The idle state where the server session is waiting for the next batch of messages from the Client Session.
- DEPENDENCIES: The state where the server session is processing dependency messages (e.g., session replay) before executing the actual query batch.
- QUERY: The state where the server session is processing the main query batch.
- EXTENDED_ERROR: The state entered when an error occurs during extended protocol processing. The server session discards all messages until a synchronization point (Sync) is reached.
- RESET_SESSION: The state where the server session is resetting its state to prepare for reuse.
- ERROR: The state entered when a fatal error occurs, leading to session termination.
- Trigger: Server authentication completes successfully.
-
Actions:
- Initialize session parameters.
- Transition to READY state to wait for client messages.
- Trigger: A batch with dependency messages is received.
-
Actions:
- Process dependency messages (e.g., session replay).
- Transition to QUERY state once dependencies are resolved.
- Trigger: A batch with no dependencies is received.
-
Actions:
- Begin processing the query batch.
- Trigger: All dependency messages in the batch are processed.
-
Actions:
- Transition to QUERY state to process the main query batch.
- Trigger: The query batch is fully processed, and the server sends a ReadyForQuery message.
-
Actions:
- Transition back to READY state to wait for the next batch.
- Trigger: An error occurs during extended protocol processing (e.g., Parse, Bind, Execute).
-
Actions:
- Discard all messages in the batch until a Sync message is received.
- Transition to EXTENDED_ERROR state to wait for recovery.
- Trigger: A Sync message is processed, and the server sends a ReadyForQuery message.
-
Actions:
- Transition back to QUERY state to resume normal processing.
- Trigger: The session is being reset for reuse.
-
Actions:
- Clear session state and prepare for reuse.
- Send message to Postgres server to reset session state (e.g., DISCARD ALL, etc)
- Once response is received session can be added back to a session pool indexed by user/database.
- Trigger: An error occurs, or the session is terminated.
-
Actions:
- Close the server connection and clean up resources.
- Client session is notified.
- If the error is fatal, the client session is terminated and error sent to client, otherwise a failover to another session is attempted.
The Server Session interacts with the Client Session through a set of well-defined callbacks. These callbacks allow the Server Session to notify the Client Session about query results, errors, and state changes.
-
Message Response: Notifies the Client Session when a query message has been processed.
- Includes success or failure status.
- Allows the Client Session to forward results or handle errors.
-
Ready For Query: Notifies the Client Session when the server is ready for the next query.
- Includes the transaction status (idle, in-transaction, or error).
- Allows the Client Session to update its state and send the next batch.
-
Error Notification: Notifies the Client Session about errors during query processing.
- Includes error details and the affected query.
-
Session Replay Completion: Notifies the Client Session when dependency messages (e.g., session replay) have been processed.
- Allows the Client Session to send the main query batch.
- Messages are received from the Client Session and added to the batch queue.
- The Server Session processes messages in order, resolving dependencies first.
- Responses are collected from the PostgreSQL server and forwarded to the Client Session.
The Server Session processes messages from the Client Session in batches. A batch is a group of messages that can be sent to the PostgreSQL server in a single pipeline.
A batch consists of:
- Dependency Messages: Messages required to synchronize session state (e.g., SET statements, PREPARE statements).
- Query Messages: The main query or queries to be executed.
- Control Messages: Messages like Sync and Flush that control the flow of the batch.
- Queueing: The Client Session queues a batch of messages for the Server Session.
- Dependency Processing: If the batch contains dependencies, they are processed first.
- Query Execution: The main query messages are processed after dependencies are resolved.
- Completion: The batch is marked complete when all messages are processed, and the server sends a ReadyForQuery message.
Dependencies are messages that must be processed before the main query batch. These include:
- Session Variables: SET statements that configure session-level parameters.
- Prepared Statements: PREPARE statements that define reusable queries.
- Cursors: DECLARE statements for cursors that survive transaction boundaries.
- Dependencies are sent to the server before the main query batch.
- The Server Session transitions to DEPENDENCIES state while processing dependencies.
- Once all dependencies are processed, the Server Session transitions to QUERY state.
The Server Session implements robust error handling to ensure proper recovery and client notification.
- The server sends an ErrorResponse message.
- The Server Session forwards the error to the Client Session.
- The Server Session transitions back to READY state after processing the error.
- An error occurs during Parse, Bind, or Execute.
- The Server Session enters EXTENDED_ERROR state.
- All messages in the batch are discarded until a Sync message is received.
- The Server Session processes the Sync message and transitions back to QUERY state.
- A fatal error occurs (e.g., connection failure, protocol violation).
- The Server Session transitions to ERROR state.
- The session is terminated, and resources are cleaned up.
The Server Session is a critical component of the Springtail proxy, responsible for managing the connection to the PostgreSQL server, processing client queries, and ensuring proper state synchronization. Its design ensures efficient query execution, robust error handling, and seamless integration with the Client Session.