Skip to content

Pass the session TimeZone to external query engines - #915

Open
sfc-gh-agedemenli wants to merge 3 commits into
mainfrom
agedemenli/external-engine-timezone
Open

sfc-gh-agedemenli wants to merge 3 commits into
mainfrom
agedemenli/external-engine-timezone

Conversation

@sfc-gh-agedemenli

@sfc-gh-agedemenli sfc-gh-agedemenli commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

#829 made connections that run a query on pgduck_server use the session's TimeZone, so that time-zone-dependent expressions, such as date_trunc('day', ts) on a timestamptz, are evaluated as PostgreSQL would. Connections to an external query engine didn't get it, so a pushed-down query on a pg_lake_remote table was still computed in the engine's own time zone: under Asia/Tokyo, date_trunc('day', ts) returned the UTC day boundary.

How

  • Each query opens its own connection to an external engine, so the session's TimeZone goes into the connection's options (-c TimeZone=<zone>), after the options of the server's conninfo. A PostgreSQL engine starts in the zone, without an extra round trip.
  • If the engine doesn't report that zone when the connection starts, as pgduck_server and engines that ignore options don't, pg_lake sends SET TimeZone. It also only sends the SET when the conninfo names a service but no options, so the service file's options aren't replaced.
  • External engines never skip the SET because their default zone matches: an engine is reached as the remote user of a user mapping, and remote users can default to different zones. Only pgduck_server still does.
  • Whether an engine accepted a zone through SET is checked the first time a backend uses it, and kept per engine, since one engine's answer doesn't hold for another.
  • An engine, or a pooler in front of it, has to accept or ignore options, answer current_setting('TimeZone'), and accept a SET TimeZone of a zone it doesn't support without an error, as pgduck_server does. The docs say so.

Tests

  • Time-zone-dependent queries on a pg_lake_remote table under several session time zones, compared with a heap table.
  • The zone reaching a PostgreSQL engine when it connects, with the server's own options still applied.
  • Remote users with different default zones, reading a table directly and through a view owned by another user.

@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 6b8dbdf to bdce580 Compare November 20, 2026 17:24
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from a584188 to 065be52 Compare November 20, 2026 17:45
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from bdce580 to 5a16057 Compare November 20, 2026 17:45
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from 065be52 to 1c96aff Compare November 25, 2026 13:22
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 5a16057 to 7680ff9 Compare November 25, 2026 13:23
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from 1c96aff to da2b434 Compare November 26, 2026 03:24
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 7680ff9 to 07298d6 Compare November 26, 2026 03:24
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from da2b434 to c6b2cc2 Compare November 26, 2026 04:10
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 07298d6 to 51b5440 Compare November 26, 2026 04:10
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 51b5440 to 7fe2200 Compare November 27, 2026 08:40
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from e371e44 to 8aa2ca7 Compare November 27, 2026 08:44
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 7fe2200 to 16a4a00 Compare November 27, 2026 08:44
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from 8aa2ca7 to c335c6e Compare November 27, 2026 11:32
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 16a4a00 to 44de39f Compare November 27, 2026 11:32
Base automatically changed from agedemenli/external-engine-table to main November 28, 2026 08:14
Connections that run a query on pgduck_server get the session's
TimeZone, so that time-zone-dependent expressions, such as date_trunc on
a timestamptz, are evaluated as PostgreSQL would. Connections to an
external query engine did not, so a pushed-down query on a
pg_lake_remote table computed in the engine's own time zone.

GetQueryEngineConnection now sets the TimeZone on these connections too,
when it is asked to, through a new SetQueryEngineTimeZone. What a backend
learns about a zone, whether the engine's default already means the same
or whether the engine accepted it, is now kept per engine, since one
engine's answer does not hold for another. An engine has to answer
current_setting('TimeZone') and SET TimeZone, as pgduck_server does.

Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-timezone branch from 44de39f to babda2e Compare November 28, 2026 08:20

@sfc-gh-mslot sfc-gh-mslot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, this fixes the case from #914. One problem: the default-zone cache is per server, but an engine's default can depend on the remote user, as the new per-engine test itself relies on.

Comment thread pg_lake_engine/src/pgduck/client.c Outdated
{
PGDuckDefaultTimeZoneEntry *entry = lfirst(entryCell);

if (entry->serverId == serverId)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

An engine's default zone can differ per remote user (ALTER ROLE ... SET TimeZone), so keying on the server alone isn't enough. With the reader role defaulting to America/Los_Angeles, the owner role to UTC, and the session in America/Los_Angeles, reading the table directly and then through a view owned by another user returns UTC for the view, because the SET is skipped.

Maybe key on server and user, or always send the SET for external engines, since each connection already pays for connect and auth?

Comment thread docs/external-query-engines.md Outdated
`SET TimeZone = '<zone>'` for IANA zone names and `Etc/GMT` offsets, so that the engine
computes with the session's `TimeZone`. pg_lake reads the engine's default zone once per
backend, and sends the session's zone on connections that run a query when it differs. The
first time a backend uses a zone on an engine, pg_lake reads it back, and falls back to

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The fallback relies on the engine ignoring a failed SET, as pgduck_server does. TrySetPGDuckTimeZone goes through CheckPGDuckResult, so an engine that errors on an unknown zone fails the query instead. Maybe say that here, or catch it?

The SET was skipped when the session's zone matched the default zone the
engine reported for its server, but an external engine is reached as the
remote user of a user mapping, and remote users can default to different
zones. A table read through a view, as the view owner's remote user, was
then computed in that user's default zone.

External engines now always get the SET, which costs one round trip on
connections that already pay for a connect and an authentication. Only
pgduck_server's default zone is still read to skip it. Whether an engine
accepted a zone is still kept per engine.

The docs also say that an engine has to accept a SET TimeZone of a zone it
does not support without an error, as pgduck_server does, since an error
fails the query instead of falling back to UTC.

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
userId);

return GetExternalEngineConnection(queryEngine.serverId, userId);
if (setSessionTimeZone)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

since we always reconnect, could we move the setting into the connection parameters? Might also be preferable for existing usages.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done. The zone now goes into options, after the server's own options, so a PostgreSQL engine gets it at connect without a round trip. The SET stays for engines that don't report that zone at startup (pgduck_server and others that ignore options), and for a conninfo that names a service without options, so the service file's options aren't replaced.

Each query opens its own connection to an external engine, so the session's
TimeZone now goes into the options of the connection, after the options of
the server's conninfo, which saves a round trip per query. The SET remains
for engines that do not report the zone when the connection starts, such as
pgduck_server and engines that ignore options, and for conninfos that name a
service but no options, whose service file options would otherwise be
replaced.

A test checks that the zone of a PostgreSQL engine comes from the client
and that the server's own options still apply.

Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants