SlideShare a Scribd company logo
Unleashing
PostgreSQL Performance
Andres Freund
PostgreSQL Developer & Committer
Email: andres@anarazel.de
Email: andres.freund@enterprisedb.com
Twitter: @AndresFreundTec
anarazel.de/talks/2018-06-06-pgvision-performance/performance.pdf
PostgreSQL [Adoption] Bottlenecks
andres@alap4:~$ psql tpch_1
psql (11beta1)
Type "help" for help.
tpch_1[10841][1]=# quit
andres@alap4:~$
Auto-Failover in PostgreSQL
-
How?
Bloat: zheap
Parallelism
Partitioning
Postgres Vision 2018: Making Postgres Even Faster
Efficiency
Analytics / Transactional Workloads
SELECT
l_returnflag,
l_linestatus,
sum(l_quantity) AS sum_qty,
sum(l_extendedprice) AS sum_base_price,
sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price,
sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge,
avg(l_quantity) AS avg_qty,
avg(l_extendedprice) AS avg_price,
avg(l_discount) AS avg_disc,
count(*) AS count_order
FROM lineitem
WHERE l_shipdate <= date '1998-12-01' - interval '74 days'
GROUP BY l_returnflag, l_linestatus
ORDER BY l_returnflag, l_linestatus;
New Hash-Table & Expression Evaluation
Engine Rewrite
Sort (cost=4313533.34..4313533.36 rows=6 width=68)
Sort Key: l_returnflag, l_linestatus
-> HashAggregate (cost=4313533.16..4313533.26 rows=6 width=68)
Group Key: l_returnflag, l_linestatus
Output: …, sum(l_quantity), sum(l_extendedprice), sum(…), ...
-> Seq Scan on lineitem (cost=0.00..1936427.80 rows=59427634 width=36)
Filter: (l_shipdate <= '1998-09-18 00:00:00'::timestamp without time zone)
New Hash-Table & Expression Evaluation
Engine Rewrite
ExecMakeFunctionResultNoSets
ExecEvalFuncArgs
ExecEvalConstExecEvalScalarVarFast
ExecEvalAnd
ExecMakeFunctionResultNoSets
ExecEvalFuncArgs
ExecEvalConstExecEvalScalarVarFast
WHERE a.col < 10 AND a.another = 3
< v10 Expression Evaluation Engine
v10+ Expression Evaluation Engine
●
WHERE a.col < 10 AND a.another = 3
– EEOP_SCAN_FETCHSOME (deform necessary cols)
– EEOP_SCAN_VAR (a.col)
– EEOP_CONST (10)
– EEOP_FUNCEXPR_STRICT (int4lt)
– EEOP_BOOL_AND_STEP_FIRST
– EEOP_SCAN_VAR (a.another)
– EEOP_CONST (3)
– EEOP_FUNCEXPR_STRICT (int4eq)
– EEOP_BOOL_AND_STEP_LAST (AND)
●
direct threaded
●
lots of indirect jumps
PG 9.5 PG 9.6 PG 10
0
5
10
15
20
25
30
35
40
45
TPCH Q-01
scale 5, fully cached
#Processes=1
#Processes=4
Version
TimeinSeconds
v10+ Expression Evaluation Engine
Just-in-Time Compilation
●
Interpreted execution → compiled execution
– removes interpretation overhead
●
Make variables constant
– removes branches, indirect jumps, reduces size
●
Inline called functions
●
PG 11: Optionally uses LLVM
SELECT
l_returnflag,
l_linestatus,
sum(l_quantity) AS sum_qty,
sum(l_extendedprice) AS sum_base_price,
sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price,
sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge,
avg(l_quantity) AS avg_qty,
avg(l_extendedprice) AS avg_price,
avg(l_discount) AS avg_disc,
count(*) AS count_order
FROM lineitem
WHERE l_shipdate <= date '1998-12-01' - interval '74 days'
GROUP BY l_returnflag, l_linestatus
ORDER BY l_returnflag, l_linestatus;
Just-in-Time Compilation in v11
Just-in-Time Compilation in v11
0 1 2 4 8 16
0
200000
400000
600000
800000
1000000
1200000
TPCH Q01 timing
scale 100, fully cached
PG 9.6
PG 10
master jit=0
master jit=1
parallelism
timeinms
JIT - Good Cases / Bad Cases
●
OLTP → bad, short query → bad
●
OLAP → good, long query → good
●
IO bound → not necessarily good
●
CPU bound → likely good
●
lots of aggregates → good
●
wide tables (i.e. lots of columns) → good
JIT – Planning
●
Naive!
●
Disabled / Enabled: jit = on / off (currently enabled by default)
●
SELECT pg_jit_available();
●
Perform JIT if query_cost > jit_above_cost
– default: 100000
●
Optimize if query_cost > jit_optimize_above_cost
– default: 500000
●
Inline if query_cost > jit_inline_above_cost
– default: 500000
●
-1 disables corresponding feature
●
Whole query decision
JIT – Planning
Sort (cost=4311583.16..4311583.18 rows=6 width=68) (actual time=[]18270.844 rows=4 loops=1)
Sort Key: l_returnflag, l_linestatus
Sort Method: quicksort Memory: 25kB
-> HashAggregate (...)
Group Key: l_returnflag, l_linestatus
-> Seq Scan on lineitem (...)
Filter: (l_shipdate <= '1998-09-18 00:00:00'::timestamp without time zone)
Rows Removed by Filter: 571965
Planning Time: 1.956 ms
JIT:
Functions: 9
Generation Time: 4.087 ms
Inlining: true
Inlining Time: 62.990 ms
Optimization: true
Optimization Time: 121.307 ms
Emission Time: 102.416 ms
Execution Time: 18291.397 ms
JIT – how to test
●
Debian:
– apt.postgresql.org has v11 beta 1 packages
– https://wiki.postgresql.org/wiki/Apt/
FAQ#I_want_to_try_the_beta_version_of_the_next_PostgreSQL_releas
e
– Install postgresql-11
●
RHEL:
– yum.postgresql.org has v11 beta 1 packages
– https://yum.postgresql.org/repopackages.php#pg11
– install postgresql11-server postgresql11-llvmjit
●
depends on EPEL
JIT Improvements: Code Generation
●
Expressions refer to per-query allocated memory
– generated code references memory locations
– lots of superflous memory reads/writes for arguments, optimizer can’t eliminate in most cases
●
massively reduces benefits of inlining
– optimizer can’t optimize away memory lots of memory references
– FIX: separate permanent and per eval memory
●
Expression step results refer to persistent memory
– move to temporary memory
●
Function Call Interface references persistent memory
– FIX: pass FunctionCallInfoData and FmgrInfo separately to functions
●
remove FunctionCallInfoData->flinfo
●
move context, resultinfo, fncollation to FmgrInfo
●
move isnull field to separate argument? Return struct?
– Non JITed expression evaluation will benefit too
JIT Improvements: Caching
●
Optimizer overhead significant
– TPCH Q01: unopt, noinline: time to optimize: 0.002s, emit: 0.036s
– TPCH Q01: time to inline: 0.080s, optimize: 0.163s, emit 0.082s
●
references to memory locations prevent caching (i.e. expression
codegen has to be optimized first)
●
Introduce per-backend LRU cache of functions keyed by hash of
emitted LRU (plus comparator)
– What to use as cache key?
●
IR? - requires generating it
●
Expression Trees?
●
Prepared Statement?
●
Shared / Non-Shared / Persistent?
●
relatively easy task, once pointers removed
JIT Improvements: Incremental JITing
JIT JITed Query Execution
Query Execution
JIT JITed Query Execution
JIT
Query Execution
JITed Query Execution
JITed Query Execution
JIT Improvements: Planning
●
Whole Query decision too coarse
– use estimates about total number of each function evaluation?
●
JIT more aggressively when using prepared statements?
– after repeated executions?
Future things to JIT
●
Executor control flow
– hard, but lots of other benefits (asynchronous execution, non-JITed will
be faster, less memory)
●
COPY parsing, input / output function invocation
– easy – medium
●
Aggregate & Hashjoin hash computation
– easy
●
in-memory tuplesort
– including tuple deforming (from MinimalTuple)
– easy
OLTP Workloads
PG 9.6 PG 10 PG 11
0
50000
100000
150000
200000
250000
pgbench readonly, scale 100 (on laptop)
1 client
16 client
version
tps
OLTP Workloads: Bottlenecks in v11
- 97.68% PostgresMain
- 32.09% exec_execute_message (inlined)
- 95.13% PortalRun
- 92.08% PortalRunSelect
- 98.38% standard_ExecutorRun
+ 98.23% ExecutePlan (inlined)
+ 0.86% printtup_startup
- 31.90% exec_bind_message (inlined)
- 53.38% PortalStart
- 86.96% standard_ExecutorStart
+ 96.43% InitPlan (inlined)
+ 3.29% CreateExecutorState
+ 10.56% GetCachedPlan
+ 8.14% start_xact_command (inlined)
+ 13.06% finish_xact_command
+ 10.31% ReadCommand (inlined)
OLTP Workloads: Bottlenecks in v11
●
Too much work done every query execution
– move to planner, helping prepared statements
– reduce duplicated work
●
Execution uses too much memory / too many small allocations
– rework executor data structures
– batch all allocations into one single memory allocation
Limit
Sort
Join
SeqScan Agg
Join
IdxScan IdxScan
Current Executor
Union All
FDWFDWFDWFDWFDWFDW Join
FDW FDW
Async Execution
Async IO
Join
HashJoin
... Hash
SeqScan
HashJoin
... Hash
SeqScan
JIT
static pg_attribute_always_inline
TupleTableSlot *
ExecHashJoinImpl(PlanState *pstate, bool
parallel)
{
...
for (;;)
{
/* lotsa code */
…
slot =
ExecProcNode(outerNode);
…
Linearized Programs
SELECT
l_returnflag,
l_linestatus,
sum(l_quantity) AS sum_qty,
sum(l_extendedprice) AS sum_base_price,
sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price,
sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge,
avg(l_quantity) AS avg_qty,
avg(l_extendedprice) AS avg_price,
avg(l_discount) AS avg_disc,
count(*) AS count_order
FROM
lineitem
WHERE
l_shipdate <= date '1998-12-01' - interval '74 days'
GROUP BY
l_returnflag,
l_linestatus
ORDER BY
l_returnflag,
l_linestatus;
0: init_sort
1: seqscan_first
2: seqscan [j empty 5] > s0
3: qual [j fail 2] < scan s0
4: hashagg_tuple [j 2] < s0
5: drain_hashagg [j empty 7]
> s1
6: sort_tuple [j 5] < s1
7: sort
8: drain_sort [j empty 10] > s2
9: return < s2 [next 8]
10: done

More Related Content

PDF
Postgres vision 2018: The Promise of zheap
 
PDF
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
PDF
Shenandoah GC: Java Without The Garbage Collection Hiccups (Christine Flood)
PDF
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PDF
Troubleshooting PostgreSQL Streaming Replication
PDF
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
PDF
Advanced Postgres Monitoring
PDF
Pgcenter overview
Postgres vision 2018: The Promise of zheap
 
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
Shenandoah GC: Java Without The Garbage Collection Hiccups (Christine Flood)
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Troubleshooting PostgreSQL Streaming Replication
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
Advanced Postgres Monitoring
Pgcenter overview

What's hot (20)

PDF
collectd & PostgreSQL
PDF
InfluxDB IOx Tech Talks: Intro to the InfluxDB IOx Read Buffer - A Read-Optim...
PDF
Deep dive into PostgreSQL statistics.
PDF
In-memory OLTP storage with persistence and transaction support
PDF
Deep dive into PostgreSQL statistics.
PDF
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
PDF
Linux tuning for PostgreSQL at Secon 2015
PDF
Troubleshooting PostgreSQL with pgCenter
PDF
Deep dive into PostgreSQL statistics.
PDF
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
PPT
Galvin-operating System(Ch10)
PDF
InfluxDB IOx Tech Talks: Query Processing in InfluxDB IOx
PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
PDF
Obtaining the Perfect Smoke By Monitoring Your BBQ with InfluxDB and Telegraf
PPTX
Nov. 4, 2011 o reilly webcast-hbase- lars george
PDF
In-core compression: how to shrink your database size in several times
PDF
PostgreSQL Meetup Berlin at Zalando HQ
PDF
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
PDF
Managing PostgreSQL with PgCenter
PDF
GitLab PostgresMortem: Lessons Learned
collectd & PostgreSQL
InfluxDB IOx Tech Talks: Intro to the InfluxDB IOx Read Buffer - A Read-Optim...
Deep dive into PostgreSQL statistics.
In-memory OLTP storage with persistence and transaction support
Deep dive into PostgreSQL statistics.
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
Linux tuning for PostgreSQL at Secon 2015
Troubleshooting PostgreSQL with pgCenter
Deep dive into PostgreSQL statistics.
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
Galvin-operating System(Ch10)
InfluxDB IOx Tech Talks: Query Processing in InfluxDB IOx
10 Reasons to Start Your Analytics Project with PostgreSQL
Obtaining the Perfect Smoke By Monitoring Your BBQ with InfluxDB and Telegraf
Nov. 4, 2011 o reilly webcast-hbase- lars george
In-core compression: how to shrink your database size in several times
PostgreSQL Meetup Berlin at Zalando HQ
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
Managing PostgreSQL with PgCenter
GitLab PostgresMortem: Lessons Learned
Ad

Similar to Postgres Vision 2018: Making Postgres Even Faster (20)

PDF
Performance improvements in PostgreSQL 9.5 and beyond
PDF
query_tuning.pdf
PDF
Postgres Performance for Humans
ODP
Basic Query Tuning Primer - Pg West 2009
ODP
Basic Query Tuning Primer
PPTX
Make streaming processing towards ANSI SQL
PDF
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
PDF
Major features postgres 11
 
PPTX
Building scalable application with sql server
PDF
Postgres can do THAT?
PDF
PostgreSQL performance improvements in 9.5 and 9.6
PDF
Postgres performance for humans
PDF
PostgreSQL 9.6 Performance-Scalability Improvements
PDF
Same plan different performance
PDF
PostgreSQL 9.5 - Major Features
PDF
Postgres 9.4 First Look
PDF
8.4 Upcoming Features
PDF
query-optimization-techniques_talk.pdf
PDF
Plpgsql russia-pgconf
PDF
Does PostgreSQL respond to the challenge of analytical queries?
Performance improvements in PostgreSQL 9.5 and beyond
query_tuning.pdf
Postgres Performance for Humans
Basic Query Tuning Primer - Pg West 2009
Basic Query Tuning Primer
Make streaming processing towards ANSI SQL
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
Major features postgres 11
 
Building scalable application with sql server
Postgres can do THAT?
PostgreSQL performance improvements in 9.5 and 9.6
Postgres performance for humans
PostgreSQL 9.6 Performance-Scalability Improvements
Same plan different performance
PostgreSQL 9.5 - Major Features
Postgres 9.4 First Look
8.4 Upcoming Features
query-optimization-techniques_talk.pdf
Plpgsql russia-pgconf
Does PostgreSQL respond to the challenge of analytical queries?
Ad

More from EDB (20)

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

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Transforming Manufacturing operations through Intelligent Integrations
PPT
Teaching material agriculture food technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Advanced Soft Computing BINUS July 2025.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
GamePlan Trading System Review: Professional Trader's Honest Take
Spectral efficient network and resource selection model in 5G networks
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
Transforming Manufacturing operations through Intelligent Integrations
Teaching material agriculture food technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Understanding_Digital_Forensics_Presentation.pptx
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
NewMind AI Monthly Chronicles - July 2025
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
madgavkar20181017ppt McKinsey Presentation.pdf
NewMind AI Weekly Chronicles - August'25 Week I
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf

Postgres Vision 2018: Making Postgres Even Faster

  • 1. Unleashing PostgreSQL Performance Andres Freund PostgreSQL Developer & Committer Email: [email protected] Email: [email protected] Twitter: @AndresFreundTec anarazel.de/talks/2018-06-06-pgvision-performance/performance.pdf
  • 3. andres@alap4:~$ psql tpch_1 psql (11beta1) Type "help" for help. tpch_1[10841][1]=# quit andres@alap4:~$
  • 10. SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= date '1998-12-01' - interval '74 days' GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag, l_linestatus; New Hash-Table & Expression Evaluation Engine Rewrite
  • 11. Sort (cost=4313533.34..4313533.36 rows=6 width=68) Sort Key: l_returnflag, l_linestatus -> HashAggregate (cost=4313533.16..4313533.26 rows=6 width=68) Group Key: l_returnflag, l_linestatus Output: …, sum(l_quantity), sum(l_extendedprice), sum(…), ... -> Seq Scan on lineitem (cost=0.00..1936427.80 rows=59427634 width=36) Filter: (l_shipdate <= '1998-09-18 00:00:00'::timestamp without time zone) New Hash-Table & Expression Evaluation Engine Rewrite
  • 13. v10+ Expression Evaluation Engine ● WHERE a.col < 10 AND a.another = 3 – EEOP_SCAN_FETCHSOME (deform necessary cols) – EEOP_SCAN_VAR (a.col) – EEOP_CONST (10) – EEOP_FUNCEXPR_STRICT (int4lt) – EEOP_BOOL_AND_STEP_FIRST – EEOP_SCAN_VAR (a.another) – EEOP_CONST (3) – EEOP_FUNCEXPR_STRICT (int4eq) – EEOP_BOOL_AND_STEP_LAST (AND) ● direct threaded ● lots of indirect jumps
  • 14. PG 9.5 PG 9.6 PG 10 0 5 10 15 20 25 30 35 40 45 TPCH Q-01 scale 5, fully cached #Processes=1 #Processes=4 Version TimeinSeconds v10+ Expression Evaluation Engine
  • 15. Just-in-Time Compilation ● Interpreted execution → compiled execution – removes interpretation overhead ● Make variables constant – removes branches, indirect jumps, reduces size ● Inline called functions ● PG 11: Optionally uses LLVM
  • 16. SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= date '1998-12-01' - interval '74 days' GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag, l_linestatus; Just-in-Time Compilation in v11
  • 17. Just-in-Time Compilation in v11 0 1 2 4 8 16 0 200000 400000 600000 800000 1000000 1200000 TPCH Q01 timing scale 100, fully cached PG 9.6 PG 10 master jit=0 master jit=1 parallelism timeinms
  • 18. JIT - Good Cases / Bad Cases ● OLTP → bad, short query → bad ● OLAP → good, long query → good ● IO bound → not necessarily good ● CPU bound → likely good ● lots of aggregates → good ● wide tables (i.e. lots of columns) → good
  • 19. JIT – Planning ● Naive! ● Disabled / Enabled: jit = on / off (currently enabled by default) ● SELECT pg_jit_available(); ● Perform JIT if query_cost > jit_above_cost – default: 100000 ● Optimize if query_cost > jit_optimize_above_cost – default: 500000 ● Inline if query_cost > jit_inline_above_cost – default: 500000 ● -1 disables corresponding feature ● Whole query decision
  • 20. JIT – Planning Sort (cost=4311583.16..4311583.18 rows=6 width=68) (actual time=[]18270.844 rows=4 loops=1) Sort Key: l_returnflag, l_linestatus Sort Method: quicksort Memory: 25kB -> HashAggregate (...) Group Key: l_returnflag, l_linestatus -> Seq Scan on lineitem (...) Filter: (l_shipdate <= '1998-09-18 00:00:00'::timestamp without time zone) Rows Removed by Filter: 571965 Planning Time: 1.956 ms JIT: Functions: 9 Generation Time: 4.087 ms Inlining: true Inlining Time: 62.990 ms Optimization: true Optimization Time: 121.307 ms Emission Time: 102.416 ms Execution Time: 18291.397 ms
  • 21. JIT – how to test ● Debian: – apt.postgresql.org has v11 beta 1 packages – https://wiki.postgresql.org/wiki/Apt/ FAQ#I_want_to_try_the_beta_version_of_the_next_PostgreSQL_releas e – Install postgresql-11 ● RHEL: – yum.postgresql.org has v11 beta 1 packages – https://yum.postgresql.org/repopackages.php#pg11 – install postgresql11-server postgresql11-llvmjit ● depends on EPEL
  • 22. JIT Improvements: Code Generation ● Expressions refer to per-query allocated memory – generated code references memory locations – lots of superflous memory reads/writes for arguments, optimizer can’t eliminate in most cases ● massively reduces benefits of inlining – optimizer can’t optimize away memory lots of memory references – FIX: separate permanent and per eval memory ● Expression step results refer to persistent memory – move to temporary memory ● Function Call Interface references persistent memory – FIX: pass FunctionCallInfoData and FmgrInfo separately to functions ● remove FunctionCallInfoData->flinfo ● move context, resultinfo, fncollation to FmgrInfo ● move isnull field to separate argument? Return struct? – Non JITed expression evaluation will benefit too
  • 23. JIT Improvements: Caching ● Optimizer overhead significant – TPCH Q01: unopt, noinline: time to optimize: 0.002s, emit: 0.036s – TPCH Q01: time to inline: 0.080s, optimize: 0.163s, emit 0.082s ● references to memory locations prevent caching (i.e. expression codegen has to be optimized first) ● Introduce per-backend LRU cache of functions keyed by hash of emitted LRU (plus comparator) – What to use as cache key? ● IR? - requires generating it ● Expression Trees? ● Prepared Statement? ● Shared / Non-Shared / Persistent? ● relatively easy task, once pointers removed
  • 24. JIT Improvements: Incremental JITing JIT JITed Query Execution Query Execution JIT JITed Query Execution JIT Query Execution JITed Query Execution JITed Query Execution
  • 25. JIT Improvements: Planning ● Whole Query decision too coarse – use estimates about total number of each function evaluation? ● JIT more aggressively when using prepared statements? – after repeated executions?
  • 26. Future things to JIT ● Executor control flow – hard, but lots of other benefits (asynchronous execution, non-JITed will be faster, less memory) ● COPY parsing, input / output function invocation – easy – medium ● Aggregate & Hashjoin hash computation – easy ● in-memory tuplesort – including tuple deforming (from MinimalTuple) – easy
  • 27. OLTP Workloads PG 9.6 PG 10 PG 11 0 50000 100000 150000 200000 250000 pgbench readonly, scale 100 (on laptop) 1 client 16 client version tps
  • 28. OLTP Workloads: Bottlenecks in v11 - 97.68% PostgresMain - 32.09% exec_execute_message (inlined) - 95.13% PortalRun - 92.08% PortalRunSelect - 98.38% standard_ExecutorRun + 98.23% ExecutePlan (inlined) + 0.86% printtup_startup - 31.90% exec_bind_message (inlined) - 53.38% PortalStart - 86.96% standard_ExecutorStart + 96.43% InitPlan (inlined) + 3.29% CreateExecutorState + 10.56% GetCachedPlan + 8.14% start_xact_command (inlined) + 13.06% finish_xact_command + 10.31% ReadCommand (inlined)
  • 29. OLTP Workloads: Bottlenecks in v11 ● Too much work done every query execution – move to planner, helping prepared statements – reduce duplicated work ● Execution uses too much memory / too many small allocations – rework executor data structures – batch all allocations into one single memory allocation
  • 33. JIT static pg_attribute_always_inline TupleTableSlot * ExecHashJoinImpl(PlanState *pstate, bool parallel) { ... for (;;) { /* lotsa code */ … slot = ExecProcNode(outerNode); …
  • 34. Linearized Programs SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem WHERE l_shipdate <= date '1998-12-01' - interval '74 days' GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag, l_linestatus; 0: init_sort 1: seqscan_first 2: seqscan [j empty 5] > s0 3: qual [j fail 2] < scan s0 4: hashagg_tuple [j 2] < s0 5: drain_hashagg [j empty 7] > s1 6: sort_tuple [j 5] < s1 7: sort 8: drain_sort [j empty 10] > s2 9: return < s2 [next 8] 10: done