Citus is the PostgreSQL extension that turns a coordinator-plus-workers fleet into one logical database with sharded tables. It is the answer when a single-node Postgres has hit its CPU, memory, or write-IOPS ceiling, and the workload is amenable to a distribution key. This article goes past install — it covers picking the distribution column, reference tables, colocation, the planner’s distributed path, and what rebalancing actually does. For the install walkthrough see Citus install on AlmaLinux.
How to verify
Start with the coordinator and look at the worker fleet:
psql -d app -c "SELECT * FROM citus_version();"
psql -d app -c "SELECT * FROM pg_dist_node ORDER BY groupid;"
psql -d app -c "SELECT logicalrelid::regclass, partmethod, colocationid
FROM pg_dist_partition;"
psql -d app -c "SELECT * FROM citus_tables;"
psql -d app -c "EXPLAIN (ANALYZE, VERBOSE)
SELECT count(*) FROM orders WHERE tenant_id = 42;"
The EXPLAIN output for a distributed query shows a Custom Scan (Citus Adaptive) node — the coordinator’s plan with the worker subplans inlined. If the query targets a single shard, only one worker runs.
What’s happening
Citus splits a logical table into N shards (default 32) by hashing the distribution column. Each shard is a regular Postgres table on a worker node — named tablename_<shardid>. The coordinator holds the metadata (pg_dist_partition, pg_dist_shard, pg_dist_placement) and rewrites SQL so each worker runs only the part of the query touching its shards.
The three table kinds matter:
- Distributed tables are sharded by the distribution column. Queries that include the distribution column in the WHERE go to one shard; queries without it fan out to all shards and aggregate at the coordinator.
- Reference tables are replicated in full to every worker. Use for small lookup tables (countries, plans, feature flags) that join with everything. Writes go through the coordinator and synchronously update every replica.
- Local tables live only on the coordinator and don’t participate in distribution.
Colocation is the lever that makes joins fast. Two distributed tables in the same colocation group share the shard placement function — tenant_id’s shard 7 of orders lives on the same worker as shard 7 of order_items. A JOIN on tenant_id runs entirely on each worker without a network round-trip.
The procedure
-
Pick the distribution column with intent. Most multi-tenant SaaS workloads use
tenant_id— every query already filters by it, and the shape gives clean isolation. Pick wrong and you get cross-shard joins that move terabytes over the network. -
Promote tables:
SELECT create_distributed_table('orders', 'tenant_id'); SELECT create_distributed_table('order_items', 'tenant_id', colocate_with => 'orders'); SELECT create_reference_table('countries');colocate_withis the magic word — it pinsorder_itemsto the same colocation group asorders. Without it, the planner has no guarantee the sametenant_idlives on the same worker. -
Verify the planner picked the path you expected:
EXPLAIN (ANALYZE, VERBOSE) SELECT o.id, oi.product_id, c.name FROM orders o JOIN order_items oi ON oi.order_id = o.id AND oi.tenant_id = o.tenant_id JOIN countries c ON c.code = o.shipping_country WHERE o.tenant_id = 42 AND o.created_at >= now() - interval '7 days';You want a single-shard fan-out (one worker, one task). If the plan shows
Task Count: 32, the WHERE didn’t constrain to a single tenant — every shard is being scanned. -
Add the shard count to the metadata, not the SQL. Default is 32. For very large clusters you can bump it (
citus.shard_count = 64) before creating tables — but each shard is a separate Postgres table per worker, so 1000 distributed tables at 64 shards each is 64,000 child tables to vacuum and analyze. -
Rebalance when adding workers:
SELECT citus_add_node('worker04.internal', 5432); SELECT * FROM citus_rebalance_start(); SELECT * FROM get_rebalance_progress();The rebalancer moves shards while serving traffic — but a heavy rebalance during peak hours will hurt. Schedule it to a low-traffic window and watch progress.
-
Pin the connection layer. Application connections must hit the coordinator. Pool with PgBouncer on the coordinator side. Direct worker access is for ops only — bypassing the coordinator means writing to a shard without the metadata catalog knowing.
Common pitfalls
- A query missing the distribution column scans all shards in parallel — fine for analytics, brutal for OLTP under load. Audit query patterns before promoting tables.
INSERT ... SELECTbetween distributed and reference tables can require repartitioning under the hood; check the EXPLAIN before running it on large data.- DDL (CREATE INDEX, ALTER TABLE) is run through the coordinator’s distributed DDL — schemas drift if you run DDL directly on workers.
- Sequences are coordinator-only; an
id bigserialPK on a distributed table allocates from the coordinator and contends. Usebigintwith application-side ID generation (Snowflake, UUID v7). - Citus reference tables replicate every write — they’re not free. Watch their size and use only for slow-changing lookup tables.
In the engagements we run, Citus is the right choice for multi-tenant SaaS that has outgrown one box — single-tenant filter, colocated joins, reference tables for small lookups. The operational discipline (distribution column audits, EXPLAIN reviews, rebalance windows) is what keeps it from regressing as the app evolves. For the operating model behind a Citus footprint, see clustered environments.