Skip to content

pg_regresql

Trust pg_class statistics for planning instead of physical relation size

Overview

PackageVersionCategoryLicenseLanguage
pg_regresql2.2.1LANGBSD-2-ClauseC
IDExtensionBinLibLoadCreateTrustRelocSchema
3230pg_regresqlNoYesYesYesNoYes-

Load via LOAD or session_preload_libraries.

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY2.2.11817161514pg_regresql-
RPMPIGSTY2.2.01817161514pg_regresql_$v-
DEBPIGSTY2.2.01817161514postgresql-$v-pg-regresql-
OS / PGPG19PG18PG17PG16PG15
el8.x86_64N/A
el8.aarch64N/A
el9.x86_64MISS
el9.aarch64MISS
el10.x86_64MISS
el10.aarch64MISS
d12.x86_64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
d12.aarch64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
d13.x86_64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
d13.aarch64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
u22.x86_64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
u22.aarch64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
u24.x86_64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
u24.aarch64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
u26.x86_64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
u26.aarch64MISS
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0
PIGSTY 2.2.0

Build

You can build the RPM / DEB packages for pg_regresql using pig build:

pig build pkg pg_regresql         # build RPM / DEB packages

Install

You can install pg_regresql directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

Install
pig install pg_regresql;          # Install for current active PG version
pig
pig ext install -y pg_regresql -v 19  # PG 19
pig ext install -y pg_regresql -v 18  # PG 18
pig ext install -y pg_regresql -v 17  # PG 17
pig ext install -y pg_regresql -v 16  # PG 16
pig ext install -y pg_regresql -v 15  # PG 15
dnf
dnf install -y pg_regresql_19       # PG 19
dnf install -y pg_regresql_18       # PG 18
dnf install -y pg_regresql_17       # PG 17
dnf install -y pg_regresql_16       # PG 16
dnf install -y pg_regresql_15       # PG 15
apt
apt install -y postgresql-19-pg-regresql   # PG 19
apt install -y postgresql-18-pg-regresql   # PG 18
apt install -y postgresql-17-pg-regresql   # PG 17
apt install -y postgresql-16-pg-regresql   # PG 16
apt install -y postgresql-15-pg-regresql   # PG 15

Preload:

shared_preload_libraries = 'pg_regresql';

Create Extension:

CREATE EXTENSION pg_regresql;

Usage

Sources:

pg_regresql makes query planning use catalog statistics instead of rescaling them from physical file size. This supports plan regression tests in databases with representative statistics but little data. Release 2.2.1 installs its SQL objects in the fixed pg_catalog schema.

Activate the Planner Hook

The hook supports PostgreSQL 15 or later. Load it as a superuser in the test session:

LOAD 'pg_regresql';
CREATE TABLE test_orders (created_at date);
EXPLAIN SELECT * FROM test_orders WHERE created_at > DATE '2024-06-01';

For new sessions on a test instance, add the library to the existing session preload list:

session_preload_libraries = 'pg_regresql'

The hook replaces table and index page/tuple estimates with pg_class values and computes visibility from catalog statistics. Inject useful statistics before comparing plans; an empty table alone is not a production workload.

Restore Statistics

On PostgreSQL 16 and 17, create the extension before loading statistics emitted by a recent pg_dump:

CREATE EXTENSION pg_regresql;
SELECT pg_restore_relation_stats(
    'schemaname', 'public', 'relname', 'test_orders',
    'relpages', 123513::integer, 'reltuples', 50000000::real,
    'relallvisible', 123513::integer
);

Version 2.2 adds pg_restore_extended_stats() and pg_clear_extended_stats() for those servers, and supports the JSON input emitted by pg_dump 19 for pg_ndistinct and pg_dependencies. On PostgreSQL 16 remove the dump’s unsupported transaction_timeout setting. PostgreSQL 18 already supplies relation/attribute restoration in core; extended restoration is in core from 19. The extension does not install these backports on 18 or later.

Upgrade and Cleanup

From SQL version 2.0, upstream requires dropping and recreating the extension. From 2.1, use ALTER EXTENSION pg_regresql UPDATE. Review dependencies before removing it. The extension is not relocatable and its install requires a superuser.

The replacement input functions named regresql_pg_*_in are deliberately detached from the extension on 16/17. DROP EXTENSION leaves them behind so the installed casts keep working; dropping the extension is therefore not a complete return to the original catalog state. Use this tool in controlled planning tests, and do not assume compatibility with other planner hooks has been tested.

PostgreSQL 19 Planner Hook

Release 2.2.1 uses build_simple_rel_hook on PostgreSQL 19 and get_relation_info_hook on earlier supported majors. After installing the matching files, run ALTER EXTENSION pg_regresql UPDATE TO '2.2.1'; on installations using 2.2. This update advances the recorded extension version and preserves the existing SQL definitions.

Was this page helpful?

Table reader