SlideShare a Scribd company logo
With great data(bases) comes
great responsibility
Large table
partitioning with
PostgreSQL and
Django
Paolo Romolini, Caterina Magini
8 December 2020 - Postgres Build 2020
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.3
• Physicist and major science and technology enthusiast
• In 2ndQuadrant from 2018 to 2020 as DBA and Support Engineer
• Now in EDB
About us
• Web developer
• Python and Django enthusiast
• Customer Portal developer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.4
Summary
• Our use case
• The problem
• Declarative partitioning as possible solution
• The adopted solution
Our use case
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.6
• Facilitate support engineers and
customers job
• Independent management of
companies and users
• Software and repositories
management
• Knowledge base
• Support Ticketing System
The Customer Portal
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.7
• Open source
• Dry principle
• MVT logic
• Excellent documentation
• Django REST Framework
Django
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.8
• Django.contrib.postgres
• JsonB and Array Fields
• Full Text Search
• PostgreSQL specific model indexes
• The Django Project uses it
• https://github.com/django/djangoproject.com
PostgreSQL and Django
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.9
Triggered by ticket events
• Pushover notifications
• Mattermost chats
• Emails
The notifications system
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.10
class EmailMessage(models.Model):
subject = models.CharField(max_length=1024)
to_recipients = ArrayField(models.CharField(max_length=256))
message = models.TextField()
from_email = models.CharField(max_length=256)
metadata = models.JSONField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True, null=True)
[...]
The Email Messages app model
The problem
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.12
• The table was growing too much
• It needed maintenance
Working in DevOps helps!
The Database Table is too large!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.13
• When an application gets deployed to production early on the performance is great
• After a while, the database starts to get bigger and queries slow down...
• ...and at a certain point, you realize the data is the bottleneck
Table partitioning is a good solution
The DBA’s opinion
A typical scenario
Declarative
partitioning as
possible
solution
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.15
Declarative partitioning in PostgreSQL
Horizontal partitioning: technique for the vertical scalability of a database
Before PostgreSQL 10 From PostgreSQL 10
• Early “partitioning” introduced in
PostgreSQL 8.1
• Heavily based on relation inheritance
• Constraints to define the partitions,
rules and trigger to route data
• Required a lot of maintenance
• Declarative partitioning
• Simplified syntax
• Partitioned tables = new type of
relationship
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.16
The partitioning evolution in PostgreSQL
<=PG 9.6 PG 10 PG 11 PG 12 PG 13
Table inheritance Declarative table
partitioning syntax
Hash partition Partition pruning
enhancements
Partition wise join
enhancements
CHECK constraints Support indexes on
partitions tables
Foreign Keys to
reference
partitioned tables
Partition pruning
improvements
Triggers FOR EACH ROW
Trigger for
partitioned tables
Faster COPY for
partitioned tables
Before ROW-LEVEL
trigger
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.17
● Divide et impera
○ Smaller tables, smaller indexes
● Easier maintenance
● Healthier delete operations and bulk loads
○ Remove a table is faster than removing a lot of records
● Overall performance gains
○ Impact of seqscans
Why partitioning?
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.18
• RANGE
• LIST
• HASH
Partitioning can also be multi-level
Partitioning strategies
Supported partitioning methods:
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.19
Multi-level partitioning
Courtesy of Muhammad Haroon (https://www.2ndquadrant.com/en/blog/scaling-iot-time-series-data-postgres-bdr/)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.20
CREATE TABLE email_messages (
id serial NOT NULL,
subject varchar(1024) NOT NULL,
message text NOT NULL,
from_email varchar(256) NOT NULL,
html_message text NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NULL,
...
send_to_multiple_recipients boolean NULL,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);
How does it work?
Partitioned table
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.21
CREATE TABLE email_messages_2020_11 PARTITION OF email_messages
FOR VALUES FROM (‘2020-11-01 01:00:00+01’) TO (‘2020-12-01 01:00:00+01’)
CREATE TABLE email_messages_2020_11 PARTITION OF email_messages
FOR VALUES FROM (‘2020-11-01 01:00:00+01’) TO (‘2020-12-01 01:00:00+01’)
How does it work?
Partitions
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.22
The list of columns or expressions that define the partitions bound:
• Range
Can have more > 1 column
• Hash
• List Can have only 1 column/expression
The partitioning key
PARTITION BY {RANGE | LIST | HASH}
({ column_name | (expression )} [ COLLATE collation ] [ opclass ] [,...])
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.23
Indexes, Primary Keys and Foreign Keys support
● CREATE INDEX on partitioned tables locally-partitioned indexes
● Support for UNIQUE and PRIMARY KEY constraints
○ Partition key has to be part of the PRIMARY KEY
● FOREIGN KEYs
○ Partitioned tables can REFERENCE other tables
○ Partitioned tables can be REFERENCED by other tables
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.24
Testing the procedure in QA
Once we understood that partitioning could be the solution, we just had to test it.
• QA environment with PostgreSQL 10
• Large table referenced by another table
• Can’t turn a not partitioned in table into a partitioned one → New table
• Most of the data in the old table are historical, no need to migrate them immediately
• LOCK the old table during the deploy
• Simulate traffic… and check that everything is OK
Needs to upgrade to PG 12
The adopted
solution
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.26
No support for table partitioning in Django
• Third-party django applications?
• Executing SQL query without Django?
Which way to take?
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.27
• PostgreSQL declarative partitioning is transparent to Django
• Doing it using Django tools
Django migrations!
The way we have chosen
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.28
• A way to track django models changes
• They are applied against the database
• You can revert them
Django migrations
$ python manage.py showmigrations email_messages
email_messages
[X] 0001_initial
[X] 0002_auto_20200716_0837
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.29
Specify a different table name
class EmailMessage(models.Model):
[...]
created_at = models.DateTimeField(auto_now_add=True, null=True)
class Meta:
db_table = “email_messages”
Django default database table names:
"%s_%s" % (self.app_label, self.model_name)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.30
Django migrations with custom SQL
class Migration(migrations.Migration):
[...]
operations = [
migrations.RunSQL(
sql=[(PARTITIONED_TABLES_SQL],
reverse_sql=[DROP_PARTITONED_TABLES_SQL],
)
]
Run the sqlmigrate command
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.31
The partition key of the Django migration
PARTITIONED_TABLES_SQL = """
CREATE TABLE "email_messages" (
"id" serial NOT NULL PRIMARY KEY,
[...]
) PARTITION BY RANGE
(created_at);
"""
PARTITIONED_TABLES_SQL = """
CREATE TABLE "email_messages" (
"id" serial NOT NULL,
[...]
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE
(created_at);
"""
→
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.32
Create the partitions with Django
• Create a new django management command
• Schedule the command as a cron job
• Add another django migration
We are ready to test it in QA!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.33
Deployment in production
• The deployment ran smoothly
• The old table was attached to the new partition
Success!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.34
Conclusion
• The declarative PostgreSQL partitions is good
• Django is a great framework
• DevOps
• That’s one way to do it, not the way!
Follow us:
https://www.linkedin.com/in/paoloromolini/
https://www.linkedin.com/in/caterina-magini/
Tweet:
#PostgresBuild2020
#PostgreSQL
#Django

More Related Content

PDF
PostgreSQL Tutorial For Beginners | Edureka
PDF
PostgreSQL Performance Tuning
PPTX
Getting started with postgresql
PDF
PostgreSQL WAL for DBAs
PDF
Presto anatomy
PDF
Understanding InfluxDB Basics: Tags, Fields and Measurements
PDF
Practical Partitioning in Production with Postgres
 
PDF
The InnoDB Storage Engine for MySQL
PostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Performance Tuning
Getting started with postgresql
PostgreSQL WAL for DBAs
Presto anatomy
Understanding InfluxDB Basics: Tags, Fields and Measurements
Practical Partitioning in Production with Postgres
 
The InnoDB Storage Engine for MySQL

What's hot (20)

PDF
Apache Helix DevOps & LSPE-IN Meetup
PDF
Side by Side with Elasticsearch & Solr, Part 2
PPTX
PostgreSQL- An Introduction
PDF
PostgreSQL Deep Internal
PDF
Linux tuning to improve PostgreSQL performance
PPTX
서비스 모니터링 구현 사례 공유 - Realtime log monitoring platform-PMon을 ...
PPT
Database performance tuning and query optimization
PPTX
Introduction to PostgreSQL
PDF
Postgresql database administration volume 1
PDF
PostgreSQL HA
PDF
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
PDF
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
PPTX
Change Data Capture to Data Lakes Using Apache Pulsar and Apache Hudi - Pulsa...
PDF
[Pgday.Seoul 2017] 2. PostgreSQL을 위한 리눅스 커널 최적화 - 김상욱
PDF
A Deep Dive into Query Execution Engine of Spark SQL
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PPTX
Presto query optimizer: pursuit of performance
PDF
[1A7]Ansible의이해와활용
PDF
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
PDF
Indexes in postgres
Apache Helix DevOps & LSPE-IN Meetup
Side by Side with Elasticsearch & Solr, Part 2
PostgreSQL- An Introduction
PostgreSQL Deep Internal
Linux tuning to improve PostgreSQL performance
서비스 모니터링 구현 사례 공유 - Realtime log monitoring platform-PMon을 ...
Database performance tuning and query optimization
Introduction to PostgreSQL
Postgresql database administration volume 1
PostgreSQL HA
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Change Data Capture to Data Lakes Using Apache Pulsar and Apache Hudi - Pulsa...
[Pgday.Seoul 2017] 2. PostgreSQL을 위한 리눅스 커널 최적화 - 김상욱
A Deep Dive into Query Execution Engine of Spark SQL
MySQL Indexing - Best practices for MySQL 5.6
Presto query optimizer: pursuit of performance
[1A7]Ansible의이해와활용
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
Indexes in postgres
Ad

Similar to Large Table Partitioning with PostgreSQL and Django (20)

PPTX
New and Improved Features in PostgreSQL 13
 
PDF
PostgreSQL 13 is Coming - Find Out What's New!
 
PDF
Practical Partitioning in Production with Postgres
PDF
EDB & ELOS Technologies - Break Free from Oracle
 
PPTX
PostgreSQL as a Strategic Tool
 
PPTX
EDB: Power to Postgres
PPTX
Enterprise-class security with PostgreSQL - 2
PPTX
Szabaduljon ki az Oracle szorításából
 
PPTX
Managing Data in Jakarta EE Applications
PPTX
Oracle to Postgres Schema Migration Hustle
 
PPTX
Break Free from Oracle
 
PPTX
Expert Guide to Migrating Legacy Databases to Postgres
 
PDF
Google BigQuery
PPTX
Un guide complet pour la migration de bases de données héritées vers PostgreSQL
 
PPTX
Replacing Oracle with EDB Postgres
 
PPTX
An Expert Guide to Migrating Legacy Databases to PostgreSQL
 
PDF
EDB 13 - New Enhancements for Security and Usability - APJ
 
PPTX
New enhancements for security and usability in EDB 13
 
PPTX
PostgreSQL as a Strategic Tool
 
PPTX
DBT ELT approach for Advanced Analytics.pptx
New and Improved Features in PostgreSQL 13
 
PostgreSQL 13 is Coming - Find Out What's New!
 
Practical Partitioning in Production with Postgres
EDB & ELOS Technologies - Break Free from Oracle
 
PostgreSQL as a Strategic Tool
 
EDB: Power to Postgres
Enterprise-class security with PostgreSQL - 2
Szabaduljon ki az Oracle szorításából
 
Managing Data in Jakarta EE Applications
Oracle to Postgres Schema Migration Hustle
 
Break Free from Oracle
 
Expert Guide to Migrating Legacy Databases to Postgres
 
Google BigQuery
Un guide complet pour la migration de bases de données héritées vers PostgreSQL
 
Replacing Oracle with EDB Postgres
 
An Expert Guide to Migrating Legacy Databases to PostgreSQL
 
EDB 13 - New Enhancements for Security and Usability - APJ
 
New enhancements for security and usability in EDB 13
 
PostgreSQL as a Strategic Tool
 
DBT ELT approach for Advanced Analytics.pptx
Ad

More from EDB (20)

PDF
Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
 
PDF
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
 
PDF
Migre sus bases de datos Oracle a la nube
 
PDF
EFM Office Hours - APJ - July 29, 2021
 
PDF
Benchmarking Cloud Native PostgreSQL
 
PDF
Las Variaciones de la Replicación de PostgreSQL
 
PDF
NoSQL and Spatial Database Capabilities using PostgreSQL
 
PDF
Is There Anything PgBouncer Can’t Do?
 
PDF
Data Analysis with TensorFlow in PostgreSQL
 
PDF
A Deeper Dive into EXPLAIN
 
PDF
IOT with PostgreSQL
 
PDF
A Journey from Oracle to PostgreSQL
 
PDF
Psql is awesome!
 
PPTX
Comment sauvegarder correctement vos données
 
PDF
Cloud Native PostgreSQL - Italiano
 
PDF
New enhancements for security and usability in EDB 13
 
PPTX
Best Practices in Security with PostgreSQL
 
PDF
Cloud Native PostgreSQL - APJ
 
PDF
Best Practices in Security with PostgreSQL
 
PDF
EDB Postgres & Tools in a Smart City Project
 
Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
 
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
 
Migre sus bases de datos Oracle a la nube
 
EFM Office Hours - APJ - July 29, 2021
 
Benchmarking Cloud Native PostgreSQL
 
Las Variaciones de la Replicación de PostgreSQL
 
NoSQL and Spatial Database Capabilities using PostgreSQL
 
Is There Anything PgBouncer Can’t Do?
 
Data Analysis with TensorFlow in PostgreSQL
 
A Deeper Dive into EXPLAIN
 
IOT with PostgreSQL
 
A Journey from Oracle to PostgreSQL
 
Psql is awesome!
 
Comment sauvegarder correctement vos données
 
Cloud Native PostgreSQL - Italiano
 
New enhancements for security and usability in EDB 13
 
Best Practices in Security with PostgreSQL
 
Cloud Native PostgreSQL - APJ
 
Best Practices in Security with PostgreSQL
 
EDB Postgres & Tools in a Smart City Project
 

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Mobile App Security Testing_ A Comprehensive Guide.pdf
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
The Rise and Fall of 3GPP – Time for a Sabbatical?

Large Table Partitioning with PostgreSQL and Django

  • 1. With great data(bases) comes great responsibility
  • 2. Large table partitioning with PostgreSQL and Django Paolo Romolini, Caterina Magini 8 December 2020 - Postgres Build 2020
  • 3. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.3 • Physicist and major science and technology enthusiast • In 2ndQuadrant from 2018 to 2020 as DBA and Support Engineer • Now in EDB About us • Web developer • Python and Django enthusiast • Customer Portal developer
  • 4. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.4 Summary • Our use case • The problem • Declarative partitioning as possible solution • The adopted solution
  • 6. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.6 • Facilitate support engineers and customers job • Independent management of companies and users • Software and repositories management • Knowledge base • Support Ticketing System The Customer Portal
  • 7. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.7 • Open source • Dry principle • MVT logic • Excellent documentation • Django REST Framework Django
  • 8. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.8 • Django.contrib.postgres • JsonB and Array Fields • Full Text Search • PostgreSQL specific model indexes • The Django Project uses it • https://github.com/django/djangoproject.com PostgreSQL and Django
  • 9. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.9 Triggered by ticket events • Pushover notifications • Mattermost chats • Emails The notifications system
  • 10. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.10 class EmailMessage(models.Model): subject = models.CharField(max_length=1024) to_recipients = ArrayField(models.CharField(max_length=256)) message = models.TextField() from_email = models.CharField(max_length=256) metadata = models.JSONField(null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True, null=True) [...] The Email Messages app model
  • 12. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.12 • The table was growing too much • It needed maintenance Working in DevOps helps! The Database Table is too large!
  • 13. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.13 • When an application gets deployed to production early on the performance is great • After a while, the database starts to get bigger and queries slow down... • ...and at a certain point, you realize the data is the bottleneck Table partitioning is a good solution The DBA’s opinion A typical scenario
  • 15. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.15 Declarative partitioning in PostgreSQL Horizontal partitioning: technique for the vertical scalability of a database Before PostgreSQL 10 From PostgreSQL 10 • Early “partitioning” introduced in PostgreSQL 8.1 • Heavily based on relation inheritance • Constraints to define the partitions, rules and trigger to route data • Required a lot of maintenance • Declarative partitioning • Simplified syntax • Partitioned tables = new type of relationship
  • 16. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.16 The partitioning evolution in PostgreSQL <=PG 9.6 PG 10 PG 11 PG 12 PG 13 Table inheritance Declarative table partitioning syntax Hash partition Partition pruning enhancements Partition wise join enhancements CHECK constraints Support indexes on partitions tables Foreign Keys to reference partitioned tables Partition pruning improvements Triggers FOR EACH ROW Trigger for partitioned tables Faster COPY for partitioned tables Before ROW-LEVEL trigger
  • 17. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.17 ● Divide et impera ○ Smaller tables, smaller indexes ● Easier maintenance ● Healthier delete operations and bulk loads ○ Remove a table is faster than removing a lot of records ● Overall performance gains ○ Impact of seqscans Why partitioning?
  • 18. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.18 • RANGE • LIST • HASH Partitioning can also be multi-level Partitioning strategies Supported partitioning methods:
  • 19. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.19 Multi-level partitioning Courtesy of Muhammad Haroon (https://www.2ndquadrant.com/en/blog/scaling-iot-time-series-data-postgres-bdr/)
  • 20. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.20 CREATE TABLE email_messages ( id serial NOT NULL, subject varchar(1024) NOT NULL, message text NOT NULL, from_email varchar(256) NOT NULL, html_message text NULL, created_at timestamp with time zone NOT NULL, updated_at timestamp with time zone NULL, ... send_to_multiple_recipients boolean NULL, PRIMARY KEY (id, created_at) ) PARTITION BY RANGE (created_at); How does it work? Partitioned table
  • 21. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.21 CREATE TABLE email_messages_2020_11 PARTITION OF email_messages FOR VALUES FROM (‘2020-11-01 01:00:00+01’) TO (‘2020-12-01 01:00:00+01’) CREATE TABLE email_messages_2020_11 PARTITION OF email_messages FOR VALUES FROM (‘2020-11-01 01:00:00+01’) TO (‘2020-12-01 01:00:00+01’) How does it work? Partitions
  • 22. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.22 The list of columns or expressions that define the partitions bound: • Range Can have more > 1 column • Hash • List Can have only 1 column/expression The partitioning key PARTITION BY {RANGE | LIST | HASH} ({ column_name | (expression )} [ COLLATE collation ] [ opclass ] [,...])
  • 23. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.23 Indexes, Primary Keys and Foreign Keys support ● CREATE INDEX on partitioned tables locally-partitioned indexes ● Support for UNIQUE and PRIMARY KEY constraints ○ Partition key has to be part of the PRIMARY KEY ● FOREIGN KEYs ○ Partitioned tables can REFERENCE other tables ○ Partitioned tables can be REFERENCED by other tables
  • 24. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.24 Testing the procedure in QA Once we understood that partitioning could be the solution, we just had to test it. • QA environment with PostgreSQL 10 • Large table referenced by another table • Can’t turn a not partitioned in table into a partitioned one → New table • Most of the data in the old table are historical, no need to migrate them immediately • LOCK the old table during the deploy • Simulate traffic… and check that everything is OK Needs to upgrade to PG 12
  • 26. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.26 No support for table partitioning in Django • Third-party django applications? • Executing SQL query without Django? Which way to take?
  • 27. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.27 • PostgreSQL declarative partitioning is transparent to Django • Doing it using Django tools Django migrations! The way we have chosen
  • 28. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.28 • A way to track django models changes • They are applied against the database • You can revert them Django migrations $ python manage.py showmigrations email_messages email_messages [X] 0001_initial [X] 0002_auto_20200716_0837
  • 29. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.29 Specify a different table name class EmailMessage(models.Model): [...] created_at = models.DateTimeField(auto_now_add=True, null=True) class Meta: db_table = “email_messages” Django default database table names: "%s_%s" % (self.app_label, self.model_name)
  • 30. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.30 Django migrations with custom SQL class Migration(migrations.Migration): [...] operations = [ migrations.RunSQL( sql=[(PARTITIONED_TABLES_SQL], reverse_sql=[DROP_PARTITONED_TABLES_SQL], ) ] Run the sqlmigrate command
  • 31. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.31 The partition key of the Django migration PARTITIONED_TABLES_SQL = """ CREATE TABLE "email_messages" ( "id" serial NOT NULL PRIMARY KEY, [...] ) PARTITION BY RANGE (created_at); """ PARTITIONED_TABLES_SQL = """ CREATE TABLE "email_messages" ( "id" serial NOT NULL, [...] PRIMARY KEY (id, created_at) ) PARTITION BY RANGE (created_at); """ →
  • 32. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.32 Create the partitions with Django • Create a new django management command • Schedule the command as a cron job • Add another django migration We are ready to test it in QA!
  • 33. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.33 Deployment in production • The deployment ran smoothly • The old table was attached to the new partition Success!
  • 34. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.34 Conclusion • The declarative PostgreSQL partitions is good • Django is a great framework • DevOps • That’s one way to do it, not the way!