envoy-guide
Module 8 · Beyond HTTP·10 min·intermediate

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

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_proxy in the chain. tcp_proxy is terminal — anything after it never runs.

Three pitfalls

TLS blinds the decoder

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.

Order matters: decoder before tcp_proxy

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.

No latency histograms per query

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

Hands-on · Stand it up, query it, count it
  1. Run Postgres + Envoy with the snippet above.

  2. Drive some traffic:

Shell
PGPASSWORD=secret psql -h localhost -p 5432 -U postgres -c '
  begin;
  select 1;
  insert into pg_temp.demo (x) select 1 where false;
  rollback;
'
  1. Confirm command counters:
Shell
curl -s localhost:9901/stats/prometheus | grep envoy_postgres

You'll see envoy_postgres_statements_select, envoy_postgres_transactions_rollback, etc.

  1. Force a decoder error to confirm the error counter works:
Shell
echo "garbage" | nc localhost 5432
curl -s localhost:9901/stats | grep pg.errors
Real-world example · A Rails fleet hammering one Postgres cluster
Problem
200+ Rails services share a Postgres cluster, and the DBAs can't tell which app is generating the spike of ROLLBACKs at 3am. Each app team blames the others; nobody wants to add per-query telemetry to their app code.
Envoy solution
Envoy with 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_statements or APM agents.

For Redis, the next lesson, Envoy goes much further — full cluster-aware routing.

Quick check

What can Envoy's MySQL/PostgreSQL filters do?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3