pg_ttl_index
pg_ttl_index
pg_ttl_index : Automatic data expiration with TTL indexes
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 2740 | pg_ttl_index | pg_ttl_index | 3.0.0 | FEAT | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--sLd-r | No | Yes | Yes | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | temporal_tables periods hll rum pg_partman pg_cron pg_task timescaledb |
pg 14 breaks
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 3.0.0 | 18 17 16 15 14 | pg_ttl_index | - |
| RPM | PIGSTY | 3.0.0 | 18 17 16 15 14 | pg_ttl_index_$v | - |
| DEB | PIGSTY | 3.0.0 | 18 17 16 15 14 | postgresql-$v-ttl-index | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
el8.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
el9.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
el9.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
el10.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
el10.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
d12.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
d12.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
d13.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
d13.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
u22.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
u22.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
u24.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
u24.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | MISS |
Source
pig build pkg pg_ttl_index; # 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_ttl_index; # install via package name, for the active PG version
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 15Config 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 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
| Function | Description |
|---|---|
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
| Parameter | Description | Default |
|---|---|---|
pg_ttl_index.naptime | Cleanup interval in seconds | 60 |
pg_ttl_index.enabled | Enable/disable worker globally | on |
ALTER SYSTEM SET pg_ttl_index.naptime = 30;
SELECT pg_reload_conf();Last updated on