skip to content

what a database actually is

files you could write yourself, plus the four things you'd get wrong: crashes, concurrency, queries and rules.

address
everything.start.what_is_a_database
part
part 1, start here
chapter
chapter 1 of 7
reading time
8 min

a database is your data plus a program that guards it, the DBMS (database management system). you could keep your customers in a file and write that program yourself.

you'd get four things wrong: crashes, two people writing at once, questions nobody planned for, and rules that keep junk out. everything else in this book is how postgres gets those four right.

the picture

you know how every project starts with "we'll just save it to a file for now"? and how "for now" quietly becomes three years?

think of a shop's paper ledger versus a bank. the ledger is a notebook (a file): anyone can scribble in it, a spilled coffee can ruin a page, and to answer "who owes us money?" you flip through every page by hand. the bank has a clerk at the window (the DBMS) who takes requests in one fixed language (SQL), writes every transaction in a journal before touching the books (the write-ahead log), makes the second customer wait while the first one's withdrawal finishes (locks), and refuses a cheque with no signature (constraints).

problem =
customers.csv · drawn by hand
  1. id,name,city
  2. 41,Mateus Kowalski,Chicago
  3. 42,Ada Lovelace,London
postgres
  1. UPDATE customers
  2. SET city = 'Cambridge'
  3. WHERE id = 42;

Ada moves to Cambridge. your code starts rewriting her line in place. postgres starts the same change.

step 1/3
drawn by hand · the file side is a model. the postgres side quotes real postgres 18.6 output
pick a problem and step through it. the left side is a model of a file your own code writes. the right side is what postgres does, and every error on it is real postgres 18.6 output, which you can run further down.

none of the four is exotic. they're what happens on a normal tuesday once more than one person uses the thing.

a file you could write yourself

here's the shop's customers, as a file. it's not a strawman: a CSV is a perfectly good way to store rows.

customers.csv
id,name,email,city,country
1,Maya Chen,maya.chen@example.com,San Francisco,US
2,Grace Okafor,,Lagos,NG
...
42,Ada Lovelace,ada@example.com,London,GB

to find Ada you'd open it, split every line on commas, and stop at the one whose first field is 42. it works. then Grace's missing email turns out to be indistinguishable from an empty one, someone named "Okafor, Grace" breaks your split, and the file is 2 GB.

the same lookup, asked of postgres (this runs in your browser, on the real shop data, press run):

sqldb=shop
SELECT id, name, email, city
FROM customers
WHERE id = 42;
output, from postgres 18.6
SELECT 1
idint4nametextemailtextcitytext
42Ada Lovelaceada@example.comLondon
(1 row)

you didn't say how to find row 42. you said which one you wanted. postgres went and got it, through an index on id it built for you when the table got its primary key. that difference (what, not how) is the subject of your first query.

the four things you'd get wrong

crashes

your program writes Ada's new city over the old one, and the power goes out halfway through. now the file holds half a line. was the rest of the file overwritten? is there a next line at all? you can't tell.

postgres never overwrites in a way it can't undo. before a change counts, it appends a description of the change to a log, the write-ahead log (WAL), and forces that log to disk. COMMIT returns only after that. after a crash, postgres replays the log, so every committed change comes back whole and every uncommitted one disappears whole. that's the write-ahead rule, and crash recovery shows it happening on a real server.

two people at once

two requests read alice's balance (100.00), each adds to it, each writes back. the second write lands on top of the first, and one deposit is gone without an error anywhere. the figure's "two writers" walks through it.

postgres makes the second writer wait for the first, then lets it see the new value. it does that without making readers wait at all, by keeping old versions of rows around (an UPDATE is an INSERT). the browser engine here has a single session, so two transactions can't race on this page. the book shows those races from recordings of a real server.

questions nobody planned for

every new question about a file is a new program. "which London customers order the most?" means reading customers, then reading orders once per London customer, then counting. the query says it in seven lines:

sqldb=shop
SELECT c.id, c.name, count(*) AS orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE c.city = 'London'
GROUP BY c.id, c.name
ORDER BY orders DESC, c.id
LIMIT 3;
output, from postgres 18.6
SELECT 3
idint4nametextordersint8
38Elena Yilmaz17
169Elena Mueller14
22Fatima Mueller13
(3 rows)

you don't pick the algorithm. postgres's planner does, from statistics it keeps about every table, and it picks again when the data changes. that's half of this book: how a query runs and how the planner chooses.

rules

a file accepts anything you append to it. a table only accepts rows that fit its declaration: name is NOT NULL, country is two characters, email is UNIQUE. try all three:

predict the output
sql
INSERT INTO customers (name, email, city, country, created_at)
VALUES (NULL, 'ada@example.com', 'London', 'GB', '2026-10-28 09:00+00');
INSERT INTO customers (name, email, city, country, created_at)
VALUES ('Bob', 'bob@example.com', 'Atlantis', 'ZZZ', '2026-10-28 09:00+00');
INSERT INTO customers (name, email, city, country, created_at)
VALUES ('Ada Again', 'ada@example.com', 'London', 'GB', '2026-10-28 09:00+00');
SELECT count(*) FROM customers;
sqldb=shop
INSERT INTO customers (name, email, city, country, created_at)
VALUES (NULL, 'ada@example.com', 'London', 'GB', '2026-10-28 09:00+00');
INSERT INTO customers (name, email, city, country, created_at)
VALUES ('Bob', 'bob@example.com', 'Atlantis', 'ZZZ', '2026-10-28 09:00+00');
INSERT INTO customers (name, email, city, country, created_at)
VALUES ('Ada Again', 'ada@example.com', 'London', 'GB', '2026-10-28 09:00+00');
SELECT count(*) FROM customers;
output, from postgres 18.6
#1 · ERROR 23502
ERROR 23502: null value in column "name" of relation "customers" violates not-null constraint
DETAIL: Failing row contains (2001, null, ada@example.com, London, GB, 2026-10-28 09:00:00+00).
#2 · ERROR 22001
ERROR 22001: value too long for type character(2)
#3 · ERROR 23505
ERROR 23505: duplicate key value violates unique constraint "customers_email_key"
DETAIL: Key (email)=(ada@example.com) already exists.
#4 · SELECT 1
countint8
2000
(1 row)

each error says which rule, which column and which value. reading an error takes one apart, and constraints is the whole toolbox.

what's inside the box

"database" gets used for three different things, so let's pull them apart.

DBMS
the program: postgres itself. it runs as a server, and programs (clients) connect to it and send SQL.
a database
one named collection inside a postgres server, like shop. one server holds many. a connection talks to one at a time.
the data
the rows, in tables, grouped into schemas. what you actually care about.
  • ├── the one this page's engine connects to
  • │ ├── schema pg_catalogtables about tables
  • │ ├── schema information_schemathe standard's view of the same
  • │ ├── schema pg_toastwhere very long values go
  • │ └──
  • │ ├── customers2,000 rows
  • │ ├── orders10,000 rows
  • │ ├── order_items24,942 rows
  • │ └── products200 rows
  • ├── database template0a pristine copy, never changed
  • └── database template1what CREATE DATABASE copies
one postgres server, the way this page's engine sees it. click a name to fold it. public is where your tables go unless you say otherwise; pg_catalog is postgres describing itself, in tables.

the catalog is just more tables, so you can query it like anything else:

sqldb=shop
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
output, from postgres 18.6
SELECT 4
table_namename
customers
order_items
orders
products
(4 rows)

and when you send a query, it passes through a few stages before rows come back. each one is a part of this book.

hover a stage.

hover a stage. the parts of postgres a single query passes through, and where each one is explained.

keylines

  • a database is data plus the program that guards it. the program is the point.
  • files get four things wrong: crashes, concurrent writers, new questions and bad rows.
  • postgres logs every change before making it, so a crash loses all of a change or none.
  • you ask for what you want in SQL. postgres decides how to get it.
  • a table refuses rows that break its rules, and tells you which rule.