Horizontal vs Vertical Partitioning

Difficulty: Intermediate

Question

What is vertical partitioning, and how does it differ from horizontal partitioning?

Answer

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.

Vertical partitioning in practice

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:

  • More rows fit per page, so scans and caches are more efficient.
  • Different access controls: sensitive columns like KYC documents can live in a table with tighter grants.
  • Fewer row rewrites when only a hot column changes, which matters in MVCC databases where an update writes a new row version.

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.

Horizontal partitioning in practice

Split orders by created_at into monthly partitions. PostgreSQL has supported declarative partitioning since version 10 with three methods:

  • RANGE: dates or numeric ranges, the most common for time series and logs.
  • LIST: explicit values such as region ('north', 'south') or tenant groups.
  • HASH: even distribution across a fixed number of partitions.

Benefits:

  • Partition pruning: WHERE created_at >= '2026-10-01' scans only the October partition.
  • Cheap data retention: dropping or detaching an old partition is nearly instant, unlike DELETE of millions of rows that creates bloat.
  • Smaller indexes per partition and faster vacuum.

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.

Comparison

AspectVerticalHorizontal
Splits byColumnsRows
Each piece hasDifferent columns, same rowsSame columns, different rows
Main goalNarrow hot tables, isolate big or sensitive dataPruning, retention, manageable sizes
Join neededYes, to reassemble a full recordNo, but cross-partition queries scan more
Typical exampleusers and user_profilesorders_2026_09, orders_2026_10

Partitioning vs sharding

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.

Two ways to split a table. Vertical cuts columns apart; horizontal cuts rows apart.
Vertical partitioningHorizontal partitioning
Example splitusers + user_profilesorders by month
Speeds upScans of hot narrow columnsQueries filtered by partition key
Main costJoin to rebuild a full rowScans all partitions without key filter
Built-in supportManual, plus TOAST for big valuesDeclarative partitioning in PostgreSQL 10+

Code examples

Vertical split of a users table

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.

Horizontal range partitioning by month

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.

Key points

  • Vertical partitioning splits columns into separate tables sharing a primary key
  • Horizontal partitioning splits rows across partitions with identical columns
  • PostgreSQL TOAST already moves very large values out of line automatically
  • Partition pruning and instant partition drops are the main horizontal benefits
  • Sharding is horizontal partitioning across multiple servers

Common follow-up questions

  • How do you choose a partition key for an orders table?
  • What is partition pruning and when does it fail to happen?
  • When would you move from partitioning to sharding?
  • Why must unique constraints on a partitioned table include the partition key?

Interview tips

  • Answer the vertical question directly first, then contrast it with horizontal
  • Give one concrete table example for each type
  • Mention the difference between partitioning and sharding; interviewers often probe it next

Concepts covered

Vertical Partitioning, Horizontal Partitioning, Partition Pruning, Sharding, TOAST

More Database Design questions