Skip to content

Commit

Permalink
add partitioning to pg_telemetry SimpleMetric model
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-codecov committed Jun 6, 2024
1 parent bf76a73 commit d290285
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 1 addition & 3 deletions shared/django_apps/db_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@
POSTGRES_EXTRA_DB_BACKEND_BASE: "django_prometheus.db.backends.postgresql" # type: ignore

# Allows to use the pgpartition command
PSQLEXTRA_PARTITIONING_MANAGER = (
"shared.django_apps.user_measurements.partitioning.manager"
)
PSQLEXTRA_PARTITIONING_MANAGER = "shared.django_apps.partitioning.manager"

DATABASE_ROUTERS = [
"shared.django_apps.db_routers.MultiDatabaseRouter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
)
from psqlextra.partitioning.config import PostgresPartitioningConfig

from shared.django_apps.pg_telemetry.models import SimpleMetric
from shared.django_apps.user_measurements.models import UserMeasurement

# Overlapping partitions will cause errors - https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE -> "create partitions"
Expand All @@ -23,5 +24,13 @@
max_age=relativedelta(months=12),
),
),
PostgresPartitioningConfig(
model=SimpleMetric,
strategy=PostgresCurrentTimePartitioningStrategy(
size=PostgresTimePartitionSize(months=1),
count=3,
max_age=relativedelta(months=6),
),
),
]
)
8 changes: 7 additions & 1 deletion shared/django_apps/pg_telemetry/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf import settings
from django.db import models
from psqlextra.models import PostgresPartitionedModel
from psqlextra.types import PostgresPartitioningMethod


class BaseModel(models.Model):
class BaseModel(PostgresPartitionedModel):
"""
Base model for timeseries metrics. It provides a timestamp field which
represents the time that the data sample was captured at and a few metadata
Expand All @@ -13,6 +15,10 @@ class BaseModel(models.Model):
Timescale for a time, we'll pick one.
"""

class PartitioningMeta:
method = PostgresPartitioningMethod.RANGE
key = ["timestamp"]

class Meta:
abstract = True

Expand Down

0 comments on commit d290285

Please sign in to comment.