Skip to content

Commit 4f22b26

Browse files
authored
fix(postgres): Preserve ordering for unchanged payloads (#14)
1 parent fba8a39 commit 4f22b26

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎docs/how-it-works.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ INSERT INTO workos.users AS t (_env, _raw, _updated_at)
6868
_updated_at = excluded._updated_at,
6969
_synced_at = NOW()
7070
WHERE excluded._updated_at >= t._updated_at
71-
AND t._raw IS DISTINCT FROM (t._raw || excluded._raw)
71+
AND (excluded._updated_at > t._updated_at
72+
OR t._raw IS DISTINCT FROM (t._raw || excluded._raw))
7273
RETURNING (xmax = 0) AS _created;
7374
```
7475

@@ -88,6 +89,10 @@ state with no error anywhere.
8889
simultaneous events, because nothing can be, so reconciliation is the backstop
8990
rather than the comparison.
9091

92+
A newer observation advances `_updated_at` even when its payload is unchanged,
93+
so a delayed event cannot overwrite it. Only an identical payload at the same
94+
timestamp is a no-op.
95+
9196
### The timestamp comes from the source, never the database
9297

9398
`_updated_at` is the object's own `updated_at` when present, otherwise the

‎packages/sync-postgres/src/db/upsert.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ on conflict (${conflict}) do update set
120120
"_updated_at" = excluded."_updated_at",
121121
"_synced_at" = now()
122122
where excluded."_updated_at" >= t."_updated_at"
123-
and t.${raw} is distinct from (${newRaw})
123+
and (excluded."_updated_at" > t."_updated_at" or t.${raw} is distinct from (${newRaw}))
124124
returning (xmax = 0) as _created`,
125125
params,
126126
}

‎packages/sync-postgres/test/upsert.test.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,37 @@ describe('write path', () => {
104104
expect((await readUser())!['email']).toBe('second@example.com')
105105
})
106106

107-
it('skips a write that would change nothing', async () => {
107+
it('skips a write with the same payload and timestamp', async () => {
108108
await upsertRows(db, opts, users, 'prod', [{ data: userPayload(), updatedAt: '2026-01-01T00:00:00.000Z' }], 'replace')
109109
const again = await upsertRows(db, opts, users, 'prod', [
110-
{ data: userPayload(), updatedAt: '2026-01-02T00:00:00.000Z' },
110+
{ data: userPayload(), updatedAt: '2026-01-01T00:00:00.000Z' },
111111
], 'replace')
112-
// Backfill and reconcile re-fetch mostly-unchanged rows; rewriting them all
113-
// would bloat WAL and churn autovacuum for nothing.
112+
// Rewriting an identical observation would bloat WAL for nothing.
114113
expect(again).toMatchObject({ created: 0, updated: 0, skipped: 1 })
115114
})
116115

116+
it.each(['merge', 'replace'] as const)('advances the timestamp for unchanged payloads in %s mode', async (mode) => {
117+
const roles = streamByName('roles')!
118+
const data = { slug: 'admin', permissions: ['read'] }
119+
await upsertRows(db, opts, roles, 'prod', [
120+
{ data, updatedAt: '2026-01-01T00:00:00.000Z' },
121+
], mode)
122+
const newer = await upsertRows(db, opts, roles, 'prod', [
123+
{ data, updatedAt: '2026-01-03T00:00:00.000Z' },
124+
], mode)
125+
const stale = await upsertRows(db, opts, roles, 'prod', [
126+
{ data: { ...data, permissions: ['write'] }, updatedAt: '2026-01-02T00:00:00.000Z' },
127+
], mode)
128+
129+
expect(newer).toMatchObject({ created: 0, updated: 1, skipped: 0 })
130+
expect(stale).toMatchObject({ created: 0, updated: 0, skipped: 1 })
131+
const result = await db.query(`select permissions, "_updated_at" from workos.roles`)
132+
expect(result.rows).toEqual([{
133+
permissions: ['read'],
134+
_updated_at: new Date('2026-01-03T00:00:00.000Z'),
135+
}])
136+
})
137+
117138
it('merges event payloads instead of nulling the fields events omit', async () => {
118139
// The whole reason `merge` exists: user.* events do not carry external_id,
119140
// metadata, last_sign_in_at or locale.

0 commit comments

Comments
 (0)