1.Heap Tables
PostgreSQL's default table storage is a heap: rows are stored in no particular guaranteed order, physically wherever there is room, rather than sorted by any key the way a clustered-index table in some other databases is. Finding a specific row by value therefore normally requires an index — without one, PostgreSQL must scan the heap looking at every row.
Key features
- A heap table stores rows in no guaranteed order — physical location depends on available free space, not any key value
- Without an index, finding rows matching a condition requires a sequential scan — reading every row in the table
- PostgreSQL's heap is unlike a clustered-index table in some other database systems, where rows ARE physically ordered by the primary key
- CLUSTER can physically reorder a heap table according to an index, once, as a one-time operation — but the table does not stay sorted as new writes come in afterward
- Every table needs at least one index to make targeted lookups fast; a heap table with no indexes at all always requires a sequential scan
Command
CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT, total NUMERIC);
-- rows land wherever there's free space -- NOT necessarily in id order
-- physically on disk, even though the PRIMARY KEY constraint (backed by
-- an index) enforces uniqueness and enables fast lookup by idA heap table stores rows in no guaranteed order — physical location depends on free space, not any key. Without an index, finding specific rows requires scanning the whole heap. Never assume a particular row order without an explicit ORDER BY, since the heap's physical layout is an implementation detail that can and does change.