TheRobotLearns Platform · ProxySQL: from first install to production

Query Rules and Extended Query Routing

  • about 15 min read

Learning objectives

  • Create pgsql_query_rules routing by username, match_digest, and destination_hostgroup, then activate them.
  • Explain rule evaluation by rule_id order, active-only filtering, and apply terminating evaluation.
  • Relate rule matching to PostgreSQL extended query protocol support and version-specific behavior.
  • Use stats_pgsql_query_digest digest_text patterns to build match_digest expressions.

Route selected queries away from default

You connect a PostgreSQL user that has a default_hostgroup value. If no matching query rule is found for the user’s queries, ProxySQL routes those queries to that hostgroup.

To send a specific PostgreSQL query to another instance, you add a row to pgsql_query_rules. A rule can match the connected username and a query digest pattern, then set a destination_hostgroup. The PostgreSQL admin interface reaches these configuration tables over port 6132.

The first-time example uses rule_id=10, active=1, username='appuser', match_digest='^SELECT .* FROM read_table', destination_hostgroup=2, and apply=1. Those values remain in the in-memory tables until you load them to the runtime. pgsql_query_rules is analogous to mysql_query_rules, so the same layered workflow applies.

You can also match full query text with match_pattern, and the table has filters for database, client_addr, proxy_addr, proxy_port, and digest.

Takeaway: pgsql_users.default_hostgroup handles unmatched traffic, and pgsql_query_rules handles selected queries.

Rule order, active rows, and apply

ProxySQL evaluates rules in ascending rule_id order. It ignores rules with active=0, so only active=1 rows participate in routing.

A rule can choose users and queries through username, match_digest, and match_pattern. The digest field filters by a query digest, and negate_match_pattern makes only nonmatching queries count. The destination_hostgroup column is the hostgroup that receives the matched query. If apply=1, no further rules are evaluated after this rule is matched and processed.

You can set re_modifiers to CASELESS, which makes regular-expression matching case-insensitive. A rule can also block a query with error_msg or return OK_msg, while routing uses destination_hostgroup.

The example rule in the first-time setup uses rule_id=10, active=1, username='appuser', match_digest='^SELECT .* FROM read_table', destination_hostgroup=2, and apply=1.

For chained routing, initial matching uses rules with flagIN=0. If a matched rule sets flagOUT to a non-zero value and apply is not set, the query is re-evaluated. Fast routing then checks pgsql_query_rules_fast_routing using its flagIN, which must align with flagOUT or apply from the main rule table.

Takeaway: rule_id sets order, active gates participation, and apply=1 ends evaluation.

Load and save the query rule

From the admin interface, make changes in the in-memory pgsql_query_rules configuration table. Configuration changes must be explicitly loaded to runtime to take effect.

The example inserts a rule with rule_id=10, active=1, username='appuser', match_digest='^SELECT .* FROM read_table', destination_hostgroup=2, and apply=1.

INSERT INTO pgsql_query_rules
  (rule_id, active, username, match_digest, destination_hostgroup, apply)
  VALUES (10, 1, 'appuser', '^SELECT .* FROM read_table', 2, 1);
SELECT * FROM pgsql_query_rules;

Run LOAD PGSQL QUERY RULES TO RUNTIME to apply the change to the running proxy. The active form is exposed in runtime_pgsql_query_rules.

LOAD PGSQL QUERY RULES TO RUNTIME;
SAVE PGSQL QUERY RULES TO DISK;

Run SAVE PGSQL QUERY RULES TO DISK to persist the change across restarts.

Takeaway: Make the change in memory, load it to runtime, and save it to disk.

Extended protocol and digest patterns

ProxySQL v3.0.3 and later support PostgreSQL’s Extended Query Protocol, including full prepared statement handling with global caching and lazy backend preparation. Rule matching uses the query rule columns: match_digest matches the query digest, while match_pattern matches the query text. A rule can also filter by database, which is the schema being accessed.

Use negate_match_pattern to consider only queries that do not match the pattern. The re_modifiers option CASELESS makes regular-expression matching case-insensitive.

For digest patterns, start from the PostgreSQL statistics tables. stats_pgsql_query_digest stores per-query statistics, including response times. Other PostgreSQL statistics tables include stats_pgsql_connection_pool and stats_pgsql_commands_counters. The hostgroup traffic query selects hostgroup, SUM(sum_time), and SUM(count_star) from stats_pgsql_query_digest and groups by hostgroup.

The routing example uses ^SELECT .* FROM read_table, so a matching rule can send those queries to a different destination_hostgroup. You can create a pgsql_query_rules row with a chosen regular expression and activate it with LOAD PGSQL QUERY RULES TO RUNTIME.

Takeaway: Inspect stats_pgsql_query_digest, then route queries with match_digest or match_pattern on ProxySQL v3.0.3+.

Route appuser reads to hostgroup 2

INSERT INTO pgsql_query_rules
  (rule_id, active, username, match_digest, destination_hostgroup, apply)
VALUES
  (10, 1, 'appuser', '^SELECT .* FROM read_table', 2, 1);

SELECT * FROM pgsql_query_rules;

LOAD PGSQL QUERY RULES TO RUNTIME;

SAVE PGSQL QUERY RULES TO DISK;

SELECT * FROM runtime_pgsql_query_rules;

The INSERT adds the sample row to in-memory pgsql_query_rules with rule_id=10, active=1, username='appuser', match_digest='^SELECT .* FROM read_table', destination_hostgroup=2, and apply=1. SELECT * FROM pgsql_query_rules; reads the in-memory configuration row. LOAD PGSQL QUERY RULES TO RUNTIME; applies active rules to the running proxy. SAVE PGSQL QUERY RULES TO DISK; persists them across restarts. SELECT * FROM runtime_pgsql_query_rules; returns the runtime projection of active rules.

Try it: query_rule_playground

Insert two rules into pgsql_query_rules: one uses rule_id=10, active=1, username='appuser', match_digest='^SELECT .* FROM read_table', destination_hostgroup=2, and apply=1; the other uses rule_id=11, active=1, the same username and digest, hostgroup 2, and apply=0. Run LOAD PGSQL QUERY RULES TO RUNTIME. Query runtime_pgsql_query_rules. Both rows appear in the runtime projection, because they are active. A query matched by rule 10 is not evaluated by rule 11, because apply=1 stops further rules.

Sources

Do more with an account

Query Rules and Extended Query Routing — ProxySQL: from first install to production