Diagnose data skew
In an AnalyticDB for PostgreSQL instance, uneven data distribution across compute nodes—data skew—causes some nodes to process more data than others, slowing down parallel queries. AnalyticDB for PostgreSQL automatically scans all database tables every hour and stores results in adbpg_toolkit.diag_skew_tables, so you can identify skewed tables and fix them before performance degrades.
Prerequisites
Before you begin, ensure that you have:
-
An AnalyticDB for PostgreSQL instance in elastic storage mode
-
One of the following minor versions:
-
AnalyticDB for PostgreSQL V6.0: V6.3.10.0 or later
-
AnalyticDB for PostgreSQL V7.0: V7.0.4.0 or later
-
To check your minor version or upgrade, see View the minor engine version and Update the minor engine version.
How it works
AnalyticDB for PostgreSQL uses Massively Parallel Processing (MPP) to distribute data across compute nodes. When data is unevenly distributed, some nodes handle more data than others—this is data skew. Skewed tables slow down queries because MPP relies on parallel processing across balanced workloads.
The diagnostics feature runs automatically at the start of each hour. It queries only metadata tables, so it does not affect query performance.
What gets scanned:
-
All user databases except the five system databases:
postgres,template0,template1,adbpgadmin, andaurora -
All tables except temporary tables and unlogged tables
-
By default, only tables 1 GB or larger (configurable)
What gets reported:
-
By default, only tables with a skew ratio of 20% or higher (configurable)
The following examples show the data distribution for an appropriate distribution key and an inappropriate distribution key.
-
Appropriate distribution key

In the example above, the primary key `id` is selected as the distribution key. The data is evenly distributed across the four compute nodes.
-
Inappropriate distribution key

In the example above, the `gender` field is selected as the distribution key. Because the `gender` field has only two values, `true` and `false`, data is stored on only two compute nodes. The other nodes do not store any data. This causes data skew.
Check for data skew
Query adbpg_toolkit.diag_skew_tables to see diagnostics results. Two options are available: query the table directly in SQL, or view results in the console.
Option 1: Query in SQL
Connect to your database and run:
SELECT * FROM adbpg_toolkit.diag_skew_tables;
To filter by schema or table:
-- All tables in a schema
SELECT * FROM adbpg_toolkit.diag_skew_tables WHERE schema_name = '<schema_name>';
-- A specific table
SELECT * FROM adbpg_toolkit.diag_skew_tables WHERE table_name = '<table_name>';
Option 2: View in the console
See Data bloat, data skew, and index statistics.
Fields in diag_skew_tables
| Field | Data type | Description |
|---|---|---|
schema_name |
name | The schema that contains the table. |
table_name |
name | The table name. |
table_size |
bigint | The table size, in bytes. |
table_skew |
real | The skew ratio, from 0 to 100 (%). Higher values indicate more severe skew. |
table_owner |
name | The table owner. |
table_distributed_policy |
text | The distribution key of the table. |
diagnose_time |
timestamp with time zone | When the diagnostic result was generated. |
When to take action
A skew ratio of 20% or higher appears in the results by default. Not all skewed tables require immediate action—consider the table size and query frequency. For large, frequently queried tables, skew ratios above 20% typically warrant fixing.
Trigger diagnostics manually
By default, diagnostics runs at the start of each hour. To check results immediately after changing a distribution key, trigger diagnostics manually:
SELECT adbpg_toolkit.diagnose_skew_tables();
Configure diagnostics parameters
Set the table size threshold
By default, tables smaller than 1 GB are not scanned. To change this threshold:
ALTER DATABASE <database_name> SET adb_diagnose_table_threshold_size TO <size_in_bytes>;
Example: Scan tables larger than 500 MB (536,870,912 bytes):
ALTER DATABASE diagnose SET adb_diagnose_table_threshold_size TO 536870912;
Set the skew ratio threshold
By default, tables with a skew ratio below 20% are not reported. To change this threshold:
ALTER DATABASE <database_name> SET adb_diagnose_skew_percent TO <skew_ratio>;
Example: Report all tables with a skew ratio above 0% in the diagnose database:
ALTER DATABASE diagnose SET adb_diagnose_skew_percent TO 0;
How the skew ratio is calculated
The skew ratio measures how unevenly data is distributed across compute nodes. Given n compute nodes where S1, S2, ..., Sn represent the data size on each node:
Avg = (S1 + S2 + ... + Sn) / n
Max = max(S1, S2, ..., Sn)
Skew ratio = (1 - Avg / Max) / (1 - 1/n) × 100%
The ratio ranges from 0% to 100%. A value of 0% means data is perfectly balanced; 100% means all data is on a single node.
Fix data skew
Data skew most commonly occurs with hash distribution when the distribution key has many repeated values. Nodes that store those repeated values end up with more data than others.
To fix skew, change the distribution key to a column with more distinct values—ideally the primary key, or a column with evenly distributed data:
ALTER TABLE <table_name> SET DISTRIBUTED BY (<column_name>);
Example: Change the distribution key of t1 to column c2:
ALTER TABLE t1 SET DISTRIBUTED BY (c2);
Not all skewed tables need to be fixed. Changing a distribution key involves redistributing table data across nodes, which takes time and resources. For smaller or infrequently queried tables, the performance gain may not justify the cost.
For guidance on choosing a distribution key, see Selection of distribution keys.
Distribution methods
AnalyticDB for PostgreSQL supports three data distribution methods:
| Method | Description | Use when |
|---|---|---|
| Hash distribution | Distributes data based on the hash value of the distribution key. | Most tables. Select a distribution key with high cardinality to avoid skew. |
| Replicated distribution | Stores a full copy of the table on every compute node. | Small dimension tables. Causes data bloat on large tables. |
| Random distribution | Distributes rows randomly across nodes. | When no suitable distribution key exists. Collocated joins are not supported, so query performance is lower than hash distribution. |