If you’ve ever used Postgres and thought, “wow, this is such a simple piece of software, I understand every part of it perfectly,” you have not yet met the query planner.Now consider sharding that database across a thousand servers. How would you serve a query?All you'd need is a system that replicates the Postgres auth system, wire protocol, and parser, plus a shard-aware distributed query planner, graceful handling of all server failure scenarios, and connection pooling that overcomes the Postgres process-per-connection architecture. Easy, right?Let's follow the journey of a Postgres query through all the layers of this elegant yet beautifully complex sharded system. Doing so will help us understand what goes into making large-scale sharded Postgres deployments appear to be a single Postgres server even when they span thousands of servers.Though a real database may have hundreds of tables, in this example we will keep the schema simple: two tables spread across four shards.CREATE TABLE customers (
id BIGINT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
country TEXT NOT NULL,







