Extensions
- pg_ttl_index 1.0.0
README
PostgreSQL TTL Index Extension
A PostgreSQL extension providing automatic TTL (Time To Live) functionality for data expiration.
Features
- Automatic data expiration based on timestamp columns
- Background worker runs cleanup every minute automatically
- Multiple tables supported with different expiry times
- Production ready with SQL injection protection
- Configurable cleanup intervals
Installation
make clean && make
sudo make install
Add to shared_preload_libraries
in postgresql.conf
:
shared_preload_libraries = 'pg_ttl_index'
Restart PostgreSQL and create the extension:
CREATE EXTENSION pg_ttl_index;
Usage
-- Create TTL indexes (data expires automatically)
SELECT ttl_create_index('user_sessions', 'created_at', 3600); -- 1 hour
SELECT ttl_create_index('temp_logs', 'log_time', 1800); -- 30 minutes
-- Remove TTL index
SELECT ttl_drop_index('user_sessions', 'created_at');
-- Manual cleanup (optional - runs automatically via background worker)
SELECT ttl_runner_safe();
Configuration
-- Change cleanup interval (default: 60 seconds)
ALTER SYSTEM SET pg_ttl_index.naptime = 30;
SELECT pg_reload_conf();
-- Disable background worker temporarily
ALTER SYSTEM SET pg_ttl_index.enabled = false;
SELECT pg_reload_conf();
How It Works
- Create TTL indexes on timestamp/date columns
- Background worker runs every minute (configurable)
- Expired data is automatically deleted
- Multiple tables supported with different expiry times
- All operations logged for monitoring
The extension is fully automatic - once configured, no manual intervention needed!