Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Supabase 20 min read How-to mediumAdept

Dynamic Table Partitioning in Postgres

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.

Related posts

  • Postgres to ClickHouse: Data Modeling Tips

    ClickHouse mediumAdept

    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. Deduplicate at query time with FINAL, argMax, ROW_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.

  • LLM Rules for Postgres

    Neon mediumAdept

    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. The rules are to create indexes CONCURRENTLY to avoid write locks, mark columns as ignored in the ORM before dropping them, index every foreign key, require id/created_at/updated_at on every table, skip indexes already covered by a composite index, and ensure frequent WHERE conditions have matching indexes. Each comes with bad/good SQL, Prisma or SQLAlchemy examples. Teams are encouraged to add new rules after each incident.

  • Making large Postgres migrations practical: 1TB in 2 hours with PeerDB

    ClickHouse mediumAdept

    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. The speedup comes from parallel snapshotting. A consistent snapshot is exported via pg_export_snapshot, the table is split into CTID ranges, and each range is streamed with binary COPY, with per-partition retries and progress visibility. Past a certain level of parallelism the source RDS network bandwidth becomes the limit, so 16 threads were no faster than 8 at 1TB. For CDC, PeerDB keeps the replication slot consumed independently of destination writes. It also reconstructs unchanged TOAST columns from batch caches and MERGE against the target, so REPLICA IDENTITY FULL is not needed.

  • Remember to use primary keys vs unique constraints in Postgres

    Neon mediumAdept

    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. Primary keys combine uniqueness with NOT NULL, create an index, act as the usual foreign-key target, and help the query planner. If you rely on UNIQUE, you need to add NOT NULL and run ALTER TABLE ... REPLICA IDENTITY USING INDEX yourself. The simpler habit is id SERIAL PRIMARY KEY, with UNIQUE kept for naturally unique columns like emails or SKUs.

  • How to Keep Staging in Sync with Production in Postgres

    Neon mediumAdept

    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. Logical replication and AWS DMS keep rows in sync but not schema or configuration, and they add load to production. All three copy PII as-is. Database branching with copy-on-write storage, as in Neon, makes staging an instantly resettable branch of production, and an intermediate anonymized branch masks sensitive columns before staging and dev branches are derived from it.

The quest for software engineering knowledge.

Look:
Layout: