Engineering Blog

Engineering deep-dives from the SlashFeed team — Elixir, Phoenix, and how the product is built.

Zero-write upserts in Ecto with IS DISTINCT FROM

A plain ON CONFLICT DO UPDATE writes the row even when nothing changed — every feed poll rewrites thousands of identical entries, bumping updated_at and burning WAL. One SQL clause, IS DISTINCT FROM, makes Postgres skip the no-op write. The catch: when it skips, RETURNING returns nothing and Ecto raises StaleEntryError. Here is the full pattern, the rescue trick that turns the error into an :unchanged result, and the three-layer RSS dedup strategy it sits inside.

Enforcing multi-tenancy at the database layer in Ecto

Application-layer tenant scoping fails silently the day you forget a WHERE clause. PostgreSQL composite foreign keys turn a cross-user reference into a constraint violation the database rejects — here is the underdocumented references/2 :with option, the two migration patterns for CASCADE and SET NULL, the MATCH SIMPLE trick for nullable columns, and a test that proves the constraint lives in the DB.