Skip to content

fix(migrations): clear placeholder dates without scanning memory_units; consolidation reads them as unknown - #5922

Merged
nicoloboschi merged 2 commits into
mainfrom
fix/placeholder-dates-no-table-scan
Oct 21, 2026
Merged

nicoloboschi merged 2 commits into
mainfrom
fix/placeholder-dates-no-table-scan

Conversation

@nicoloboschi

@nicoloboschi nicoloboschi commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #5828 (not released yet).

Problem. Migration c7e2f9a4d1b8 cleared year-1/9999 dates with one UPDATE memory_units ... WHERE over four date columns. occurred_start, occurred_end and mentioned_at are only indexed behind bank_id, so it read the whole table in every tenant schema at startup — to fix a handful of rows.

Fix.

  • The migration now clears only event_date, which has its own index (idx_memory_units_event_date): two index range probes, no table scan. It has to stay for that column: retain's temporal-link SQL subtracts event_date, and PostgreSQL 16 and older fail with cannot subtract infinite timestamps (checked on postgres:16). PG only — Oracle has no infinite timestamps.
  • The other three columns are left as stored. Every reader now treats a placeholder as an unknown date: recall already did (fix(recall): a year-1/9999 date no longer breaks temporal recall and consolidation (#5820) #5828), and consolidation's date merging (_as_dt, _min_date, _max_date) now runs each date through finite_utc too. Before, a stored infinity there raised can't compare offset-naive and offset-aware datetimes.

Tests (test_temporal_infinite_dates.py)

  • Consolidation merging over stored -infinity/infinity rows: fails with the TypeError without the fix, passes with it.
  • The migration nulls placeholder event_date values (infinite and year-9999), keeps real ones, and leaves the other columns alone.

No system story: the API refuses placeholder dates since #5828, so a client-driven test can't create the old rows.

…olidation reads placeholder dates as unknown

The migration added in #5828 cleared year-1/9999 dates with an UPDATE filtered on four
date columns that no index leads with, so it read every memory_units row in every schema
at startup to fix a handful of rows. It is now a no-op (the revision stays so databases
that already ran it keep a valid history).

Instead, consolidation's date merging (_as_dt, _min_date, _max_date) normalises each
date with finite_utc, as recall already does, so a stored infinity reads as an unknown
bound rather than raising 'can't compare offset-naive and offset-aware datetimes'.
Review finding: on PostgreSQL 16 and older, retain's temporal-link SQL subtracts
event_date, and an old infinity there fails with 'cannot subtract infinite timestamps'
- the read-side fix can't reach SQL. event_date has a standalone index
(idx_memory_units_event_date), so the migration now clears just that column as two
index range probes instead of scanning the table. PG only: Oracle has no infinite
timestamps.

- test: the migration nulls placeholder event_date and leaves the other columns
- test: say why the consolidation test forges and reads rows with raw SQL
@nicoloboschi nicoloboschi changed the title fix(migrations): no full memory_units scan for placeholder dates; consolidation reads them as unknown fix(migrations): clear placeholder dates without scanning memory_units; consolidation reads them as unknown Oct 21, 2026
@nicoloboschi
nicoloboschi merged commit 48bd16e into main Oct 21, 2026
233 of 234 checks passed
@nicoloboschi
nicoloboschi deleted the fix/placeholder-dates-no-table-scan branch October 21, 2026 11:34
@nicoloboschi nicoloboschi mentioned this pull request Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant