Skip to content

fix(dbreads): use timezone-aware now() for stats windows - #1555

Draft
Harsh63870 wants to merge 2 commits into
flatcar:mainfrom
Harsh63870:fix/dbreads-timezone-now-utc
Draft

fix(dbreads): use timezone-aware now() for stats windows#1555
Harsh63870 wants to merge 2 commits into
flatcar:mainfrom
Harsh63870:fix/dbreads-timezone-now-utc

Conversation

@Harsh63870

Copy link
Copy Markdown

Summary

Fixes #1541.

Several reporting and rollout-policy queries compared timestamptz columns (last_check_for_updates, last_update_granted_ts) to now() at time zone 'utc'. That expression returns a timestamp without time zone. When Postgres compares it back to timestamptz, it converts using the session timezone, so the window shifts by the UTC offset whenever the DB session is not UTC.

That skews:

  • Dashboard counts (GetGroupInstancesStats, GetGroupVersionBreakdown, app instance counts, instance list filters)
  • Rollout enforcement (GetGroupUpdatesStatsupdates_in_progress / updates_timed_out / period rate limits), which can pause updates early (safe mode) or hold instances too long

Timeline queries in the same package already used plain now(), so two reporting paths reading the same columns disagreed.

Changes

  1. Query fix: replace now() at time zone 'utc' with timezone-aware now() in:
    • backend/pkg/api/internal/dbreads/applications.go
    • backend/pkg/api/internal/dbreads/instances.go
    • backend/pkg/api/internal/dbreads/groups.go
  2. Defense in depth: append TimeZone=UTC to the DB URL on connect when the operator has not already set a timezone (withUTCSessionTimezone in api.go). Explicit TimeZone / timezone / options=-c TimeZone=... are left alone.
  3. Tests:
    • TestGroupInstanceStatsStableAcrossSessionTimezones — same 1-day window returns 3 under UTC, America/New_York, and Asia/Kolkata
    • TestWithUTCSessionTimezone — DSN helper behavior
  4. CHANGELOG Unreleased bugfix entry

Test plan

  • cd backend && go test ./pkg/api/ -run 'TestWithUTCSessionTimezone|TestGroupInstanceStatsStableAcrossSessionTimezones'
  • cd backend && go test ./pkg/api/ -run 'TestGetGroup|TestRegisterInstance|TestGetInstances'
  • Optional: on a Postgres with SET TIME ZONE 'Asia/Kolkata', confirm group instance totals match UTC before/after

Notes

Sample fixture SQL (sample_data.sql) still uses now() at time zone 'utc' for inserts only; those are not the comparison bug described in #1541 and were left unchanged to keep this PR focused.

Replace now() at time zone 'utc' comparisons against timestamptz columns so instance stats and rollout policy windows stay correct when the Postgres session timezone is not UTC. Also pin TimeZone=UTC on connect.

Signed-off-by: Harsh63870 <harshvardhanpandey372@gmail.com>
Copilot AI lite review requested due to automatic review settings August 10, 2026 16:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Signed-off-by: Harsh63870 <harshvardhanpandey372@gmail.com>
Copilot AI review requested due to automatic review settings August 10, 2026 16:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Harsh63870

Copy link
Copy Markdown
Author

@ervcz , resolved the merge conflict in backend/pkg/api/api.go against upstream/main, upstream had relocated nowUTC to pkg/api/runtime, so this branch now keeps only withUTCSessionTimezone and updates the timezone regression test to use runtimeSvc / runtime.NewInstanceApplication, no changes to the original fix logic, just adapted to the new location...

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.

Instance stats and rollout policy queries break when the Postgres session timezone is not UTC

2 participants