Skip to content

Commit 4c46699

Browse files
Kristiansen, Erling Gustav MolandKristiansen, Erling Gustav Moland
authored andcommitted
feat: add pg_partman container image
pg_partman is a PostgreSQL extension for automated table partition management. It supports both time-based and ID-based partitioning with automatic creation and maintenance of child tables. The background worker (pg_partman_bgw) can automatically run partition maintenance at configured intervals, removing the need for external cron jobs. Closes #205 Signed-off-by: Kristiansen, Erling Gustav Moland <Erling.Kristiansen@skatteetaten.no>
1 parent ce743ae commit 4c46699

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

pg-partman/Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: Copyright © contributors to CloudNativePG, established as CloudNativePG a Series of LF Projects, LLC.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
ARG BASE=ghcr.io/cloudnative-pg/postgresql:18-minimal-trixie
5+
FROM $BASE AS builder
6+
7+
ARG PG_MAJOR
8+
ARG EXT_VERSION
9+
10+
USER 0
11+
12+
# Install extension via `apt-get`
13+
RUN apt-get update && apt-get install -y --no-install-recommends \
14+
"postgresql-${PG_MAJOR}-partman=${EXT_VERSION}"
15+
16+
FROM scratch
17+
ARG PG_MAJOR
18+
19+
# Licenses
20+
COPY --from=builder /usr/share/doc/postgresql-${PG_MAJOR}-partman/copyright /licenses/postgresql-${PG_MAJOR}-partman/
21+
22+
# Libraries
23+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_partman_bgw.so /lib/
24+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/bitcode/ /lib/bitcode/
25+
26+
# Share
27+
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/pg_partman* /share/extension/
28+
29+
# Binaries (maintenance scripts)
30+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/check_unique_constraint.py /bin/
31+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/dump_partition.py /bin/
32+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/vacuum_maintenance.py /bin/
33+
34+
USER 65532:65532

pg-partman/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# pg_partman
2+
<!--
3+
SPDX-FileCopyrightText: Copyright © contributors to CloudNativePG, established as CloudNativePG a Series of LF Projects, LLC.
4+
SPDX-License-Identifier: Apache-2.0
5+
-->
6+
7+
[pg_partman](https://github.com/pgpartman/pg_partman) is a PostgreSQL extension
8+
for automated table partition management. It supports both time-based and
9+
ID-based partitioning with automatic creation and maintenance of child tables.
10+
11+
The extension includes a background worker (`pg_partman_bgw`) that can
12+
automatically run partition maintenance at configured intervals, removing the
13+
need for external cron jobs.
14+
15+
For more information, see the
16+
[official documentation](https://github.com/pgpartman/pg_partman).
17+
18+
## Usage
19+
20+
The `pg_partman` extension must be loaded via `shared_preload_libraries` to
21+
enable the background worker. The worker periodically runs
22+
`partman.run_maintenance_proc()` to create new partitions and drop expired ones.
23+
24+
### 1. Add the pg_partman extension image to your Cluster
25+
26+
Define the `pg-partman` extension under the `postgresql.extensions` section of
27+
your `Cluster` resource. For example:
28+
29+
```yaml
30+
apiVersion: postgresql.cnpg.io/v1
31+
kind: Cluster
32+
metadata:
33+
name: cluster-pg-partman
34+
spec:
35+
imageName: ghcr.io/cloudnative-pg/postgresql:18-minimal-trixie
36+
instances: 1
37+
38+
storage:
39+
size: 1Gi
40+
41+
postgresql:
42+
shared_preload_libraries:
43+
- pg_partman_bgw
44+
extensions:
45+
- name: pg-partman
46+
image:
47+
# renovate: suite=trixie-pgdg depName=postgresql-18-partman
48+
reference: ghcr.io/cloudnative-pg/pg-partman:5.4.3-18-trixie
49+
```
50+
51+
### 2. Enable the extension in a database
52+
53+
You can install `pg_partman` in a specific database by creating or updating a
54+
`Database` resource. For example, to enable it in the `app` database:
55+
56+
```yaml
57+
apiVersion: postgresql.cnpg.io/v1
58+
kind: Database
59+
metadata:
60+
name: cluster-pg-partman-app
61+
spec:
62+
name: app
63+
owner: app
64+
cluster:
65+
name: cluster-pg-partman
66+
extensions:
67+
- name: pg_partman
68+
# renovate: suite=trixie-pgdg depName=postgresql-18-partman
69+
version: '5.4.3'
70+
```
71+
72+
### 3. Verify installation
73+
74+
Once the database is ready, connect to it with `psql` and run:
75+
76+
```sql
77+
\dx
78+
```
79+
80+
You should see `pg_partman` listed among the installed extensions.
81+
82+
## Included utilities
83+
84+
This image also bundles the following Python maintenance scripts in `/bin/`:
85+
86+
- `check_unique_constraint.py` — validates unique constraints across partitions
87+
- `dump_partition.py` — exports individual partitions for archival
88+
- `vacuum_maintenance.py` — targeted vacuum operations on partitioned tables
89+
90+
> [!NOTE]
91+
> These scripts require a Python 3 runtime and the `psycopg2` library, which
92+
> are not included in the minimal base image. They are provided for
93+
> environments where Python is available on the host or in a sidecar container.
94+
95+
## Contributors
96+
97+
This extension is maintained by:
98+
99+
- Erling Kristiansen (@egkristi)
100+
101+
The maintainers are responsible for:
102+
103+
- Monitoring upstream releases and security vulnerabilities.
104+
- Ensuring compatibility with supported PostgreSQL versions.
105+
- Reviewing and merging contributions specific to this extension's container
106+
image and lifecycle.
107+
108+
---
109+
110+
## Licenses and Copyright
111+
112+
This container image contains software that may be licensed under various
113+
open-source licenses.
114+
115+
All relevant license and copyright information for the `pg_partman` extension
116+
and its dependencies are bundled within the image at:
117+
118+
```text
119+
/licenses/
120+
```
121+
122+
By using this image, you agree to comply with the terms of the licenses
123+
contained therein.

pg-partman/metadata.hcl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: Copyright © contributors to CloudNativePG, established as CloudNativePG a Series of LF Projects, LLC.
2+
# SPDX-License-Identifier: Apache-2.0
3+
metadata = {
4+
name = "pg-partman"
5+
sql_name = "pg_partman"
6+
image_name = "pg-partman"
7+
licenses = ["PostgreSQL"]
8+
shared_preload_libraries = ["pg_partman_bgw"]
9+
postgresql_parameters = {}
10+
extension_control_path = []
11+
dynamic_library_path = []
12+
ld_library_path = []
13+
bin_path = ["bin"]
14+
env = {}
15+
auto_update_os_libs = false
16+
required_extensions = []
17+
create_extension = true
18+
19+
versions = {
20+
bookworm = {
21+
"18" = {
22+
// renovate: suite=bookworm-pgdg depName=postgresql-18-partman
23+
package = "5.4.3-1.pgdg12+1"
24+
// renovate: suite=bookworm-pgdg depName=postgresql-18-partman extractVersion=^(?<version>\d+\.\d+\.\d+)
25+
sql = "5.4.3"
26+
}
27+
}
28+
trixie = {
29+
"18" = {
30+
// renovate: suite=trixie-pgdg depName=postgresql-18-partman
31+
package = "5.4.3-1.pgdg13+1"
32+
// renovate: suite=trixie-pgdg depName=postgresql-18-partman extractVersion=^(?<version>\d+\.\d+\.\d+)
33+
sql = "5.4.3"
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)