MySQL and PostgreSQL proxies
Get per-statement metrics for SQL traffic without rewriting queries.
What the SQL filters are (and aren't)
envoy.filters.network.mysql_proxy and
envoy.filters.network.postgres_proxy are wire-protocol decoders,
not query routers. They sit on top of tcp_proxy (Lesson 28), parse
SQL frames as bytes flow through, and emit per-statement metrics:
- How many SELECTs, INSERTs, UPDATEs, DELETEs.
- How many transactions committed vs rolled back.
- How many decoder errors.
- How many sessions upgraded to SSL.
They never rewrite or redirect queries. There's no read/write splitting, no shard fan-out, no statement-level RBAC. The value is operational: a uniform Prometheus surface for "what is the database actually doing" without instrumenting every client driver.
Minimal envoy.yaml
filter_chains:
- filters:
- name: envoy.filters.network.postgres_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.postgres_proxy.v3.PostgresProxy
stat_prefix: pg
enable_sql_parsing: true
terminate_ssl: false
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: pg_tcp
cluster: postgres_upstream
For MySQL, swap postgres_proxy for mysql_proxy (same stat_prefix
field, no SQL-parsing toggle). The order of filters matters:
The decoder filter must come before
tcp_proxyin the chain.tcp_proxyis terminal — anything after it never runs.
Three pitfalls
Both filters only parse cleartext. If clients connect with
sslmode=require and Envoy doesn't terminate TLS
(terminate_ssl: true for Postgres), you'll only see
sessions_upgraded_to_ssl increment and zero per-statement
counters. To get statement-level metrics on TLS-encrypted traffic, Envoy
must terminate TLS.
tcp_proxy is terminal. Any filter after it is dead code.
The wire-protocol decoder has to be earlier in the chain so it sees
bytes before they're forwarded.
You get counters (statements_select,
transactions_commit) but not p99 query duration. Pair with
the database-side pg_stat_statements for that.
Hands-on: see SQL stats by command
-
Run Postgres + Envoy with the snippet above.
-
Drive some traffic:
PGPASSWORD=secret psql -h localhost -p 5432 -U postgres -c '
begin;
select 1;
insert into pg_temp.demo (x) select 1 where false;
rollback;
'
- Confirm command counters:
curl -s localhost:9901/stats/prometheus | grep envoy_postgres
You'll see envoy_postgres_statements_select,
envoy_postgres_transactions_rollback, etc.
- Force a decoder error to confirm the error counter works:
echo "garbage" | nc localhost 5432
curl -s localhost:9901/stats | grep pg.errors
postgres_proxy in front of the cluster, labelling metrics by downstream_remote_address via access logs. Within a week the team pinpoints a checkout service whose retry loop was racing on a unique index. No app code changed; the proxy provided the missing visibility.When this is enough — and when it isn't
The SQL proxies are perfect when you want observability without code changes. They're not enough when you need:
- Read/write splitting — use a real proxy like PgBouncer or ProxySQL.
- Query rewriting / sharding — same.
- Per-query latency — use
pg_stat_statementsor APM agents.
For Redis, the next lesson, Envoy goes much further — full cluster-aware routing.
What can Envoy's MySQL/PostgreSQL filters do?