Skip to content

Commit d290285

Browse files
committed
add partitioning to pg_telemetry SimpleMetric model
1 parent bf76a73 commit d290285

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

shared/django_apps/db_settings.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@
161161
POSTGRES_EXTRA_DB_BACKEND_BASE: "django_prometheus.db.backends.postgresql" # type: ignore
162162

163163
# Allows to use the pgpartition command
164-
PSQLEXTRA_PARTITIONING_MANAGER = (
165-
"shared.django_apps.user_measurements.partitioning.manager"
166-
)
164+
PSQLEXTRA_PARTITIONING_MANAGER = "shared.django_apps.partitioning.manager"
167165

168166
DATABASE_ROUTERS = [
169167
"shared.django_apps.db_routers.MultiDatabaseRouter",

shared/django_apps/user_measurements/partitioning.py shared/django_apps/partitioning.py

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77
from psqlextra.partitioning.config import PostgresPartitioningConfig
88

9+
from shared.django_apps.pg_telemetry.models import SimpleMetric
910
from shared.django_apps.user_measurements.models import UserMeasurement
1011

1112
# Overlapping partitions will cause errors - https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE -> "create partitions"
@@ -23,5 +24,13 @@
2324
max_age=relativedelta(months=12),
2425
),
2526
),
27+
PostgresPartitioningConfig(
28+
model=SimpleMetric,
29+
strategy=PostgresCurrentTimePartitioningStrategy(
30+
size=PostgresTimePartitionSize(months=1),
31+
count=3,
32+
max_age=relativedelta(months=6),
33+
),
34+
),
2635
]
2736
)

shared/django_apps/pg_telemetry/models.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django.conf import settings
22
from django.db import models
3+
from psqlextra.models import PostgresPartitionedModel
4+
from psqlextra.types import PostgresPartitioningMethod
35

46

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

18+
class PartitioningMeta:
19+
method = PostgresPartitioningMethod.RANGE
20+
key = ["timestamp"]
21+
1622
class Meta:
1723
abstract = True
1824

0 commit comments

Comments
 (0)