How do you shard a PostgreSQL table across servers with postgres_fdw or Citus?

May 14, 2025

I searched for postgresql distributed and opened Gustavo Valle’s article on sharding strategies, followed by the PostgreSQL wiki’s sharding page. Valle uses customer orders to explain how to choose a shard key. The wiki raises a different question. Its stated purpose is to “maintain a todo list for adding the sharding functionality in the core.”12 So what could you actually do with a PostgreSQL installation in May 2025?

Say a shop writes every order to one table. Partitioning lets the application keep using that table name while PostgreSQL stores groups of rows separately. Sharding puts those groups on different servers. PostgreSQL 17 can route inserts into partitions, including partitions backed by remote PostgreSQL tables. Citus 13.0, available by February 2025, can also distribute an existing table and move its shards when you add machines.3456

Do I have to create every partition on day one?

Suppose the shop wants to remove a month of old orders at a time. A date range works well because all the rows that expire together live in the same partition. At the time you create the parent table, you choose the partitioning method and key. You can add each month’s partition when you need it.3

CREATE TABLE orders_by_month (
    order_id bigint NOT NULL,
    customer_id bigint NOT NULL,
    ordered_on date NOT NULL
) PARTITION BY RANGE (ordered_on);

CREATE TABLE orders_2025_05 PARTITION OF orders_by_month
    FOR VALUES FROM ('2025-05-01') TO ('2025-06-01');

INSERT INTO orders_by_month VALUES (101, 42, DATE '2025-05-14');
SELECT tableoid::regclass FROM orders_by_month WHERE order_id = 101;

The last query identifies the table holding the row: orders_2025_05. June 1 is outside this partition because the upper bound is exclusive. You can add the June partition while the shop is running. Until there is a matching partition or a default partition, a June insert fails. When lock duration matters, prepare a standalone table with a matching CHECK constraint, then use ATTACH PARTITION. Attaching takes a lighter lock on the parent than creating a partition directly.7

An existing ordinary table takes a different migration path. PostgreSQL 17 requires a new partitioned parent. If all the old rows fit one partition’s bounds, you can attach the old table as that partition and keep its rows in place. If the old rows span several intended partitions, plan how to copy them, capture writes that arrive during the copy, and switch the application to the new parent.37

How does an insert reach another PostgreSQL server?

Suppose the shop now wants customers 1–999 on server A and customers 1000 and up on server B. PostgreSQL ships postgres_fdw, a foreign data wrapper that exposes a table on another PostgreSQL server through a local table definition. Enable it in the entry database with CREATE EXTENSION postgres_fdw. Define each remote connection with CREATE SERVER and give the application role a USER MAPPING for each server.4

Here is the routing definition for that example. It assumes server objects named shop_a and shop_b, working user mappings, and a physical public.orders table with matching columns on each remote server. Enforce the corresponding customer range on each physical table too, so direct writes there obey the same rule.8

CREATE TABLE orders_by_customer (
    order_id bigint NOT NULL,
    customer_id bigint NOT NULL
) PARTITION BY RANGE (customer_id);

CREATE FOREIGN TABLE orders_a PARTITION OF orders_by_customer
    FOR VALUES FROM (1) TO (1000)
    SERVER shop_a OPTIONS (schema_name 'public', table_name 'orders');

CREATE FOREIGN TABLE orders_b PARTITION OF orders_by_customer
    FOR VALUES FROM (1000) TO (MAXVALUE)
    SERVER shop_b OPTIONS (schema_name 'public', table_name 'orders');

INSERT INTO orders_by_customer VALUES (102, 1042);
EXPLAIN (VERBOSE, COSTS OFF)
SELECT order_id FROM orders_by_customer WHERE customer_id = 1042;

The insert lands on server B. For the lookup, PostgreSQL can rule out orders_a from the customer ID. This is partition pruning. Look for a foreign scan of orders_b and the remote SQL in the plan. A lookup using only order_id = 102 gives the planner no customer range to eliminate. If a third server joins, you still own the placement map. Moving customers 1500 and up to it requires copying their rows, coordinating concurrent writes, and changing the partition bounds and foreign-table definitions at cutover.34

Can I shard a table that is already taking orders?

Citus is a separately released open-source PostgreSQL extension. Its coordinator keeps track of which worker server holds each shard. With row-based sharding, you choose a distribution column such as customer_id. Citus hashes the value to find the shard, then consults its placement records to find the worker. It can send a single-customer lookup to one worker or have workers count their own orders in parallel and combine the counts.59

Consider an ordinary orders table that already holds data on the coordinator of a configured Citus cluster. Suppose orders are identified within each customer account, so its primary key is (customer_id, order_id). That key includes the distribution column, as Citus requires for a primary key on this kind of table. It also gives replication a way to identify the row when an order changes.1011

-- Run on the coordinator, outside an explicit transaction block.
SELECT create_distributed_table_concurrently('orders', 'customer_id');

The ordinary create_distributed_table call blocks writes while it copies existing rows. The concurrent form uses logical replication to catch up with changes made during the copy. If the table uses only order_id as its primary key, resolve the uniqueness requirement before choosing customer-based distribution. Changing to a composite key would allow two customers to use the same order ID. Also check foreign keys involving the table and its replica identity, the key replication uses for updates and deletes.1110

What happens when I add a third worker?

A shard is a unit Citus can move. One worker can already hold several shards, so an additional worker can take over some of them while each customer keeps the same distribution key. On the coordinator, register the prepared worker and start a rebalance.6

SELECT citus_add_node('worker-c', 5432);
SELECT citus_rebalance_start();
SELECT * FROM citus_rebalance_status();

Registering the worker makes it available. The rebalance job moves existing shards. During a move, logical replication copies a shard and catches up with intervening writes. At cutover, Citus briefly locks writes to that shard and updates its placement. The next lookup for an affected customer can then reach the new worker through the same coordinator.6

What does the shop need?Which mechanism fits?
Retire May’s orders together on one serverUse a local range partition on the order date.
Keep two known customer ranges on designated serversDefine foreign partitions with postgres_fdw and maintain their placement explicitly.
Distribute a growing table while customers keep placing ordersChoose a Citus distribution column that fits the queries and constraints, then use concurrent distribution.
Give a new worker a share of existing ordersRegister the worker and start a Citus shard rebalance.

References

Footnotes

  1. Valle, G. (2024, March 28). Exploring effective sharding strategies with PostgreSQL (“Understanding Sharding”). Medium. Article. ↩

  2. PostgreSQL Wiki contributors. (n.d.). WIP PostgreSQL sharding (opening paragraph). PostgreSQL Wiki. Page. ↩

  3. PostgreSQL Global Development Group. (n.d.). Table partitioning (§§ 5.12.2, 5.12.4). PostgreSQL 17 Documentation. Declarative partitioning and partition pruning. ↩ ↩2 ↩3 ↩4

  4. PostgreSQL Global Development Group. (n.d.). postgres_fdw (introduction and § F.36.6). PostgreSQL 17 Documentation. Setup and remote query optimization. ↩ ↩2 ↩3

  5. Citus Data. (2025, February). Release notes for Citus 13.0. Citus Data. PostgreSQL 17 support. ↩ ↩2

  6. Citus Data. (n.d.). Cluster management. Citus 13.0 Documentation. Adding a worker and rebalancing existing shards. ↩ ↩2 ↩3

  7. PostgreSQL Global Development Group. (n.d.). Partition maintenance (§ 5.12.2.2). PostgreSQL 17 Documentation. Adding and attaching partitions. ↩ ↩2

  8. PostgreSQL Global Development Group. (n.d.). CREATE FOREIGN TABLE (“Synopsis” and “Notes”). PostgreSQL 17 Documentation. Foreign partitions and constraint enforcement. ↩

  9. Citus Data. (n.d.). Concepts. Citus 13.0 Documentation. Coordinator and workers. ↩

  10. Citus Data. (n.d.). Creating and modifying distributed objects. Citus 13.0 Documentation. Adding and removing constraints. ↩ ↩2

  11. Citus Data. (2022, September). Release notes for Citus 11.1. Citus Data. Concurrent distribution and its restrictions. ↩ ↩2

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-05-14). How do you shard a PostgreSQL table across servers with postgres_fdw or Citus?. https://caiborui.com/blog/2025-05-14-en-postgresql-sharding-postgres-fdw-citus

@misc{borui2025-63192281,
  author = {borui},
  title = {{How do you shard a PostgreSQL table across servers with postgres_fdw or Citus?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-05-14-en-postgresql-sharding-postgres-fdw-citus}
}