Index

Module 01 · Database InternalsOne Database, to the Bolt

Build sort and aggregate with a memory limit

The Query Planner
The Query Planner · Open board

The plan as a tree of steps, with the steps that must see every row before they can answer. Sort and aggregate are two of those steps; this lesson builds them with a limit on how much memory each may use.

Before you start. The plan, running watched Postgres sort and group with work_mem as the limit, and counted temp-file blocks as the sign of a spill. Sorting & Searching has merge sort in memory. This lesson builds the version that goes to disk, so you can see why the numbers in 0579 come out as they do. The code is in teaching/code/database-internals/02-sortagg; it reads Go.

Continue reading with a plan

The first three lessons of every track are free. This lesson is included with a plan, which unlocks every lesson, board, video, lab and paper for one price.

View plans →Already a member? Sign in

Lab

Your query has an index and Postgres refuses to use it. At what fraction of the table does reading all of it in order start to win?

Drag What fraction of the table the query actually matches upward one notch at a time, and watch The plan it picks.

Open The selectivity crossover →

Your sort spills to disk. Give it more memory and it stops. Why does nobody set work_mem to a gigabyte?

Click The default, then A big desk and The whole room, drag Sessions running it at once, and watch The whole box.

Open The work_mem dial →

Related paper

Review Database Internals →

Saved in this browser, and to your account when you sign in.