TheRobotLearns Platform · ProxySQL: from first install to production
COM_STMT Lifecycle and Execution Routing
Learning objectives
- Trace COM_STMT_PREPARE cache-hit flow from hash computation to client metadata response.
- Trace COM_STMT_PREPARE cache-miss flow through backend prepare, ID capture, global entry, and client mapping.
- Translate COM_STMT_EXECUTE client IDs through global entries to current backend connection IDs.
- Predict execute-time routing using current session rules, not only the prepare hostgroup.
- Relate COM_STMT_CLOSE to removing client mappings and decrementing client references.
Why the same ID breaks forwarding
A client prepares a query once and receives a numeric identifier. Later, it sends execute and close messages that reference that identifier. Each backend connection has its own numeric namespace, so the same query prepared on two backends receives different statement IDs.
ProxySQL multiplexes many client connections onto shared backend connections. The execute may land on a backend connection that never saw the prepare. ProxySQL therefore cannot forward the client identifier unchanged.
Doing so would make execution fail or produce incorrect results. The client should also not need to know it is talking to a proxy. It sends standard MySQL protocol messages and receives standard responses.
ProxySQL defines three identifier levels to make that work. A client-side ID is the numeric identifier meaningful only within one client session. A global ID is process-wide and represents a unique statement. A backend-side ID is the numeric identifier scoped to one backend connection.
A backend-side ID is the numeric identifier scoped to one backend connection. The mapping keeps each side inside its own namespace.
Takeaway: ProxySQL needs client-side, global, and backend-side IDs to avoid wrong execution.
How prepare finds a global entry
When a client sends COM_STMT_PREPARE, ProxySQL intercepts the request before any backend sees it. It computes a content hash from the SQL text, username, and schema. The hash is the key for the global prepared statement cache.
If the hash matches an existing entry, ProxySQL returns cached metadata and never contacts a backend. The client receives column count, parameter count, and field descriptions from the existing global entry. ProxySQL assigns a client-side numeric ID to that global entry and increments the client reference count.
If the hash does not match, ProxySQL forwards COM_STMT_PREPARE to a backend selected by query rules or the default hostgroup. The backend response carries metadata and a backend-side numeric statement ID. ProxySQL creates a new global entry with the SQL text, parameter metadata, and a newly assigned global ID.
It records the backend-side ID in the backend map for that connection. It maps the client-side ID to the global ID in the per-connection client map. It returns metadata to the client.
Takeaway: Cache hits return metadata; cache misses create global and backend maps.
How execute chooses current backend
When a client sends COM_STMT_EXECUTE, ProxySQL first looks up the client-side ID in the per-connection client map. If that map has no entry, the statement was never prepared in this session and ProxySQL returns an error.
If a global ID is found, ProxySQL re-evaluates query rules using the statement SQL text and current session context. Since v2.1.0, ProxySQL no longer locks execution to the hostgroup used during preparation. This matters when a statement prepared against a reader hostgroup is executed inside a write transaction.
ProxySQL selects a connection from the resolved target hostgroup. It checks that connection's backend map for the global ID. If the backend map has a backend-side ID, ProxySQL forwards COM_STMT_EXECUTE with that ID and the client parameter bindings.
If the backend map lacks the ID, ProxySQL sends COM_STMT_PREPARE to that backend using global cache metadata. The backend returns a new numeric ID, ProxySQL records it in the backend map, and execution proceeds. The client sees no indication that an extra prepare happened.
Takeaway: Execute uses the client map, global ID, and current rules.
Why close does not touch backends
When a client sends COM_STMT_CLOSE, ProxySQL removes the client-side numeric ID from the per-connection client map. It recycles that client-side ID so the session can reuse it for a future prepare. It decrements the client reference count on the corresponding global cache entry.
It does not contact backend connections during the close. Other clients may still execute the same statement, and future executions can reuse backend statements without lazy preparation.
A global entry is retained while either reference count is greater than zero. It becomes eligible for eviction only when client references and backend references both reach zero. Backend reference count changes when backend connections are destroyed.
If a backend connection is lost, its backend map is discarded and the global entry remains. On a later execute routed to another connection, lazy preparation runs from global metadata. If a backend reports a metadata mismatch after DDL, ProxySQL re-prepares on the backend and calls update_metadata to refresh the global entry.
Takeaway: COM_STMT_CLOSE removes client mapping and decrements client refs only.
Trace the protocol commands
SELECT 'COM_STMT_PREPARE' AS protocol_command;
SELECT 'COM_STMT_EXECUTE' AS protocol_command;
SELECT 'COM_STMT_CLOSE' AS protocol_command;This example returns the names COM_STMT_PREPARE, COM_STMT_EXECUTE, and COM_STMT_CLOSE.
The first query names COM_STMT_PREPARE, which a client sends when preparing a statement.
The second query names COM_STMT_EXECUTE, which ProxySQL processes after translating a client-side ID to a global ID.
The third query names COM_STMT_CLOSE, which removes the client-side mapping and decrements the client reference count.
Sources
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statements Architecture
- Prepared Statements
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statements Architecture
- Prepared Statements
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statement Lifecycle
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
Do more with an account
- Graded quizzes is available on your account.
- Saving your progress is available on your account.
- Hands-on labs is available on your account.
- AI tutoring is available on your account.