TheRobotLearns Platform · ProxySQL: from first install to production
Multiplexing Loss and Cache Hygiene
Learning objectives
- Predict when open prepared statements block full client-session backend multiplexing.
- Identify conditions that allow multiplexing to resume after prepared-statement cleanup.
- Explain how text-protocol PREPARE creates persistent locking that affects connection efficiency.
- Use stats_mysql_prepared_statements_info to count cached statements, unreferenced rows, and active references.
- Correlate sustained cache growth with dynamic SQL, unclosed statements, and memory pressure.
Why closing can leave state
You send COM_STMT_CLOSE, yet ProxySQL does not contact the backend connections. A global cache entry is the shared record for a prepared statement. The client-side cleanup removes the numeric statement ID from the per-connection client map. It recycles that ID and decrements the client reference count on that entry.
ProxySQL keeps the statement prepared on any backend connections that have it in their backend map, a connection-level mapping to the backend statement ID. That retention is intentional because other client sessions may be executing the same statement. Those sessions can reuse those backends without paying the lazy preparation cost. A close can reduce one count while backend state remains.
The global cache entry is retained while either reference count is greater than zero. A backend count stays positive when at least one backend connection still has the statement. A client count stays positive when at least one session still holds it.
Takeaway: COM_STMT_CLOSE reduces client references, not backend state or cache eligibility.
How cache entries are reused
Another client sends COM_STMT_PREPARE for the same SQL text, username, and schema. ProxySQL computes a content hash from those three fields. It looks up that hash in the global prepared statement cache, which is shared by all client connections.
If the hash matches an existing entry, ProxySQL does not contact a backend. It retrieves the global entry and its global ID. It assigns a new client-side numeric ID to that global entry, increments the client reference count, and returns cached metadata. The metadata includes column count, parameter count, and field descriptions.
If no matching entry exists, ProxySQL forwards COM_STMT_PREPARE to a backend. The backend returns metadata and its own numeric statement ID. ProxySQL stores the SQL text, parameter metadata, and a new global ID in the global cache. The client receives the same COM_STMT_PREPARE response format either way.
A change to the SQL text, username, or schema can make a lookup miss and create another global entry. Each retained entry remains while a client or backend reference keeps it alive.
Takeaway: Same text, user, and schema can hit the cache; a new context creates a new global entry.
Routing still finds new backends
A statement prepared on one hostgroup can later execute in a different hostgroup. Since v2.1.0, ProxySQL does not lock execution to the hostgroup used during preparation. It re-evaluates query rules at execution time using the statement's SQL text and current session context.
Execution first translates the client-side ID to the global ID. If that mapping is missing, ProxySQL returns an error because the statement was never prepared in this session. Then it selects a connection from the resolved hostgroup's pool.
ProxySQL checks whether that backend connection has the statement in its backend map. If yes, it translates the global ID to the backend-side numeric ID and forwards COM_STMT_EXECUTE. If no, it lazy-prepares the statement on that backend first. Lazy preparation matters because backend connections are numerous and transient, so pre-preparing every statement on every connection would create overhead.
The client sees a normal execute response. It does not see the extra prepare caused by a new backend connection.
Takeaway: Execution routing is re-evaluated, and lazy preparation lets a session run on a fresh backend.
Transient backends keep cache state
A backend connection can disappear from network failure, server restart, or connection pool eviction. ProxySQL discards that connection's backend map entry with the connection. The global cache entry is unaffected. When the next execute reaches a new backend from the same hostgroup, lazy preparation runs again using the cached metadata.
A schema change can make cached metadata stale. If ALTER TABLE or DROP COLUMN changes a referenced table, the stored metadata may no longer match the backend schema. When ProxySQL sees a backend error during COM_STMT_EXECUTE, it re-prepares the statement and calls update_metadata.
One backend event discards only that backend's map entry, while global cache state can remain. Client sessions may still hold counts, and other backends may still hold statements. The global entry only becomes eligible for eviction when the last client and backend references are gone.
Takeaway: Lost backends trigger lazy preparation, while stale metadata is refreshed by re-preparing.
Lifecycle labels you can print
SELECT 'cache hit' AS step, 'increment client reference count' AS effect
UNION ALL SELECT 'COM_STMT_CLOSE', 'backend connections are not contacted'
UNION ALL SELECT 'eviction', 'both ref counts reach zero';The first row says that a cache hit increments the client reference count. The second row says that COM_STMT_CLOSE does not contact backend connections. The third row says that eviction becomes possible when both ref counts reach zero.
Try it: config_layer_sim
Run the three SELECT rows from the worked example in order. For each row, decide whether the step affects the client count, the backend count, both, or neither. Compare that choice with the effect label printed by the statement.
Sources
- 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 Statement Lifecycle
- 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 Statement Lifecycle
- 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 Statement Lifecycle
- 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 Statement Lifecycle
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.