Skip to content

leapfrog

Ordered intersection of B-tree indexes through a PostgreSQL planner hook

leapfrog · Ordered intersection of B-tree indexes through a PostgreSQL planner hook

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102271leapfrogleapfrog0.0.5FEATPostgreSQLC

PostgreSQL · 18

Usage

Sources:

leapfrog 0.0.5 is an experimental PostgreSQL 18 planner extension. It combines ordinary B-tree indexes into an ordered intersection, allowing suitable filtered queries with ordering and a limit to use fewer specialized indexes. Other PostgreSQL majors are unsupported.

Enable and Inspect

Install the SQL helpers as a superuser and load the library in the session that plans queries:

CREATE EXTENSION leapfrog;
LOAD 'leapfrog';
SELECT leapfrog_version(), leapfrog_is_loaded();

CREATE TABLE orders (
    id bigint PRIMARY KEY,
    customer_id bigint NOT NULL,
    product_id bigint NOT NULL
);
CREATE INDEX ON orders (customer_id, id);
CREATE INDEX ON orders (product_id, id);
ANALYZE orders;
EXPLAIN
SELECT * FROM orders
WHERE customer_id = 100 AND product_id = 200
ORDER BY id DESC LIMIT 100;

The planner may choose Custom Scan (Index Merge Intersect) when its estimated cost is favorable. An empty demonstration table normally keeps a native plan. The outer index ends with the sort column; leading index columns must have equality conditions. Each additional index must contribute an independent equality or partial-index predicate.

Loading and Settings

CREATE EXTENSION registers the SQL helpers; every subsequent session still needs to load the library. LOAD 'leapfrog' applies to the current session. An administrator can configure session_preload_libraries for new sessions, or shared_preload_libraries for all backends, with a server restart for the latter.

SettingDefaultPurpose
leapfrog.enabledonOffer the custom path
leapfrog.forceoffPrefer eligible custom paths for diagnostics
leapfrog.max_indexes4Maximum streams per intersection, from 2 to 16
leapfrog.min_indexes2Developer control for minimum stream count
leapfrog.skip_after64Initial skip-ahead threshold; 0 disables skipping

leapfrog.force can select slower plans. Use actual query plans and timings before changing defaults. The normal planner retains its other paths.

Limits and Maintenance

Sort keys can use types with a B-tree operator family and can contain nulls. Each index stream must supply the requested order, collation and null placement. Both ascending and descending nbtree v4 indexes are supported. Ranges, lists and expression index columns are unsupported for the equality prefix.

Duplicate-key TID groups are bounded by work_mem; an overflowing group falls back to row-condition checks. Open scans can hold index pages pinned and delay vacuum. This extension creates no application tables and does not replace existing indexes.

DROP EXTENSION does not unload a library already loaded into a backend. Disable its planner contribution with SET leapfrog.enabled = off, and reconnect sessions after replacing the library. Use a proper installation or package replacement; overwriting a loaded shared-library file in place can crash its users.

Table reader