Skip to content

Added pg_partman extension#159

Open
egkristi wants to merge 1 commit into
cloudnative-pg:mainfrom
egkristi:dev/pg-partman
Open

Added pg_partman extension#159
egkristi wants to merge 1 commit into
cloudnative-pg:mainfrom
egkristi:dev/pg-partman

Conversation

@egkristi

@egkristi egkristi commented Mar 24, 2026

Copy link
Copy Markdown

Following the template and Contribution instructions.
This is the pg_partman extension.
Closes #205

@egkristi
egkristi requested review from a team and NiccoloFei as code owners March 24, 2026 10:14
@gbartolini

Copy link
Copy Markdown
Contributor

Thanks @egkristi for the contribution. However, as the contribution guide says, we need an issue to be created before the PR. Could you please do it and then link the PR to the issue? Thanks.

@tix79a

tix79a commented May 18, 2026

Copy link
Copy Markdown

Hello, any news about this extension ?

@egkristi egkristi mentioned this pull request May 18, 2026
4 tasks
@egkristi
egkristi force-pushed the dev/pg-partman branch 2 times, most recently from 6bec61a to 7878f87 Compare May 19, 2026 05:57
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 cloudnative-pg#205

Signed-off-by: Kristiansen, Erling Gustav Moland <Erling.Kristiansen@skatteetaten.no>
Comment thread pg-partman/Dockerfile
Comment on lines +29 to +32
# Binaries (maintenance scripts)
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/check_unique_constraint.py /bin/
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/dump_partition.py /bin/
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/vacuum_maintenance.py /bin/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

Comment thread pg-partman/metadata.hcl
extension_control_path = []
dynamic_library_path = []
ld_library_path = []
bin_path = ["bin"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following this standard

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gbartolini

Copy link
Copy Markdown
Contributor

Do you think you can resolve the conflict?

Is it feasible to add a specific smoke test for the extension?

@duje-gradient

Copy link
Copy Markdown

Hi, any updates on when this extension will be released in an official image? It would be highly appreciated by the community as it saves teams from maintaining custom builds.

Comment thread pg-partman/Dockerfile
Comment on lines +30 to +32
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/check_unique_constraint.py /bin/
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/dump_partition.py /bin/
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/bin/vacuum_maintenance.py /bin/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This python scripts have hard dependencies on python and psycopg2 so adding them straight to the /bin directory is not enough to have them working properly. The dependencies should be packaged as well in the extension container in a way they can be resolved by those scripts at runtime. As far as I understand, they are there mainly for maintenance operations and are not referenced directly by the extension. I'd rather avoid including them here as I believe the extension container should be kept as light as possible. Those scripts seem more client-side tools as they connect to Postgres over the network; they don't need to run inside the Postgres container at all. We can explore and document alternatives to execute them externally in a way they can run in a K8s env and target a CNPG Cluster.

@GabriFedi97 GabriFedi97 Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to execute the scripts externally via a Kubernetes Job created in the CNPG Cluster namespace. Leaving the Job definition as a POC:

apiVersion: batch/v1
kind: Job
metadata:
  name: partman-maint-poc
  namespace: <your-cluster-namespace>
  labels:
    app: partman-maint-poc
spec:
  backoffLimit: 0
  ttlSecondsAfterFinished: 3600
  template:
    metadata:
      labels:
        app: partman-maint-poc
    spec:
      restartPolicy: Never
      containers:
      - name: maint
        image: python:3-slim
        # --- Connection info shared from the CNPG-generated app secret ---
        env:
        - name: DB_URI
          valueFrom: { secretKeyRef: { name: pg-partman-app, key: uri } }
        - name: PGHOST
          value: pg-partman-rw
        - name: PGPORT
          valueFrom: { secretKeyRef: { name: pg-partman-app, key: port } }
        - name: PGUSER
          valueFrom: { secretKeyRef: { name: pg-partman-app, key: username } }
        - name: PGPASSWORD
          valueFrom: { secretKeyRef: { name: pg-partman-app, key: password } }
        - name: PGDATABASE
          valueFrom: { secretKeyRef: { name: pg-partman-app, key: dbname } }
        # Verify the CNPG server cert against the cluster CA
        - name: PGSSLMODE
          value: verify-full
        - name: PGSSLROOTCERT
          value: /etc/ca/ca.crt
        volumeMounts:
        - name: ca
          mountPath: /etc/ca
          readOnly: true
        command: ["bash", "-c"]
        args:
        - |
          set -euo pipefail

          echo "### 1. Install dependencies"
          apt-get update && apt-get install -y --no-install-recommends curl
          pip install --root-user-action=ignore --no-cache-dir psycopg2-binary

          echo "### 2. Fetch the pg_partman maintenance scripts (v5.4.3)"
          mkdir -p /opt/partman && cd /opt/partman
          for s in check_unique_constraint dump_partition vacuum_maintenance; do
            curl -sfLO "https://raw.githubusercontent.com/pgpartman/pg_partman/v5.4.3/bin/common/$s.py" \
            || curl -sfLO "https://raw.githubusercontent.com/pgpartman/pg_partman/v5.4.3/bin/$s.py"
          done
          chmod +x ./*.py
          ls -l ./*.py

          echo "### 3. Prove the scripts run (deps satisfied)"
          python vacuum_maintenance.py --help

          echo "### 4. Best-effort real run against the cluster"
          python vacuum_maintenance.py -c "$DB_URI" --all \
            && echo "vacuum_maintenance completed" \
            || echo "NOTE: script ran but exited non-zero (likely partman privileges/config) — mechanism is proven"

          echo "### DONE"
      volumes:
      - name: ca
        secret:
          secretName: pg-partman-ca

To make it work properly I had to grant the app user the permissions to the part_config and part_config_sub tables:

GRANT ALL PRIVILEGES ON TABLE part_config, part_config_sub TO app;

This is merely a POC to show the approach. We can revisit the details in case we agree on the proposal.

Comment thread pg-partman/README.md
Comment on lines +84 to +94
This image also bundles the following Python maintenance scripts in `/bin/`:

- `check_unique_constraint.py` — validates unique constraints across partitions
- `dump_partition.py` — exports individual partitions for archival
- `vacuum_maintenance.py` — targeted vacuum operations on partitioned tables

> [!NOTE]
> These scripts require a Python 3 runtime and the `psycopg2` library, which
> are not included in the minimal base image. They are provided for
> environments where Python is available on the host or in a sidecar container.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section would have to change to document the alternative way to execute the scripts in case we agree on https://github.com/cloudnative-pg/postgres-extensions-containers/pull/159/changes#r3601885000.

Comment thread pg-partman/metadata.hcl
extension_control_path = []
dynamic_library_path = []
ld_library_path = []
bin_path = ["bin"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[New Extension]: pg_partman

5 participants