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 same shape as the local timescaledb package: SQLAlchemy engine/dialect helpers, SQLModel base models and sync 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

import deltax


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

    __tablename__ = "metrics"
    __partition_interval__ = "1 day"
    __premake__ = 3
    __enable_compression__ = True
    __segment_by__ = ["sensor_id"]
    __order_by__ = ["time"]
    __compression_policy_after__ = "7 days"
    __drop_after__ = "90 days"


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

DeltaxModel uses a composite primary key of id and time, matching the partitioning constraints PostgreSQL applies to time-partitioned tables.

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)


@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.1.tar.gz (54.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.1-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pg_deltax-0.0.1.tar.gz
  • Upload date:
  • Size: 54.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.1.tar.gz
Algorithm Hash digest
SHA256 a2b253a6096247bc8ad01b2fb738f12120d16a8ddbb0773297a057a910a54eb8
MD5 8d7cf53237faed21544b32a9b433bcb7
BLAKE2b-256 082c69b71ede96d7123e3be8a1a12ee5a412c9e8693e61be7032c87a2e1c006f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_deltax-0.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: pg_deltax-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9aab781a653f221c5658cd72916294725a91e3f7e5dd203674a7f22a93b3cc87
MD5 c22ce35675dcd541824ad0a81626c362
BLAKE2b-256 f4b1480451cc0adc9b1c43e9ffccc8eeff3b52f9db4671d9f28200c6f9c0114d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_deltax-0.0.1-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