what an index actually is
a sorted copy of a few columns with an address back to each row. the back of a textbook, for tables.
- address
- everything.indexes.what_an_index_is
- part
- part 8, indexes, from the outside
- chapter
- chapter 1 of 12
- reading time
- 7 min
an index is a separate, sorted copy of one or a few columns, where every entry also carries the address of its row (the ctid: a page and a slot). to find a value, postgres searches the sorted copy, then follows the address. the table itself stays exactly as it was, in whatever order the rows went in.
the picture
you know the index at the back of a textbook? "saffron, 212". the words are sorted so you can find one fast, and each one tells you where to look. the book's pages aren't reordered to make that work. the index is extra pages at the back, and when the book gets a new edition, somebody has to update them too.
a postgres index is exactly that. here's a real one: the first twelve customers in a table of their own, and an index on name.
- (0,1)1Maya Chen
- (0,2)2Grace Okafor
- (0,3)3Sam Lee
- (0,4)4Zoë Martin
- (0,5)5Zoe Martin
- (0,6)6José Silva
- (0,7)7Ming Li
- (0,8)8Sean O'Neil
- (0,9)9Sam Lee
- (0,10)10Null
- (0,11)11Ana Lima
- (0,12)12Ravi Kumar
11 entries for 12 rows. pick an entry to follow its address.
name next to its table. the index is sorted by name, the table is in the order the rows were inserted, and every entry carries the address of its row. pick an entry to follow it. 11 entries for 12 rows: the two Sam Lees share one entry with two addresses.two things to notice. the lines cross: index order and table order have nothing to do with each other. and nothing in the index is a whole row. it's the key and an address, and to get the city of Ana Lima postgres still goes to (0,11) and reads it from the table.
read it yourself
pageinspect reads index pages raw. here's the same twelve-row index, the way postgres stores it:
CREATE TABLE few AS SELECT id, name, city FROM customers WHERE id <= 12 ORDER BY id;
CREATE INDEX few_name ON few (name);
SELECT itemoffset, ctid, htid, tids, left(data, 23) AS data_start
FROM bt_page_items('few_name', 1)
ORDER BY itemoffset;| itemoffsetint2 | ctidtid | htidtid | tidstid[] | data_starttext |
|---|---|---|---|---|
| 1 | (0,11) | (0,11) | null | 13 41 6e 61 20 4c 69 6d |
| 2 | (0,2) | (0,2) | null | 1b 47 72 61 63 65 20 4f |
| 3 | (0,6) | (0,6) | null | 19 4a 6f 73 c3 a9 20 53 |
| 4 | (0,1) | (0,1) | null | 15 4d 61 79 61 20 43 68 |
| 5 | (0,7) | (0,7) | null | 11 4d 69 6e 67 20 4c 69 |
| 6 | (0,10) | (0,10) | null | 0b 4e 75 6c 6c 00 00 00 |
| 7 | (0,12) | (0,12) | null | 17 52 61 76 69 20 4b 75 |
| 8 | (16,8194) | (0,3) | {"(0,3)","(0,9)"} | 11 53 61 6d 20 4c 65 65 |
| 9 | (0,8) | (0,8) | null | 19 53 65 61 6e 20 4f 27 |
| 10 | (0,5) | (0,5) | null | 17 5a 6f 65 20 4d 61 72 |
| 11 | (0,4) | (0,4) | null | 19 5a 6f c3 ab 20 4d 61 |
htid is the heap address of the row, (page, slot). the Sam Lee entry has a tids array: one key, two addresses (its ctid column isn't an address at all there, which is why pageinspect shows htid too), which is how a b-tree stores duplicates since postgres 13 (deduplication). data is the key itself, as bytes: a length header, then the name's characters (41 6e 61 is "Ana").
and the order: Zoe Martin before Zoë Martin, Null after Ming Li. the browser's database uses the C collation, which sorts by bytes, and ë is two bytes that sort after every plain letter. on a server set up with a language collation, the same index can come out in a different order (collations).
the table doesn't move
CREATE INDEX customers_name ON customers (name);
SELECT * FROM customers LIMIT 3;CREATE INDEX customers_name ON customers (name);
SELECT id, name FROM customers LIMIT 3;
SELECT relname, relkind, pg_relation_size(oid) / 8192 AS pages
FROM pg_class
WHERE relname IN ('customers', 'customers_name')
ORDER BY relname;| idint4 | nametext |
|---|---|
| 1 | Maya Chen |
| 2 | Grace Okafor |
| 3 | Sam Lee |
| relnamename | relkindchar | pagesint8 |
|---|---|---|
| customers | r | 24 |
| customers_name | i | 8 |
Maya, Grace, Sam: storage order, not name order (and this block is marked unordered on purpose, since a LIMIT without ORDER BY promises nothing). two relations, two files. relkind r is a table and i an index; the index on 2,000 names is 8 pages next to the table's 24. (the files behind a table shows where they live on disk.)
why sorted helps
twelve entries fit on one page. 2,000 don't, so the index grows a tree: leaf pages hold the entries in order, and a root page above them holds one separator key per leaf, saying where each leaf starts. a lookup reads the root, picks a leaf, reads the leaf, and has the address.
here's what that buys, for one row:
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM customers WHERE name = 'Ada Lovelace';
CREATE INDEX customers_name ON customers (name);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM customers WHERE name = 'Ada Lovelace';24 pages against 3. on 2,000 rows that's small change. on 20 million rows the seq scan is about 10,000 times bigger, and the index lookup gets a level or two deeper, because each level of the tree multiplies the number of pages it can tell apart by a few hundred. how the tree stays that shallow, splits and all, is inside indexes.
what it costs
the index is a second copy, so it's paid for twice: once in space (8 pages here), and once on every write. an INSERT into customers now also inserts into customers_name; an UPDATE that changes a name writes a new index entry; and the old entries stay until VACUUM cleans them up. what indexes cost measures it.
and a copy only helps queries that can use its order. the next chapters are about which ones those are: when postgres uses an index (few rows, yes; many rows, no) and why isn't my index used? (a function on the column, a cast, a leading wildcard).
keylines
- an index is a sorted copy of a few columns, plus an address (ctid) per row.
- a lookup searches the copy, then follows the address into the table.
- the table never moves. indexes don't sort SELECT * for you.
- a tree of pages keeps the search a few page reads deep, even for millions of rows.
- every index is paid for in space and on every write.