Skip to content

Conversation

@tjgreen42
Copy link

@tjgreen42 tjgreen42 commented Jan 15, 2026

Summary

Fix time_bucket() with timezone parameter during DST transitions:

  • Apply offset in UTC space to avoid creating non-existent local times
  • Handle DST fall-back ambiguity when converting bucket back to timestamptz

Problems Fixed

#7059: Offset + timezone during DST spring-forward

The offset was applied to local time after timezone conversion, which could create non-existent times.

Example (Asia/Amman): On 2021-03-26, clocks jumped from 00:00 to 01:00. Subtracting 15 minutes from 01:00 local produced 00:45, which doesn't exist.

#8851: Offset + timezone during DST fall-back

Same root cause as #7059, causing incorrect timestamps around fall-back transitions.

#9136: Origin + timezone during DST fall-back

When using origin parameter during DST fall-back, the bucket could start AFTER the input timestamp. Postgres picks the standard time interpretation for ambiguous local times, but if the input was in daylight time, the bucket ended up in the future.

Solution

  1. Apply offset in UTC space (before timezone conversion) instead of local time
  2. After converting bucket back to timestamptz, subtract periods as needed until bucket ≤ timestamp
Before: timestamptz → local → subtract offset → bucket → add offset → timestamptz
After:  timestamptz → subtract offset (UTC) → local → bucket → [adjust for DST] → timestamptz → add offset (UTC)

Testing

Added regression tests for DST boundary cases from all three issues.

Fixes #7059
Fixes #8851
Fixes #9136

@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.30%. Comparing base (d347098) to head (f279330).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9129      +/-   ##
==========================================
- Coverage   82.46%   82.30%   -0.17%     
==========================================
  Files         243      244       +1     
  Lines       47938    46866    -1072     
  Branches    12234    12230       -4     
==========================================
- Hits        39534    38574     -960     
+ Misses       3536     3481      -55     
+ Partials     4868     4811      -57     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tjgreen42 tjgreen42 force-pushed the fix/time-bucket-dst-offset-7059 branch 3 times, most recently from fbd83be to a6be389 Compare January 15, 2026 04:31
@tjgreen42 tjgreen42 marked this pull request as ready for review January 15, 2026 04:35
@tjgreen42 tjgreen42 requested review from a team and erimatnor January 15, 2026 04:35
@tjgreen42 tjgreen42 enabled auto-merge (squash) January 15, 2026 04:36
@tjgreen42 tjgreen42 force-pushed the fix/time-bucket-dst-offset-7059 branch 4 times, most recently from 8da65e1 to fffd7b8 Compare January 16, 2026 01:15
When both timezone and offset parameters were specified, the offset
was incorrectly applied to local time after timezone conversion. This
could create non-existent times during DST transitions.

For example, in Asia/Amman on 2021-03-26, clocks jumped from 00:00 to
01:00, skipping the entire 00:00-00:59 hour. Subtracting 15 minutes
from 01:00 local time would produce 00:45, which doesn't exist.

The fix applies the offset in UTC space before timezone conversion,
ensuring DST transitions are handled correctly.

Fixes #7059
@tjgreen42 tjgreen42 force-pushed the fix/time-bucket-dst-offset-7059 branch from fffd7b8 to e463b70 Compare January 16, 2026 01:29
@tjgreen42 tjgreen42 requested a review from akuzm January 16, 2026 01:56
Update expected output files to match actual time_bucket function results
when using timezone and offset parameters across DST transitions.
@tjgreen42 tjgreen42 requested a review from svenklemm January 16, 2026 03:49
@akuzm
Copy link
Member

akuzm commented Jan 16, 2026

Looks that it also fixes this one #9136 ?

@erimatnor
Copy link
Member

Looks that it also fixes this one #9136 ?

Yeah, looks related. We should add the test in that issue to this PR. @tjgreen42

@tjgreen42
Copy link
Author

Looks that it also fixes this one #9136 ?

Yeah, looks related. We should add the test in that issue to this PR. @tjgreen42

Slightly different case actually -- this PR fixes the situation for timestamp and offset, but not for timestamp and origin. But let me see about folding in a fix for origin too.

During DST fall-back, PostgreSQL's timestamp_zone picks the standard
time interpretation when converting an ambiguous local time back to
timestamptz. If the original timestamp was in daylight time, the bucket
could incorrectly start AFTER the timestamp.

Fix by subtracting periods until the bucket is at or before the
timestamp. Also add test case for issue #9136.

Fixes #9136
@@ -0,0 +1 @@
Fixes: #7059 #8851 #9136 Fix time_bucket with timezone during DST
Copy link
Member

Choose a reason for hiding this comment

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

This is supposed to be the PR number since we compile changelog from this file, the actual bugs should go into commit message/linked to PR. Any external contributors reporting any of the bugs should get a thank you line

@@ -0,0 +1 @@
Fixes: #7059 #8851 #9136 Fix time_bucket with timezone during DST
Copy link
Member

@svenklemm svenklemm Jan 17, 2026

Choose a reason for hiding this comment

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

Suggested change
Fixes: #7059 #8851 #9136 Fix time_bucket with timezone during DST
Fixes: #9129 Fix time_bucket with timezone during DST
Thanks: @cracksalad and @eyadmba for reporting a bug with timezone handling in time_bucket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants