syncdb
so that you can add partitioning commands. pip install git+https://github.com/podshumok/django-post-syncdb-hooks
INSTALLED APPS = ( # ... 'post_syncdb_hooks', 'post_syncdb_hooks.partitioning', 'your.app', # ... )
yourapp/models.py
: from django.db import models class MyModel(models.Model): date_created = models.DateTimeField() my_data = models.IntegerField(db_index=True)
yourapp/sql/post_syncdb-hook.postgresql_psycopg2.sql
(you can edit the intervals for the needs): SELECT month_partition_creation( date_trunc('MONTH', NOW())::date, date_trunc('MONTH', NOW() + INTERVAL '1 year' )::date, 'yourapp_mymodel', 'date_created');
syncdb
: ./manage.py syncdb
... and sections are created.syncdb
, then there will be no indexes in the created sections. To fix this, you need to run syncdb
again.INSERT INTO
queries for PostgreSQL, adding RETURNING...
to them to get the id
inserted record. And the partitioning method we use does not support this feature.RETURNING
anywhere: from django.db import connections, DEFAULT_DB_ALIAS connections[DEFAULT_DB_ALIAS].features.can_return_id_from_insert = False
RETURNING
not used only with it: from post_syncdb_hooks.partitioning import to_partition class MyModel(models.Model): "..." #... @to_partition def save(self, *args, **kwargs): return super(MyModel, self).save(*args, **kwargs)
QuerySet
's by the field for which partitioning is implemented (in the example, date_created
): qs = MyModel.objects.filter(date_created__lte=..., date_created__gt=...)
count()
all the records: for example, the paginator
in the admin paginator
likes to do it.post_syncdb_hooks
post_syncdb_hooks
contains management.py
, which connects the receiver to the post_syncdb
signal. This receiver or hook is called for all installed applications. It scans whether the sql
folder is next to the models.py
file of an application, and if it is, whether there are post_syncdb-hook.sql
or post_syncdb-hook.(backend).sql
that can be run.post_syncdb_hooks.partitioning
contains exactly this SQL file. In it, we create a SQL function that takes four parameters: the start date, the end date, the table name, and the field name. When the function is called, sections for the specified field are created for the specified table, starting from the start date to the end date in monthly steps, as well as a trigger for INSERT INTO
and section-table indexes (if the indices are specified for the main table).psql
has the -E
option, which causes it to output SQL generated by internal commands. So that, sudo -u postgres psql -E db
db=# \di ********* QUERY ********** SELECT n.nspname as "Schema", c.relname as "Name", CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type", pg_catalog.pg_get_userbyid(c.relowner) as "Owner", c2.relname as "Table" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid WHERE c.relkind IN ('i','') AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2; ************************** db=#
Source: https://habr.com/ru/post/179873/
All Articles