Skip to content

pg_ttl_index

Automatic data expiration with TTL indexes

pg_ttl_index · Automatic data expiration with TTL indexes

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2730pg_ttl_indexpg_ttl_index3.0.0FEATPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd-rNoYesYesYesyesno
pg 14 breaks; rm README from RPM/DEB

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY3.0.019 18 17 16 15pg_ttl_index-
RPMPIGSTY3.0.019 18 17 16 15pg_ttl_index_$v-
DEBPIGSTY3.0.019 18 17 16 15postgresql-$v-ttl-index-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
el8.aarch64N/APIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
el9.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
el9.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
el10.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
el10.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
d12.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
d12.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
d13.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
d13.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
u22.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
u22.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
u24.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
u24.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
u26.x86_64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
u26.aarch64PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0PIGSTY 3.0.0
PackageVersionOSORGSIZEFile URL
pg_ttl_index_193.0.0el9.x86_64pigsty20.7 KiBpg_ttl_index_19-3.0.0-1PGSTY.el9.x86_64.rpm
pg_ttl_index_193.0.0el9.aarch64pigsty20.5 KiBpg_ttl_index_19-3.0.0-1PGSTY.el9.aarch64.rpm
pg_ttl_index_193.0.0el10.x86_64pigsty20.8 KiBpg_ttl_index_19-3.0.0-1PGSTY.el10.x86_64.rpm
pg_ttl_index_193.0.0el10.aarch64pigsty20.8 KiBpg_ttl_index_19-3.0.0-1PGSTY.el10.aarch64.rpm
postgresql-19-ttl-index3.0.0d12.x86_64pigsty14.0 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~bookworm_amd64.deb
postgresql-19-ttl-index3.0.0d12.aarch64pigsty13.9 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~bookworm_arm64.deb
postgresql-19-ttl-index3.0.0d13.x86_64pigsty13.9 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~trixie_amd64.deb
postgresql-19-ttl-index3.0.0d13.aarch64pigsty14.0 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~trixie_arm64.deb
postgresql-19-ttl-index3.0.0u22.x86_64pigsty13.8 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~jammy_amd64.deb
postgresql-19-ttl-index3.0.0u22.aarch64pigsty13.6 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~jammy_arm64.deb
postgresql-19-ttl-index3.0.0u24.x86_64pigsty13.7 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~noble_amd64.deb
postgresql-19-ttl-index3.0.0u24.aarch64pigsty13.6 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~noble_arm64.deb
postgresql-19-ttl-index3.0.0u26.x86_64pigsty13.8 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~resolute_amd64.deb
postgresql-19-ttl-index3.0.0u26.aarch64pigsty13.6 KiBpostgresql-19-ttl-index_3.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ttl_index_183.0.0el8.x86_64pigsty20.5 KiBpg_ttl_index_18-3.0.0-1PGSTY.el8.x86_64.rpm
pg_ttl_index_183.0.0el8.aarch64pigsty20.5 KiBpg_ttl_index_18-3.0.0-1PGSTY.el8.aarch64.rpm
pg_ttl_index_183.0.0el9.x86_64pigsty20.7 KiBpg_ttl_index_18-3.0.0-1PGSTY.el9.x86_64.rpm
pg_ttl_index_183.0.0el9.aarch64pigsty20.5 KiBpg_ttl_index_18-3.0.0-1PGSTY.el9.aarch64.rpm
pg_ttl_index_183.0.0el10.x86_64pigsty20.8 KiBpg_ttl_index_18-3.0.0-1PGSTY.el10.x86_64.rpm
pg_ttl_index_183.0.0el10.aarch64pigsty20.8 KiBpg_ttl_index_18-3.0.0-1PGSTY.el10.aarch64.rpm
postgresql-18-ttl-index3.0.0d12.x86_64pigsty14.0 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~bookworm_amd64.deb
postgresql-18-ttl-index3.0.0d12.aarch64pigsty13.9 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~bookworm_arm64.deb
postgresql-18-ttl-index3.0.0d13.x86_64pigsty14.0 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~trixie_amd64.deb
postgresql-18-ttl-index3.0.0d13.aarch64pigsty14.0 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~trixie_arm64.deb
postgresql-18-ttl-index3.0.0u22.x86_64pigsty13.8 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~jammy_amd64.deb
postgresql-18-ttl-index3.0.0u22.aarch64pigsty13.6 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~jammy_arm64.deb
postgresql-18-ttl-index3.0.0u24.x86_64pigsty13.7 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~noble_amd64.deb
postgresql-18-ttl-index3.0.0u24.aarch64pigsty13.6 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~noble_arm64.deb
postgresql-18-ttl-index3.0.0u26.x86_64pigsty13.9 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~resolute_amd64.deb
postgresql-18-ttl-index3.0.0u26.aarch64pigsty13.6 KiBpostgresql-18-ttl-index_3.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ttl_index_173.0.0el8.x86_64pigsty20.5 KiBpg_ttl_index_17-3.0.0-1PGSTY.el8.x86_64.rpm
pg_ttl_index_173.0.0el8.aarch64pigsty20.5 KiBpg_ttl_index_17-3.0.0-1PGSTY.el8.aarch64.rpm
pg_ttl_index_173.0.0el9.x86_64pigsty20.7 KiBpg_ttl_index_17-3.0.0-1PGSTY.el9.x86_64.rpm
pg_ttl_index_173.0.0el9.aarch64pigsty20.5 KiBpg_ttl_index_17-3.0.0-1PGSTY.el9.aarch64.rpm
pg_ttl_index_173.0.0el10.x86_64pigsty20.8 KiBpg_ttl_index_17-3.0.0-1PGSTY.el10.x86_64.rpm
pg_ttl_index_173.0.0el10.aarch64pigsty20.8 KiBpg_ttl_index_17-3.0.0-1PGSTY.el10.aarch64.rpm
postgresql-17-ttl-index3.0.0d12.x86_64pigsty13.9 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~bookworm_amd64.deb
postgresql-17-ttl-index3.0.0d12.aarch64pigsty13.9 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~bookworm_arm64.deb
postgresql-17-ttl-index3.0.0d13.x86_64pigsty13.9 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~trixie_amd64.deb
postgresql-17-ttl-index3.0.0d13.aarch64pigsty14.0 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~trixie_arm64.deb
postgresql-17-ttl-index3.0.0u22.x86_64pigsty13.7 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~jammy_amd64.deb
postgresql-17-ttl-index3.0.0u22.aarch64pigsty13.6 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~jammy_arm64.deb
postgresql-17-ttl-index3.0.0u24.x86_64pigsty13.7 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~noble_amd64.deb
postgresql-17-ttl-index3.0.0u24.aarch64pigsty13.6 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~noble_arm64.deb
postgresql-17-ttl-index3.0.0u26.x86_64pigsty13.8 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~resolute_amd64.deb
postgresql-17-ttl-index3.0.0u26.aarch64pigsty13.6 KiBpostgresql-17-ttl-index_3.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ttl_index_163.0.0el8.x86_64pigsty20.5 KiBpg_ttl_index_16-3.0.0-1PGSTY.el8.x86_64.rpm
pg_ttl_index_163.0.0el8.aarch64pigsty20.5 KiBpg_ttl_index_16-3.0.0-1PGSTY.el8.aarch64.rpm
pg_ttl_index_163.0.0el9.x86_64pigsty20.7 KiBpg_ttl_index_16-3.0.0-1PGSTY.el9.x86_64.rpm
pg_ttl_index_163.0.0el9.aarch64pigsty20.5 KiBpg_ttl_index_16-3.0.0-1PGSTY.el9.aarch64.rpm
pg_ttl_index_163.0.0el10.x86_64pigsty20.8 KiBpg_ttl_index_16-3.0.0-1PGSTY.el10.x86_64.rpm
pg_ttl_index_163.0.0el10.aarch64pigsty20.8 KiBpg_ttl_index_16-3.0.0-1PGSTY.el10.aarch64.rpm
postgresql-16-ttl-index3.0.0d12.x86_64pigsty13.9 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~bookworm_amd64.deb
postgresql-16-ttl-index3.0.0d12.aarch64pigsty13.9 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~bookworm_arm64.deb
postgresql-16-ttl-index3.0.0d13.x86_64pigsty13.9 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~trixie_amd64.deb
postgresql-16-ttl-index3.0.0d13.aarch64pigsty14.0 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~trixie_arm64.deb
postgresql-16-ttl-index3.0.0u22.x86_64pigsty13.7 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~jammy_amd64.deb
postgresql-16-ttl-index3.0.0u22.aarch64pigsty13.6 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~jammy_arm64.deb
postgresql-16-ttl-index3.0.0u24.x86_64pigsty13.7 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~noble_amd64.deb
postgresql-16-ttl-index3.0.0u24.aarch64pigsty13.6 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~noble_arm64.deb
postgresql-16-ttl-index3.0.0u26.x86_64pigsty13.8 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~resolute_amd64.deb
postgresql-16-ttl-index3.0.0u26.aarch64pigsty13.6 KiBpostgresql-16-ttl-index_3.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ttl_index_153.0.0el8.x86_64pigsty20.6 KiBpg_ttl_index_15-3.0.0-1PGSTY.el8.x86_64.rpm
pg_ttl_index_153.0.0el8.aarch64pigsty20.6 KiBpg_ttl_index_15-3.0.0-1PGSTY.el8.aarch64.rpm
pg_ttl_index_153.0.0el9.x86_64pigsty21.1 KiBpg_ttl_index_15-3.0.0-1PGSTY.el9.x86_64.rpm
pg_ttl_index_153.0.0el9.aarch64pigsty20.8 KiBpg_ttl_index_15-3.0.0-1PGSTY.el9.aarch64.rpm
pg_ttl_index_153.0.0el10.x86_64pigsty21.2 KiBpg_ttl_index_15-3.0.0-1PGSTY.el10.x86_64.rpm
pg_ttl_index_153.0.0el10.aarch64pigsty21.1 KiBpg_ttl_index_15-3.0.0-1PGSTY.el10.aarch64.rpm
postgresql-15-ttl-index3.0.0d12.x86_64pigsty14.1 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~bookworm_amd64.deb
postgresql-15-ttl-index3.0.0d12.aarch64pigsty14.1 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~bookworm_arm64.deb
postgresql-15-ttl-index3.0.0d13.x86_64pigsty14.1 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~trixie_amd64.deb
postgresql-15-ttl-index3.0.0d13.aarch64pigsty14.1 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~trixie_arm64.deb
postgresql-15-ttl-index3.0.0u22.x86_64pigsty14.1 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~jammy_amd64.deb
postgresql-15-ttl-index3.0.0u22.aarch64pigsty13.9 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~jammy_arm64.deb
postgresql-15-ttl-index3.0.0u24.x86_64pigsty14.0 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~noble_amd64.deb
postgresql-15-ttl-index3.0.0u24.aarch64pigsty13.8 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~noble_arm64.deb
postgresql-15-ttl-index3.0.0u26.x86_64pigsty14.1 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~resolute_amd64.deb
postgresql-15-ttl-index3.0.0u26.aarch64pigsty13.8 KiBpostgresql-15-ttl-index_3.0.0-1PGSTY~resolute_arm64.deb

Source

github.com/ibrahimkarimeddin/postgres-extensions-pg_ttl

pig build pkg pg_ttl_index;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install pg_ttl_index;		# install by package name

pig install pg_ttl_index -v 19;   # install for PG 19
pig install pg_ttl_index -v 18;   # install for PG 18
pig install pg_ttl_index -v 17;   # install for PG 17
pig install pg_ttl_index -v 16;   # install for PG 16
pig install pg_ttl_index -v 15;   # install for PG 15

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_ttl_index';

Create this extension with:

CREATE EXTENSION pg_ttl_index;

Usage

pg_ttl_index: Automatic data expiration with TTL indexes

pg_ttl_index provides automatic data expiration by associating a TTL (time-to-live) with a timestamp column. A background worker periodically deletes rows whose timestamp exceeds the configured expiration interval.

Quick Start

-- Start the background worker
SELECT ttl_start_worker();

-- Create a table with a timestamp column
CREATE TABLE user_sessions (
    id SERIAL PRIMARY KEY,
    user_id INTEGER,
    session_data JSONB,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Expire rows after 1 hour (3600 seconds)
SELECT ttl_create_index('user_sessions', 'created_at', 3600);

Functions

FunctionDescription
ttl_start_worker()Start the background worker for automatic cleanup
ttl_worker_status()Check if the worker is running
ttl_runner()Manually trigger cleanup
ttl_create_index(table, column, expire_seconds [, batch_size])Configure TTL expiration
ttl_drop_index(table, column)Remove TTL configuration
ttl_summary()List all TTL indexes with stats

Examples

Session management with 24-hour expiry:

SELECT ttl_create_index('sessions', 'created_at', 86400, 5000);

Log retention for 7 days:

SELECT ttl_create_index('app_logs', 'logged_at', 604800);

Cache entries with custom expiry column (0 means the column itself holds the absolute expiry time):

SELECT ttl_create_index('cache_entries', 'expires_at', 0);

Monitoring

SELECT * FROM ttl_summary();

Pause cleanup for a specific table:

UPDATE ttl_index_table SET active = false WHERE table_name = 'user_sessions';

Configuration

ParameterDescriptionDefault
pg_ttl_index.naptimeCleanup interval in seconds60
pg_ttl_index.enabledEnable/disable worker globallyon
ALTER SYSTEM SET pg_ttl_index.naptime = 30;
SELECT pg_reload_conf();

Table reader