backend/pkg/api: make instance stats queries timezone independent - #1591
Open
Kanika0306 wants to merge 1 commit into
Open
backend/pkg/api: make instance stats queries timezone independent#1591Kanika0306 wants to merge 1 commit into
Kanika0306 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
Fixes a PostgreSQL database session timezone sensitivity issue in Nebraska's
instance_statsfunctionality (InstanceStatsQueryandGetInstanceStatsByTimestamp).Problem
InstanceStatsQueryandGetInstanceStatsByTimestampconstructed SQL expressions formatted astimestamp '...'. In PostgreSQL SQL syntax,timestamp '...'casts string literals totimestamp(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 totimestamptzusing the sessionTimeZone, shifting query boundaries and returning 0 or incomplete results.Solution
time.Timevalues to UTC (.UTC()) at function boundaries inInstanceStatsQueryandGetInstanceStatsByTimestamp.timestamp ?withtimestamptz ?ingoquexpressions fortimestampandtimestampMinusDuration.timestamptz '...'forces PostgreSQL to parse the literal as an absolute UTC epoch instant regardless of session timezone.TestUpdateInstanceStatsTimezoneinbackend/pkg/api/instances_test.goto verify query equivalence across UTC andAmerica/New_Yorksession 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%)