ELSEIF
Your brief EB
214 stories from 185 feeds 1201 clusters Refreshed 6 minutes ago next pull 16:37

DATABASES Signal 514 2 feeds carried it

Sharded Postgres query traverses router, four shards, and distributed planner to return unified result

A single SQL query executes across multiple Postgres shards via a router that replicates Postgres auth, protocol, and query planning.

WHY IT MATTERS

Engineers scaling Postgres must understand how sharding transforms a simple query into a distributed operation. The router’s complexity is hidden from applications, but its failure modes and performance costs are not. This lifecycle reveals where latency, consistency, and availability trade-offs emerge in production.

Written by elseif from the cluster below · every claim links back to a source

The three things worth knowing

01

The router authenticates clients and parses queries without exposing shard topology to applications.

02

Shard keys determine row placement, but co-location of joined tables is not guaranteed, complicating joins.

03

Postgres wire protocol support (Simple and Extended) ensures compatibility with any client driver.

THE READ

What the cluster adds up to.

ORIGINAL ANALYSIS

A sharded Postgres query begins with authentication handled by a router that mimics Postgres’s SCRAM-SHA-256 exchange. The router validates credentials and session settings before accepting the query, acting as a single entry point for the distributed system. This design isolates applications from shard topology, but the router itself becomes a critical dependency. Its failure would break all queries, even if individual shards remain healthy. The router’s implementation in Go suggests a focus on performance, but its role as a centralized bottleneck introduces operational risk.

The query then enters the protocol layer, where the router must support both Simple and Extended Postgres wire protocols. Simple protocol sends the entire SQL statement in one message, while Extended protocol splits it into Parse, Bind, Describe, Execute, and Sync steps. This dual support ensures compatibility with any Postgres client, but it also doubles the router’s parsing and planning workload. The router must reconstruct the query from fragmented messages in Extended mode, adding latency. For engineers, this means query performance can vary unpredictably based on driver settings, even for identical SQL.

Shard keys determine where rows are stored, but the example reveals a fundamental challenge: joined tables may not be co-located. Here, `customers` and `orders` are sharded by their primary keys, so a customer’s orders could reside on a different server. The router must fetch rows from multiple shards, perform the join in memory, and return a unified result. This distributed join is transparent to the application but introduces network latency and potential consistency issues if shards are out of sync. The trade-off is clear: sharding enables horizontal scaling, but at the cost of join performance and complexity in query planning.

The router’s distributed query planner must rewrite the original SQL to target individual shards. For the given query, it likely fetches `orders` from all shards first, then uses the `customer_id` values to request matching `customers` rows. This two-phase approach minimizes data transfer but requires careful handling of failures. If a shard is unavailable, the router must either fail the entire query or return partial results, depending on the system’s consistency guarantees. Engineers must design queries with sharding in mind, as operations that span many shards will incur higher latency and resource costs.

Written by elseif from the cluster below · checked for specifics the sources never contained

THE CLUSTER

Same story, 2 feeds.

ORDERED BY FIRST SEEN
Blog — PlanetScale The lifecycle of a sharded Postgres query Open ↗
Blog — PlanetScale via Lobsters The lifecycle of a sharded Postgres query Open ↗