Skip to content

backend/pkg/api: make instance stats queries timezone independent - #1591

Open
Kanika0306 wants to merge 1 commit into
flatcar:mainfrom
Kanika0306:fix/instance-stats-timezone
Open

backend/pkg/api: make instance stats queries timezone independent#1591
Kanika0306 wants to merge 1 commit into
flatcar:mainfrom
Kanika0306:fix/instance-stats-timezone

Conversation

@Kanika0306

Copy link
Copy Markdown

Summary of Changes

Fixes a PostgreSQL database session timezone sensitivity issue in Nebraska's instance_stats functionality (InstanceStatsQuery and GetInstanceStatsByTimestamp).

Problem

InstanceStatsQuery and GetInstanceStatsByTimestamp constructed SQL expressions formatted as timestamp '...'. In PostgreSQL SQL syntax, timestamp '...' casts string literals to timestamp (without time zone). When the database session timezone was set to a non-UTC location (e.g. America/New_York), PostgreSQL implicitly converted the naive timestamp to timestamptz using the session TimeZone, shifting query boundaries and returning 0 or incomplete results.

Solution

  1. Normalized Timestamp Inputs: Normalize Go time.Time values to UTC (.UTC()) at function boundaries in InstanceStatsQuery and GetInstanceStatsByTimestamp.
  2. Timezone-Aware SQL Literals: Replaced timestamp ? with timestamptz ? in goqu expressions for timestamp and timestampMinusDuration. timestamptz '...' forces PostgreSQL to parse the literal as an absolute UTC epoch instant regardless of session timezone.
  3. Regression Test: Added TestUpdateInstanceStatsTimezone in backend/pkg/api/instances_test.go to verify query equivalence across UTC and America/New_York session timezones.

Testing

  • go test -count=1 -v ./pkg/api -run 'TestUpdateInstanceStats' (Passed)
  • go test -v ./pkg/api (Passed)
  • go test -cover ./pkg/api (Passed - 80.0%)

InstanceStatsQuery and GetInstanceStatsByTimestamp previously used SQL expressions formatted as 'timestamp ...'. In PostgreSQL syntax, 'timestamp ...' casts the literal string to timestamp without time zone. When database session timezones were set to non-UTC locations (e.g. America/New_York), PostgreSQL implicitly converted the naive timestamp to timestamptz using the session TimeZone setting. This caused instance_stats queries to shift query boundaries and return incomplete or zero results.

This change normalizes Go time inputs to UTC and updates the goqu SQL expressions to use 'timestamptz ...'. This forces PostgreSQL to evaluate the literal as an absolute UTC instant regardless of the database session timezone. A regression test is added to verify query equivalence across UTC and non-UTC session timezones.

Signed-off-by: Kanika0306 <kanikakatare0306@gmail.com>
@Kanika0306
Kanika0306 requested a review from a team as a code owner August 15, 2026 19:56
Copilot AI lite review requested due to automatic review settings August 15, 2026 19:56

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.

@Kanika0306
Kanika0306 requested a lite review from Copilot August 15, 2026 19:57

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.

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.

2 participants