Data distribution policies

Updated at:
Copy as MD

AnalyticDB for PostgreSQL distributes table rows across compute nodes when you create a table. The distribution scheme you choose affects query performance, join efficiency, and storage balance. Picking the right scheme upfront avoids data reshuffling at query time.

Three schemes are available: hash distribution, random distribution, and replicated distribution.

Choose a distribution scheme

When choosing a scheme, optimize for two goals:

  • Even data distribution — All compute nodes should hold roughly equal data volumes. Skewed data forces some nodes to do more work than others, creating bottlenecks.

  • Local join operations — Joins run faster when related rows are on the same compute node. When two tables share the same distribution key, the database can join them locally without moving data across the network.

The following table summarizes when to use each scheme:

Scheme Use when Avoid when
Hash distribution The table is large; you run frequent joins; a high-cardinality column is available No suitable distribution key exists
Random distribution No column is suitable for hashing Tables need to be joined on a specific key
Replicated distribution The table is small and frequently joined with large tables The table is large — replication is costly in storage

Syntax

CREATE TABLE table_name (...) [ DISTRIBUTED BY (column [, ...]) | DISTRIBUTED RANDOMLY | DISTRIBUTED REPLICATED ]

Hash distribution

DISTRIBUTED BY (column, [ ... ])

Hash distribution is the default scheme. Rows are assigned to compute nodes based on the hash value of the distribution column — rows with the same hash value always go to the same node.

Use hash distribution for large tables that participate in joins or aggregations. When two tables share the same distribution key, the database joins them locally on each compute node without redistributing data across the network.

Choose a good distribution key

The distribution key determines how evenly data is spread. Use a column that:

  • Has many unique values — high-cardinality columns (such as a primary key or user ID) minimize skew.

  • Is used in frequent joins — distributing joined tables on the same column enables local joins.

If you omit the DISTRIBUTED clause, AnalyticDB for PostgreSQL uses the primary key or the first key that the system considers suitable as the distribution key. If no suitable distribution key is identified, the system falls back to random distribution.

Example

CREATE TABLE products (
    name        varchar(40),
    prod_id     integer,
    supplier_id integer
) DISTRIBUTED BY (prod_id);

Random distribution

DISTRIBUTED RANDOMLY

Random distribution spreads rows across all compute nodes using a round-robin algorithm. Unlike hash distribution, rows with the same values may land on different nodes.

Use random distribution only when no column is suitable as a distribution key.

Example

CREATE TABLE random_stuff (
    things  text,
    doodads text,
    etc     text
) DISTRIBUTED RANDOMLY;

Replicated distribution

DISTRIBUTED REPLICATED

Replicated distribution stores a full copy of the table on every compute node. Use it for small lookup or dimension tables that are frequently joined with large fact tables to increase join performance.

Important

Use replicated distribution only for small tables. Replicating data to every compute node is costly in storage, and the cost grows with table size.

Example

CREATE TABLE replicated_stuff (
    things  text,
    doodads text,
    etc     text
) DISTRIBUTED REPLICATED;