pg_dbms_stats
pg_dbms_stats · Statistics management extension for stabilizing PostgreSQL execution plans using fixed statistics.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100137 | pg_dbms_stats | pg_dbms_stats | 14.0 | ADMIN | BSD-3-Clause | C |
PostgreSQL · 14
Usage
Sources:
pg_dbms_stats 14.0 stores, restores, and locks planner statistics so a PostgreSQL 14 optimizer can continue seeing a chosen statistics snapshot. Use it to reduce plan changes caused by ANALYZE or distribution drift; it does not pin a plan or freeze table data.
Enable the Extension
Install and register pg_dbms_stats in every database where the library will intercept planner catalog reads. Loading the library in a database without the dbms_stats schema can make ordinary statements fail, so complete per-database registration before enabling cluster-wide preload.
For automatic loading, add it to shared_preload_libraries and restart PostgreSQL.
For controlled session testing, the upstream manual also permits LOAD 'pg_dbms_stats' after the extension exists in that database.
Backup, Restore, and Lock
Restore also locks the restored statistics. Database-, schema-, table-, and column-level variants are available for backup, restore, lock, unlock, and import. Target objects need existing PostgreSQL statistics before they can be meaningfully backed up or locked.
Important Objects
dbms_stats.backup_historyrecords backup sets.dbms_stats.relation_stats_effectiveanddbms_stats.column_stats_effectiveshow the values currently presented to the planner.dbms_stats.statssummarizes locked statistics.dbms_stats.clean_up_stats()removes locked rows left behind after tables or columns are dropped.- Export scripts and import functions move statistics through server-side files; imports require an absolute path readable by the PostgreSQL server account.
Operational Boundaries
Locked statistics do not guarantee an unchanged plan: indexes, schema, planner GUCs, PostgreSQL code, parameter values, and table size still affect planning. Monitor query plans and unlock quickly if the fixed snapshot becomes harmful.
Dropped objects do not automatically remove their locked rows, so schedule dbms_stats.clean_up_stats(). Statistics containing polymorphic anyarray values need the manual’s special dump/restore sequence. Version 14.0 is the PostgreSQL 14 branch; use the matching upstream branch and regression-test upgrades rather than loading it on another major version.