---
title: "Dynamic Table Partitioning in Postgres"
description: "Postgres range partitioning can be retrofitted onto an existing \"large table\" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock."
url: "https://swe.quest/articles/dynamic-table-partitioning-in-postgres-13de90b4fe06270e"
canonical: "https://supabase.com/blog/postgres-dynamic-table-partitioning"
source: "Supabase"
published: "2023-10-03"
difficulty: "medium"
kind: "How-to"
reading_minutes: 20
tags: ["postgres","schema-migrations","sql","scalability"]
---

# Dynamic Table Partitioning in Postgres

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2023-10-03, 20 min read, How-to. Original: [https://supabase.com/blog/postgres-dynamic-table-partitioning](https://supabase.com/blog/postgres-dynamic-table-partitioning)

## Summary

Postgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock. The walkthrough builds a simulated chat app with pl/pgSQL functions that create partitions dynamically. It also covers why large tables hurt (btree depth, vacuum, archiving), the requirement that the partition key be part of composite primary and foreign keys, and ORM support gaps such as Django's lack of composite keys.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Scaling](https://swe.quest/tags/scalability.md) (Distributed systems)

## Prerequisites

- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- SQL DDL ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- B-tree indexes ([easy posts on Storage engines](https://swe.quest/tags/storage-engines.md?difficulty=easy))

## Keywords

PostgreSQL, Declarative Partitioning, ATTACH PARTITION, PL/pgSQL, Supabase, Django

## Related posts

- [Postgres to ClickHouse: Data Modeling Tips](https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-aec1f81e14af15d4.md): ClickHouse · 2024-08-28 · Medium · 9 min read · Analytics & OLAP, PostgreSQL, Event-driven architecture, SQL, Replication
  Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (\_peerdb\_version, \_peerdb\_is\_deleted), so duplicates persist until background merges run.
- [LLM Rules for Postgres](https://swe.quest/articles/llm-rules-for-postgres-ad82e8ad652f6e5a.md): Neon · 2025-08-29 · Medium · 4 min read · PostgreSQL, Code review, Schema migrations, AI for coding, LLMs
  Six explicit Postgres rules, drawn from building the wispbit review bot, let an LLM code reviewer or coding agent catch common migration and query mistakes before production.
- [Making large Postgres migrations practical: 1TB in 2 hours with PeerDB](https://swe.quest/articles/making-large-postgres-migrations-practical-1tb-in-2-hours-with-peerdb-cc7872fe91229eda.md): ClickHouse · 2026-02-19 · Medium · 13 min read · PostgreSQL, Replication, Schema migrations, Performance engineering, Data pipelines
  PeerDB migrated a 1TB, 3.6-billion-row Postgres table in about 1 hour 50 minutes with 8 threads, compared with 8 hours 40 minutes for native logical replication and 17 hours for pg\_dump/pg\_restore.
- [Remember to use primary keys vs unique constraints in Postgres](https://swe.quest/articles/remember-to-use-primary-keys-vs-unique-constraints-in-postgres-429326ad5b407be8.md): Neon · 2024-05-15 · Medium · 5 min read · PostgreSQL, Schema migrations, Replication
  Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication.
- [How to Keep Staging in Sync with Production in Postgres](https://swe.quest/articles/how-to-keep-staging-in-sync-with-production-in-postgres-2a3ed8d4d274df22.md): Neon · 2025-12-23 · Medium · 5 min read · PostgreSQL, Replication, Privacy, Testing
  Postgres staging environments drift from production, and the traditional fixes each fall short. pg\_dump/pg\_restore gives a clean copy but takes staging offline and runs for hours on large databases.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
