SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
Adventures on live
partitioning
Matteo MelliDEV
Working @ OnGres (www.ongres.com)
Software Developer
PostgreSQL support
@teoincontatto
teoincontatto
PGCONF.EU 2017
About ONGRES
➔
IT firm specialized on R&D on Databases, more
specifically PostgreSQL:

Training

Consulting and development

PostgreSQL Support
➔
Developers of ToroDB (www.torodb.com), an open-
source, document-store database that works on top of
PostgreSQL and is compatible with MongoDB.
➔
Partners of www.pythian.com, reference multinational
company providing database support and data
services.
PGCONF.EU 2017
Our customer
Applications
do simple
queries
Applications
do simple
queries
Bach
processing
that add
and modify
data
Bach
processing
that add
and modify
data
Application SaaS
2K Users2K Users
PGCONF.EU 2017
active
master
hot-
standby
System characteristics
passive
master
Availability Zone A
AWS Region
AWS RDS
BatchBatch ApplicationApplication
Availability Zone B
PGCONF.EU 2017
System characteristics
➔
PostgreSQL 9.6
➔
Amazon RDS db.r3.xlarge
➔
200MB/s throughput (max 1.2GB/s)
➔
HA with Multi-AZ active/passive
➔
Hot-standby replica
PGCONF.EU 2017
Big table problem
✓ ~100GB size (50% indexes) table
✓ ~1000M rows table
✓ Table growth due to batch process (from
few MB to some GB per night)
✓ Queries slow down (up to x10 slower)
✓ CUD slow down (up to x100 slower)
PGCONF.EU 2017
Big table problem
PGCONF.EU 2017
The requirements
✓ Removing indexes not an option
1
✓ Partitioning is the way to go
2
✓ 2 days of outage at maximum
3
PGCONF.EU 2017
big_table
Type of partitioning
➔
Using table INHERITANCE
➔
Cloning PK and FKs
➔
Range checks on integer column part_id
➔
Partition key on a 2 JOINs far away
table
✔
If fk_mid_id is NULL or fk_part_id is NULL
row is logically deleted
big_id
fk_mid_id
mid_table
mid_id
fk_part_id
part_table
part_id
PGCONF.EU 2017
The naive approach
✓ 2 days maintenance window (will be enough!)
✓ big_table empty copy plus partition key
✓ Homogeneous ranges of partition key
✓ Copy directly to partitions from ad-hoc view
11
22
33
44
PGCONF.EU 2017
The naive approach
CREATE TABLE big_table_part
(fk_part_id integer)
INHERITS (big_table);
ALTER TABLE big_table_part
NO INHERIT big_table;
big_table_part
big_id
fk_mid_id
fk_part_id
PGCONF.EU 2017
The naive approach
CREATE TABLE big_table_part_info
SELECT 'big_table_' || n AS name,
n AS start, n + 200 AS next
FROM generate_series(0, 1800, 200)
AS n
name
big_part_1
start
0
next
200
big_part_2 200 400
... ... ...
big_part_10 1800 2000
big_table_part
_info
name
start
next
PGCONF.EU 2017
The naive approach
DO $$DECLARE name, start, next
BEGIN
FOR name, start, next
IN SELECT name, start, next
FROM big_table_part_info LOOP
EXECUTE
'CREATE TABLE ' || name || '('
|| ' CHECK part_id >= ' || start
|| ' AND part_id < ' || next || ')'
|| ' INHERITS (big_table_part)’;
EXECUTE
'CREATE INDEX ' || name || '_pk_idx'
|| ' ON ' || name || '(big_id)’;
…
END LOOP;
END$$
big_table_2big_table_1 big_table_n…
big_table_2
_pk_idx
big_table_1
_pk_idx
big_table_n
_pk_idx
…
………
PGCONF.EU 2017
The naive approach
big_table_view
SELECT p.part_id, b.*
FROM big_table AS b
JOIN mid_table ON (…)
JOIN part_key_table AS p ON (...)
big_table
big_id
fk_mid_id
mid_table
mid_id
fk_part_id
part_table
part_id
PGCONF.EU 2017
The naive approach
big_table_view
DO $$DECLARE name, start, next
BEGIN
FOR name, start, next
IN SELECT name, start, next
FROM big_table_part_info LOOP
EXECUTE
'INSERT INTO ' || name
|| ' SELECT *’
|| ' FROM big_table_view’
|| ' WHERE part_id >= ' || start
|| ' AND part_id < ' || next
|| ' FOR UPDATE’
END LOOP
END$$
big_table
big_id
fk_mid_id
mid_table
mid_id
fk_part_id
part_table
part_id
big_table_2big_table_1 big_table_n…
PGCONF.EU 2017
The naive approach
✓ 2 days maintenance window (will be enough!)
✓ big_table empty copy plus partition key
✓ Homogeneous ranges of partition key
✓ Copy directly to partitions from ad-hoc view
11
22
33
44
WRONG! It takes too long, full scan 3 tables repeated per partition (~8 hour each)
PGCONF.EU 2017
The tuned approach
✓ 2 days maintenance window (will be enough!)
✓ Copy of big_table from ad-hoc view
✓ Homogeneous ranges of partition key
✓ Copy directly to partitions from ad-hoc view
11
33
44
55
✓ big_table empty copy plus partition key 22
PGCONF.EU 2017
The tuned approach
big_table_view
CREATE TABLE big_table_copy
(fk_part_id integer)
INHERITS (big_table);
ALTER TABLE big_table_copy
NO INHERIT big_table;
INSERT INTO big_table_copy
SELECT * FROM big_table_view;
big_table
big_id
fk_mid_id
mid_table
mid_id
fk_part_id
part_table
part_id
big_table_copy
big_id
fk_mid_id
fk_part_id
PGCONF.EU 2017
The tuned approach
DO $$DECLARE name, start, next
BEGIN
FOR name, start, next
IN SELECT name, start, next
FROM big_table_part_info LOOP
EXECUTE
'INSERT INTO ' || name
|| ' SELECT *’
|| ' FROM big_table_copy'
|| ' WHERE part_id >= ' || start
|| ' AND part_id < ' || next
|| ' FOR UPDATE’
END LOOP
END$$
big_table_copy
big_id
fk_mid_id
fk_part_id
big_table_2big_table_1 big_table_n…
PGCONF.EU 2017
The tuned approach
WRONG! Homogeneous range <> Homogeneous data distribution
✓ 2 days maintenance window (will be enough!)
✓ Copy of big_table from ad-hoc view
✓ Homogeneous ranges of partition key
✓ Copy directly to partitions from ad-hoc view
11
33
44
55
✓ big_table empty copy plus partition key 22
PGCONF.EU 2017
The tuned approach
Homogeneous range partitions
1
2
3
4
5
6
7
8
9
10
0 20 40 60 80 100 120 140 160 180
Range distribution
Homogenerous range paritions
Millions of rows
Partition#
PGCONF.EU 2017
The smart approach
✓ 2 days maintenance window (will be enough!)
✓ Copy directly to partitions from ad-hoc view
✓ big_table empty copy plus partition key
✓ Count group of rows 11
22
33
44
55
66
✓ Partition by ranges based on count of partition key
✓ Copy of big_table plus partition key
PGCONF.EU 2017
The smart approach
Count based partitions
Homogeneous range partitions
1
2
3
4
5
6
7
8
9
10
11
12
13
0 20 40 60 80 100 120 140 160 180
Range distribution
Homogenerous range paritions
Count based paritions
Millions of rows
Partition#
PGCONF.EU 2017
The smart approach
CREATE TABLE big_table_part_info AS
SELECT 'big_table_1' AS name,
0 AS start, 100 AS next
UNION ALL
SELECT 'big_table_2' AS name,
100 AS start, 200 AS next
UNION ALL
…
UNION ALL
SELECT 'big_table_10' AS name,
1750 AS start, 1900 AS nextname
big_part_1
start
0
next
100
big_part_2 100 200
... ... ...
big_part_10 1750 1900
big_table_part
_info
name
start
next
PGCONF.EU 2017
BINGO
PGCONF.EU 2017
The little problem
We forgot our replica!
Under heavy load, RDS replica seemed to stop replicating via network
and switch to WAL shipping, which was extremely slow and lag grew
to days!
PGCONF.EU 2017
Solving the little problem
Upgrade wal_keep_segments from 64 to
4096 so replica stay with SR
Upgrade wal_keep_segments from 64 to
4096 so replica stay with SR
Nice idea but replica still
get out of sync?!
Nice idea but replica still
get out of sync?!
PGCONF.EU 2017
Solving the little problem
Let’s create a new replica and remove
the old one then!
Let’s create a new replica and remove
the old one then!
But sometimes the
replica could take a day
to catch up.
But sometimes the
replica could take a day
to catch up.
PGCONF.EU 2017
Solving the little problem
How to make replica stay within SR?How to make replica stay within SR?
Copy data by chunks
monitoring replication lag
Copy data by chunks
monitoring replication lag
PGCONF.EU 2017
Solving the little problem
How to make it in a window of 2 days?How to make it in a window of 2 days?
Don’t do it in a window,
do it LIVE! lag
Don’t do it in a window,
do it LIVE! lag
PGCONF.EU 2017
Do it LIVE!
➔
Application are not aware
✔
Trick application to think it is using the real table! (INSTEAD OF
to the rescue)
➔
Shit happens
✔
Ability to pause/resume the process if needed
➔
Short enough resource usage duration
✔
Be polite, do not starve resources
✔
Also, don’t forget the little problem (lag monitoring)
➔
Preserve data consistency
✔
Obviously
PGCONF.EU 2017
The live approach
1. Count group rows
2. 8 hours maintenance window (will be enough!)
3. Indexes to help retrieve partition key faster
4. Empty copy of the table plus partition key
5. Partition by range based on count of partition key
6. Create helper table for the new inserted rows
7. Rename big_table and create a fake view with trigger to handle
changes
8. Copy data to the partitions by chunks of 1M rows (LIVE part)
PGCONF.EU 2017
The live approach - the fake view
Applications “thinks” they are using the real
big_table…
Applications “thinks” they are using the real
big_table…
...INSTEAD OF that, they’re
accessing a view that fakes the real
big_table. The view create an
abstraction that allow to read real
data and (with the help of a trigger)
to modify real data.
...INSTEAD OF that, they’re
accessing a view that fakes the real
big_table. The view create an
abstraction that allow to read real
data and (with the help of a trigger)
to modify real data.
PGCONF.EU 2017
The live approach - the fake view
big_table
(a view with an INSTEAD OF trigger)
SELECT * FROM big_table_old
UNION ALL
SELECT * FROM big_table_fow_new_records
BatchBatch ApplicationApplication
big_table_old
big_id
fk_mid_id
big_table_for
_new_records
big_id
fk_mid_id
PGCONF.EU 2017
The live approach - the fake view
big_table
(a view with an INSTEAD OF trigger)
ON INSERT
fk_part_id := (SELECT p.part_id
FROM mid_table AS m
JOIN part_key_table AS p ON (…)
WHERE mid_id = NEW.fk_mid_id)
big_table_old
big_id
fk_mid_id
big_table_for
_new_records
big_id
fk_mid_id
big_table_part
big_id
fk_mid_id
fk_part_id
big_table_<x>
PGCONF.EU 2017
The live approach - the fake view
big_table
(a view with an INSTEAD OF trigger)
ON UPDATE & ON DELETE
big_table_old
big_id
fk_mid_id
big_table_for
_new_records
big_id
fk_mid_id
big_table_part
big_id
fk_mid_id
fk_part_id
big_table_<x>
PGCONF.EU 2017
The live approach - the fake view
big_table
(a view with an INSTEAD OF trigger)
ON UPDATE & ON DELETE
WARNING
ON UPDATE we forbid change
big_id and fk_part_id,
or the whole process
could break!!
big_table_old
big_id
fk_mid_id
big_table_for
_new_records
big_id
fk_mid_id
big_table_part
big_id
fk_mid_id
fk_part_id
big_table_<x>
PGCONF.EU 2017
The live approach - copy a chunk of data
✓ Copy 1M rows to intermediate table
✓ Move from intermediate table to partitions
✓Two step approach then!
11
22
PGCONF.EU 2017
The live approach - copy a chunk of data
CREATE TABLE IF NOT EXISTS last_big_id(big_id bigint)
last_big_id
big_id
PGCONF.EU 2017
The live approach - copy a chunk of data
WITH big_table_chunk AS
(INSERT INTO big_table_part
SELECT b.*, p.part_id
FROM big_table_old AS b
JOIN mid_table ON (…)
JOIN part_key_table AS p ON (…)
WHERE big_id > (
SELECT coalesce(max(big_id),-1)
FROM last_big_id)
ORDER BY big_id LIMIT 1000000
FOR UPDATE RETURNING big_id)
INSERT INTO last_big_id SELECT max(big_id)
FROM big_table_chunk
big_table
big_id
fk_mid_id
mid_table
mid_id
fk_part_id
part_table
part_id
big_table_part
big_id
fk_mid_id
fk_part_id
PGCONF.EU 2017
The live approach - copy a chunk of data
DO $$DECLARE name, start, next
BEGIN
FOR name, start, next
IN SELECT name, start, next
FROM big_table_part_info LOOP
EXECUTE
'INSERT INTO ' || name
|| ' SELECT *'
|| ' FROM big_table_part'
|| ' WHERE part_id >= ' || start
|| ' AND part_id < ' || next
|| ' FOR UPDATE’
END LOOP
TRUNCATE ONLY big_table_part;
END$$
big_table_part
big_id
fk_mid_id
fk_part_id
big_table_2big_table_1 big_table_n…
PGCONF.EU 2017
The live approach - Some data!
PGCONF.EU 2017
Postgresql 10 partitioning
➔
A new way to create a partitions that simplify and remove
risk of errors when doing it manually (beware you still have to
create PKs, FKs and indexes manually)
➔
So, how to apply this live partitioning technique to postgresql
10?
CREATE TABLE big_table_1
PARTITION OF big_table_part
FOR VALUES FROM (1) TO (666);
PGCONF.EU 2017
Postgresql 10 partitioning
➔
Copy from view to parent table would fail since the parent table of
a partition is not a real table:
✔
We will need an intermediate table (big_table_inter) to hold the 1M rows
readed from big_table_old
✔
Also, when calculating last_big_id before copying a chunk we will have to
do a:
SELECT max(big_id) FROM (
SELECT max(big_id) AS bid_ig FROM
big_table_part
UNION ALL
SELECT max(big_id) AS big_id FROM
big_table_inter
) AS tmp
SELECT relname, relkind
FROM pg_class
WHERE relname
LIKE 'big_table_%';
relname relkind
big_table_part
big_table_1
P
r
PGCONF.EU 2017
Questions?

Más contenido relacionado

La actualidad más candente

Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in RRsquared Academy
 
Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...Dr. Volkan OBAN
 
Beyond php it's not (just) about the code
Beyond php   it's not (just) about the codeBeyond php   it's not (just) about the code
Beyond php it's not (just) about the codeWim Godden
 
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPlotly
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearchRyosuke Nakamura
 
Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersAaron Patterson
 

La actualidad más candente (7)

Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
 
Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...Produce nice outputs for graphical, tabular and textual reporting in R-Report...
Produce nice outputs for graphical, tabular and textual reporting in R-Report...
 
Beyond php it's not (just) about the code
Beyond php   it's not (just) about the codeBeyond php   it's not (just) about the code
Beyond php it's not (just) about the code
 
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of WranglingPLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
Validation
ValidationValidation
Validation
 
Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty Burgers
 

Similar a Adventures on live partitioning

Free The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own DomainFree The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own DomainKen Collins
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeWim Godden
 
MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0oysteing
 
Best Practices with ODI : Flexibility
Best Practices with ODI : FlexibilityBest Practices with ODI : Flexibility
Best Practices with ODI : FlexibilityGurcan Orhan
 
What's new in Vaadin 8, how do you upgrade, and what's next?
What's new in Vaadin 8, how do you upgrade, and what's next?What's new in Vaadin 8, how do you upgrade, and what's next?
What's new in Vaadin 8, how do you upgrade, and what's next?Marcus Hellberg
 
MySQL User Group NL - MySQL 8
MySQL User Group NL - MySQL 8MySQL User Group NL - MySQL 8
MySQL User Group NL - MySQL 8Frederic Descamps
 
Web-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exportsWeb-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exportsDirk Cludts
 
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...Dirk Cludts
 
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan
 
Make everything realtime & collaborative - JS Summit 2014
Make everything realtime & collaborative - JS Summit 2014Make everything realtime & collaborative - JS Summit 2014
Make everything realtime & collaborative - JS Summit 2014Joseph Gentle
 
The future will be Realtime & Collaborative
The future will be Realtime & CollaborativeThe future will be Realtime & Collaborative
The future will be Realtime & CollaborativeJoseph Gentle
 
Cachopo - Scalable Stateful Services - Madrid Elixir Meetup
Cachopo - Scalable Stateful Services - Madrid Elixir MeetupCachopo - Scalable Stateful Services - Madrid Elixir Meetup
Cachopo - Scalable Stateful Services - Madrid Elixir MeetupAbel Muíño
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpMatthew Davis
 
You Can Do It in SQL
You Can Do It in SQLYou Can Do It in SQL
You Can Do It in SQLDatabricks
 
Core .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 EnhancementsCore .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 EnhancementsRobert MacLean
 
DrupalCon Chicago Practical MongoDB and Drupal
DrupalCon Chicago Practical MongoDB and DrupalDrupalCon Chicago Practical MongoDB and Drupal
DrupalCon Chicago Practical MongoDB and DrupalDoug Green
 

Similar a Adventures on live partitioning (20)

Free The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own DomainFree The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own Domain
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 
MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0
 
Best Practices with ODI : Flexibility
Best Practices with ODI : FlexibilityBest Practices with ODI : Flexibility
Best Practices with ODI : Flexibility
 
ELAVARASAN.pdf
ELAVARASAN.pdfELAVARASAN.pdf
ELAVARASAN.pdf
 
What's new in Vaadin 8, how do you upgrade, and what's next?
What's new in Vaadin 8, how do you upgrade, and what's next?What's new in Vaadin 8, how do you upgrade, and what's next?
What's new in Vaadin 8, how do you upgrade, and what's next?
 
Data herding
Data herdingData herding
Data herding
 
Data herding
Data herdingData herding
Data herding
 
MySQL User Group NL - MySQL 8
MySQL User Group NL - MySQL 8MySQL User Group NL - MySQL 8
MySQL User Group NL - MySQL 8
 
Web-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exportsWeb-IT Support and Consulting - dBase exports
Web-IT Support and Consulting - dBase exports
 
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel ...
 
Sylius, the good choice
Sylius, the good choiceSylius, the good choice
Sylius, the good choice
 
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2
 
Make everything realtime & collaborative - JS Summit 2014
Make everything realtime & collaborative - JS Summit 2014Make everything realtime & collaborative - JS Summit 2014
Make everything realtime & collaborative - JS Summit 2014
 
The future will be Realtime & Collaborative
The future will be Realtime & CollaborativeThe future will be Realtime & Collaborative
The future will be Realtime & Collaborative
 
Cachopo - Scalable Stateful Services - Madrid Elixir Meetup
Cachopo - Scalable Stateful Services - Madrid Elixir MeetupCachopo - Scalable Stateful Services - Madrid Elixir Meetup
Cachopo - Scalable Stateful Services - Madrid Elixir Meetup
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
 
You Can Do It in SQL
You Can Do It in SQLYou Can Do It in SQL
You Can Do It in SQL
 
Core .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 EnhancementsCore .NET Framework 4.0 Enhancements
Core .NET Framework 4.0 Enhancements
 
DrupalCon Chicago Practical MongoDB and Drupal
DrupalCon Chicago Practical MongoDB and DrupalDrupalCon Chicago Practical MongoDB and Drupal
DrupalCon Chicago Practical MongoDB and Drupal
 

Último

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 

Último (20)

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 

Adventures on live partitioning

  • 2. Matteo MelliDEV Working @ OnGres (www.ongres.com) Software Developer PostgreSQL support @teoincontatto teoincontatto
  • 3. PGCONF.EU 2017 About ONGRES ➔ IT firm specialized on R&D on Databases, more specifically PostgreSQL:  Training  Consulting and development  PostgreSQL Support ➔ Developers of ToroDB (www.torodb.com), an open- source, document-store database that works on top of PostgreSQL and is compatible with MongoDB. ➔ Partners of www.pythian.com, reference multinational company providing database support and data services.
  • 4. PGCONF.EU 2017 Our customer Applications do simple queries Applications do simple queries Bach processing that add and modify data Bach processing that add and modify data Application SaaS 2K Users2K Users
  • 5. PGCONF.EU 2017 active master hot- standby System characteristics passive master Availability Zone A AWS Region AWS RDS BatchBatch ApplicationApplication Availability Zone B
  • 6. PGCONF.EU 2017 System characteristics ➔ PostgreSQL 9.6 ➔ Amazon RDS db.r3.xlarge ➔ 200MB/s throughput (max 1.2GB/s) ➔ HA with Multi-AZ active/passive ➔ Hot-standby replica
  • 7. PGCONF.EU 2017 Big table problem ✓ ~100GB size (50% indexes) table ✓ ~1000M rows table ✓ Table growth due to batch process (from few MB to some GB per night) ✓ Queries slow down (up to x10 slower) ✓ CUD slow down (up to x100 slower)
  • 9. PGCONF.EU 2017 The requirements ✓ Removing indexes not an option 1 ✓ Partitioning is the way to go 2 ✓ 2 days of outage at maximum 3
  • 10. PGCONF.EU 2017 big_table Type of partitioning ➔ Using table INHERITANCE ➔ Cloning PK and FKs ➔ Range checks on integer column part_id ➔ Partition key on a 2 JOINs far away table ✔ If fk_mid_id is NULL or fk_part_id is NULL row is logically deleted big_id fk_mid_id mid_table mid_id fk_part_id part_table part_id
  • 11. PGCONF.EU 2017 The naive approach ✓ 2 days maintenance window (will be enough!) ✓ big_table empty copy plus partition key ✓ Homogeneous ranges of partition key ✓ Copy directly to partitions from ad-hoc view 11 22 33 44
  • 12. PGCONF.EU 2017 The naive approach CREATE TABLE big_table_part (fk_part_id integer) INHERITS (big_table); ALTER TABLE big_table_part NO INHERIT big_table; big_table_part big_id fk_mid_id fk_part_id
  • 13. PGCONF.EU 2017 The naive approach CREATE TABLE big_table_part_info SELECT 'big_table_' || n AS name, n AS start, n + 200 AS next FROM generate_series(0, 1800, 200) AS n name big_part_1 start 0 next 200 big_part_2 200 400 ... ... ... big_part_10 1800 2000 big_table_part _info name start next
  • 14. PGCONF.EU 2017 The naive approach DO $$DECLARE name, start, next BEGIN FOR name, start, next IN SELECT name, start, next FROM big_table_part_info LOOP EXECUTE 'CREATE TABLE ' || name || '(' || ' CHECK part_id >= ' || start || ' AND part_id < ' || next || ')' || ' INHERITS (big_table_part)’; EXECUTE 'CREATE INDEX ' || name || '_pk_idx' || ' ON ' || name || '(big_id)’; … END LOOP; END$$ big_table_2big_table_1 big_table_n… big_table_2 _pk_idx big_table_1 _pk_idx big_table_n _pk_idx … ………
  • 15. PGCONF.EU 2017 The naive approach big_table_view SELECT p.part_id, b.* FROM big_table AS b JOIN mid_table ON (…) JOIN part_key_table AS p ON (...) big_table big_id fk_mid_id mid_table mid_id fk_part_id part_table part_id
  • 16. PGCONF.EU 2017 The naive approach big_table_view DO $$DECLARE name, start, next BEGIN FOR name, start, next IN SELECT name, start, next FROM big_table_part_info LOOP EXECUTE 'INSERT INTO ' || name || ' SELECT *’ || ' FROM big_table_view’ || ' WHERE part_id >= ' || start || ' AND part_id < ' || next || ' FOR UPDATE’ END LOOP END$$ big_table big_id fk_mid_id mid_table mid_id fk_part_id part_table part_id big_table_2big_table_1 big_table_n…
  • 17. PGCONF.EU 2017 The naive approach ✓ 2 days maintenance window (will be enough!) ✓ big_table empty copy plus partition key ✓ Homogeneous ranges of partition key ✓ Copy directly to partitions from ad-hoc view 11 22 33 44 WRONG! It takes too long, full scan 3 tables repeated per partition (~8 hour each)
  • 18. PGCONF.EU 2017 The tuned approach ✓ 2 days maintenance window (will be enough!) ✓ Copy of big_table from ad-hoc view ✓ Homogeneous ranges of partition key ✓ Copy directly to partitions from ad-hoc view 11 33 44 55 ✓ big_table empty copy plus partition key 22
  • 19. PGCONF.EU 2017 The tuned approach big_table_view CREATE TABLE big_table_copy (fk_part_id integer) INHERITS (big_table); ALTER TABLE big_table_copy NO INHERIT big_table; INSERT INTO big_table_copy SELECT * FROM big_table_view; big_table big_id fk_mid_id mid_table mid_id fk_part_id part_table part_id big_table_copy big_id fk_mid_id fk_part_id
  • 20. PGCONF.EU 2017 The tuned approach DO $$DECLARE name, start, next BEGIN FOR name, start, next IN SELECT name, start, next FROM big_table_part_info LOOP EXECUTE 'INSERT INTO ' || name || ' SELECT *’ || ' FROM big_table_copy' || ' WHERE part_id >= ' || start || ' AND part_id < ' || next || ' FOR UPDATE’ END LOOP END$$ big_table_copy big_id fk_mid_id fk_part_id big_table_2big_table_1 big_table_n…
  • 21. PGCONF.EU 2017 The tuned approach WRONG! Homogeneous range <> Homogeneous data distribution ✓ 2 days maintenance window (will be enough!) ✓ Copy of big_table from ad-hoc view ✓ Homogeneous ranges of partition key ✓ Copy directly to partitions from ad-hoc view 11 33 44 55 ✓ big_table empty copy plus partition key 22
  • 22. PGCONF.EU 2017 The tuned approach Homogeneous range partitions 1 2 3 4 5 6 7 8 9 10 0 20 40 60 80 100 120 140 160 180 Range distribution Homogenerous range paritions Millions of rows Partition#
  • 23. PGCONF.EU 2017 The smart approach ✓ 2 days maintenance window (will be enough!) ✓ Copy directly to partitions from ad-hoc view ✓ big_table empty copy plus partition key ✓ Count group of rows 11 22 33 44 55 66 ✓ Partition by ranges based on count of partition key ✓ Copy of big_table plus partition key
  • 24. PGCONF.EU 2017 The smart approach Count based partitions Homogeneous range partitions 1 2 3 4 5 6 7 8 9 10 11 12 13 0 20 40 60 80 100 120 140 160 180 Range distribution Homogenerous range paritions Count based paritions Millions of rows Partition#
  • 25. PGCONF.EU 2017 The smart approach CREATE TABLE big_table_part_info AS SELECT 'big_table_1' AS name, 0 AS start, 100 AS next UNION ALL SELECT 'big_table_2' AS name, 100 AS start, 200 AS next UNION ALL … UNION ALL SELECT 'big_table_10' AS name, 1750 AS start, 1900 AS nextname big_part_1 start 0 next 100 big_part_2 100 200 ... ... ... big_part_10 1750 1900 big_table_part _info name start next
  • 27. PGCONF.EU 2017 The little problem We forgot our replica! Under heavy load, RDS replica seemed to stop replicating via network and switch to WAL shipping, which was extremely slow and lag grew to days!
  • 28. PGCONF.EU 2017 Solving the little problem Upgrade wal_keep_segments from 64 to 4096 so replica stay with SR Upgrade wal_keep_segments from 64 to 4096 so replica stay with SR Nice idea but replica still get out of sync?! Nice idea but replica still get out of sync?!
  • 29. PGCONF.EU 2017 Solving the little problem Let’s create a new replica and remove the old one then! Let’s create a new replica and remove the old one then! But sometimes the replica could take a day to catch up. But sometimes the replica could take a day to catch up.
  • 30. PGCONF.EU 2017 Solving the little problem How to make replica stay within SR?How to make replica stay within SR? Copy data by chunks monitoring replication lag Copy data by chunks monitoring replication lag
  • 31. PGCONF.EU 2017 Solving the little problem How to make it in a window of 2 days?How to make it in a window of 2 days? Don’t do it in a window, do it LIVE! lag Don’t do it in a window, do it LIVE! lag
  • 32. PGCONF.EU 2017 Do it LIVE! ➔ Application are not aware ✔ Trick application to think it is using the real table! (INSTEAD OF to the rescue) ➔ Shit happens ✔ Ability to pause/resume the process if needed ➔ Short enough resource usage duration ✔ Be polite, do not starve resources ✔ Also, don’t forget the little problem (lag monitoring) ➔ Preserve data consistency ✔ Obviously
  • 33. PGCONF.EU 2017 The live approach 1. Count group rows 2. 8 hours maintenance window (will be enough!) 3. Indexes to help retrieve partition key faster 4. Empty copy of the table plus partition key 5. Partition by range based on count of partition key 6. Create helper table for the new inserted rows 7. Rename big_table and create a fake view with trigger to handle changes 8. Copy data to the partitions by chunks of 1M rows (LIVE part)
  • 34. PGCONF.EU 2017 The live approach - the fake view Applications “thinks” they are using the real big_table… Applications “thinks” they are using the real big_table… ...INSTEAD OF that, they’re accessing a view that fakes the real big_table. The view create an abstraction that allow to read real data and (with the help of a trigger) to modify real data. ...INSTEAD OF that, they’re accessing a view that fakes the real big_table. The view create an abstraction that allow to read real data and (with the help of a trigger) to modify real data.
  • 35. PGCONF.EU 2017 The live approach - the fake view big_table (a view with an INSTEAD OF trigger) SELECT * FROM big_table_old UNION ALL SELECT * FROM big_table_fow_new_records BatchBatch ApplicationApplication big_table_old big_id fk_mid_id big_table_for _new_records big_id fk_mid_id
  • 36. PGCONF.EU 2017 The live approach - the fake view big_table (a view with an INSTEAD OF trigger) ON INSERT fk_part_id := (SELECT p.part_id FROM mid_table AS m JOIN part_key_table AS p ON (…) WHERE mid_id = NEW.fk_mid_id) big_table_old big_id fk_mid_id big_table_for _new_records big_id fk_mid_id big_table_part big_id fk_mid_id fk_part_id big_table_<x>
  • 37. PGCONF.EU 2017 The live approach - the fake view big_table (a view with an INSTEAD OF trigger) ON UPDATE & ON DELETE big_table_old big_id fk_mid_id big_table_for _new_records big_id fk_mid_id big_table_part big_id fk_mid_id fk_part_id big_table_<x>
  • 38. PGCONF.EU 2017 The live approach - the fake view big_table (a view with an INSTEAD OF trigger) ON UPDATE & ON DELETE WARNING ON UPDATE we forbid change big_id and fk_part_id, or the whole process could break!! big_table_old big_id fk_mid_id big_table_for _new_records big_id fk_mid_id big_table_part big_id fk_mid_id fk_part_id big_table_<x>
  • 39. PGCONF.EU 2017 The live approach - copy a chunk of data ✓ Copy 1M rows to intermediate table ✓ Move from intermediate table to partitions ✓Two step approach then! 11 22
  • 40. PGCONF.EU 2017 The live approach - copy a chunk of data CREATE TABLE IF NOT EXISTS last_big_id(big_id bigint) last_big_id big_id
  • 41. PGCONF.EU 2017 The live approach - copy a chunk of data WITH big_table_chunk AS (INSERT INTO big_table_part SELECT b.*, p.part_id FROM big_table_old AS b JOIN mid_table ON (…) JOIN part_key_table AS p ON (…) WHERE big_id > ( SELECT coalesce(max(big_id),-1) FROM last_big_id) ORDER BY big_id LIMIT 1000000 FOR UPDATE RETURNING big_id) INSERT INTO last_big_id SELECT max(big_id) FROM big_table_chunk big_table big_id fk_mid_id mid_table mid_id fk_part_id part_table part_id big_table_part big_id fk_mid_id fk_part_id
  • 42. PGCONF.EU 2017 The live approach - copy a chunk of data DO $$DECLARE name, start, next BEGIN FOR name, start, next IN SELECT name, start, next FROM big_table_part_info LOOP EXECUTE 'INSERT INTO ' || name || ' SELECT *' || ' FROM big_table_part' || ' WHERE part_id >= ' || start || ' AND part_id < ' || next || ' FOR UPDATE’ END LOOP TRUNCATE ONLY big_table_part; END$$ big_table_part big_id fk_mid_id fk_part_id big_table_2big_table_1 big_table_n…
  • 43. PGCONF.EU 2017 The live approach - Some data!
  • 44. PGCONF.EU 2017 Postgresql 10 partitioning ➔ A new way to create a partitions that simplify and remove risk of errors when doing it manually (beware you still have to create PKs, FKs and indexes manually) ➔ So, how to apply this live partitioning technique to postgresql 10? CREATE TABLE big_table_1 PARTITION OF big_table_part FOR VALUES FROM (1) TO (666);
  • 45. PGCONF.EU 2017 Postgresql 10 partitioning ➔ Copy from view to parent table would fail since the parent table of a partition is not a real table: ✔ We will need an intermediate table (big_table_inter) to hold the 1M rows readed from big_table_old ✔ Also, when calculating last_big_id before copying a chunk we will have to do a: SELECT max(big_id) FROM ( SELECT max(big_id) AS bid_ig FROM big_table_part UNION ALL SELECT max(big_id) AS big_id FROM big_table_inter ) AS tmp SELECT relname, relkind FROM pg_class WHERE relname LIKE 'big_table_%'; relname relkind big_table_part big_table_1 P r