Skip to content

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

Supabase 21 min read Research mediumAdept

Implementing "seen by" functionality with Postgres

Four ways to track post views in Postgres are benchmarked: a naive counter column, an hstore of viewer IDs per post, an association table, and the HyperLogLog extension. HyperLogLog is the recommended default because its tradeoffs are good and it adds no large architectural liabilities. hstore is fine for a quick prototype and performed best with integer IDs. The benchmark uses synthetic users and posts with an 80/20 poster skew, replayed through autocannon with a 10/60/30 mix of list-viewers, read-count and record-view requests.

Related posts

  • London Meetup Report: Scaling Analytics with PostHog and Introducing HouseWatch

    ClickHouse mediumAdept

    PostHog scaled its product analytics platform by moving from Postgres to ClickHouse, with an events table holding roughly 62 TB uncompressed for a single column. Two optimizations drove most of the gains: materializing frequently queried JSON properties into columns, and a person overrides table that records merged user IDs with versions, is joined at query time, and is periodically rolled back into the events. Together these cut Y Combinator's average query time from 18s to 1s and P95 from 60s to 4s, and a user-facing sampling control lets users trade accuracy for speed. PostHog also introduced HouseWatch, an open-source React app deployed via Docker for monitoring long-running ClickHouse queries, analyzing table and column disk usage, killing queries, and managing migrations.

  • Introducing Postgres Query Insights in ClickHouse Cloud

    ClickHouse mediumAdept

    Postgres Query Insights, now in preview for ClickHouse Cloud Managed Postgres, ranks every normalized query pattern by impact and aggregates per-execution counters (CPU, cache vs disk reads, temp spills, unlaunched parallel workers, WAL) to explain why a pattern is slow. A worked example traces a p99 regression to a sort spilling to disk, confirmed with EXPLAIN (ANALYZE, BUFFERS) and fixed with an index or work_mem tuning. Telemetry comes from pg_stat_ch, an Apache 2.0 extension that normalizes literals at parse-analyze time so values and PII never leave the database. It enqueues raw per-statement events via a non-blocking try-lock on a shared-memory ring buffer at about 3% overhead, dropping events under pressure rather than back-pressuring Postgres.

  • Anatomy of a (Postgres) search engine

    PlanetScale mediumAdept

    Inverted indexes power full-text search by mapping each term to a sorted, compressed postings list of document IDs, with optional positional data for phrase/span queries and frequency statistics for BM25 scoring. Postings can be delta-encoded or stored as bitmaps for fast SIMD intersection, and block-level statistics let top-k queries skip whole blocks. Indexes are built as immutable segments plus a mutable segment, with deletes handled by tombstones and periodic merges that trade heavy I/O against per-query overhead. Inside Postgres, such an index must map to ctids, be WAL-logged, reuse freed pages, run merges in background workers, and cooperate with VACUUM.

  • PostgreSQL Stories: From slow query to fast—via stats

    Render mediumAdept

    A simple LIMIT 1 join between a ~100k-row postgres_dbs table and a ~90M-row events table ran for hours. The cause was a merge join that scanned ~79M non-matching events sorted ahead of the 'dpg-' serviceId prefix. Adding manual lower and upper bounds on the indexed JSON expression brought the query down to milliseconds, but the real fault was that PostgreSQL only kept statistics on whole JSON objects, not on data->>'serviceId'. Running CREATE STATISTICS on that expression gave the planner accurate distributions, and the original query then finished in under a millisecond without rewrites.

  • LangChain - Why we Choose ClickHouse to Power LangSmith

    ClickHouse mediumAdept

    LangSmith, LangChain's LLM observability and evaluation platform, began fully on Postgres but outgrew it as users logged most of their production traces: ingestion lagged, precomputed materializations were rigid and costly, and lock contention grew. LangChain added ClickHouse rather than Druid or Pinot because it accepts plain INSERTs without Kafka-based ingestion services and runs locally, self-managed and in the cloud. Lessons include designing sorting keys carefully, using ReplacingMergeTree for updates, and leaning on EXPLAIN because the query planner is less forgiving than Postgres. For trace-ID lookups outside the sorting key, materialized views act as an inverted index that maps trace and run IDs to the main table's sorting-key values, avoiding full scans more simply than bloom-filter skip indices.

The quest for software engineering knowledge.

Look:
Layout: