Part 10 / Topic 9 of 9
Temporary and unlogged tables
Two table flavours trade durability for convenience or speed:
Temporary tables live for your session and vanish on disconnect — invisible to everyone else, perfect for staging an awkward import before merging it properly:
CREATE TEMP TABLE import_scratch (sku text, price numeric);
INSERT INTO import_scratch VALUES ('chai', 120), ('coffee', 150);
SELECT count(*) AS staged FROM import_scratch;
CREATE TABLE
INSERT 0 2
staged
--------
2
(1 row)
Unlogged tables persist across sessions but skip the write-ahead log — the crash-recovery journal you'll meet properly in Stage 4. Writes run noticeably faster; the price is printed on the tin: after a crash, an unlogged table comes back empty (and it isn't replicated). Caches, rebuildable staging, derived scratch data — fine. Anything you'd miss — never.
CREATE UNLOGGED TABLE page_cache (path text PRIMARY KEY, body text);
CREATE TABLE
The decision tool: "Can I regenerate this from elsewhere without tears?" If yes, temp and unlogged are free speed; if no, they were never candidates.
🧪 Quick quiz — answer from memory, then check.
- Who can see your temporary table, and until when?
- What exactly do you risk with UNLOGGED?
Show the answers
- Only your session, until it disconnects (or you drop it).
- The table is truncated on crash recovery and skipped by replication — fine only for regenerable data.
Done here? The practice set for Part 10 — 6 exercises with solutions — is on the part page.