SlideShare a Scribd company logo
1 of 16
EnterpriseDB, Postgres Plus and Dynatune are trademarks of EnterpriseDB Corporation. Other names may be trademarks of their respective owners. © 2011. All rights reserved. PostgreSQL Partitioning Using Inheritance and Check Constraints Presented by Chetan Suttraway November 22, 2011 Chetan.Suttraway @enterprisedb.com
© 2011 EnterpriseDB. All rights reserved. Partitioning  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
© 2011 EnterpriseDB. All rights reserved. Partitioning via Inheritance ,[object Object],[object Object],CREATE TABLE orders( id  INT NOT NULL, address  TEXT NOT NULL, order_date TIMESTAMP NOT NULL ); ,[object Object],[object Object],CREATE TABLE orders_part_2011 ( CHECK ( order_date >= DATE '2011-01-01' AND  order_date < DATE '2012-01-01' ) ) INHERITS (orders) TABLESPACE tbsp2; CREATE TABLE orders_part_2010 ( CHECK ( order_date < DATE '2011-01-01' ) ) INHERITS (orders); No Primary Key! Match datatype on constraints Simple partition names Plan reading becomes easy
© 2011 EnterpriseDB. All rights reserved. Partitioning via Inheritance ,[object Object],[object Object],CREATE INDEX orders_part_2011_idx ON orders_part_2011(order_date); CREATE INDEX orders_part_2010_idx ON orders_part_2010(order_date); ,[object Object],[object Object],CONSTRAINT_EXCLUSION = ON; ,[object Object],SET constraint_exclusion = 'PARTITION'; ,[object Object],ALTER DATABASE somedb SET constraint_exclusion = on;
Partitioning via Inheritance © 2011 EnterpriseDB. All rights reserved. ,[object Object],[object Object],CREATE OR REPLACE FUNCTION orders_insert1() RETURNS TRIGGER AS $$ DECLARE  vsql Text; BEGIN vsql :=  'INSERT INTO orders_part_'|| to_char(NEW.order_date, 'YYYY' )|| ' VALUES ('||NEW.id||','||quote_litera(NEW.address)||','||quote_literal(NEW.order_date)||')'; RETURN NULL; END; $$ LANGUAGE plpgsql; Low Maintenance Quoting, NULL issues Fast, No locking for updation of function ,[object Object],[object Object],[object Object],CREATE TRIGGER orders_insert_trigger BEFORE INSERT ON orders FOR EACH ROW EXECUTE PROCEDURE orders_insert();
Partitioning via Inheritance © 2011 EnterpriseDB. All rights reserved. CREATE OR REPLACE FUNCTION orders_insert() RETURNS TRIGGER AS $$ BEGIN IF (NEW.order_date >= DATE '2011-01-01' AND NEW.order_date < DATE '2012-01-01') THEN INSERT INTO orders_part_2011 VALUES (NEW.*); ELSIF (NEW.order_date < DATE '2011-01-01') THEN INSERT INTO orders_part_2010 VALUES (NEW.*); ELSE RAISE EXCEPTION 'Date out of range. check orders_insert() function!'; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER orders_insert_trigger BEFORE INSERT ON orders FOR EACH ROW EXECUTE PROCEDURE orders_insert(); High Maintenance No quoting, NULL issues Fast, No locking for updation of function
© 2011 EnterpriseDB. All rights reserved. Partitioning via Inheritance pg=# INSERT INTO orders VALUES(1, 'pune', '2011-08-22'); INSERT 0 0 SELECT * FROM orders; id |  address  |  order_date  ----+--------------+--------------------------- 1 | pune  | 22-AUG-11 00:00:00 2 | bengaluru | 22-FEB-10 00:00:00 (2 rows) pg=# INSERT INTO orders VALUES(2, 'pune', '2010-02-22'); INSERT 0 0 pg=# UPDATE orders SET address = 'bengaluru' WHERE id = 2; UPDATE 1 SELECT * FROM orders_part_2011; id | address |  order_date  ----+---------+------------------------ 1 | pune  | 22-AUG-11 00:00:00 (1 row) SELECT * FROM orders_part_2010; id |  address  |  order_date  ----+-----------+-------------------- 2 | bengaluru | 22-FEB-10 00:00:00 (1 row) SELECT * FROM ONLY orders; id | address | order_date  ----+---------+------------ (0 rows)
© 2011 EnterpriseDB. All rights reserved. Partitioning via Inheritance EXPLAIN SELECT * FROM orders WHERE order_date = '02-JAN-11'; QUERY PLAN  ---------------------------------------------------------------------------------------- Result  (cost=0.00..26.01 rows=7 width=40) ->  Append  (cost=0.00..26.01 rows=7 width=40) ->  Seq Scan on orders  (cost=0.00..23.75 rows=6 width=44) Filter: (order_date = '02-JAN-11 00:00:00'::timestamp without time zone) ->  Seq Scan on  orders_part_2011  orders  (cost=0.00..2.26 rows=1 width=18) Filter: (order_date = '02-JAN-11 00:00:00'::timestamp without time zone) (6 rows) EXPLAIN SELECT * FROM orders WHERE order_date = '02-JAN-10'; QUERY PLAN  ---------------------------------------------------------------------------------------- Result  (cost=0.00..24.76 rows=7 width=44) ->  Append  (cost=0.00..24.76 rows=7 width=44) ->  Seq Scan on orders  (cost=0.00..23.75 rows=6 width=44) Filter: (order_date = '02-JAN-10 00:00:00'::timestamp without time zone) ->  Seq Scan on  orders_part_2010  orders  (cost=0.00..1.01 rows=1 width=44) Filter: (order_date = '02-JAN-10 00:00:00'::timestamp without time zone) (6 rows
© 2011 EnterpriseDB. All rights reserved. Query Planning EXPLAIN SELECT * FROM orders WHERE order_date = now(); QUERY PLAN  ------------------------------------------------------------------------------------ Result  (cost=0.00..30.03 rows=8 width=41) ->  Append  (cost=0.00..30.03 rows=8 width=41) ->  Seq Scan on orders  (cost=0.00..26.50 rows=6 width=44) Filter: (order_date = now()) ->  Seq Scan on orders_part_2011 orders  (cost=0.00..2.51 rows=1 width=18) Filter: (order_date = now()) ->  Seq Scan on orders_part_2010 orders  (cost=0.00..1.01 rows=1 width=44) Filter: (order_date = now()) (8 rows) ,[object Object]
© 2011 EnterpriseDB. All rights reserved. Constraint Exclusion ,[object Object],[object Object]
mutually exclusive ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
© 2011 EnterpriseDB. All rights reserved. Inheritance ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
© 2011 EnterpriseDB. All rights reserved. Uniqueness ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
© 2011 EnterpriseDB. All rights reserved. Automating Maintenance ,[object Object],[object Object],[object Object],[object Object]
© 2011 EnterpriseDB. All rights reserved. Querying Over Partitions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
© 2011 EnterpriseDB. All rights reserved. Querying Over Partitions...MAX pg=# EXPLAIN  SELECT MAX(order_date) FROM orders; QUERY PLAN  -------------------------------------------------------------------------------- Result  (cost=26.56..26.57 rows=1 width=0) InitPlan 1 (returns $0) ->  Limit  (cost=26.52..26.56 rows=1 width=8) ->  Merge Append  (cost=26.52..73.51 rows=1196 width=8) Sort Key: public.orders.order_date ->  Sort  (cost=26.47..29.21 rows=1094 width=8) Sort Key: public.orders.order_date ->  Seq Scan on orders  (cost=0.00..21.00 rows=1094 width=8) Filter: (order_date IS NOT NULL) ->  Index Scan Backward using  orders_part_2011_idx  on orders_pa rt_2011 orders  (cost=0.00..14.02 rows=101 width=8) Index Cond: (order_date IS NOT NULL) ->  Index Scan Backward using  orders_part_old_idx  on orders_par t_old orders  (cost=0.00..8.27 rows=1 width=8) Index Cond: (order_date IS NOT NULL) (13 rows)

More Related Content

What's hot

Database management system file
Database management system fileDatabase management system file
Database management system file
Ankit Dixit
 
T sql denali code Day of .Net
T sql denali code Day of .NetT sql denali code Day of .Net
T sql denali code Day of .Net
KathiK58
 

What's hot (19)

MariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit holeMariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit hole
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace Walkthrough
 
Database management system file
Database management system fileDatabase management system file
Database management system file
 
Using histograms to get better performance
Using histograms to get better performanceUsing histograms to get better performance
Using histograms to get better performance
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
 
New SQL features in latest MySQL releases
New SQL features in latest MySQL releasesNew SQL features in latest MySQL releases
New SQL features in latest MySQL releases
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
T sql denali code Day of .Net
T sql denali code Day of .NetT sql denali code Day of .Net
T sql denali code Day of .Net
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
 
Higher-Order Components — Ilya Gelman
Higher-Order Components — Ilya GelmanHigher-Order Components — Ilya Gelman
Higher-Order Components — Ilya Gelman
 
Sql queries
Sql queriesSql queries
Sql queries
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Cube rollup slides
Cube rollup slidesCube rollup slides
Cube rollup slides
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react application
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Explain that explain
Explain that explainExplain that explain
Explain that explain
 

Viewers also liked

Viewers also liked (7)

Plproxy
PlproxyPlproxy
Plproxy
 
Implementing Parallelism in PostgreSQL - PGCon 2014
Implementing Parallelism in PostgreSQL - PGCon 2014Implementing Parallelism in PostgreSQL - PGCon 2014
Implementing Parallelism in PostgreSQL - PGCon 2014
 
Getting Started with PL/Proxy
Getting Started with PL/ProxyGetting Started with PL/Proxy
Getting Started with PL/Proxy
 
Table partitioning in PostgreSQL + Rails
Table partitioning in PostgreSQL + RailsTable partitioning in PostgreSQL + Rails
Table partitioning in PostgreSQL + Rails
 
PostgreSQL Partitioning, PGCon 2007
PostgreSQL Partitioning, PGCon 2007PostgreSQL Partitioning, PGCon 2007
PostgreSQL Partitioning, PGCon 2007
 
Escalabilidade, Sharding, Paralelismo e Bigdata com PostgreSQL? Yes, we can!
Escalabilidade, Sharding, Paralelismo e Bigdata com PostgreSQL? Yes, we can!Escalabilidade, Sharding, Paralelismo e Bigdata com PostgreSQL? Yes, we can!
Escalabilidade, Sharding, Paralelismo e Bigdata com PostgreSQL? Yes, we can!
 
Developing with the Go client for Apache Kafka
Developing with the Go client for Apache KafkaDeveloping with the Go client for Apache Kafka
Developing with the Go client for Apache Kafka
 

Similar to Chetan postgresql partitioning

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)
TĂąm
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 

Similar to Chetan postgresql partitioning (20)

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'roll
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
 
Optimizer percona live_ams2015
Optimizer percona live_ams2015Optimizer percona live_ams2015
Optimizer percona live_ams2015
 
Introduction to Parallel Execution
Introduction to Parallel ExecutionIntroduction to Parallel Execution
Introduction to Parallel Execution
 
Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
Checking clustering factor to detect row migration
Checking clustering factor to detect row migrationChecking clustering factor to detect row migration
Checking clustering factor to detect row migration
 
Basic Query Tuning Primer - Pg West 2009
Basic Query Tuning Primer - Pg West 2009Basic Query Tuning Primer - Pg West 2009
Basic Query Tuning Primer - Pg West 2009
 
Basic Query Tuning Primer
Basic Query Tuning PrimerBasic Query Tuning Primer
Basic Query Tuning Primer
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
 
Query parameterization
Query parameterizationQuery parameterization
Query parameterization
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 

More from OpenSourceIndia

Zend server presentation for osi days
Zend server presentation for osi daysZend server presentation for osi days
Zend server presentation for osi days
OpenSourceIndia
 
Osi days 2011 venkat mangudi
Osi days 2011  venkat mangudiOsi days 2011  venkat mangudi
Osi days 2011 venkat mangudi
OpenSourceIndia
 
20111121 osi keynote
20111121 osi keynote20111121 osi keynote
20111121 osi keynote
OpenSourceIndia
 
Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02
OpenSourceIndia
 
Gotunitsosidays2011 111121093234-phpapp02
Gotunitsosidays2011 111121093234-phpapp02Gotunitsosidays2011 111121093234-phpapp02
Gotunitsosidays2011 111121093234-phpapp02
OpenSourceIndia
 
Megha_Osi my sql productroadmap
Megha_Osi my sql productroadmapMegha_Osi my sql productroadmap
Megha_Osi my sql productroadmap
OpenSourceIndia
 
Sriram simplify os_sdevelopment
Sriram simplify os_sdevelopmentSriram simplify os_sdevelopment
Sriram simplify os_sdevelopment
OpenSourceIndia
 
Rajashekaran vengalil building cross browser html5 websites
Rajashekaran vengalil building cross browser html5 websitesRajashekaran vengalil building cross browser html5 websites
Rajashekaran vengalil building cross browser html5 websites
OpenSourceIndia
 
Naveen nimmu sdn future of networking
Naveen nimmu sdn   future of networkingNaveen nimmu sdn   future of networking
Naveen nimmu sdn future of networking
OpenSourceIndia
 
Harsha s ipmi_tool_osi
Harsha s ipmi_tool_osiHarsha s ipmi_tool_osi
Harsha s ipmi_tool_osi
OpenSourceIndia
 
Gil yehuda commoditization open source
Gil yehuda commoditization open sourceGil yehuda commoditization open source
Gil yehuda commoditization open source
OpenSourceIndia
 
Divyanshu open stack presentation -osi-ppt
Divyanshu open stack presentation -osi-pptDivyanshu open stack presentation -osi-ppt
Divyanshu open stack presentation -osi-ppt
OpenSourceIndia
 
Azri solutions leaner techniques for faster portals get drupalled
Azri solutions leaner techniques for faster portals   get drupalledAzri solutions leaner techniques for faster portals   get drupalled
Azri solutions leaner techniques for faster portals get drupalled
OpenSourceIndia
 
Ashish pandey huawei osi_days2011_cgroups_understanding_better
Ashish pandey huawei osi_days2011_cgroups_understanding_betterAshish pandey huawei osi_days2011_cgroups_understanding_better
Ashish pandey huawei osi_days2011_cgroups_understanding_better
OpenSourceIndia
 
Sumit& archit osi nov-2011-displays-in-mobile-devices
Sumit& archit osi nov-2011-displays-in-mobile-devicesSumit& archit osi nov-2011-displays-in-mobile-devices
Sumit& archit osi nov-2011-displays-in-mobile-devices
OpenSourceIndia
 

More from OpenSourceIndia (17)

Zend server presentation for osi days
Zend server presentation for osi daysZend server presentation for osi days
Zend server presentation for osi days
 
Osi days 2011 venkat mangudi
Osi days 2011  venkat mangudiOsi days 2011  venkat mangudi
Osi days 2011 venkat mangudi
 
Spring osi
Spring osiSpring osi
Spring osi
 
20111121 osi keynote
20111121 osi keynote20111121 osi keynote
20111121 osi keynote
 
Cloud foundry osi
Cloud foundry osiCloud foundry osi
Cloud foundry osi
 
Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02
 
Gotunitsosidays2011 111121093234-phpapp02
Gotunitsosidays2011 111121093234-phpapp02Gotunitsosidays2011 111121093234-phpapp02
Gotunitsosidays2011 111121093234-phpapp02
 
Megha_Osi my sql productroadmap
Megha_Osi my sql productroadmapMegha_Osi my sql productroadmap
Megha_Osi my sql productroadmap
 
Sriram simplify os_sdevelopment
Sriram simplify os_sdevelopmentSriram simplify os_sdevelopment
Sriram simplify os_sdevelopment
 
Rajashekaran vengalil building cross browser html5 websites
Rajashekaran vengalil building cross browser html5 websitesRajashekaran vengalil building cross browser html5 websites
Rajashekaran vengalil building cross browser html5 websites
 
Naveen nimmu sdn future of networking
Naveen nimmu sdn   future of networkingNaveen nimmu sdn   future of networking
Naveen nimmu sdn future of networking
 
Harsha s ipmi_tool_osi
Harsha s ipmi_tool_osiHarsha s ipmi_tool_osi
Harsha s ipmi_tool_osi
 
Gil yehuda commoditization open source
Gil yehuda commoditization open sourceGil yehuda commoditization open source
Gil yehuda commoditization open source
 
Divyanshu open stack presentation -osi-ppt
Divyanshu open stack presentation -osi-pptDivyanshu open stack presentation -osi-ppt
Divyanshu open stack presentation -osi-ppt
 
Azri solutions leaner techniques for faster portals get drupalled
Azri solutions leaner techniques for faster portals   get drupalledAzri solutions leaner techniques for faster portals   get drupalled
Azri solutions leaner techniques for faster portals get drupalled
 
Ashish pandey huawei osi_days2011_cgroups_understanding_better
Ashish pandey huawei osi_days2011_cgroups_understanding_betterAshish pandey huawei osi_days2011_cgroups_understanding_better
Ashish pandey huawei osi_days2011_cgroups_understanding_better
 
Sumit& archit osi nov-2011-displays-in-mobile-devices
Sumit& archit osi nov-2011-displays-in-mobile-devicesSumit& archit osi nov-2011-displays-in-mobile-devices
Sumit& archit osi nov-2011-displays-in-mobile-devices
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Chetan postgresql partitioning

  • 1. EnterpriseDB, Postgres Plus and Dynatune are trademarks of EnterpriseDB Corporation. Other names may be trademarks of their respective owners. © 2011. All rights reserved. PostgreSQL Partitioning Using Inheritance and Check Constraints Presented by Chetan Suttraway November 22, 2011 Chetan.Suttraway @enterprisedb.com
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. Partitioning via Inheritance © 2011 EnterpriseDB. All rights reserved. CREATE OR REPLACE FUNCTION orders_insert() RETURNS TRIGGER AS $$ BEGIN IF (NEW.order_date >= DATE '2011-01-01' AND NEW.order_date < DATE '2012-01-01') THEN INSERT INTO orders_part_2011 VALUES (NEW.*); ELSIF (NEW.order_date < DATE '2011-01-01') THEN INSERT INTO orders_part_2010 VALUES (NEW.*); ELSE RAISE EXCEPTION 'Date out of range. check orders_insert() function!'; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER orders_insert_trigger BEFORE INSERT ON orders FOR EACH ROW EXECUTE PROCEDURE orders_insert(); High Maintenance No quoting, NULL issues Fast, No locking for updation of function
  • 7. © 2011 EnterpriseDB. All rights reserved. Partitioning via Inheritance pg=# INSERT INTO orders VALUES(1, 'pune', '2011-08-22'); INSERT 0 0 SELECT * FROM orders; id | address | order_date ----+--------------+--------------------------- 1 | pune | 22-AUG-11 00:00:00 2 | bengaluru | 22-FEB-10 00:00:00 (2 rows) pg=# INSERT INTO orders VALUES(2, 'pune', '2010-02-22'); INSERT 0 0 pg=# UPDATE orders SET address = 'bengaluru' WHERE id = 2; UPDATE 1 SELECT * FROM orders_part_2011; id | address | order_date ----+---------+------------------------ 1 | pune | 22-AUG-11 00:00:00 (1 row) SELECT * FROM orders_part_2010; id | address | order_date ----+-----------+-------------------- 2 | bengaluru | 22-FEB-10 00:00:00 (1 row) SELECT * FROM ONLY orders; id | address | order_date ----+---------+------------ (0 rows)
  • 8. © 2011 EnterpriseDB. All rights reserved. Partitioning via Inheritance EXPLAIN SELECT * FROM orders WHERE order_date = '02-JAN-11'; QUERY PLAN ---------------------------------------------------------------------------------------- Result (cost=0.00..26.01 rows=7 width=40) -> Append (cost=0.00..26.01 rows=7 width=40) -> Seq Scan on orders (cost=0.00..23.75 rows=6 width=44) Filter: (order_date = '02-JAN-11 00:00:00'::timestamp without time zone) -> Seq Scan on orders_part_2011 orders (cost=0.00..2.26 rows=1 width=18) Filter: (order_date = '02-JAN-11 00:00:00'::timestamp without time zone) (6 rows) EXPLAIN SELECT * FROM orders WHERE order_date = '02-JAN-10'; QUERY PLAN ---------------------------------------------------------------------------------------- Result (cost=0.00..24.76 rows=7 width=44) -> Append (cost=0.00..24.76 rows=7 width=44) -> Seq Scan on orders (cost=0.00..23.75 rows=6 width=44) Filter: (order_date = '02-JAN-10 00:00:00'::timestamp without time zone) -> Seq Scan on orders_part_2010 orders (cost=0.00..1.01 rows=1 width=44) Filter: (order_date = '02-JAN-10 00:00:00'::timestamp without time zone) (6 rows
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. © 2011 EnterpriseDB. All rights reserved. Querying Over Partitions...MAX pg=# EXPLAIN SELECT MAX(order_date) FROM orders; QUERY PLAN -------------------------------------------------------------------------------- Result (cost=26.56..26.57 rows=1 width=0) InitPlan 1 (returns $0) -> Limit (cost=26.52..26.56 rows=1 width=8) -> Merge Append (cost=26.52..73.51 rows=1196 width=8) Sort Key: public.orders.order_date -> Sort (cost=26.47..29.21 rows=1094 width=8) Sort Key: public.orders.order_date -> Seq Scan on orders (cost=0.00..21.00 rows=1094 width=8) Filter: (order_date IS NOT NULL) -> Index Scan Backward using orders_part_2011_idx on orders_pa rt_2011 orders (cost=0.00..14.02 rows=101 width=8) Index Cond: (order_date IS NOT NULL) -> Index Scan Backward using orders_part_old_idx on orders_par t_old orders (cost=0.00..8.27 rows=1 width=8) Index Cond: (order_date IS NOT NULL) (13 rows)
  • 17.

Editor's Notes

  1. Should this table be partitioned? - Adds Complexity - Adds adminstration cost - Number of rows - Types of queries - Data growth
  2. Inheritance does not automatically propagate data from INSERT or COPY commands to other tables in the inheritance hierarchy
  3. It may not be good idea to set this parameter for all queries in system, as not all queries require this feature.
  4. Modifying a Trigger function requires no special locking.
  5. Modifying a Trigger function requires no special locking.
  6. Partition keys are not supposed to be updated. However in that case, we need to delete from child and do insert on base table.
  7. Now() is stable function. STABLE indicates that within a single table scan the function will consistently return the same result for the same argument values, but that its result could change across SQL statements. This is the appropriate selection for functions whose results depend on database lookups, parameter variables (such as the current time zone), etc. Also note that the current_timestamp family of functions qualify as stable, since their values do not change within a transaction.
  8. A table can inherit from more than one parent table, in which case it has the union of the columns defined by the parent tables. Any columns declared in the child table&apos;s definition are added to these. If the same column name appears in multiple parent tables, or in both a parent table and the child&apos;s definition, then these columns are &amp;quot;merged&amp;quot; so that there is only one such column in the child table. To be merged, columns must have the same data types, else an error is raised. The merged column will have copies of all the check constraints coming from any one of the column definitions it came from, and will be marked not-null if any of them are.
  9. When checking the foreign key, child tables are not considered. You can work around it using additional table indyvidual_pks (indyvidual_pk integer primary key) with all primary keys from both parent and child, which will be maintained using triggers (very simple — insert to indyvidual_pks on insert, delete from it on delete, update it on update, if it changes indyvidual_pk). Then you point foreign keys to this additional table instead of a child. There&apos;ll be some small performance hit, but only when adding/deleting rows.
  10. Race condition among maintenance and partitioning trigger. management trigger - creates a new partition, updates the partitioning trigger because of the new partition, etc partitioning trigger - redirects the row into an appropriate partition