Fix a top cause of slow queries in PostgreSQL (no slow query log needed)
PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans. In a simulated blog schema with 13 million comments, deleting a single user took 19 seconds, almost all of it in foreign-key triggers. Adding non-unique B-tree indexes on articles.author_id, comments.author_id and comments.article_id cut the delete to 342 ms, a 55x speedup, with the comments-by-article trigger dropping from 18 s to 26 ms. The rule of thumb is to index foreign key columns in referencing tables, and it is mandatory when ON DELETE propagation is defined.
Related posts
- Choosing the right Postgres indexes
incident.io mediumAdeptPostgres indexes pay off for frequently run queries that discard most rows they read, need results in a specific order, or must enforce uniqueness. In a 500k-row example, adding a B-tree index cuts a lookup from 215ms to 0.05ms. Put the most frequently filtered columns first in composite indexes, prefer reusable indexes over narrowly tailored ones, and use partial indexes (e.g. WHERE archived_at IS NULL) to keep them small. Missing indexes can be spotted with a Grafana panel on pg_stat_user_tables_seq_tup_read and confirmed with EXPLAIN ANALYZE.
- 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.
- The silent syntax difference in foreign keys between Postgres and MySQL
Neon mediumAdept
MySQL parses but silently ignores inline column-level `REFERENCES` clauses, so a foreign key written Postgres-style (`customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE`) creates no constraint, no index and no cascade behavior in MySQL. Orphaned rows insert without error. Only a separate table-level `FOREIGN KEY (...) REFERENCES ...` clause is enforced. Use the explicit `FOREIGN KEY` form in both databases, review schemas when porting between them, and test that constraints actually reject invalid data.
- 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.
- You can’t UPDATE what you can’t find: ClickHouse vs PostgreSQL
ClickHouse mediumAdept
ClickHouse 25.7's SQL-standard UPDATEs were benchmarked against PostgreSQL 17.5 on identical hardware using the 600M-row TPC-H lineitem table, with cold and hot runs. ClickHouse wins bulk updates by up to 4,000× because most bulk WHERE clauses can't use the primary key index. That forces full scans, where PostgreSQL takes 380–390 s and ClickHouse's columnar, parallel scan takes 0.09–5.23 s. Point updates are roughly at parity and cached point updates stay competitive. The comparison isn't apples-to-apples: PostgreSQL wraps every statement in a transaction and fsyncs WAL at commit by default, while ClickHouse skips per-statement transactions and defaults to fsync_after_insert=false.