New in pganalyze: Server Groups and Merged Queries

Many production Postgres deployments eventually outgrow a single server. They run in a cluster, or as one of many shards using app-side routing or technologies such as Citus. When it comes to monitoring these setups, it can be challenging to summarize information accurately.

Earlier this year pganalyze launched support for Clusters, a special type of Server Group, along with the ability to see index recommendations across the cluster. Based on the positive feedback, we’ve continued investing in a better experience for large deployments, specifically beyond clusters based on physical replication in Postgres.

Today we’re excited to announce support for creating your own Server Groups in pganalyze. Group any set of servers, such as shards, an environment, or a team’s servers, and see merged query activity across the group, both in aggregate and in the per-query drill-down. This is especially helpful for sharding, where you might have dozens of servers running a similar workload. You can assess the impact of a new query, or discover a query that performs poorly on only some of the shards.

Once you have a dozen or more servers, finding a query that’s slow on only some of them gets hard for any Postgres monitoring tool. Until now, cluster-wide Query Performance in pganalyze listed a query once per member instead of merging it into one row, so you had to compare the same query across rows by hand. Some tools out there show query stats one server at a time, so finding the slow shard means opening each server to track the query down. Others add up the same query across servers, but the total hides which server is slow. The totals can also be incomplete because a query only counts on the servers where it ranks among the most expensive, and gets dropped otherwise.

pganalyze collects every query Postgres tracks on each server, not only the most expensive ones, and matches the same query across servers by its fingerprint. It sums the totals before the list is sorted and paged, so the overall workload is captured and the most expensive queries appear first in the list. To help identify bottlenecks, each merged query highlights the slowest database, and flags it as an outlier when it runs often enough to matter and is significantly slower than its peers.

How to Create a Server Group in pganalyze

A server group is built from explicit membership, match rules, or both. Pick servers directly, or nest other server groups one level deep.

Clusters and Server Groups in the Server dropdown

For rule-based membership, you can use match rules to match on server name, tag, cluster ID, or role. Match on a pattern like accounts-db-*, and every shard you add in the future joins the group automatically. Before saving, a preview shows which servers will join and which will be dropped.

Common ways to manage membership:

  • Sharded workloads: one group per shard set, so Query Performance shows the whole workload in one view
  • Environments: match on the tag env: production, and every production server joins one group
  • Team ownership: the payments team can group the servers and clusters it owns, and members can be scoped to see only what’s theirs

Server Group create flow with a rule-based match on a tag

See the Server Groups docs for more on managing and using server groups.

Tuning Sharded Databases with Group-Wide Query Performance

Navigate to Query Performance with a server group selected in the Server dropdown to see each database across the group’s servers. The new DB Count column shows how many databases are grouped by that name.

Query Performance across a server group, with one row per database group

Pick a database to see a list of queries, one row per fingerprint, with stats summed across members. If your members share no queries, the page looks the same as before. This works for clusters too, so a primary and its replicas now share one row per fingerprint.

Two columns on a merged row help you find the slow database:

  • Runs on: the server of the slowest database
  • Slowest DB: the average time of the slowest database. It turns red when the average time is over 3× the median of the other databases running this query. Databases with under 10% of the average calls are excluded, so one slow call doesn’t outrank a million fast ones. Hovering over this cell shows how much slower it is than the median

To see only the imbalanced queries, check Outliers only. Or expand a row to see its databases, ranked by average time with outliers in red. Each one links to that query on its server, where you can drill further into samples and EXPLAIN plans. The slowest 10 show first, and Show all lists the rest.

Merged queries in one database group, with an outlier expanded to list its databases slowest first

See the Group-Wide Query Performance docs for more details.

Compare One Query Across Every Server in a Group

Click a merged query to see it across the group. Above the SQL statement, three tiles show how slow, how widespread, and out of how many: Slowest Database, Outliers, and Servers. Below that, the Servers panel charts calls per minute for the top 5 servers, with the rest grouped as Other databases, along with a table that lists every server running the query and its individual stats.

A query across every member of a group, with the slowest database, outlier count, and calls per minute by server

Merged Query Details Across the Organization

You don’t need a group to see a query everywhere it runs. Clear the group from the dropdown, and you’ll see the same page through a wider lens: that query across every server in your organization.

We don’t show outlier highlights at this level. Outside a group context, two databases running the same query can belong to unrelated apps, so comparing them would only add noise.

The same query across every server in the organization, without outlier highlights

How It Works Behind the Scenes: Fingerprints vs. queryid

Merging is built on the query fingerprint computed by the collector using pg_query. Postgres has its own identifier, the queryid, but it isn’t portable. It depends on internal object IDs, machine architecture, and the Postgres major version. Postgres only promises a match across servers with the same catalog, like a primary and its physical replicas. Shards built separately aren’t covered, and neither is a fleet partway through an upgrade.

A fingerprint is generated from the query’s parse tree alone, so it ignores case, whitespace, comments, constants, and the length of IN lists. It doesn’t depend on which server ran the query, so the same query on dozens of shards shares the same fingerprint:

PgQuery.fingerprint("SELECT * FROM projects WHERE account_id = 42")
# => "6f57f128eb7a1dc4"
PgQuery.fingerprint("select * from projects where account_id = $1 /* shard 2 */")
# => "6f57f128eb7a1dc4"

See pg_query for Postgres 16 for more on how fingerprints work.

On the Roadmap

Here’s what we’re working on next:

  • Alerts and Check-Up for server groups, so with a group selected, you see only the alerts on its servers
  • A single dashboard for servers and server groups, replacing the current All Servers and All Server Groups pages
  • MCP support for server groups and merged queries, so you can ask the pganalyze MCP server about a group the same way you’d ask about a single server

Try it Now

Server groups and merged queries are available now for all pganalyze cloud customers, and Enterprise Server customers will get them in an upcoming release. Open the Server dropdown and click Add Server Group to create one.


Enjoy blog posts like this?

Get them once a month to your inbox