Automatic statistics collection
The query optimizer relies on accurate statistics to generate efficient execution plans. AnalyticDB for PostgreSQL automatically keeps statistics up to date through two complementary mechanisms: asynchronous collection (autovacuum-based) and synchronous collection (gp_autostats-based). This topic explains how each mechanism works and how to configure them.
Limitations
Only AnalyticDB for PostgreSQL instances of V1.0.5.0 or later support automatic statistics collection.
To update the minor version of an instance, see Update the minor version of an instance.
Asynchronous automatic statistics collection
How it works
The autovacuum process monitors tables for insert, update, and delete activity. When the following condition is met for a table, AnalyticDB for PostgreSQL runs ANALYZE automatically:
changed rows > (table rows × autovacuum_analyze_scale_factor) + autovacuum_analyze_threshold
Where:
-
changed rows — rows affected by insert, update, or delete since the last
ANALYZE -
table rows — total row count from the last
ANALYZE -
autovacuum_analyze_scale_factor — proportion threshold; default 10%
-
autovacuum_analyze_threshold — minimum row count before the proportion is evaluated; default 50 rows
Lock behavior
Asynchronous collection holds a 4-level lock on the table. If a business process requests a conflicting lock, the auto-analyze process releases its lock so the business process can proceed.
Partitioned tables
For partitioned tables, AnalyticDB for PostgreSQL runs ANALYZE on each subpartition and merges the results into the parent partition. This ensures the optimizer has accurate statistics across the full partition hierarchy.
Monitor the auto-analyze process
Asynchronous collection is scheduled by the autovacuum process. Use the following queries to check its status.
-
In AnalyticDB for PostgreSQL V6.0:
SELECT * FROM pg_stat_activity WHERE query LIKE 'autovacuum%'; -
In AnalyticDB for PostgreSQL V7.0:
SELECT * FROM pg_stat_activity WHERE backend_type = 'autovacuum worker'; -
List tables that need
ANALYZE(withautovacuum_analyze_scale_factor= 10% andautovacuum_analyze_threshold= 50):SELECT schemaname, c.relname, c.reltuples, n_mod_since_analyze, last_autoanalyze, (n_mod_since_analyze::float / c.reltuples::float) AS analyze_ratio FROM pg_stat_all_tables s, pg_class c WHERE c.oid = s.relid AND c.reltuples > 50 AND (n_mod_since_analyze::float > 50 + c.reltuples::float * 0.1) ORDER BY (n_mod_since_analyze::float / c.reltuples::float) DESC;
Instance-level parameters
Most instances work well with the default values. To modify an instance-level parameter, submit a ticket.
| Parameter | Description | Default value |
|---|---|---|
autovacuum_enabled |
Whether to enable auto-analyze | TRUE |
autovacuum_naptime |
Minimum interval between two auto-analyze runs. Unit: minutes. | 1 |
autovacuum_analyze_scale_factor |
Proportion threshold for triggering auto-analyze. A higher value requires a larger proportion of data to change before analysis runs. | 10% |
autovacuum_analyze_threshold |
Row count threshold for triggering auto-analyze. A higher value requires more rows to change before analysis runs. Unit: rows. | 50 |
autovacuum_max_execute_workers |
Maximum number of concurrent auto-analyze tasks. | max(3, cpucores/2), where cpucores is the number of CPU cores on a single compute node |
Table-level parameters
Override instance-level defaults for specific tables using ALTER TABLE.
| Parameter | Description |
|---|---|
autovacuum_analyze_scale_factor |
Proportion threshold for triggering auto-analyze on this table. |
autovacuum_analyze_threshold |
Row count threshold for triggering auto-analyze on this table. Unit: rows. |
Modify table-level parameters
Syntax
ALTER TABLE <table_name> SET (<parameter> = <value>);
Examples
-
Raise the proportion threshold to 80% for the
testtable:ALTER TABLE test SET (autovacuum_analyze_scale_factor = 0.8); -
Disable auto-analyze for the
testtable:Setting
autovacuum_enabled = falsedisables the entire autovacuum feature for that table, not just auto-analyze. To disable only auto-analyze, increase the value ofautovacuum_analyze_scale_factorinstead.ALTER TABLE test SET (autovacuum_enabled = false);
Reset table-level parameters to defaults
Syntax
ALTER TABLE <table_name> RESET (<parameter>);
Example
Reset the proportion threshold for the test table:
ALTER TABLE test RESET (autovacuum_analyze_scale_factor);
Synchronous automatic statistics collection
Asynchronous collection runs on a schedule, so statistics may lag behind data changes by approximately 1 minute. Synchronous collection eliminates this delay: before the system completes the execution of a statement that changes data, AnalyticDB for PostgreSQL automatically collects statistics in a synchronous manner.
Synchronous automatic statistics collection does not support partitioned tables.
How it works
Configure synchronous collection using the gp_autostats_mode parameter, which supports three modes:
-
`none` — disables synchronous collection. Statistics are updated only by asynchronous collection or manual
ANALYZE. -
`on_no_stats` (default) — runs
ANALYZEthe first time a table without statistics is modified by any of the following statements: After the first analysis, subsequent updates rely on asynchronous or manual collection.-
CREATE TABLE AS SELECT -
INSERT -
COPY
-
-
`on_change` — runs
ANALYZEafter any of the following statements change more rows thangp_autostats_on_change_threshold:-
CREATE TABLE AS SELECT -
INSERT -
UPDATE -
DELETE -
COPY
-
Parameters
Most instances work well with the default values. To fine-tune synchronous collection, configure the parameters at the session level rather than modifying instance-level settings.
| Parameter | Description | Default value | Valid values |
|---|---|---|---|
gp_autostats_mode |
Mode for synchronous collection during statements other than functions or stored procedures | on_no_stats |
none, on_no_stats, on_change |
gp_autostats_mode_in_functions |
Mode for synchronous collection during function or stored procedure execution | none |
none, on_no_stats, on_change |
gp_autostats_on_change_threshold |
Row count threshold for on_change mode. If a statement changes more rows than this value, ANALYZE runs synchronously. Unit: rows. |
2147483647 |
[0, 2147483647] |
Session-level configuration
Configure synchronous collection for the current session without modifying instance-level settings.
-
Enable
on_no_statsmode for functions and stored procedures in the current session:SET gp_autostats_mode_in_functions = on_no_stats; -
Disable synchronous collection for the current session:
SET gp_autostats_mode_in_functions = on_no_stats; SET gp_autostats_mode = NONE; -
Enable
on_changemode with a threshold of 100,000 rows for the current session:SET gp_autostats_mode = on_change; SET gp_autostats_on_change_threshold = 100000;