Skip to main content
In this lesson we’ll test different shard keys on our Citus tables and observe which keys produce even data distribution versus which cause shard-size skew. The goal is to select a shard key that balances storage and query load across worker nodes.

1) Check the current distribution column

Query the Citus metadata table citus_tables to see which column each distributed table uses:
This tells you the column Citus currently hashes on for each distributed table.

2) Inspect how rows are spread across shards

Use run_command_on_shards to run a per-shard row count and inspect balance. For example, count rows per shard on users_by_country:
From the output above you can see per-shard row totals. Ideally, a well-chosen shard key produces roughly even counts across shards; significant imbalance signals a poor key for uniform distribution.

3) How to evaluate shard-key candidates

When evaluating shard keys, prefer columns with these properties:
When picking a shard key, aim for high-cardinality columns that are commonly used in your queries’ WHERE clauses. Avoid columns with highly skewed distributions (e.g., a small set of very frequent values) since they create hotspot shards. If you need to change distribution for an existing large table, create a new distributed table with the desired column and migrate data—there is no single ALTER TABLE ... DISTRIBUTE BY that rehashes a large table in place.

4) Example: distribute a table by a different column

To create a distributed table that uses user_id as the distribution column:
After redistributing (or after testing other candidate columns), re-run run_command_on_shards to compare per-shard counts and verify improved balance.

5) Troubleshooting and tips

  • If one shard is much larger than others, inspect the frequency distribution of the shard key values to find hotspots.
  • Consider adding a compound shard key (multiple columns) if single columns don’t provide good distribution and your queries include those columns.
  • Test redistribution in a staging environment before running large-scale data migrations in production.

Watch Video

Practice Lab