PostgreSQL AT TIME ZONE on an Unknown Literal Explained
A bare literal and an explicitly cast one pick different timezone() overloads, and the result types are opposites. Here is how overload resolution decides, and how to write the cast you mean.

Two queries differ only by a cast and return different types. select '2026-09-14 11:54 pm' at time zone 'Asia/Kolkata' returns timestamp without time zone. Add ::timestamp to the literal and the same expression returns timestamp with time zone. Nothing errors in either case, even though the literal in the first query has no declared type when AT TIME ZONE reaches it. This was reported on Stack Overflow against PostgreSQL 16.15.
The asker's reading was that the bare literal is resolved as timestamp, the time zone is applied, and the result somehow loses its offset. That reading is wrong. The literal never becomes a plain timestamp. PostgreSQL silently picks a different function overload, and that overload does the opposite conversion.
The cost shows up when the timestamp comes from untyped SQL, such as a hand-written query, a string-built statement or a test fixture. You get a plain timestamp with no offset, and the value depends on the session's TimeZone setting. Nothing warns you that it is not the instant you meant. The mechanism, taken from the accepted answer, is below.
AT TIME ZONE is a function call in disguise
The documentation calls AT TIME ZONE an operator, but it has no operator-style name and does not appear in pg_operator. The parser grammar in src/backend/parser/gram.y handles it the way it handles other not-quite-operators such as BETWEEN and IS NOT NULL. The rule a_expr AT TIME ZONE a_expr builds a function call to timezone, with the zone as the first argument and the timestamp expression as the second, marked COERCE_SQL_SYNTAX.
So the first query is rewritten to select timezone('Asia/Kolkata','2026-09-14 11:54 pm'). The docs say as much: timezone(zone, timestamp) is equivalent to timestamp AT TIME ZONE zone. Because it is a function call, PostgreSQL's function overload resolution rules apply (chapter 10.3, Type Conversion: Functions), not the operator rules or the value-storage rules.
Two unknown arguments, six candidates
With no casts, both arguments are of type unknown. The resolver has to choose among every two-argument timezone function. You can list them yourself:
select oid, oid::regprocedure, prosrc, prorettype::regtype
from pg_proc
where proname = 'timezone'
and cardinality(proargtypes::regtype[]) = 2
order by proargtypes;| oid | signature | prosrc | returns |
|---|---|---|---|
| 2069 | timezone(text, timestamp) | timestamp_zone | timestamptz |
| 1159 | timezone(text, timestamptz) | timestamptz_zone | timestamp |
| 2037 | timezone(text, timetz) | timetz_zone | timetz |
| 2070 | timezone(interval, timestamp) | timestamp_izone | timestamptz |
| 1026 | timezone(interval, timestamptz) | timestamptz_izone | timestamp |
| 2038 | timezone(interval, timetz) | timetz_izone | timetz |
Per the accepted answer, the three interval variants drop out because 'Asia/Kolkata' cannot be converted to an interval. That leaves three text variants. The only difference between them is what the second argument is allowed to be.
The tie-break that picks timestamptz
Step 4.a of the resolution rules discards candidates whose inputs cannot be converted implicitly, and it treats unknown literals as convertible to anything. That does not narrow the three survivors, so resolution falls through to 4.d. This step keeps the candidates that accept preferred types, within the input's type category, at the most positions where conversion is needed.
All three remaining second-argument types are in category D, Date/Time. Of those, only timestamptz is marked preferred:
select typname, typispreferred
from pg_type
where typcategory = 'D'
order by typispreferred desc;The first row is timestamptz with true, and date, time, timestamp and timetz are all false. So oid 1159, timestamptz_zone, wins, and the query becomes timezone('Asia/Kolkata'::text, '2026-09-14 11:54 pm'::timestamptz). The asker's assumption that the literal resolved to plain timestamp is the opposite of what happened.
Where your session TimeZone gets in
The literal contains no zone designator. The implicit cast to timestamptz therefore follows the input rule for that type, quoted in the answer from chapter 8.5.1.3: if no time zone is stated in the input string, it is assumed to be in the time zone indicated by the system's TimeZone parameter. The session setting turns your naive wall-clock string into an absolute instant first.
Only then does timestamptz_zone run. It takes that instant and returns the wall-clock time in Asia/Kolkata as a plain timestamp. This is the reverse of what the asker expected, which was 'interpret this as Kolkata time'. The bare form really means 'interpret this in my session zone, then show me Kolkata's clock'.
The question reports 23:54:00 back, unchanged from the input. That is consistent with a session whose TimeZone is already at the Kolkata offset, though the question does not state the session setting. This is an inference from the mechanism, not something the question confirms. With a different session zone the same query would return a shifted wall-clock value.
Why the explicit ::timestamp cast flips the result
With '2026-09-14 11:54 pm'::timestamp at time zone 'Asia/Kolkata', the second argument is no longer unknown. It is a known timestamp without time zone, so step 4.a leaves exactly one viable candidate: timezone(text, timestamp), oid 2069, timestamp_zone. No tie-break happens and no implicit timestamptz cast is inserted.
That function takes a naive timestamp, declares it to be Kolkata local time, and returns a timestamptz. The answer's warning about reading it applies here. The cast binds to the literal, not to the AT TIME ZONE result, so the expression parses as (('...'::timestamp) at time zone 'Asia/Kolkata') and not '...'::(timestamp at time zone 'Asia/Kolkata'). The result type is timestamptz, and the question reports a +05:30 offset, with the caveat 'or similar'. How a timestamptz is displayed depends on the session, so do not assume that offset on your own server.
The interval subtraction queries
Queries 3 and 4 add - '1 day'::interval and - '1 day 50 seconds'::interval. They do not change the diagnosis. Subtracting an interval from the result keeps whatever type timezone() returned. Query 3 stays timestamp without time zone because it started from the timestamptz_zone overload. Query 4 stays timestamp with time zone because it started from timestamp_zone.
One detail in the question needs a check. The reported output of Query 3 is still 2026-09-14 23:54:00, with the day not subtracted. Query 4's reported output, 2026-09-13 23:53:10+05:30, does reflect the subtraction. The thread does not explain the Query 3 figure, so I would not build on it. If you reproduce this, run the query yourself and check what you get.
Making the intent explicit
The two meanings you might want are different operations, so write them as such:
-- naive wall-clock string is Kolkata local time; result is timestamptz
select '2026-09-14 11:54 pm'::timestamp at time zone 'Asia/Kolkata';
-- string is an instant (session zone or an explicit offset); result is Kolkata wall clock
select '2026-09-14 11:54 pm'::timestamptz at time zone 'Asia/Kolkata';The second query is what the bare literal quietly turned into. When in doubt, put pg_typeof(...) around the expression, or rerun the pg_proc query above. The return type tells you which overload was picked. In tests and migrations, put the cast on every literal that feeds AT TIME ZONE, so the overload does not depend on a tie-break nobody reads.
Where this explanation stops
The explanation covers untyped literals, and the parameter case is related but not identical. Table columns and typed expressions never hit the tie-break, because their types are already known and step 4.a leaves one candidate. The thread does not say how driver-supplied bind parameters are typed, so check that against your driver instead of assuming it matches the literal case.
- Results from the bare form depend on the session
TimeZone, so the same SQL can return different values from psql, an app server and a pooled connection configured differently. - The thread is a single report against PostgreSQL 16.15. It does not say whether overload choices or the preferred-type flags differ in other versions, so verify on the version you run.
- There is no error to catch. Only a type check in a test, such as asserting
pg_typeof, or a code-review rule requiring explicit casts will find this before production does.
This article draws on AT TIME ZONE with unknown-type string literal: no error, but timezone not applied? and an answer to it, on the Stack Exchange network, licensed under CC BY-SA 4.0.
- #at time zone
- #implicit-conversion
- #postgresql
- #sql
- #timestamp
- #timezone



Comments