SlideShare a Scribd company logo
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Real-time Analytics on
PostgreSQL at any Scale
Marco Slot <marco@citusdata.com>
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
You offer a product or service (e.g. SaaS, IoT platform, network telemetry, …)
that generates large volumes of time series data.
How to build an analytical dashboard for your customers that:
• Supports a large number of concurrent users
• Reflects new data within minutes
• Has subsecond response times
• Supports advanced analytics
What is real-time analytics?
2
(Heap Analytics)
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Pipeline of Collect - Aggregate - Query:
Real-time analytics architecture
3
Event source
Event source
Event source
Event source
Storage
(Database) Aggregate
Rollups
(Database)
Dashboard
(App)
Collect
Queries
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Pipeline of Collect - Aggregate - Query:
Real-time analytics architecture
4
Event source
Event source
Event source
Event source
Storage
(Database) Aggregate
Rollups
(Database)
Dashboard
(App)
Collect
Queries
Postgres/Citus
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Define a table for storing raw events:
CREATE TABLE events (
event_id bigserial,
event_time timestamptz default now(),
customer_id bigint,
event_type text,
…
event_details jsonb
);
Raw data table
5
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
COPY is by far the fastest way of loading data.
COPY events (customer_id, event_time, … ) FROM STDIN;
A few parallel COPY streams can load hundreds of thousands of events per
second!
Load data
6
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
To achieve fast data loading:
• Use COPY
• Don’t use indexes
To achieve fast reading of new events for aggregation:
• Use an index
Fast data loading
7
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
To achieve fast data loading:
• Use COPY
• Don’t use large indexes
To achieve fast reading of new events for aggregation:
• Use an index
Block-range index is suitable for ordered columns:
CREATE INDEX event_time_idx ON events USING BRIN (event_time);
Fast data loading
8
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Pre-computed aggregates for a period and set of (group by) dimensions.
Can be further filtered and aggregated to generate charts.
What is a rollup?
9
Period Customer Country Site Hit Count
SELECT…
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Append new data to a raw events table (avoid indexes!):
COPY events FROM ...
Periodically aggregate events into rollup table (index away!):
INSERT INTO rollup SELECT … FROM events … GROUP BY …
Application queries the rollup table:
SELECT … FROM rollup WHERE customer_id = 1238 …
Postgres recipe for real-time analytics
10
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Keep your data sorted into buckets
Partitioning
11
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Partitioning keeps indexes small by dividing tables into partitions:
Benefits:
• Avoid fragmentation
• Smaller indexes
• Partition pruning for queries that filter by partition column
• Drop old data quickly, without bloat/fragmentation
Partitioning
12
COPY COPY
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Defining a partitioned table:
CREATE TABLE events (...) PARTITION BY (event_time);
Setting up hourly partitioning with pg_partman:
SELECT partman.create_parent('public.events', 'event_time',
'native', 'hourly');
https://www.citusdata.com/blog/2018/01/24/citus-and-pg-partman-creating-a-sca
lable-time-series-database-on-PostgreSQL/
CREATE EXTENSION pg_partman
13
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
If you’re using partitioning, pg_partman can drop old partitions:
UPDATE partman.part_config
SET retention_keep_table = false, retention = '1 month'
WHERE parent_table = 'public.events';
Periodically run maintenance:
SELECT partman.run_maintenance();
Expiring old data in a partitioned table
14
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Run pg_partman maintenance every hour using pg_cron:
SELECT cron.schedule('3 * * * *', $$
SELECT partman.run_maintenance()
$$);
https://github.com/citusdata/pg_cron
Periodic partitioning maintenance
15
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
High vs. Low Cardinality
Designing Rollup Tables
16
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Define rollup tables containing aggregates:
CREATE TABLE rollup_by_period_and_dimensions (
<period>
<dimensions>
<aggregates>
primary key (<dimensions>,<period>)
);
Primary key index covers many queries, can also add additional indices:
CREATE INDEX usc_idx ON rollup (customer_id, site_id);
Rollup table
17
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Two rollups are smaller than one: A*B + A*C < A*B*C
But… up to 2x more aggregation work.
Choosing granularity and dimensions
18
Time Customer Country Aggregates
Time Customer Site Aggregates
Time Customer Country Site Aggregates
~100 rows per period/customer
~20 rows per period/customer
~20*100=2000 rows per period/customer
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Find balance between query performance and table management.
1. Identify dimensions, metrics (aggregates)
2. Try rollup with all dimensions:
3. Test compression/performance (goal is >5x smaller)
4. If too slow / too big, split rollup table based on query patterns
5. Go to 3
Usually ends up with 5-10 rollup tables
Guidelines for designing rollups
19
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Append-only vs. Incremental
Running Aggregations
20
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Use INSERT INTO rollup SELECT … FROM events … to populate rollup table.
Append-only aggregation (insert):
Supports all aggregates, including exact distinct, percentiles
Harder to handle late data
Incremental aggregation (upsert):
Supports late data
Cannot handle all aggregates (though can approximate using HLL, TopN)
Append-only vs. Incremental
21
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Aggregate events for a particular time period and append them to the rollup
table, once all the data for the period is available.
INSERT INTO rollup
SELECT period, dimensions, aggregates
FROM events
WHERE event_time::date = '2018-09-04'
GROUP BY period, dimensions;
Should keep track of which periods have been aggregated.
Append-only Aggregation
22
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Aggregate new events and upsert into rollup table.
INSERT INTO rollup
SELECT period, dimensions, aggregates
FROM events
WHERE event_id BETWEEN s AND e
GROUP BY period, dimensions
ON CONFLICT (dimensions, period) DO UPDATE
SET aggregates = aggregates + EXCLUDED.aggregates;
Need to be able to incrementally build aggregates.
Need to keep track of which events have been aggregated.
Incremental Aggregation
23
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Incremental aggregation
Technique for incremental aggregation using a sequence number shown on
the Citus Data blog.
Incrementally approximate distinct count:
HyperLogLog extension
Incrementally approximate top N:
TopN extension
24
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
CREATE EXTENSION Citus
Scaling out your analytics pipeline
25
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Citus is an open source extension to Postgres (9.6, 10, 11) for transparently
distributing tables across many Postgres servers.
CREATE EXTENSION citus
26
Coordinator
create_distributed_table('events', 'customer_id');events
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
Multi-tenancy
Tenant ID provides a natural sharding dimension for many applications.
Citus automatically co-locates event and rollup data for the same
SELECT create_distributed_table('events', 'tenant_id');
SELECT create_distributed_table('rollup', 'tenant_id');
Aggregations can be done locally, without network traffic:
INSERT INTO rollup SELECT tenant_id, … FROM events …
Dashboard queries are always for a particular tenant:
SELECT … FROM rollup WHERE tenant_id = 1238 …
27
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
COPY asynchronously scatters rows to different shards
Data loading in Citus
28
Coordinator
COPYevents
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
INSERT … SELECT can be parallelised across shards.
Aggregation in Citus
29
Coordinator
events
create_distributed_table('rollup', 'customer_id');
INSERT INTO rollup
SELECT … FROM events
GROUP BY customer_id, …rollup
INSERT INTO rollup_102182
SELECT … FROM events_102010
GROUP BY …
INSERT INTO rollup_102180
SELECT … FROM events_102008
GROUP BY …
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
SELECT on rollup for a particular customer (from the dashboard) can be
routed to the appropriate shard.
Querying rollups in Citus
30
Coordinator
events SELECT … FROM rollup
WHERE customer_id = 12834 …
…rollup
SELECT … FROM events_102180
WHERE customer_id = 1283 …
…
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
You should use:
• COPY to load raw data into a table
• BRIN index to find new events during aggregation
• Partitioning with pg_partman to expire old data
• Rollup tables built from raw event data
• Append-only aggregation if you need exact percentile/distinct count
• Incremental aggregation if you can have late data
• HLL to incrementally approximate distinct count
• TopN to incrementally approximate heavy hitters
• Citus to scale out
Summary
31
Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018
marco@citusdata.com
Q&A
32

More Related Content

PDF
Satellite Based Positioning.
PPTX
Coordinate system 1st
PPTX
applications of first order non linear partial differential equation
PDF
Map making using qgis
PPSX
Lecture notes - 2Regionalization (2).ppsx
PPTX
Numerical solution of system of linear equations
PPTX
PostGIS and Spatial SQL
PDF
Legendre Function
Satellite Based Positioning.
Coordinate system 1st
applications of first order non linear partial differential equation
Map making using qgis
Lecture notes - 2Regionalization (2).ppsx
Numerical solution of system of linear equations
PostGIS and Spatial SQL
Legendre Function

Similar to Real time analytics at any scale | PostgreSQL User Group NL | Marco Slot (20)

PDF
Distributing Queries the Citus Way | PostgresConf US 2018 | Marco Slot
PDF
Using Postgres and Citus for Lightning Fast Analytics, also ft. Rollups | Liv...
PDF
Horizontally Scalable Relational Databases with Spark: Spark Summit East talk...
PDF
Building a Complex, Real-Time Data Management Application
PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
PDF
Why PostgreSQL for Analytics Infrastructure (DW)?
PDF
Acunu Analytics: Simpler Real-Time Cassandra Apps
PDF
Advanced Postgres Monitoring
PDF
Distributed Computing on PostgreSQL | PGConf EU 2017 | Marco Slot
PDF
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PDF
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
PDF
Performance improvements in PostgreSQL 9.5 and beyond
PDF
PostgreSQL 9.4 and Beyond @ FOSSASIA 2015 Singapore
PDF
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PDF
Pg 95 new capabilities
PPTX
Using the PostgreSQL Extension Ecosystem for Advanced Analytics
PDF
Really Big Elephants: PostgreSQL DW
PPTX
Oracle to Azure PostgreSQL database migration webinar
PPTX
PostgreSQL as an Alternative to MSSQL
PPTX
Modern sql
Distributing Queries the Citus Way | PostgresConf US 2018 | Marco Slot
Using Postgres and Citus for Lightning Fast Analytics, also ft. Rollups | Liv...
Horizontally Scalable Relational Databases with Spark: Spark Summit East talk...
Building a Complex, Real-Time Data Management Application
10 Reasons to Start Your Analytics Project with PostgreSQL
Why PostgreSQL for Analytics Infrastructure (DW)?
Acunu Analytics: Simpler Real-Time Cassandra Apps
Advanced Postgres Monitoring
Distributed Computing on PostgreSQL | PGConf EU 2017 | Marco Slot
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
Performance improvements in PostgreSQL 9.5 and beyond
PostgreSQL 9.4 and Beyond @ FOSSASIA 2015 Singapore
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
Pg 95 new capabilities
Using the PostgreSQL Extension Ecosystem for Advanced Analytics
Really Big Elephants: PostgreSQL DW
Oracle to Azure PostgreSQL database migration webinar
PostgreSQL as an Alternative to MSSQL
Modern sql
Ad

More from Citus Data (20)

PDF
Architecting peta-byte-scale analytics by scaling out Postgres on Azure with ...
PDF
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
PDF
JSONB Tricks: Operators, Indexes, and When (Not) to Use It | PostgresOpen 201...
PDF
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
PDF
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
PDF
When it all goes wrong | PGConf EU 2019 | Will Leinweber
PDF
Amazing SQL your ORM can (or can't) do | PGConf EU 2019 | Louise Grandjonc
PDF
What Microsoft is doing with Postgres & the Citus Data acquisition | PGConf E...
PDF
Deep Postgres Extensions in Rust | PGCon 2019 | Jeff Davis
PDF
Why Postgres Why This Database Why Now | SF Bay Area Postgres Meetup | Claire...
PDF
A story on Postgres index types | PostgresLondon 2019 | Louise Grandjonc
PDF
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
PDF
The Art of PostgreSQL | PostgreSQL Ukraine | Dimitri Fontaine
PDF
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
PDF
When it all goes wrong (with Postgres) | RailsConf 2019 | Will Leinweber
PDF
The Art of PostgreSQL | PostgreSQL Ukraine Meetup | Dimitri Fontaine
PDF
How to write SQL queries | pgDay Paris 2019 | Dimitri Fontaine
PDF
When it all Goes Wrong |Nordic PGDay 2019 | Will Leinweber
PDF
Why PostgreSQL Why This Database Why Now | Nordic PGDay 2019 | Claire Giordano
PDF
Scaling Multi-Tenant Applications Using the Django ORM & Postgres | PyCaribbe...
Architecting peta-byte-scale analytics by scaling out Postgres on Azure with ...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
JSONB Tricks: Operators, Indexes, and When (Not) to Use It | PostgresOpen 201...
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
When it all goes wrong | PGConf EU 2019 | Will Leinweber
Amazing SQL your ORM can (or can't) do | PGConf EU 2019 | Louise Grandjonc
What Microsoft is doing with Postgres & the Citus Data acquisition | PGConf E...
Deep Postgres Extensions in Rust | PGCon 2019 | Jeff Davis
Why Postgres Why This Database Why Now | SF Bay Area Postgres Meetup | Claire...
A story on Postgres index types | PostgresLondon 2019 | Louise Grandjonc
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
The Art of PostgreSQL | PostgreSQL Ukraine | Dimitri Fontaine
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
When it all goes wrong (with Postgres) | RailsConf 2019 | Will Leinweber
The Art of PostgreSQL | PostgreSQL Ukraine Meetup | Dimitri Fontaine
How to write SQL queries | pgDay Paris 2019 | Dimitri Fontaine
When it all Goes Wrong |Nordic PGDay 2019 | Will Leinweber
Why PostgreSQL Why This Database Why Now | Nordic PGDay 2019 | Claire Giordano
Scaling Multi-Tenant Applications Using the Django ORM & Postgres | PyCaribbe...
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx

Real time analytics at any scale | PostgreSQL User Group NL | Marco Slot

  • 1. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Real-time Analytics on PostgreSQL at any Scale Marco Slot <marco@citusdata.com>
  • 2. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 You offer a product or service (e.g. SaaS, IoT platform, network telemetry, …) that generates large volumes of time series data. How to build an analytical dashboard for your customers that: • Supports a large number of concurrent users • Reflects new data within minutes • Has subsecond response times • Supports advanced analytics What is real-time analytics? 2 (Heap Analytics)
  • 3. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Pipeline of Collect - Aggregate - Query: Real-time analytics architecture 3 Event source Event source Event source Event source Storage (Database) Aggregate Rollups (Database) Dashboard (App) Collect Queries
  • 4. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Pipeline of Collect - Aggregate - Query: Real-time analytics architecture 4 Event source Event source Event source Event source Storage (Database) Aggregate Rollups (Database) Dashboard (App) Collect Queries Postgres/Citus
  • 5. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Define a table for storing raw events: CREATE TABLE events ( event_id bigserial, event_time timestamptz default now(), customer_id bigint, event_type text, … event_details jsonb ); Raw data table 5
  • 6. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 COPY is by far the fastest way of loading data. COPY events (customer_id, event_time, … ) FROM STDIN; A few parallel COPY streams can load hundreds of thousands of events per second! Load data 6
  • 7. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 To achieve fast data loading: • Use COPY • Don’t use indexes To achieve fast reading of new events for aggregation: • Use an index Fast data loading 7
  • 8. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 To achieve fast data loading: • Use COPY • Don’t use large indexes To achieve fast reading of new events for aggregation: • Use an index Block-range index is suitable for ordered columns: CREATE INDEX event_time_idx ON events USING BRIN (event_time); Fast data loading 8
  • 9. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Pre-computed aggregates for a period and set of (group by) dimensions. Can be further filtered and aggregated to generate charts. What is a rollup? 9 Period Customer Country Site Hit Count SELECT…
  • 10. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Append new data to a raw events table (avoid indexes!): COPY events FROM ... Periodically aggregate events into rollup table (index away!): INSERT INTO rollup SELECT … FROM events … GROUP BY … Application queries the rollup table: SELECT … FROM rollup WHERE customer_id = 1238 … Postgres recipe for real-time analytics 10
  • 11. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Keep your data sorted into buckets Partitioning 11
  • 12. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Partitioning keeps indexes small by dividing tables into partitions: Benefits: • Avoid fragmentation • Smaller indexes • Partition pruning for queries that filter by partition column • Drop old data quickly, without bloat/fragmentation Partitioning 12 COPY COPY
  • 13. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Defining a partitioned table: CREATE TABLE events (...) PARTITION BY (event_time); Setting up hourly partitioning with pg_partman: SELECT partman.create_parent('public.events', 'event_time', 'native', 'hourly'); https://www.citusdata.com/blog/2018/01/24/citus-and-pg-partman-creating-a-sca lable-time-series-database-on-PostgreSQL/ CREATE EXTENSION pg_partman 13
  • 14. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 If you’re using partitioning, pg_partman can drop old partitions: UPDATE partman.part_config SET retention_keep_table = false, retention = '1 month' WHERE parent_table = 'public.events'; Periodically run maintenance: SELECT partman.run_maintenance(); Expiring old data in a partitioned table 14
  • 15. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Run pg_partman maintenance every hour using pg_cron: SELECT cron.schedule('3 * * * *', $$ SELECT partman.run_maintenance() $$); https://github.com/citusdata/pg_cron Periodic partitioning maintenance 15
  • 16. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 High vs. Low Cardinality Designing Rollup Tables 16
  • 17. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Define rollup tables containing aggregates: CREATE TABLE rollup_by_period_and_dimensions ( <period> <dimensions> <aggregates> primary key (<dimensions>,<period>) ); Primary key index covers many queries, can also add additional indices: CREATE INDEX usc_idx ON rollup (customer_id, site_id); Rollup table 17
  • 18. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Two rollups are smaller than one: A*B + A*C < A*B*C But… up to 2x more aggregation work. Choosing granularity and dimensions 18 Time Customer Country Aggregates Time Customer Site Aggregates Time Customer Country Site Aggregates ~100 rows per period/customer ~20 rows per period/customer ~20*100=2000 rows per period/customer
  • 19. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Find balance between query performance and table management. 1. Identify dimensions, metrics (aggregates) 2. Try rollup with all dimensions: 3. Test compression/performance (goal is >5x smaller) 4. If too slow / too big, split rollup table based on query patterns 5. Go to 3 Usually ends up with 5-10 rollup tables Guidelines for designing rollups 19
  • 20. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Append-only vs. Incremental Running Aggregations 20
  • 21. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Use INSERT INTO rollup SELECT … FROM events … to populate rollup table. Append-only aggregation (insert): Supports all aggregates, including exact distinct, percentiles Harder to handle late data Incremental aggregation (upsert): Supports late data Cannot handle all aggregates (though can approximate using HLL, TopN) Append-only vs. Incremental 21
  • 22. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Aggregate events for a particular time period and append them to the rollup table, once all the data for the period is available. INSERT INTO rollup SELECT period, dimensions, aggregates FROM events WHERE event_time::date = '2018-09-04' GROUP BY period, dimensions; Should keep track of which periods have been aggregated. Append-only Aggregation 22
  • 23. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Aggregate new events and upsert into rollup table. INSERT INTO rollup SELECT period, dimensions, aggregates FROM events WHERE event_id BETWEEN s AND e GROUP BY period, dimensions ON CONFLICT (dimensions, period) DO UPDATE SET aggregates = aggregates + EXCLUDED.aggregates; Need to be able to incrementally build aggregates. Need to keep track of which events have been aggregated. Incremental Aggregation 23
  • 24. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Incremental aggregation Technique for incremental aggregation using a sequence number shown on the Citus Data blog. Incrementally approximate distinct count: HyperLogLog extension Incrementally approximate top N: TopN extension 24
  • 25. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 CREATE EXTENSION Citus Scaling out your analytics pipeline 25
  • 26. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Citus is an open source extension to Postgres (9.6, 10, 11) for transparently distributing tables across many Postgres servers. CREATE EXTENSION citus 26 Coordinator create_distributed_table('events', 'customer_id');events
  • 27. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 Multi-tenancy Tenant ID provides a natural sharding dimension for many applications. Citus automatically co-locates event and rollup data for the same SELECT create_distributed_table('events', 'tenant_id'); SELECT create_distributed_table('rollup', 'tenant_id'); Aggregations can be done locally, without network traffic: INSERT INTO rollup SELECT tenant_id, … FROM events … Dashboard queries are always for a particular tenant: SELECT … FROM rollup WHERE tenant_id = 1238 … 27
  • 28. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 COPY asynchronously scatters rows to different shards Data loading in Citus 28 Coordinator COPYevents
  • 29. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 INSERT … SELECT can be parallelised across shards. Aggregation in Citus 29 Coordinator events create_distributed_table('rollup', 'customer_id'); INSERT INTO rollup SELECT … FROM events GROUP BY customer_id, …rollup INSERT INTO rollup_102182 SELECT … FROM events_102010 GROUP BY … INSERT INTO rollup_102180 SELECT … FROM events_102008 GROUP BY …
  • 30. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 SELECT on rollup for a particular customer (from the dashboard) can be routed to the appropriate shard. Querying rollups in Citus 30 Coordinator events SELECT … FROM rollup WHERE customer_id = 12834 … …rollup SELECT … FROM events_102180 WHERE customer_id = 1283 … …
  • 31. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 You should use: • COPY to load raw data into a table • BRIN index to find new events during aggregation • Partitioning with pg_partman to expire old data • Rollup tables built from raw event data • Append-only aggregation if you need exact percentile/distinct count • Incremental aggregation if you can have late data • HLL to incrementally approximate distinct count • TopN to incrementally approximate heavy hitters • Citus to scale out Summary 31
  • 32. Marco Slot | Citus Data | PostgreSQL Meetup Amsterdam: November 2018 marco@citusdata.com Q&A 32