Skip to main content

Python helpers for the pg_deltax PostgreSQL extension with SQLModel, FastAPI, and Django support.

Project description

pg-deltax

pg-deltax is a Python companion package for the pg_deltax PostgreSQL extension. It keeps the Python layer thin: SQLAlchemy engine/dialect helpers, SQLModel table shapes, explicit wrappers for the pg_deltax SQL functions, FastAPI session glue, and a Django app/backend/migration layer.

DeltaX functions are schema-qualified as deltax.<function> to match the extension docs.

Install

pip install pg-deltax
pip install "pg-deltax[fastapi]"
pip install "pg-deltax[django]"

The database must have pg_deltax installed and loadable by PostgreSQL.

The distribution name is pg-deltax; the Python import package is deltax.

SQLModel

from sqlmodel import Field
from sqlmodel import Session
from sqlmodel import SQLModel

import deltax


class Metric(deltax.DeltaxModel, table=True):
    sensor_id: int = Field(index=True)
    value: float

    __tablename__ = "metrics"


engine = deltax.create_engine("postgresql+psycopg://user:pass@localhost/db")
SQLModel.metadata.create_all(engine)

with Session(engine) as session:
    deltax.activate_deltax_extension(session)
    deltax.create_deltatable(session, Metric, partition_interval="1 day", premake=3)
    deltax.enable_compression(
        session,
        Metric,
        segment_by=["sensor_id"],
        order_by=["time"],
    )
    deltax.set_compression_policy(session, Metric, "7 days")
    deltax.set_retention(session, Metric, "90 days")

DeltaxModel uses a composite primary key of id and time, matching the partitioning constraints PostgreSQL applies to time-partitioned tables. DeltaX settings live in explicit Deltatable specs or direct function calls, matching the pg_deltax extension API instead of SQLModel class attributes. For startup sync, a Deltatable spec runs the same pg_deltax setup from an explicit value:

metric_deltatable = deltax.Deltatable(
    Metric,
    partition_interval="1 day",
    premake=3,
    compression=deltax.CompressionConfig(
        segment_by=["sensor_id"],
        order_by=["time"],
        compress_after="7 days",
    ),
    drop_after="90 days",
)

deltax.create_all(engine, deltatables=[metric_deltatable])

Query helpers expose DeltaX analytics functions:

from sqlmodel import Session

with Session(engine) as session:
    rows = deltax.time_bucket_query(
        session,
        Metric,
        interval="1 hour",
        time_field="time",
        metric_field="value",
    )

FastAPI

from fastapi import Depends, FastAPI
from sqlmodel import Session

import deltax

engine = deltax.create_engine("postgresql+psycopg://user:pass@localhost/db")
get_session = deltax.fastapi.create_session_dependency(engine)

app = FastAPI()


@app.on_event("startup")
def startup() -> None:
    deltax.create_all(engine, deltatables=[metric_deltatable])


@app.get("/metrics")
def metrics(session: Session = Depends(get_session)):
    return deltax.time_bucket_query(session, Metric, metric_field="value")

Django

Add the app and use the backend if you want automatic extension creation:

INSTALLED_APPS = [
    "django.contrib.contenttypes",
    "deltax.django",
]

DATABASES = {
    "default": {
        "ENGINE": "deltax.django.db.backends.postgresql",
        "NAME": "app",
        "USER": "postgres",
        "PASSWORD": "postgres",
        "HOST": "localhost",
        "PORT": "5432",
        "OPTIONS": {"deltax_auto_create_extension": True},
    }
}

Define models with the Django shims:

from deltax.django.db import models


class Metric(models.DeltaxModel):
    time = models.DeltaxDateTimeField(interval="1 day", primary_key=True)
    sensor_id = models.IntegerField()
    value = models.FloatField()

Use migration operations to manage DeltaX:

from django.db import migrations
from deltax.django.db import migrations as deltax_migrations


class Migration(migrations.Migration):
    operations = [
        deltax_migrations.CreateExtension(),
        deltax_migrations.CreateDeltatable("Metric", time_column="time"),
        deltax_migrations.EnableCompression("Metric", segment_by=["sensor_id"]),
        deltax_migrations.SetCompressionPolicy("Metric", compress_after="7 days"),
        deltax_migrations.SetRetention("Metric", drop_after="90 days"),
    ]

Querysets can annotate with DeltaX functions:

Metric.objects.time_bucket("1 hour").values("bucket").annotate(avg=models.Avg("value"))

Live pg_deltax check

Use Docker Compose to build a PostgreSQL image with the upstream pg_deltax extension and run the live integration test:

docker compose up --build --abort-on-container-exit --exit-code-from live-test live-test

See docs/live-pg-deltax.md for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_deltax-0.0.2.tar.gz (55.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_deltax-0.0.2-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file pg_deltax-0.0.2.tar.gz.

File metadata

  • Download URL: pg_deltax-0.0.2.tar.gz
  • Upload date:
  • Size: 55.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pg_deltax-0.0.2.tar.gz
Algorithm Hash digest
SHA256 92ef127eb717601633b2f01050a5254268e520f0dc4fef77c917d02ae9f6d7ed
MD5 a734418c432bb68a406689e12ab997c7
BLAKE2b-256 57f471c3cd316bc09eaaf205ca2015c80116769dd35fa724d4a2ae026c73e634

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_deltax-0.0.2.tar.gz:

Publisher: pypi.yaml on jmitchel3/deltaX-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_deltax-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pg_deltax-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pg_deltax-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 13cbafcfbdbff11c546dd807068e299ae8ed4030240e24791dd7b3db2ef24ab2
MD5 557b591f574cbe084c340ad97855d9d6
BLAKE2b-256 9f24574660dc6f6ed485ac12f67f6e1fdcf00925f09edc77543e11c3ba9e8df6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_deltax-0.0.2-py3-none-any.whl:

Publisher: pypi.yaml on jmitchel3/deltaX-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page