And why you may need to repeatAT TIME ZONE 'UTC' twice.

SUMMARY

  • AT TIME ZONE 'UTC' converts the data type from timestamptz to timestamp (example).
  • Now you are inadvertently using the timestamp without time zone (aka timestamp) data type, which is markedly discouraged.
  • There are tons of footguns with timestamp.
  • For example, the equality of timestamp and timestamptz will always be false.
  • Adding a month with + INTERVAL '1 months' is timezone-dependent. If your product operates in UTC, you must ensure the timezone is in UTC before adding a month with <timestamptz_column> AT TIME ZONE 'UTC' + INTERVAL '1 months'.... but now the result is a timestamp, not a timestamptz.
  • To convert the above back to timestamptz in order to avoid issues with equality, you will need to invoke AT TIME ZONE 'UTC again. The final form is: (<timestamptz_column> AT TIME ZONE 'UTC' + INTERVAL '1 months') AT TIME ZONE 'UTC'