Difficulty: Intermediate
What is vertical partitioning, and how does it differ from horizontal partitioning?
Vertical partitioning splits a table by columns: frequently used columns stay in one table and rarely used or bulky columns move to a separate table that shares the same primary key. Horizontal partitioning splits a table by rows: each partition has the same columns but holds a different subset of rows, for example one partition per month. Vertical partitioning makes hot rows narrower and faster to scan; horizontal partitioning keeps each piece small so queries and maintenance touch only the relevant rows.
Take a users table with id, name, email, phone, city plus bio TEXT, profile_settings JSONB and resume_pdf BYTEA. Login and listing pages only need the small columns, but every row read drags the big ones along. Splitting gives:
users(id, name, email, phone, city): narrow, many rows per page, mostly cached in memory.user_profiles(user_id PK/FK, bio, profile_settings, resume_pdf): read only on the profile page, joined one-to-one by id.Benefits:
Costs: an extra join when you need both halves, and a one-to-one relationship to keep consistent.
PostgreSQL already does a form of this automatically. When a row exceeds about 2 KB, TOAST compresses large values and, if still needed, moves them out of line into a separate storage table, so big TEXT or JSONB values are not read unless selected. Manual vertical splits still help when columns are wide but not huge, or for access control.
Split orders by created_at into monthly partitions. PostgreSQL has supported declarative partitioning since version 10 with three methods:
('north', 'south') or tenant groups.Benefits:
WHERE created_at >= '2026-10-01' scans only the October partition.DELETE of millions of rows that creates bloat.Costs: queries that do not filter on the partition key must check every partition; unique constraints must include the partition key; too many partitions add planning overhead.
| Aspect | Vertical | Horizontal |
|---|---|---|
| Splits by | Columns | Rows |
| Each piece has | Different columns, same rows | Same columns, different rows |
| Main goal | Narrow hot tables, isolate big or sensitive data | Pruning, retention, manageable sizes |
| Join needed | Yes, to reassemble a full record | No, but cross-partition queries scan more |
| Typical example | users and user_profiles | orders_2026_09, orders_2026_10 |
Sharding is horizontal partitioning across multiple database servers, each owning a subset of rows, such as users by user_id hash. Plain partitioning keeps all partitions on one server. Sharding scales writes and storage beyond a single machine, but adds cross-shard queries, rebalancing and distributed transactions, so teams usually partition first and shard only when one primary is no longer enough.
| Vertical partitioning | Horizontal partitioning | |
|---|---|---|
| Example split | users + user_profiles | orders by month |
| Speeds up | Scans of hot narrow columns | Queries filtered by partition key |
| Main cost | Join to rebuild a full row | Scans all partitions without key filter |
| Built-in support | Manual, plus TOAST for big values | Declarative partitioning in PostgreSQL 10+ |
CREATE TABLE users (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
city TEXT
);
-- Rarely read, bulky columns in a 1:1 table
CREATE TABLE user_profiles (
user_id BIGINT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
bio TEXT,
profile_settings JSONB,
resume_pdf BYTEA
);
The users table stays narrow for listing and login queries; the profile page joins the second table only when needed.
CREATE TABLE orders (
id BIGINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
amount_paise BIGINT NOT NULL,
PRIMARY KEY (id, created_at) -- must include the partition key
) PARTITION BY RANGE (created_at);
CREATE TABLE orders_2026_10 PARTITION OF orders
FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');
-- Pruned to one partition
SELECT sum(amount_paise) FROM orders WHERE created_at >= '2026-10-01';
-- Retention: near-instant compared with DELETE
ALTER TABLE orders DETACH PARTITION orders_2025_10;
Each month is its own table under the hood. Queries filtered by created_at skip other months, and old months are removed by detaching or dropping a partition.
Vertical Partitioning, Horizontal Partitioning, Partition Pruning, Sharding, TOAST