skip to content

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.

index few_name, in key order
table, in storage order
  • (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.

an address (ctid): page, slotthe entry you picked, and its row
real output, postgres 18.6 (PGlite 0.5.12) · index-map
id, name
an index on 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:

sqldb=shop · ext=pageinspect
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;
output, from postgres 18.6
#1 · SELECT 12
#2 · CREATE INDEX
#3 · SELECT 11
itemoffsetint2ctidtidhtidtidtidstid[]data_starttext
1(0,11)(0,11)null13 41 6e 61 20 4c 69 6d
2(0,2)(0,2)null1b 47 72 61 63 65 20 4f
3(0,6)(0,6)null19 4a 6f 73 c3 a9 20 53
4(0,1)(0,1)null15 4d 61 79 61 20 43 68
5(0,7)(0,7)null11 4d 69 6e 67 20 4c 69
6(0,10)(0,10)null0b 4e 75 6c 6c 00 00 00
7(0,12)(0,12)null17 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)null19 53 65 61 6e 20 4f 27
10(0,5)(0,5)null17 5a 6f 65 20 4d 61 72
11(0,4)(0,4)null19 5a 6f c3 ab 20 4d 61
(11 rows)

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

predict the output
after CREATE INDEX on customers (name), what order does SELECT * FROM customers come back in?
sql
CREATE INDEX customers_name ON customers (name);
SELECT * FROM customers LIMIT 3;
sqldb=shop
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;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · SELECT 3
idint4nametext
1Maya Chen
2Grace Okafor
3Sam Lee
(3 rows)
#3 · SELECT 2
relnamenamerelkindcharpagesint8
customersr24
customers_namei8
(2 rows)

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.

Ada LovelaceAisha Ahmed… 190 moreChen RossiChen Santos… 2 more pagesRavi PetrovRavi Reyes… 176 moreZoë MartinChen SantosMateus Yilmaz… 2 moreRavi Petrovrootleavesheap 0heap 8heap 9heap 18
child and sibling linkshigh key
real output, postgres 18.6 (PGlite 0.5.12) · index-map
the real index on customers(name): a root over 6 leaf pages, each leaf holding about 200 names in order (the figure shows a few per page), and the heap pages their addresses point into. a lookup reads the root, one leaf, and then the heap page the address names.

here's what that buys, for one row:

sqldb=shop
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';
output, from postgres 18.6
#1 · EXPLAIN
Seq Scan on customers (actual rows=1.00 loops=1) Filter: (name = 'Ada Lovelace'::text) Rows Removed by Filter: 1999 Buffers: shared read=24 Planning: Buffers: shared hit=51 read=4 dirtied=1
#2 · CREATE INDEX
#3 · EXPLAIN
Bitmap Heap Scan on customers (actual rows=1.00 loops=1) Recheck Cond: (name = 'Ada Lovelace'::text) Heap Blocks: exact=1 Buffers: shared hit=1 read=2 -> Bitmap Index Scan on customers_name (actual rows=1.00 loops=1) Index Cond: (name = 'Ada Lovelace'::text) Index Searches: 1 Buffers: shared read=2 Planning: Buffers: shared hit=20 read=1
no index: Seq Scan24 pages
index on name3 pages
real output, postgres 18.6 (PGlite 0.5.12) · index-map
pages read to find Ada Lovelace by name, from the same capture: every page of the table, or the index's way down plus one heap page.

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.