Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/schema-partitioning.md
1---2title: Partition Large Tables for Better Performance3impact: MEDIUM-HIGH4impactDescription: 5-20x faster queries and maintenance on large tables5tags: partitioning, large-tables, time-series, performance6---78## Partition Large Tables for Better Performance910Partitioning splits a large table into smaller pieces, improving query performance and maintenance operations.1112**Incorrect (single large table):**1314```sql15create table events (16id bigint generated always as identity,17created_at timestamptz,18data jsonb19);2021-- 500M rows, queries scan everything22select * from events where created_at > '2024-01-01'; -- Slow23vacuum events; -- Takes hours, locks table24```2526**Correct (partitioned by time range):**2728```sql29create table events (30id bigint generated always as identity,31created_at timestamptz not null,32data jsonb33) partition by range (created_at);3435-- Create partitions for each month36create table events_2024_01 partition of events37for values from ('2024-01-01') to ('2024-02-01');3839create table events_2024_02 partition of events40for values from ('2024-02-01') to ('2024-03-01');4142-- Queries only scan relevant partitions43select * from events where created_at > '2024-01-15'; -- Only scans events_2024_01+4445-- Drop old data instantly46drop table events_2023_01; -- Instant vs DELETE taking hours47```4849When to partition:5051- Tables > 100M rows52- Time-series data with date-based queries53- Need to efficiently drop old data5455Reference: [Table Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html)56