pg_regresql
pg_regresql : Trust pg_class statistics for planning instead of physical relation size
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3230 | pg_regresql | pg_regresql | 2.0.0 | LANG | BSD-2-Clause | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--sLd-r | No | Yes | Yes | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | pg_hint_plan hypopg plan_filter auto_explain |
Activate it with LOAD pg_regresql or session_preload_libraries.
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 2.0.0 | 18 17 16 15 14 | pg_regresql | - |
| RPM | PIGSTY | 2.0.0 | 18 17 16 15 14 | pg_regresql_$v | - |
| DEB | PIGSTY | 2.0.0 | 18 17 16 15 14 | postgresql-$v-pg-regresql | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el8.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el9.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el9.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el10.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el10.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d12.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d12.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d13.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d13.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u22.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u22.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u24.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u24.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
Source
pig build pkg pg_regresql; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_regresql; # install via package name, for the active PG version
pig install pg_regresql -v 18; # install for PG 18
pig install pg_regresql -v 17; # install for PG 17
pig install pg_regresql -v 16; # install for PG 16
pig install pg_regresql -v 15; # install for PG 15
pig install pg_regresql -v 14; # install for PG 14Config this extension to shared_preload_libraries:
shared_preload_libraries = 'pg_regresql';Create this extension with:
CREATE EXTENSION pg_regresql;Usage
Sources: extension README, control file, portable stats article
pg_regresql is a PostgreSQL extension that makes the planner trust catalog statistics from pg_class instead of recomputing relation size from physical file blocks. It is the extension part of the RegreSQL project, intended for realistic plan regression testing with injected production statistics.
Problem
The upstream extension README explains that PostgreSQL normally does not fully trust pg_class.relpages and pg_class.reltuples when estimating relation size. Instead, planner code reads the current physical file size and rescales statistics from that.
That behavior is useful for stale-statistics safety, but it breaks test setups where catalog statistics were intentionally restored from another environment and the local table files are much smaller.
What It Overrides
pg_regresql hooks into get_relation_info_hook after estimate_rel_size() and replaces planner estimates with catalog values.
| Planner field | Default source | pg_regresql source |
|---|---|---|
rel->pages | smgrnblocks() via table access method | pg_class.relpages |
rel->tuples | density scaled by physical pages | pg_class.reltuples |
rel->allvisfrac | relallvisible / physical pages | pg_class.relallvisible / relpages |
IndexOptInfo->pages | RelationGetNumberOfBlocks() | pg_class.relpages for the index |
IndexOptInfo->tuples | copied from rel->tuples | pg_class.reltuples for the index |
Installation
The upstream README documents three installation paths:
sudo pgxn install pg_regresqlmake PG_SOURCE=/path/to/postgresql
make install PG_SOURCE=/path/to/postgresqlmake USE_PGXS=1
make install USE_PGXS=1The control file ships as pg_regresql.control with default_version = '2.0' and module_pathname = '$libdir/pg_regresql'.
Activation
The extension becomes active when the shared library is loaded:
LOAD 'pg_regresql';
EXPLAIN SELECT ...;For a whole test instance, the README recommends:
session_preload_libraries = 'pg_regresql'This is the important runtime configuration: package installation alone is not the point; the planner hook only takes effect after the library is loaded for the session or instance.
Typical Workflow
The main use case is plan regression testing with restored production statistics. After injecting catalog statistics into a CI or test database, pg_regresql makes the planner use those restored values instead of the tiny local heap size.
The README gives this example:
SELECT pg_restore_relation_stats(
'schemaname', 'public',
'relname', 'test_orders',
'relpages', 123513::integer,
'reltuples', 50000000::real,
'relallvisible', 123513::integer
);
LOAD 'pg_regresql';
EXPLAIN SELECT * FROM test_orders WHERE created_at > '2024-06-01';That pattern is useful for:
- reproducing production plans locally
- testing schema migrations against realistic plan estimates
- simulating table growth and index choices
- improving partition-planning experiments
Compatibility
- PostgreSQL 14 and newer in this repository packaging
- upstream README notes the hook itself exists since PostgreSQL 8.3
- intended to coexist with extensions such as
pg_hint_planandhypopg, though upstream marks that as not yet fully tested