Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 01 · Database Internals · One Database, to the Bolt · about 21 min to read

Play it as a journeyTutor comes with a plan

The plan, running

The Query Planner
The Query Planner · Open board

The planner’s side of a query: statistics in, one chosen plan out. This lesson starts where that board stops, at the moment the chosen plan begins to run and needs memory to do it.

Before you start. The Query Planner showed the planner: the part of Postgres that reads a summary of your data and bets on one way to answer a query. Database 07: The Query Layer built the other half by hand: a plan is a tree of small steps, and each step pulls rows from the one below it, one row at a time. This lesson is about what those steps do when the rows they must hold do not fit in the memory they were given.

Lesson 11 of 26 in Database Internals

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • August 2014: a patch so one step would stop growing
  • The plan is a tree, and rows flow up it
  • Two kinds of step: ones that pass, ones that must see everything
  • work_mem is an allowance per step, not a budget per server
  • Extra hands: parallel workers
  • A sort that fits, and one that does not
  • The runs, printed by Postgres
  • LIMIT changes the algorithm
  • A hash aggregate, in batches
  • Parallel workers
  • One row at a time, and the other way
  • Finding spill on a live server
  • The one thing

Terms it explains: Hashagg, work_mem, Cardinality, sort, hash aggregate, hash, run, external merge sort, Gather, worker, vectorized.

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 papers

Review Database Internals