pgttl 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pgttl-0.1.0/MANIFEST.in +3 -0
- pgttl-0.1.0/Makefile +7 -0
- pgttl-0.1.0/PKG-INFO +137 -0
- pgttl-0.1.0/README.md +123 -0
- pgttl-0.1.0/docs/design.md +99 -0
- pgttl-0.1.0/docs/implementation.md +130 -0
- pgttl-0.1.0/docs/installation.md +116 -0
- pgttl-0.1.0/docs/python.md +128 -0
- pgttl-0.1.0/pg_ttl--0.1.0.sql +440 -0
- pgttl-0.1.0/pg_ttl.c +126 -0
- pgttl-0.1.0/pg_ttl.control +6 -0
- pgttl-0.1.0/pgttl/__init__.py +87 -0
- pgttl-0.1.0/pgttl/django/__init__.py +38 -0
- pgttl-0.1.0/pgttl/django/apps.py +12 -0
- pgttl-0.1.0/pgttl/django/checks.py +83 -0
- pgttl-0.1.0/pgttl/django/migrations.py +37 -0
- pgttl-0.1.0/pgttl.egg-info/PKG-INFO +137 -0
- pgttl-0.1.0/pgttl.egg-info/SOURCES.txt +24 -0
- pgttl-0.1.0/pgttl.egg-info/dependency_links.txt +1 -0
- pgttl-0.1.0/pgttl.egg-info/requires.txt +4 -0
- pgttl-0.1.0/pgttl.egg-info/top_level.txt +1 -0
- pgttl-0.1.0/pyproject.toml +23 -0
- pgttl-0.1.0/setup.cfg +4 -0
- pgttl-0.1.0/test_django.py +204 -0
- pgttl-0.1.0/test_integration.sh +91 -0
- pgttl-0.1.0/test_pgttl.py +253 -0
pgttl-0.1.0/MANIFEST.in
ADDED
pgttl-0.1.0/Makefile
ADDED
pgttl-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pgttl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python bindings for the PostgreSQL TTL extension
|
|
5
|
+
Author: Grant Jenks
|
|
6
|
+
Classifier: Development Status :: 4 - Beta
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Topic :: Database
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Requires-Dist: psycopg>=3
|
|
12
|
+
Provides-Extra: django
|
|
13
|
+
Requires-Dist: Django>=5.2; extra == "django"
|
|
14
|
+
|
|
15
|
+
# PostgreSQL TTL: Automatic Row Expiration
|
|
16
|
+
|
|
17
|
+
`pg_ttl` is a PostgreSQL extension that supports time-to-live (TTL) on rows
|
|
18
|
+
in ordinary tables.
|
|
19
|
+
|
|
20
|
+
PostgreSQL is excellent at managing its own storage. Wouldn't it be nice
|
|
21
|
+
if applications could tell the database how long to keep individual rows
|
|
22
|
+
and then forget about expiration?
|
|
23
|
+
|
|
24
|
+
The design follows [SQLite TTL](https://github.com/grantjenks/sqlite-ttl):
|
|
25
|
+
the application sets a deadline on a row, and a native worker removes it
|
|
26
|
+
after the deadline. Existing table columns, indexes, and queries need not
|
|
27
|
+
change.
|
|
28
|
+
|
|
29
|
+
**Status:** Early development. Tested with PostgreSQL 16 and psycopg 3. This
|
|
30
|
+
is soft TTL, not a security deadline.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
Build the server extension using the matching PostgreSQL headers and PGXS:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
make PG_CONFIG=/path/to/pg_config
|
|
38
|
+
make PG_CONFIG=/path/to/pg_config install
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Configure these settings in `postgresql.conf` and restart PostgreSQL:
|
|
42
|
+
|
|
43
|
+
```conf
|
|
44
|
+
shared_preload_libraries = 'pg_ttl'
|
|
45
|
+
pg_ttl.databases = 'example'
|
|
46
|
+
pg_ttl.role = 'ttl_owner'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Create the named database and non-superuser maintenance role first. Install
|
|
50
|
+
the extension and grant access to the trusted role as described in
|
|
51
|
+
[Installation and Deployment](docs/installation.md).
|
|
52
|
+
Workers start automatically for the configured databases on server startup.
|
|
53
|
+
|
|
54
|
+
## Quickstart
|
|
55
|
+
|
|
56
|
+
Connect to the configured database as the trusted table-owner role.
|
|
57
|
+
The extension's SQL functions live in the `pgttl` schema:
|
|
58
|
+
|
|
59
|
+
```sql
|
|
60
|
+
SET search_path = public, pgttl;
|
|
61
|
+
|
|
62
|
+
CREATE TABLE sessions (
|
|
63
|
+
id bigint PRIMARY KEY,
|
|
64
|
+
data text
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
SELECT ttl_enable('sessions');
|
|
68
|
+
|
|
69
|
+
BEGIN;
|
|
70
|
+
INSERT INTO sessions VALUES (42, 'temporary');
|
|
71
|
+
SELECT ttl_set('sessions', 42, 5);
|
|
72
|
+
COMMIT;
|
|
73
|
+
|
|
74
|
+
SELECT * FROM sessions;
|
|
75
|
+
-- 42 | temporary
|
|
76
|
+
|
|
77
|
+
-- Wait for expiration and maintenance.
|
|
78
|
+
SELECT * FROM sessions;
|
|
79
|
+
-- No rows.
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Renew a deadline by calling `ttl_set` again or remove it with `ttl_clear`.
|
|
83
|
+
Rows without a scheduled expiration remain in the table.
|
|
84
|
+
|
|
85
|
+
## Python Bindings
|
|
86
|
+
|
|
87
|
+
Install the Python bindings with pip:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
pip install pgttl
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The native extension must be installed and configured on the PostgreSQL
|
|
94
|
+
server. Use an existing psycopg connection:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import psycopg
|
|
98
|
+
import pgttl
|
|
99
|
+
|
|
100
|
+
with psycopg.connect("dbname=example user=ttl_owner", autocommit=True) as db:
|
|
101
|
+
pgttl.require(db)
|
|
102
|
+
db.execute("CREATE TABLE sessions (id bigint PRIMARY KEY, data text)")
|
|
103
|
+
pgttl.enable(db, "sessions")
|
|
104
|
+
with db.transaction():
|
|
105
|
+
db.execute("INSERT INTO sessions VALUES (%s, %s)", (42, "temporary"))
|
|
106
|
+
pgttl.set_ttl(db, "sessions", 42, 5)
|
|
107
|
+
|
|
108
|
+
print(pgttl.status(db))
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
No Python maintenance thread is needed. PostgreSQL owns expiration even
|
|
112
|
+
after the connection is closed.
|
|
113
|
+
|
|
114
|
+
## Features
|
|
115
|
+
|
|
116
|
+
- Automatic expiration on ordinary tables with integer, text or UUID keys.
|
|
117
|
+
- Transactional scheduling, renewal and clearing.
|
|
118
|
+
- Native deletion with ordinary SQL constraints and triggers.
|
|
119
|
+
- Bounded maintenance and health reporting.
|
|
120
|
+
- Explicit schema validation and acceptance.
|
|
121
|
+
- Optional transactional audit log with automatic retention.
|
|
122
|
+
- Optional Django instance helpers, migrations and system checks.
|
|
123
|
+
|
|
124
|
+
## Documentation
|
|
125
|
+
|
|
126
|
+
- [Installation and Deployment](docs/installation.md)
|
|
127
|
+
- [SQL API](docs/design.md)
|
|
128
|
+
- [Implementation Details](docs/implementation.md)
|
|
129
|
+
- [Python Bindings](docs/python.md)
|
|
130
|
+
|
|
131
|
+
## Related Work
|
|
132
|
+
|
|
133
|
+
[pg_ttl_index](https://pgxn.org/dist/pg_ttl_index/) offers automatic
|
|
134
|
+
expiration using a timestamp column and a configured retention interval.
|
|
135
|
+
[pg_cron](https://github.com/citusdata/pg_cron) can run scheduled DELETE
|
|
136
|
+
statements. [pg_partman](https://github.com/pgpartman/pg_partman) can remove
|
|
137
|
+
old time partitions efficiently.
|
pgttl-0.1.0/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# PostgreSQL TTL: Automatic Row Expiration
|
|
2
|
+
|
|
3
|
+
`pg_ttl` is a PostgreSQL extension that supports time-to-live (TTL) on rows
|
|
4
|
+
in ordinary tables.
|
|
5
|
+
|
|
6
|
+
PostgreSQL is excellent at managing its own storage. Wouldn't it be nice
|
|
7
|
+
if applications could tell the database how long to keep individual rows
|
|
8
|
+
and then forget about expiration?
|
|
9
|
+
|
|
10
|
+
The design follows [SQLite TTL](https://github.com/grantjenks/sqlite-ttl):
|
|
11
|
+
the application sets a deadline on a row, and a native worker removes it
|
|
12
|
+
after the deadline. Existing table columns, indexes, and queries need not
|
|
13
|
+
change.
|
|
14
|
+
|
|
15
|
+
**Status:** Early development. Tested with PostgreSQL 16 and psycopg 3. This
|
|
16
|
+
is soft TTL, not a security deadline.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Build the server extension using the matching PostgreSQL headers and PGXS:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
make PG_CONFIG=/path/to/pg_config
|
|
24
|
+
make PG_CONFIG=/path/to/pg_config install
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Configure these settings in `postgresql.conf` and restart PostgreSQL:
|
|
28
|
+
|
|
29
|
+
```conf
|
|
30
|
+
shared_preload_libraries = 'pg_ttl'
|
|
31
|
+
pg_ttl.databases = 'example'
|
|
32
|
+
pg_ttl.role = 'ttl_owner'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Create the named database and non-superuser maintenance role first. Install
|
|
36
|
+
the extension and grant access to the trusted role as described in
|
|
37
|
+
[Installation and Deployment](docs/installation.md).
|
|
38
|
+
Workers start automatically for the configured databases on server startup.
|
|
39
|
+
|
|
40
|
+
## Quickstart
|
|
41
|
+
|
|
42
|
+
Connect to the configured database as the trusted table-owner role.
|
|
43
|
+
The extension's SQL functions live in the `pgttl` schema:
|
|
44
|
+
|
|
45
|
+
```sql
|
|
46
|
+
SET search_path = public, pgttl;
|
|
47
|
+
|
|
48
|
+
CREATE TABLE sessions (
|
|
49
|
+
id bigint PRIMARY KEY,
|
|
50
|
+
data text
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
SELECT ttl_enable('sessions');
|
|
54
|
+
|
|
55
|
+
BEGIN;
|
|
56
|
+
INSERT INTO sessions VALUES (42, 'temporary');
|
|
57
|
+
SELECT ttl_set('sessions', 42, 5);
|
|
58
|
+
COMMIT;
|
|
59
|
+
|
|
60
|
+
SELECT * FROM sessions;
|
|
61
|
+
-- 42 | temporary
|
|
62
|
+
|
|
63
|
+
-- Wait for expiration and maintenance.
|
|
64
|
+
SELECT * FROM sessions;
|
|
65
|
+
-- No rows.
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Renew a deadline by calling `ttl_set` again or remove it with `ttl_clear`.
|
|
69
|
+
Rows without a scheduled expiration remain in the table.
|
|
70
|
+
|
|
71
|
+
## Python Bindings
|
|
72
|
+
|
|
73
|
+
Install the Python bindings with pip:
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
pip install pgttl
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The native extension must be installed and configured on the PostgreSQL
|
|
80
|
+
server. Use an existing psycopg connection:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import psycopg
|
|
84
|
+
import pgttl
|
|
85
|
+
|
|
86
|
+
with psycopg.connect("dbname=example user=ttl_owner", autocommit=True) as db:
|
|
87
|
+
pgttl.require(db)
|
|
88
|
+
db.execute("CREATE TABLE sessions (id bigint PRIMARY KEY, data text)")
|
|
89
|
+
pgttl.enable(db, "sessions")
|
|
90
|
+
with db.transaction():
|
|
91
|
+
db.execute("INSERT INTO sessions VALUES (%s, %s)", (42, "temporary"))
|
|
92
|
+
pgttl.set_ttl(db, "sessions", 42, 5)
|
|
93
|
+
|
|
94
|
+
print(pgttl.status(db))
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
No Python maintenance thread is needed. PostgreSQL owns expiration even
|
|
98
|
+
after the connection is closed.
|
|
99
|
+
|
|
100
|
+
## Features
|
|
101
|
+
|
|
102
|
+
- Automatic expiration on ordinary tables with integer, text or UUID keys.
|
|
103
|
+
- Transactional scheduling, renewal and clearing.
|
|
104
|
+
- Native deletion with ordinary SQL constraints and triggers.
|
|
105
|
+
- Bounded maintenance and health reporting.
|
|
106
|
+
- Explicit schema validation and acceptance.
|
|
107
|
+
- Optional transactional audit log with automatic retention.
|
|
108
|
+
- Optional Django instance helpers, migrations and system checks.
|
|
109
|
+
|
|
110
|
+
## Documentation
|
|
111
|
+
|
|
112
|
+
- [Installation and Deployment](docs/installation.md)
|
|
113
|
+
- [SQL API](docs/design.md)
|
|
114
|
+
- [Implementation Details](docs/implementation.md)
|
|
115
|
+
- [Python Bindings](docs/python.md)
|
|
116
|
+
|
|
117
|
+
## Related Work
|
|
118
|
+
|
|
119
|
+
[pg_ttl_index](https://pgxn.org/dist/pg_ttl_index/) offers automatic
|
|
120
|
+
expiration using a timestamp column and a configured retention interval.
|
|
121
|
+
[pg_cron](https://github.com/citusdata/pg_cron) can run scheduled DELETE
|
|
122
|
+
statements. [pg_partman](https://github.com/pgpartman/pg_partman) can remove
|
|
123
|
+
old time partitions efficiently.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# SQL API
|
|
2
|
+
|
|
3
|
+
The SQL functions live in the `pgttl` schema. Qualify calls explicitly or
|
|
4
|
+
set `search_path` to include `pgttl`.
|
|
5
|
+
|
|
6
|
+
| Function | Purpose |
|
|
7
|
+
| --- | --- |
|
|
8
|
+
| ttl_enable(table) | Register an ordinary table. |
|
|
9
|
+
| ttl_disable(table) | Unregister and discard its pending expirations. |
|
|
10
|
+
| ttl_set(table, id, seconds) | Set or renew a row's expiration. |
|
|
11
|
+
| ttl_clear(table, id) | Remove the expiration without deleting the row. |
|
|
12
|
+
| ttl_status() | Inspect maintenance health and deadlines. |
|
|
13
|
+
| ttl_config(name[, value]) | Inspect or configure maintenance. |
|
|
14
|
+
| ttl_validate() | Report table registration and schema diagnostics. |
|
|
15
|
+
| ttl_accept_schema(table) | Accept a changed schema with intact identity. |
|
|
16
|
+
|
|
17
|
+
Use PostgreSQL's `regclass` type to identify tables, including tables outside
|
|
18
|
+
the public schema. Primary keys may be smallint, integer, bigint, text,
|
|
19
|
+
varchar, or UUID, with deterministic collation and default key operator
|
|
20
|
+
classes. The server resolves the actual stored key before scheduling.
|
|
21
|
+
|
|
22
|
+
```sql
|
|
23
|
+
CREATE TABLE jobs (id uuid PRIMARY KEY, payload text);
|
|
24
|
+
SELECT pgttl.ttl_enable('jobs');
|
|
25
|
+
|
|
26
|
+
BEGIN;
|
|
27
|
+
INSERT INTO jobs VALUES ('b8ce5e52-66fc-481b-9ba7-959ba9d1a39f', 'job');
|
|
28
|
+
SELECT pgttl.ttl_set('jobs', 'b8ce5e52-66fc-481b-9ba7-959ba9d1a39f'::uuid, 60);
|
|
29
|
+
COMMIT;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
A deadline belongs to the enclosing transaction. Its wall-clock expiration is
|
|
33
|
+
calculated at scheduling time and stored as a `timestamptz`. Renew it by
|
|
34
|
+
replacing the deadline. A nonpositive integer TTL makes the row eligible for
|
|
35
|
+
deletion immediately after commit. The library does not commit a client's
|
|
36
|
+
transaction.
|
|
37
|
+
|
|
38
|
+
Only single-column primary keys on owned, persistent, ordinary tables
|
|
39
|
+
are supported. Partitioned and inherited tables, composite keys, custom
|
|
40
|
+
operator classes and row-level security are not supported.
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
Settings are persisted in the database:
|
|
45
|
+
|
|
46
|
+
| Setting | Default | Range |
|
|
47
|
+
| --- | ---: | --- |
|
|
48
|
+
| poll_interval_ms | 100 | 10–60,000 |
|
|
49
|
+
| batch_size | 100 | 1–10,000 |
|
|
50
|
+
| max_lag_seconds | 60 | 1–86,400 |
|
|
51
|
+
| audit | 0 | 0 or 1 |
|
|
52
|
+
| audit_retention_seconds | 604800 | 0 or positive integer seconds |
|
|
53
|
+
|
|
54
|
+
The maximum-lag value is a policy hint; it does not block SQL queries.
|
|
55
|
+
Auditing is disabled by default. Audit records survive disabling auditing,
|
|
56
|
+
subject to retention. Zero retention means indefinite retention.
|
|
57
|
+
|
|
58
|
+
```sql
|
|
59
|
+
SELECT pgttl.ttl_config('audit', 1);
|
|
60
|
+
SELECT pgttl.ttl_config('audit_retention_seconds', 604800);
|
|
61
|
+
SELECT * FROM pgttl._ttl_audit ORDER BY id;
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Health
|
|
65
|
+
|
|
66
|
+
`ttl_status()` returns a JSON object with fields including worker,
|
|
67
|
+
last_check, last_error, deleted_count, next_expiry, overdue_count and
|
|
68
|
+
overdue_seconds. A successful heartbeat alone does not prove that
|
|
69
|
+
expiration is keeping up. PostgreSQL logs errors that prevent a worker
|
|
70
|
+
transaction from completing.
|
|
71
|
+
|
|
72
|
+
## Schema Validation
|
|
73
|
+
|
|
74
|
+
`ttl_validate()` returns table_name, status, and detail columns for all
|
|
75
|
+
registered tables, including those without scheduled expirations.
|
|
76
|
+
|
|
77
|
+
- `ok`: valid registration with an unchanged schema fingerprint.
|
|
78
|
+
- `warning`: schema changed but key and identity triggers remain intact.
|
|
79
|
+
- `error`: current identity or key is invalid. Maintenance refuses deletion.
|
|
80
|
+
|
|
81
|
+
A developer may accept a warning using `ttl_accept_schema(table)`. This
|
|
82
|
+
only updates the stored fingerprint; it does not recreate triggers or change
|
|
83
|
+
deadlines. If a migration changes the primary key or identity markers,
|
|
84
|
+
disable TTL explicitly and re-register when appropriate. PostgreSQL DDL
|
|
85
|
+
that rewrites a table also invalidates its registration: key values may
|
|
86
|
+
change without a row trigger firing, even if the declared type is unchanged. This discards
|
|
87
|
+
pending expirations. The extension does not automatically repair them.
|
|
88
|
+
|
|
89
|
+
## Deletion Semantics
|
|
90
|
+
|
|
91
|
+
Maintenance uses ordinary SQL DELETE statements. Database constraints and
|
|
92
|
+
SQL triggers still apply. Django ORM cascades, model deletion methods and
|
|
93
|
+
Python signals do not run. An incoming foreign key can block expiration
|
|
94
|
+
unless the database schema defines a suitable deletion action.
|
|
95
|
+
|
|
96
|
+
An expired row remains visible until maintenance deletes it. PostgreSQL's
|
|
97
|
+
MVCC means even a deleted row may remain visible to an older snapshot.
|
|
98
|
+
An application that needs exact expiry must apply its own deadline
|
|
99
|
+
predicate or appropriate authorization check.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Implementation Details
|
|
2
|
+
|
|
3
|
+
A native C worker is registered with the PostgreSQL postmaster using
|
|
4
|
+
[background workers](https://www.postgresql.org/docs/current/bgworker.html).
|
|
5
|
+
It uses the [Server Programming Interface](https://www.postgresql.org/docs/current/spi.html)
|
|
6
|
+
to execute the extension's SQL functions in ordinary transactions.
|
|
7
|
+
The rest of the implementation uses PostgreSQL SQL and PL/pgSQL.
|
|
8
|
+
|
|
9
|
+
## Metadata
|
|
10
|
+
|
|
11
|
+
The extension stores metadata in the `pgttl` schema:
|
|
12
|
+
|
|
13
|
+
- `_ttl_tables`: relation OID, key column identity and type, unique
|
|
14
|
+
lifetime token and schema fingerprint.
|
|
15
|
+
- `_ttl_entries`: relation OID, canonical row key, set time, seconds and
|
|
16
|
+
deadline, indexed by deadline.
|
|
17
|
+
- `_ttl_config`: persisted settings.
|
|
18
|
+
- `_ttl_health`: worker status and counts.
|
|
19
|
+
- `_ttl_audit`: optional expiration history.
|
|
20
|
+
|
|
21
|
+
The small supported set of key types has stable text representations.
|
|
22
|
+
Metadata uses binary `C` collation to preserve text key identity, including
|
|
23
|
+
numeric-looking text such as "001". The worker casts canonical keys back to
|
|
24
|
+
their registered PostgreSQL types for indexed lookups. Scheduling first
|
|
25
|
+
resolves the actual stored key, rather than recording an alternate spelling.
|
|
26
|
+
|
|
27
|
+
An OID alone is not an identity. Registration creates randomly named row
|
|
28
|
+
and TRUNCATE triggers. INSERT, DELETE, and key-changing UPDATE clear
|
|
29
|
+
associated deadlines. TRUNCATE clears all table deadlines. A DDL event
|
|
30
|
+
trigger removes registrations for dropped relations. Another event trigger
|
|
31
|
+
invalidates registrations on PostgreSQL DDL table rewrites, which can change
|
|
32
|
+
key values without firing row triggers even when the declared type stays
|
|
33
|
+
the same. This deliberately errs on the side of requiring re-registration.
|
|
34
|
+
Before deleting,
|
|
35
|
+
validate both the registered primary key (including typmod and collation)
|
|
36
|
+
and identity trigger definitions.
|
|
37
|
+
Dropping and recreating a table is not treated as continuation of its prior
|
|
38
|
+
lifetime. Logical dump/restore of registered tables is unsupported without
|
|
39
|
+
explicit disable/re-registration.
|
|
40
|
+
|
|
41
|
+
## Schema Changes
|
|
42
|
+
|
|
43
|
+
Registration stores a fingerprint of table identity, columns, defaults and
|
|
44
|
+
constraints. `ttl_validate()` reports drift and invalid identity even when
|
|
45
|
+
there are no pending deadlines. `ttl_accept_schema()` only accepts a changed
|
|
46
|
+
fingerprint if the key and registration remain intact. It does not infer
|
|
47
|
+
a safe migration or automatically rebind metadata. Do not disable triggers
|
|
48
|
+
or use replication modes that bypass them on registered tables.
|
|
49
|
+
|
|
50
|
+
## Transactions and Concurrency
|
|
51
|
+
|
|
52
|
+
The worker runs as the configured non-superuser owner role. Each transaction:
|
|
53
|
+
|
|
54
|
+
1. Find due candidates in deadline order.
|
|
55
|
+
2. Lock the actual target row using `FOR UPDATE SKIP LOCKED`.
|
|
56
|
+
3. Lock and reread the current deadline; skip renewed or cleared deadlines.
|
|
57
|
+
4. Delete the row, clear metadata, optionally record an audit event.
|
|
58
|
+
5. Commit and yield.
|
|
59
|
+
|
|
60
|
+
Scheduling uses the same row-before-metadata lock order. Candidate attempts
|
|
61
|
+
use subtransactions so constraint failures do not prevent unrelated eligible
|
|
62
|
+
deletions in the same batch. Locks are bounded and retried on later polls.
|
|
63
|
+
A transaction scans at most ten times the batch size and deletes at most
|
|
64
|
+
the batch size. Long-lived locked or blocked rows at the front of a very
|
|
65
|
+
large backlog can still delay later candidates.
|
|
66
|
+
|
|
67
|
+
A deletion suppressed by a trigger, or one that leaves the target key
|
|
68
|
+
present, is an error and rolls back. Native constraints, including
|
|
69
|
+
database-level cascades, are enforced. Dead tuples produced by expiration
|
|
70
|
+
are reclaimed through ordinary vacuum/autovacuum. Applications' triggers
|
|
71
|
+
must work with the maintenance role and qualify any referenced tables if
|
|
72
|
+
they depend on a specific search path. Triggers that block for a long time,
|
|
73
|
+
execute arbitrary additional side effects, or rely on application-local
|
|
74
|
+
state may be incompatible.
|
|
75
|
+
|
|
76
|
+
## Expiration Audit Log
|
|
77
|
+
|
|
78
|
+
Auditing is optional and disabled by default:
|
|
79
|
+
|
|
80
|
+
```sql
|
|
81
|
+
SELECT pgttl.ttl_config('audit', 1);
|
|
82
|
+
SELECT pgttl.ttl_config('audit_retention_seconds', 604800);
|
|
83
|
+
SELECT * FROM pgttl._ttl_audit ORDER BY id;
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Each event contains an increasing bigint identity, table name, canonical
|
|
87
|
+
row ID with its PostgreSQL key type, scheduled expiration time, and actual
|
|
88
|
+
deletion time. The audit record and deletion occur in the same transaction.
|
|
89
|
+
If insertion fails, the deletion rolls back and the deadline is retained.
|
|
90
|
+
|
|
91
|
+
Only rows directly deleted by TTL maintenance generate events. Manual
|
|
92
|
+
deletions, indirect cascaded rows, and missing rows do not. No row payload
|
|
93
|
+
is retained.
|
|
94
|
+
|
|
95
|
+
The worker removes records older than `audit_retention_seconds` in bounded
|
|
96
|
+
batches, including when no application rows are due. The default is seven
|
|
97
|
+
days. Zero retains indefinitely. Disabling auditing stops new events but does
|
|
98
|
+
not clear the existing log or stop retention.
|
|
99
|
+
|
|
100
|
+
Consumers can poll by event ID and persist a cursor. IDs increase during
|
|
101
|
+
ordinary maintenance even when retention removes older entries, but gaps
|
|
102
|
+
from rolled-back transactions are normal. This is an audit trail and a
|
|
103
|
+
lightweight transactional outbox, not guaranteed delivery, encrypted
|
|
104
|
+
storage, or a promise of physical erasure.
|
|
105
|
+
|
|
106
|
+
## Worker Lifecycle
|
|
107
|
+
|
|
108
|
+
One postmaster-started worker per configured database connects to that
|
|
109
|
+
database under the same non-superuser role. The database list is explicit:
|
|
110
|
+
no privileged discovery process or application-side scheduler is required.
|
|
111
|
+
PostgreSQL restarts workers following failure and on server restart, and
|
|
112
|
+
only starts these workers after recovery on a primary.
|
|
113
|
+
|
|
114
|
+
The worker's status reports heartbeat age, latest row-deletion failure,
|
|
115
|
+
total deleted rows, and current backlog. Errors preventing a transaction
|
|
116
|
+
from committing are also reported in the server log; a stale heartbeat is
|
|
117
|
+
then an important diagnostic.
|
|
118
|
+
|
|
119
|
+
## Privileges
|
|
120
|
+
|
|
121
|
+
TTL is restricted to the configured role and tables it owns. The role is
|
|
122
|
+
trusted with access to internal metadata; public access is revoked. User-
|
|
123
|
+
defined SQL triggers run under the normal PostgreSQL privilege rules, not
|
|
124
|
+
under a privileged maintenance superuser. The DDL cleanup function is a
|
|
125
|
+
fixed security-definer function that deletes only registrations for relations
|
|
126
|
+
reported as dropped by PostgreSQL.
|
|
127
|
+
|
|
128
|
+
A generalized multi-role privilege model, row-level security, arbitrary
|
|
129
|
+
key types, partitioned tables, logical backup/restore of TTL state, and
|
|
130
|
+
production-scale benchmarks remain future work.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Installation and Deployment
|
|
2
|
+
|
|
3
|
+
The Python wheel contains the
|
|
4
|
+
client bindings only. To obtain the server source from PyPI:
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
pip download --no-binary=:all: --no-deps pgttl
|
|
8
|
+
tar -xzf pgttl-0.1.0.tar.gz
|
|
9
|
+
cd pgttl-0.1.0
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The native C extension is built with PGXS. Use the `pg_config` from the
|
|
13
|
+
same PostgreSQL major version that will load the extension.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
make PG_CONFIG=/path/to/pg_config
|
|
17
|
+
make PG_CONFIG=/path/to/pg_config install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Installing the library and SQL files may require administrator privileges.
|
|
21
|
+
It does not change a running server's configuration.
|
|
22
|
+
|
|
23
|
+
## Database Setup
|
|
24
|
+
|
|
25
|
+
Create a non-superuser role for TTL-managed tables. This example also creates
|
|
26
|
+
a database; adapt authentication and role provisioning to your environment.
|
|
27
|
+
|
|
28
|
+
```sql
|
|
29
|
+
CREATE ROLE ttl_owner LOGIN;
|
|
30
|
+
CREATE DATABASE example OWNER ttl_owner;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Connect to `example` as a database administrator:
|
|
34
|
+
|
|
35
|
+
```sql
|
|
36
|
+
CREATE EXTENSION pg_ttl;
|
|
37
|
+
GRANT USAGE ON SCHEMA pgttl TO ttl_owner;
|
|
38
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA pgttl TO ttl_owner;
|
|
39
|
+
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA pgttl TO ttl_owner;
|
|
40
|
+
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA pgttl TO ttl_owner;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The extension is installed in the fixed schema `pgttl` because PostgreSQL
|
|
44
|
+
reserves the `pg_` prefix for system schemas. The extension, C library and
|
|
45
|
+
configuration settings are named `pg_ttl`.
|
|
46
|
+
|
|
47
|
+
These grants are for a **trusted** role. The first version deliberately
|
|
48
|
+
requires that role to own each registered application table. Direct metadata
|
|
49
|
+
access is not intended for untrusted clients. The schema, metadata and
|
|
50
|
+
functions are not accessible to PUBLIC by default. Use appropriate database
|
|
51
|
+
authentication and do not grant the trusted role to clients arbitrarily.
|
|
52
|
+
|
|
53
|
+
## Server Configuration
|
|
54
|
+
|
|
55
|
+
Append the library to any existing `shared_preload_libraries` setting and
|
|
56
|
+
configure the database list and role in `postgresql.conf`:
|
|
57
|
+
|
|
58
|
+
```conf
|
|
59
|
+
shared_preload_libraries = 'pg_ttl'
|
|
60
|
+
pg_ttl.databases = 'example'
|
|
61
|
+
pg_ttl.role = 'ttl_owner'
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Use a comma-separated list to maintain multiple databases under the same
|
|
65
|
+
role. Quote database names containing uppercase letters or commas. Each
|
|
66
|
+
database needs its own installed extension and grants. Ensure the server's
|
|
67
|
+
`max_worker_processes` has capacity for one worker per configured database
|
|
68
|
+
in addition to its other workloads.
|
|
69
|
+
|
|
70
|
+
Restart PostgreSQL. Workers start on a writable primary after recovery;
|
|
71
|
+
a configured database without the extension is checked periodically but
|
|
72
|
+
not modified. Recovery of interrupted transactions is PostgreSQL's job.
|
|
73
|
+
The postmaster restarts workers that exit unexpectedly. No application
|
|
74
|
+
connections are required.
|
|
75
|
+
|
|
76
|
+
A server restart is required to change the database list or role. Explicit
|
|
77
|
+
configuration avoids a privileged launcher scanning arbitrary databases.
|
|
78
|
+
Managed servers that prohibit installing custom C extensions cannot use
|
|
79
|
+
this implementation.
|
|
80
|
+
|
|
81
|
+
## Maintenance
|
|
82
|
+
|
|
83
|
+
```sql
|
|
84
|
+
SELECT pgttl.ttl_status();
|
|
85
|
+
SELECT * FROM pgttl.ttl_validate();
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Monitor both the heartbeat and overdue age. A healthy worker can still
|
|
89
|
+
have overdue rows if locks, constraints or volume prevent maintenance from
|
|
90
|
+
keeping up. Status is database-local, not a cluster-wide assurance.
|
|
91
|
+
|
|
92
|
+
## Backups and Restores
|
|
93
|
+
|
|
94
|
+
Physical backups include TTL metadata. Logical dumps do not preserve the
|
|
95
|
+
extension's internal registry, deadlines or audit log. Disable registrations
|
|
96
|
+
and discard deadlines before producing a logical dump, or arrange an explicit
|
|
97
|
+
application-level export and restoration strategy separately. Identity
|
|
98
|
+
triggers created by registration are dependent on the extension function;
|
|
99
|
+
restoring them without registration metadata is unsupported. Never infer
|
|
100
|
+
the identity of a restored table from its name or old OID. Register and
|
|
101
|
+
schedule restored rows deliberately.
|
|
102
|
+
|
|
103
|
+
## Development
|
|
104
|
+
|
|
105
|
+
Build and install the extension against a test PostgreSQL installation.
|
|
106
|
+
The integration suite starts and stops a fresh, isolated server using a
|
|
107
|
+
private Unix socket; it never changes the configuration of an existing server.
|
|
108
|
+
|
|
109
|
+
```sh
|
|
110
|
+
python -m pip install '.[django]' 'psycopg[binary]'
|
|
111
|
+
./test_integration.sh
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Use a recent minor release of your chosen PostgreSQL version in deployment.
|
|
115
|
+
The workflow targets PostgreSQL 16 and 19 with Django 5.2 LTS and the
|
|
116
|
+
latest stable Django release. Local integration tests use PostgreSQL 16.
|