‹ BackHN Continuity

Thread

Footguns with Postgres “at time zone 'UTC'”

169 points · 114 comments · birdculture

  1. heurekamala · · focus · HN ↗
    The text says: "Comparing a timestamp and timestamptz will always result in false."

    That is wrong. Postgres changes the timestamp to timestamptz very quietly in the background. Wether it uses the time zone of the session for this. If it is true or false depends on the TimeZone setting. This is more bad than "always false". In production with UTC it works. On a laptop of a California developer it does not work.

    Just tested:

      SET TIME ZONE 'America/Los_Angeles';
      SELECT '2026-03-01 00:00:00'::timestamp = '2026-03-01 00:00:00+00'::timestamptz; /* f   */
    
      SET TIME ZONE 'UTC';
      SELECT '2026-03-01 00:00:00'::timestamp = '2026-03-01 00:00:00+00'::timestamptz; /* t  */
    
    If you can, use Postgres 16. The double AT TIME ZONE 'UTC' thing from the text is not needed there. There you have date_add with a time zone as the third argument:

    date_add(b.month_start, interval '1 month', 'UTC')

    This adds the month in UTC and it stays a timestamptz.

    1. whizzter · · focus · HN ↗
      Automatic conversion using "global" state between "local"/"human" time (5pm where I am now) and points in time (ie with timezone) is one of the biggest sins imho for many libraries/db's/languages. Ran into it a bunch of times using C# as well.
      1. reactordev · · focus · HN ↗
        The two most difficult things in software engineering...

        Naming.

        Timestamps.

        1. tyre · · focus · HN ↗
          You must not have invalidated your cache since a third thing.
          1. reactordev · · focus · HN ↗
            Obviously there are more but the original joke circa 1999 was this. Made Y2K even more exciting.
            1. dleary · · focus · HN ↗
              > but the original joke circa 1999 was this.

              No, the original joke, which GP is referring to, is “There are only two hard things in computer science. Naming things and cache invalidation.”

              It predates 1999 and the Y2K bug by a fair amount. I first saw it on Usenet in the early 90s, around 1994 I think.

              1. cryptonector · · focus · HN ↗
                > There are only two hard things in computer science. Naming things and cache invalidation.

                More like

                  There are only two hard things in
                  computer science. Naming, cache
                  invalidation, and off-by-one bugs.
                1. fragmede · · focus · HN ↗
                  too, parallel processing!
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.