Skip to content

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

Wealthfront Engineering 8 min read Case study mediumAdept

Optimizing Redshift Write Patterns: Tackling Tombstones and Ghost Rows

Repeated DELETE FROM + COPY snapshot reloads in Amazon Redshift left some tables with up to 90% ghost rows, about 22 billion rows on disk for 2.4 billion live ones. VACUUM could not reclaim the space because it frees only whole 1 MB blocks, and live and deleted rows were interleaved across most blocks. Wealthfront fixed bloated tables with deep copies as a stop-gap, then moved to two write patterns. Tables with dependent views use TRUNCATE + ALTER TABLE APPEND FROM, which requires pinning an explicit diststyle. All other tables use a staging-table load followed by an atomic rename swap, which keeps AUTO diststyle. Storage dropped 2-10x, and one multi-terabyte load fell from over 5 hours to about 1 hour.

Assumes: SQL DDL and transactionsdata warehouse basicscolumnar storage concepts

Related posts

  • Postgres Bloat Minimization

    Supabase mediumAdept

    Postgres stores table rows in 8KB heap pages and writes a new row version on every update, so dead versions accumulate until vacuum reclaims them and updates the free-space and visibility maps. Bloat usually means autovacuum never started, because the default 20% scale factor is too high for large tables, or it started but couldn't finish because of long-running idle transactions or SHARE UPDATE EXCLUSIVE locks. The fixes include lowering per-table scale factors, setting idle_in_transaction_session_timeout, cancelling stuck backends, and raising autovacuum_max_workers or autovacuum_work_mem. VACUUM FULL or pg_repack returns space to the filesystem after mass deletes; VACUUM FULL takes an exclusive lock and pg_repack does not.

  • Vantage's Journey from Redshift and Postgres to ClickHouse

    ClickHouse easyNovice

    Vantage, a cloud cost platform handling billions of billing records, outgrew Postgres when constant deletes and reinserts kept vacuum running and pushed indexes out of memory. Redshift, tried next with Postgres as a cache, struggled with updates, needed data inserted in sort order, required expensive vacuum sort jobs, and forced billions of rows to be copied daily between the two systems. A 60-day migration to ClickHouse Cloud replaced the delete-and-reinsert pattern with ReplacingMergeTree and an incrementing import-version column, letting background merges handle updates. The result was double the resources at roughly Redshift's price, with query performance comparable to well-indexed pre-aggregated Postgres tables.

  • Post mortem on Linear incident from Jan 24th, 2024

    Linear mediumAdept

    A database migration using `TRUNCATE TABLE ... CASCADE` on two new, unused tables silently wiped every production table with foreign keys to them, including issue and document descriptions, comments, notifications and reactions. Layered caching in Linear's sync engine hid the loss for about 30 minutes, and sync-lag warnings were excluded from alerting. Because point-in-time recovery had never been tested, the team restored a full backup from hours earlier, causing an hour of downtime and five hours of lost changes. They then replayed sync-engine action logs per workspace, with dry-runs first, recovering over 99% of data within 36 hours; follow-ups include revoking TRUNCATE privileges, linting migrations, PITR tooling and a read-only mode.

  • I don't trust ON DELETE CASCADE

    PlanetScale mediumAdept

    Postgres ON DELETE CASCADE turns a single-row delete into an unbounded transaction that can cause outages at scale. The cascade holds RowExclusiveLock on every table in the tree, so a migration requesting AccessExclusiveLock queues behind it and blocks all later writes. It can also generate hundreds of gigabytes of WAL that stall synchronous replication, and it falls back to sequential scans on unindexed foreign keys. With logical replication it can leave orphaned rows on subscribers, which skip cascade triggers, and it fails outright on published tables lacking a replica identity. The recommended alternative is soft deletes via a deleted_at column, followed by batched background reaping and vacuum, keeping transactions to hundreds or low thousands of rows.

  • Improving Parquet Dedupe on Hugging Face Hub

    Hugging Face mediumAdept

    Byte-level content-defined chunking dedupes Parquet appends almost perfectly: 10,000 appended rows give 99.1% dedupe and need about 20MB of new storage. A single-row edit drops dedupe to 89% (230MB new) because absolute file offsets in ColumnChunk/ColumnMetaData headers change in every column header. Deleting a row shifts the fixed-size row groups, and since each column is compressed, everything after the deletion becomes new data; turning off compression fixes dedupe but nearly doubles file size. Splitting row groups by a hash of a key column (content-defined row groups) keeps both compression and dedupe, and the proposed fixes are relative offsets in the format and CDC-aware row grouping in Parquet writers.

The quest for software engineering knowledge.

Look:
Layout: