Repository navigation
Pass the session TimeZone to external query engines - #915
sfc-gh-agedemenli wants to merge 3 commits into
Conversation
6b8dbdf to
bdce580
Compare
a584188 to
065be52
Compare
bdce580 to
5a16057
Compare
065be52 to
1c96aff
Compare
5a16057 to
7680ff9
Compare
1c96aff to
da2b434
Compare
7680ff9 to
07298d6
Compare
da2b434 to
c6b2cc2
Compare
07298d6 to
51b5440
Compare
51b5440 to
7fe2200
Compare
e371e44 to
8aa2ca7
Compare
7fe2200 to
16a4a00
Compare
8aa2ca7 to
c335c6e
Compare
16a4a00 to
44de39f
Compare
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>
44de39f to
babda2e
Compare
sfc-gh-mslot
left a comment
There was a problem hiding this comment.
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.
| { | ||
| PGDuckDefaultTimeZoneEntry *entry = lfirst(entryCell); | ||
|
|
||
| if (entry->serverId == serverId) |
There was a problem hiding this comment.
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?
| `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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
since we always reconnect, could we move the setting into the connection parameters? Might also be preferable for existing usages.
There was a problem hiding this comment.
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>
#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 atimestamptz, are evaluated as PostgreSQL would. Connections to an external query engine didn't get it, so a pushed-down query on apg_lake_remotetable was still computed in the engine's own time zone: underAsia/Tokyo,date_trunc('day', ts)returned the UTC day boundary.How
options(-c TimeZone=<zone>), after theoptionsof the server'sconninfo. A PostgreSQL engine starts in the zone, without an extra round trip.optionsdon't, pg_lake sendsSET TimeZone. It also only sends theSETwhen theconninfonames aservicebut nooptions, so the service file's options aren't replaced.SETbecause 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.SETis checked the first time a backend uses it, and kept per engine, since one engine's answer doesn't hold for another.options, answercurrent_setting('TimeZone'), and accept aSET TimeZoneof a zone it doesn't support without an error, as pgduck_server does. The docs say so.Tests
pg_lake_remotetable under several session time zones, compared with a heap table.optionsstill applied.