Query Execution Techniques in PostgreSQL
Neil Conway <nconway@truviso.com>
Truviso, Inc.
October 20, 2007
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 1 / 42
Query Execution Techniques in PostgreSQL Neil Conway - - PowerPoint PPT Presentation
Query Execution Techniques in PostgreSQL Neil Conway <nconway@truviso.com> Truviso, Inc. October 20, 2007 Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 1 / 42 Introduction Goals Describe how Postgres works
Truviso, Inc.
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 1 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 2 / 42
1 The big picture: the roles of the planner and executor 2 Plan trees and the Iterator model 3 Scan evaluation: table, index, and bitmap scans 4 Join evaluation: nested loops, sort-merge join, and hash join 5 Aggregate evaluation: grouping via sorting, grouping via hashing 6 Reading EXPLAIN Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 2 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 3 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 4 / 42
1 Enumerate the set of plans for a given query 2 Estimate the cost of executing a given query plan Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 4 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 5 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 6 / 42
1 Scans: heap & index scans, function scans, subquery scans, . . . 2 Joins 3 Grouping, aggregation and HAVING 4 Sorting (ORDER BY) 5 Set operations 6 Projection (apply target list expressions)
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 7 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 8 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 9 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 9 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 10 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 10 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 11 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 12 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 12 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 13 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 14 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 15 / 42
1 For each relevant index on the target table:
2 Combine bitmaps with bitwise AND or OR, as appropriate 3 Use the bitmap to scan the heap in order
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 16 / 42
1 Materialize the Cartesian product of R and S
2 Take the subset that matches the join key
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 17 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 18 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 19 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 19 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 20 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 20 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 21 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 22 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 22 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 23 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 24 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 25 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 25 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 26 / 42
1 Read the first tuple from each input run into an in-memory heap 2 Repeatedly push the smallest tuple in the heap to the output run;
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 26 / 42
1 Read the first tuple from each input run into an in-memory heap 2 Repeatedly push the smallest tuple in the heap to the output run;
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 26 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 27 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 27 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 27 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 28 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 28 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 29 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 29 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 30 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 30 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 31 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 31 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 32 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 32 / 42
1 Form groups (“map”)
2 Evaluate aggregate functions for each group (“reduce”)
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 33 / 42
1 Form groups (“map”)
2 Evaluate aggregate functions for each group (“reduce”)
1 s = initcond 2 For each value vi in the group:
3 final = ffunc(s) Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 33 / 42
1 Take the inputs in order of the grouping key
2 For each group, compute aggregates over it and emit the result
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 34 / 42
1 Create a hash table with one bucket per group 2 For each input row:
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 35 / 42
1 Create a hash table with one bucket per group 2 For each input row:
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 35 / 42
1 Duplicate elimination (unless ALL is specified) 2 Perform set operation itself: UNION, INTERSECT, EXCEPT Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 36 / 42
1 Duplicate elimination (unless ALL is specified) 2 Perform set operation itself: UNION, INTERSECT, EXCEPT
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 36 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 37 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 38 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 39 / 42
Merge Join (cost=18291.23..21426.96 rows=3231 width=14) (actual time=14.024..212.427 rows=225 loops=1) Merge Cond: (t1.tag_id = t2.tag_id)
Index Scan using t1_pkey_idx on t1 (cost=0.00..2855.74 rows=92728 width=14) (actual time=0.041..115.231 rows=54170 loops=1)
Sort (cost=18291.23..18299.31 rows=3231 width=8) (actual time=13.967..14.289 rows=225 loops=1) Sort Key: t2.tag_id Sort Method: quicksort Memory: 26kB
Bitmap Heap Scan on t2 (cost=5659.07..18102.90 rows=3231 width=8) (actual time=12.731..13.493 rows=225 loops=1) Recheck Cond: ((field1 = ANY (’{5, 10, 15, ...}’::integer[])) AND (is_deleted IS NULL))
Bitmap Index Scan on t2_field1_idx (cost=0.00..5658.26 rows=3231 width=0) (actual time=12.686..12.686 rows=225 loops=1) Index Cond: (field1 = ANY (’{5, 10, 15, ...}’::integer[])) Total runtime: 212.939 ms
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 40 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 41 / 42
Neil Conway (Truviso) Query Execution in PostgreSQL October 20, 2007 42 / 42