'Django Postgres table partitioning not working with 'pgpartition' command
I have a huge "Logs" postgres table being used extensively in my Django project. Logs table has more than 20 million records and its slowing down the queries and page load time is also increased. I am using below Django and Postgres versions:- Django: 4.0 Postgres: 13
I read about table partitioning and decided to use "django-postgres-extra" as I don't want to manage migration files.
I followed all the steps mentioned in below link but I am not able to create partitioned tables using "pgpartition" command. https://django-postgres-extra.readthedocs.io/en/master/table_partitioning.html
Am I missing something here? models.py changes:-
from django.db import models
from psqlextra.types import PostgresPartitioningMethod
from psqlextra.models import PostgresPartitionedModel
from psqlextra.manager import PostgresManager
class Logs(PostgresPartitionedModel):
class PartitioningMeta:
method = PostgresPartitioningMethod.RANGE
key = ["ctime"]
objects = PostgresManager()
ctime = models.DateTimeField(auto_now_add=True)
logname = models.CharField(max_length=20)
builds = models.ForeignKey(Build)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.postgres',
'psqlextra',
'logs',
]
DATABASES = {
'default': {
'ENGINE': 'psqlextra.backend',
'NAME': 'logdb',
'USER': 'root',
'PASSWORD': 'crypt',
'HOST': 'localhost',
'PORT': 5432,
}
}
PSQLEXTRA_PARTITIONING_MANAGER = 'logs.partition.manager'
Created a file named 'partition' under logs app and defined a manager object as per doc link above. partition.py
from dateutil.relativedelta import relativedelta
from logs.models import Logs
from psqlextra.partitioning import (
PostgresPartitioningManager,
PostgresCurrentTimePartitioningStrategy,
PostgresTimePartitionSize,
partition_by_current_time,
)
from psqlextra.partitioning.config import PostgresPartitioningConfig
manager = PostgresPartitioningManager([
# 3 partitions ahead, each partition is one month
# delete partitions older than 6 months
# partitions will be named `[table_name]_[year]_[3-letter month name]`.
PostgresPartitioningConfig(
model=Logs,
strategy=PostgresCurrentTimePartitioningStrategy(
size=PostgresTimePartitionSize(months=1),
count=1,
max_age=relativedelta(months=1),
),
),
])
When I ran "python manage.py pgmakemigrations" command it created a migration file as below:- 0002_alter_log_managers.py
# Generated by Django 4.0.3 on 2022-05-19 12:22
from django.db import migrations
import psqlextra.manager.manager
class Migration(migrations.Migration):
dependencies = [
('logs', '0001_initial'),
]
operations = [
migrations.AlterModelManagers(
name='logs',
managers=[
('objects', psqlextra.manager.manager.PostgresManager()),
],
),
]
But as soon as I run "pgpartition" command I get below error:-
python manage.py pgpartition
Traceback (most recent call last):
File "/home/user/party/manage.py", line 22, in <module>
main()
File "/home/user/party/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/user/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/user/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/venv/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/venv/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
output = self.handle(*args, **options)
File "/home/user/venv/lib/python3.10/site-packages/psqlextra/management/commands/pgpartition.py", line 72, in handle
plan = partitioning_manager.plan(
File "/home/user/venv/lib/python3.10/site-packages/psqlextra/partitioning/manager.py", line 52, in plan
model_plan = self._plan_for_config(
File "/home/user/venv/lib/python3.10/site-packages/psqlextra/partitioning/manager.py", line 84, in _plan_for_config
table = self._get_partitioned_table(connection, config.model)
File "/home/user/venv/lib/python3.10/site-packages/psqlextra/partitioning/manager.py", line 121, in _get_partitioned_table
raise PostgresPartitioningError(
psqlextra.partitioning.error.PostgresPartitioningError: Model Logs, with table logs_logs does not exists in the database. Did you run `python manage.py migrate`?
Not sure what I am missing here...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
