skip to content

postgres in your browser

a real postgres 18 compiled to webassembly runs every query here. what it can do, and the short list of what it can't.

address
everything.start.postgres_in_your_browser
part
part 1, start here
chapter
chapter 3 of 7
reading time
9 min

every run button on this site runs your SQL on a real postgres 18.6, compiled to webassembly by the PGlite project, inside this browser tab. nothing is faked and nothing leaves your machine.

it's a lab postgres: one session, no background processes, nothing saved when you leave. everything else, the parser, the planner, the executor, the pages on "disk", is the real thing.

the picture

you know how most SQL tutorials either show you output someone pasted in years ago, or ask you to install a server before you've written a single query?

think of a flight simulator built around the real plane's flight computer. the software making the decisions is the same code that flies the real plane (the postgres source, compiled for the browser). the cockpit around it is a mockup (a browser tab): there's no weather, no other traffic, and when you climb out, the flight never happened. for learning to fly, that's exactly what you want.

ask it who it is:

sqlempty db
SELECT version();
output, from postgres 18.6
SELECT 1
versiontext
(1 row)

PostgreSQL 18.6: the same release you'd install on a server. wasm32-unknown-emscripten is the "machine" it was compiled for: webassembly, through the emscripten toolchain. 32-bit is why the book's datasets stay under 30,000 rows a table, so a phone can hold them.

what happens when you press run

hover a stage.

hover a stage. the first run downloads the engine once; every run after that starts from a fresh copy of the dataset in memory.

the first run costs a download. this is what's in it, gzipped, and nothing comes from anywhere but this site:

pglite.wasm (postgres itself)3.45 MB
pglite.data (catalog, timezones...)2.14 MB
initdb.wasm0.15 MB
javascript glue0.14 MB
nine extensions0.11 MB
the engine, by file, as the site serves it: 5,936,497 bytes in all. it's fetched once, and only when you go to press run (hovering the button starts it).

then it builds the dataset a block asks for (db=shop, db=bank...) by running its plain SQL (the files in datasets/), and keeps a snapshot in memory. every run restores that snapshot, so nothing you do can break the next block. go ahead:

sqldb=shop
TRUNCATE customers CASCADE;
SELECT count(*) AS customers FROM customers;
output, from postgres 18.6
#1 · TRUNCATE TABLE
NOTICE: truncate cascades to table "orders"
NOTICE: truncate cascades to table "order_items"
#2 · SELECT 1
customersint8
0
(1 row)

every customer gone, and (that's the CASCADE) every order and line item that pointed at one. now run this:

sqldb=shop
SELECT count(*) AS customers FROM customers;
output, from postgres 18.6
SELECT 1
customersint8
2000
(1 row)

all 2,000 are back. each block starts from the same database the book used, which is why your output can be checked against the book's at all. when blocks should share state (create a table, then use it), the book chains them into a session, and the block says "continues session".

one level deeperhow a fresh copy can be that fast

the dataset isn't rebuilt per run. the first time, the engine runs initdb, loads the SQL, runs VACUUM (ANALYZE) and a CHECKPOINT, then dumps its whole data directory into one blob (about 61 MB raw for shop). a run hands that blob to a new PGlite instance as its starting data directory. measured in node on an apple M4 Pro: about 1.3 s to build shop once, then 90 to 104 ms for a fresh copy plus its first query. a browser is slower than node, but not by enough to notice.

the same postgres

the parser, the catalog, the planner and the executor are postgres's own C code, so plans are real plans and errors are real errors, down to the character position. the storage is real too: tables are 8 KB pages, every change is written to the WAL first, and the lab database ships six extensions for looking inside them:

sqldb=lab
SELECT extname
FROM pg_extension
ORDER BY extname;
output, from postgres 18.6
SELECT 7
extnamename
amcheck
pageinspect
pg_buffercache
pg_freespacemap
pg_visibility
pg_walinspect
plpgsql
(7 rows)

that's how the internals chapters can show you a page, a b-tree or WAL records from inside the very engine that's running your queries.

the short list of what it can't do

here it is, all of it. every line was run, on this engine and on a real postgres 18.6 server.

here: oneserver: one per client, plus helpers
this page · PGlite 0.5.12, postgres 18.6
SELECT pid, backend_type FROM pg_stat_activity;
 pid |    backend_type
-----+--------------------
  42 | standalone backend
a real server · postgres 18.6
SELECT backend_type FROM pg_stat_activity;
 background writer
 checkpointer
 client backend
 io worker (× 3)
 logical replication launcher
 walwriter

where the book shows the real thing: two sessions racing are recorded on a real server and replayed.

real output: PGlite 0.5.12 (postgres 18.6), and a postgres 18.6 server started for this page
pick a difference. the dashed panel is this page's engine, the solid one a real server, and the link says where the book shows the real thing. every line here is actual output.

a few of those, run right here. first, how many sessions does this postgres have?

sqlempty db
SELECT pid, backend_type FROM pg_stat_activity ORDER BY pid;
output, from postgres 18.6
SELECT 1
pidint4backend_typetext
42standalone backend
(1 row)

one: a standalone backend, which is what postgres calls itself when it runs in single-user mode, with no server process around it. a real server, asked the same thing, lists its helpers too. (two client backends, because the checker that ran this keeps two connections open. it also turns autovacuum off so its numbers hold still; a default server would list an autovacuum launcher as well.)

sqlempty db
SELECT backend_type, count(*) AS processes
FROM pg_stat_activity
GROUP BY backend_type
ORDER BY backend_type;
output, from a real postgres 18.6 server
SELECT 6
backend_typetextprocessesint8
background writer1
checkpointer1
client backend2
io worker3
logical replication launcher1
walwriter1
(6 rows)

checked on a real postgres 18.6 server. the browser engine can't run this one.

no helpers means no background work. the settings say so:

sqlempty db
SELECT name, setting
FROM pg_settings
WHERE name IN ('max_worker_processes', 'max_parallel_workers', 'io_method', 'fsync')
ORDER BY name;
output, from postgres 18.6
SELECT 4
nametextsettingtext
fsyncoff
io_methodsync
max_parallel_workers0
max_worker_processes0
(4 rows)

max_worker_processes is 0, so autovacuum and parallel queries never start, even though SHOW autovacuum says on. reads happen synchronously (io_method = sync, where a server defaults to worker), and fsync = off, because there's no disk to be durable on: it all lives in memory, and closing the tab throws it away.

timeouts don't fire either. postgres implements them with a timer signal, and inside a webassembly worker nothing delivers one while a query is running:

sqlempty db
SET statement_timeout = '50ms';
SELECT 'still here' AS answer FROM pg_sleep(0.3);
output, from postgres 18.6
#1 · SET
#2 · SELECT 1
answertext
still here
(1 row)

it slept the full 0.3 seconds and answered. on a real server the same two lines are cancelled:

sqlempty db
-- the same two lines, on a real server
SET statement_timeout = '50ms';
SELECT 'still here' AS answer FROM pg_sleep(0.3);
output, from a real postgres 18.6 server
#1 · SET
#2 · ERROR 57014
ERROR 57014: canceling statement due to statement timeout

checked on a real postgres 18.6 server. the browser engine can't run this one.

so the page keeps its own clock: after 10 seconds it stops the whole engine and starts a new one.

text sorts in byte order by default, because the database was created with the C collation. capital letters come before lowercase ones, and é after everything:

sqlempty db
SELECT x
FROM (VALUES ('b'), ('B'), ('a'), ('_z'), ('é'), ('Z'), ('apple'), ('Apple')) AS v (x)
ORDER BY x;
output, from postgres 18.6
SELECT 8
xtext
Apple
B
Z
_z
a
apple
b
é
(8 rows)

ICU's language-neutral order is here, if you ask for it by name, and it matches a real server's unicode collation exactly:

sqlempty db
SELECT x
FROM (VALUES ('b'), ('B'), ('a'), ('_z'), ('é'), ('Z'), ('apple'), ('Apple')) AS v (x)
ORDER BY x COLLATE "unicode";
output, from postgres 18.6
SELECT 8
xtext
_z
a
apple
Apple
b
B
é
Z
(8 rows)

what's missing is ICU's per-language data, so German or Swedish rules can't be loaded:

sqlempty db
CREATE COLLATION german (provider = icu, locale = 'de-DE');
output, from postgres 18.6
WARNING: ICU locale "de-DE" has unknown language "de"
HINT: To disable ICU locale validation, set the parameter "icu_validation_level" to "disabled".
CREATE COLLATION

the warning is the tell: the collation gets created, but it falls back to the root order. text and collations shows real language collations on a server.

some things the run button won't even send, because there's no one on the other end. forced through the engine anyway (PGlite 0.5.12 in node), this is what happens:

forced through the engine, PGlite 0.5.12
COPY customers FROM STDIN;
  the engine exits (wasm exit status 1): no client is there to send rows
 
CREATE EXTENSION pg_stat_statements;
  ERROR 0A000: extension "pg_stat_statements" is not available
  HINT: The extension must first be installed on the system where PostgreSQL is running.
 
SELECT pg_terminate_backend(42);
  WARNING: could not send signal to process 42: Operation not permitted
  f
 
ALTER SYSTEM SET work_mem = '8MB';
SELECT pg_reload_conf();
  ALTER SYSTEM
  WARNING: failed to send signal to postmaster: Operation not permitted
  f

and you can make a second database, but you can't get into it: switching databases means a new connection (\c in psql), and there's only ever one.

sqlempty db
CREATE DATABASE shop2;
SELECT datname FROM pg_database ORDER BY datname;
SELECT current_database();
output, from postgres 18.6
#1 · CREATE DATABASE
#2 · SELECT 4
datnamename
postgres
shop2
template0
template1
(4 rows)
#3 · SELECT 1
current_databasename
postgres
(1 row)
quick check
which of these can you watch on a run button on this site?

where the real server shows up

when a chapter's point needs something from that list, it says so and switches engines. three ways, each labelled:

  • native blocks, like the two above: checked on a real postgres 18.6 server, shown with their output, and no run button.
  • captured traces: two or three sessions scripted against a real server, every step recorded (locks, waits, row versions), then drawn as figures you can step through. lost updates and deadlocks are built that way.
  • psql transcripts: a real terminal session, pasted whole. psql and a real server is where you set one up yourself.

keylines

  • every run button runs real postgres 18.6, compiled to webassembly, in your tab.
  • each block starts from a fresh copy of its dataset, so you can't break anything.
  • one session, no background processes, no durability: that's the whole list of differences.
  • text sorts in byte order unless you ask for COLLATE "unicode".
  • when a chapter needs a real server, you get native output or a captured trace, labelled.