# Postgres timestamptz vs JS Date — the Self-Match Trap
<!--
ref: @protocol/postgres-timestamp-self-match
tags: [postgres, timestamptz, javascript, typeorm, gotcha, query]
triggers: [row matches itself, MoreThan createdAt, newer record query returns the same row, banner never goes away, is there a newer version, timestamp comparison off by microseconds]
trigger_prompt: When a "find records newer than this one" query keeps returning the record it was given, or a "newer version exists" indicator never clears.
category: core
audience: [agent-author, claude-code-user]
version: 2
updated_at: 2026-08-04T18:02:36.272Z
-->

Postgres `timestamptz` stores microseconds. A JavaScript `Date` holds only milliseconds. So a row's own timestamp, round-tripped through JS, compares as *strictly less than* the value stored in the database — and a "find newer" query matches the row you started from.

## The trap

```ts
// row.createdAt in Postgres:  2026-05-17 10:00:00.123456+00
// row.createdAt as a JS Date: 2026-05-17 10:00:00.123    ← .456 µs silently dropped

find({ where: { key: row.key, createdAt: MoreThan(row.createdAt) } })
// → returns `row` itself, because .123456 > .123
```

Every ORM that hydrates timestamps into `Date` inherits this: TypeORM, Prisma, Sequelize, raw `pg`. Nothing errors. The query is valid and the result is wrong.

## Symptom

A "there is a newer version of this" banner, badge, or notification that appears and never clears — because the check that would clear it keeps finding the current record and concluding a newer one exists.

The bug is easy to misread as caching, replication lag, or a stale client, because the query looks obviously correct.

## The fix

**Exclude by identity, not by timestamp alone.** The primary key is exact; the timestamp is lossy.

```ts
find({
  where: { key: row.key, createdAt: MoreThan(row.createdAt), id: Not(row.id) },
})
```

Or order and offset instead of comparing: fetch the newest matching row and compare its `id` to the one you hold.

**Generally:** any predicate meant to say "some *other* row" must express otherness with the identifier. A timestamp comparison that happens to exclude the row today does so only by luck of precision.

---

*Related: `@content/youtube/latent-space/2026-10-28-why-agents-love-supabase-with-paul-copplestone-and-ant-wilso` · `@protocol/vue-facing-decorator-traps` · `@protocol/python-js-word-boundary-trap` · `@protocol/postgres-skip-locked-limit-trap` · `@protocol/zsh-tied-variable-trap`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*