TheRobotLearns Platform · ProxySQL: from first install to production
PostgreSQL Statistics and Session Inspection
Learning objectives
- Aggregate traffic by hostgroup in stats_pgsql_query_digest using count_star and sum_time.
- Inspect connection-pool and command-counter statistics, then identify frequent or expensive normalized queries from digest tables.
- Verify routing rules via stats_pgsql_query_rules hits and monitor frontend connections via stats_pgsql_users.
- Read current sessions from stats_pgsql_processlist or stats_pgsql_stat_activity, then review bounded query-event rows for SQLSTATE and error context.
- Reset digest statistics for delta measurements by selecting from stats_pgsql_query_digest_reset.
Aggregate PostgreSQL traffic by hostgroup
You add 10.0.0.1, 10.0.0.2, and 10.0.0.3 to hostgroup 1 through pgsql_servers. SELECT * FROM pgsql_servers returns those configuration rows. ProxySQL keeps PostgreSQL statistics in the stats schema. A hostgroup is an integer identifier used by pgsql_servers, pgsql_users, and pgsql_query_rules to group backend servers.
The stats_pgsql_query_digest table holds per-query statistics, including response times. The sample query groups that table by hostgroup, then adds sum_time and count_star for each group. This query reads stats_pgsql_query_digest instead of changing it. You connect to the PostgreSQL-compatible Admin interface on port 6132 to run it.
SELECT hostgroup hg, SUM(sum_time), SUM(count_star)
FROM stats_pgsql_query_digest
GROUP BY hostgroup;The output groups rows by hostgroup. SUM(count_star) adds the count_star values in each group. SUM(sum_time) adds the sum_time values in each group.
The table gives per-query statistics, so these totals describe the statements captured for each hostgroup. After editing backend rows, LOAD PGSQL ... TO RUNTIME applies them to the running proxy. SAVE PGSQL ... TO DISK persists them across restarts. PostgreSQL configuration uses tables and variables prefixed with pgsql_.
Takeaway: Group stats_pgsql_query_digest by hostgroup and inspect SUM(count_star) and SUM(sum_time).
Read pool and command counters
The PostgreSQL statistics list names three key tables. stats_pgsql_connection_pool gives connection and traffic statistics per hostgroup. stats_pgsql_commands_counters gives query type distribution and execution times. stats_pgsql_query_digest gives per-query statistics, including response times.
The listed PostgreSQL statistics tables use the pgsql_ prefix. The digest query added SUM(sum_time) and SUM(count_star) for each hostgroup. The pool table supplies connection and traffic rows for each hostgroup. The command-counter table supplies query type and execution-time rows.
Performance tracking includes connection usage, query latency, and error rates. These tables cover the connection and latency parts of that list. The stats schema is separate from the pgsql_* configuration tables. Configuration tables include pgsql_servers, pgsql_users, and pgsql_query_rules.
These stats give real-time insight into performance and health. You read them through the PostgreSQL-compatible Admin interface on port 6132. The Admin workflow uses LOAD PGSQL ... TO RUNTIME and SAVE PGSQL ... TO DISK commands. Configuration rows become live after a runtime load, while statistics rows report what the proxy has observed.
Takeaway: Use stats_pgsql_connection_pool for per-hostgroup pool and traffic rows, and stats_pgsql_commands_counters for command type and timing rows.
Confirm routing and user caps
You inserted rule 10 in pgsql_query_rules. The rule sets active=1 and username='appuser'. It also sets match_digest='^SELECT .* FROM read_table'. It routes to destination_hostgroup=2 and stops with apply=1.
ProxySQL evaluates rules in ascending rule_id order. Only active=1 rules are considered. If apply=1 matches, further rule evaluation stops. The destination_hostgroup field routes matched queries to the specified hostgroup.
pgsql_users controls frontend and backend identity. A row with frontend=1 authenticates to the ProxySQL instance. The max_connections field defines the maximum number of frontend connections for a user. Currently, PostgreSQL users require both frontend and backend to be 1.
Setting transaction_persistent=1 keeps transactions in the hostgroup where they started. You load rules with LOAD PGSQL QUERY RULES TO RUNTIME and save them with SAVE PGSQL QUERY RULES TO DISK. Run these statements to inspect the configured rule and user rows.
SELECT * FROM pgsql_query_rules;
SELECT * FROM pgsql_users;The first SELECT returns the rows from pgsql_query_rules. The second SELECT returns the rows from pgsql_users. A loaded rule can still stop later checks because apply=1 ends evaluation for that query.
Takeaway: Use pgsql_query_rules order and apply, plus pgsql_users defaults and max_connections, to confirm the routing path.
Aggregate PostgreSQL time and counts
SELECT hostgroup hg, SUM(sum_time), SUM(count_star)
FROM stats_pgsql_query_digest
GROUP BY hostgroup;The SELECT groups stats_pgsql_query_digest rows by hostgroup. SUM(sum_time) adds the sum_time values in each group. SUM(count_star) adds the count_star values in each group. The GROUP BY hostgroup clause creates one output group per hostgroup. The table holds per-query statistics, including response times.
Try it: query_rule_playground
Insert rule 10, run LOAD PGSQL QUERY RULES TO RUNTIME, run SELECT * FROM pgsql_query_rules, then run the hostgroup aggregate on stats_pgsql_query_digest. You will see rule 10 in the rule rows and SUM(sum_time) and SUM(count_star) values in the aggregate rows.
Sources
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- PostgreSQL Tables
- PostgreSQL Tables
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- PostgreSQL Tables
- PostgreSQL Tables
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
- How to Configure ProxySQL for PostgreSQL for the First Time
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.