Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/external-query-engines.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ DuckDB does not have. The engine has to return these columns in the declared typ
`uuid_extract_version_pg`, and the macros `lake_regexp_matches`, `generate_series_int`,
`generate_series_int_step` and `jsonb`. It also uses DuckDB's `json` and `icu` extensions,
and `spatial` for geospatial queries.
- The session's `TimeZone`, as an IANA zone name or an `Etc/GMT` offset, so that the engine
computes with it. pg_lake adds `-c TimeZone=<zone>` to the `options` of every connection
that runs a query, after the `options` of the server's `conninfo`, and sends
`SET TimeZone = '<zone>'` if the engine does not report that zone when the connection
starts, as PostgreSQL does. An engine, or a connection pooler in front of it, has to accept
`options` or ignore it. If the `conninfo` names a `service` but no `options`, pg_lake only
sends the `SET`, so as not to replace the options of the service file. The first time a
backend uses a zone on an engine, it follows the `SET` with
`SELECT current_setting('TimeZone')`, which has to answer with the zone, to check that the
engine took it, and falls back to `UTC` with a warning if it did not. An engine has to
accept a `SET TimeZone` of a zone that it does not support without an error, as
pgduck_server does; an error fails the query instead.
- `explain (format 'json') <query>`, which returns DuckDB's plan in the second column of one
row, for `EXPLAIN`.
- Only the engine's own tables and views for a table name. An engine must not read a name that
Expand Down
3 changes: 3 additions & 0 deletions pg_lake_engine/include/pg_lake/pgduck/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ extern PGDLLEXPORT PGDuckConnection * TryGetPGDuckConnection(char **errorMessage
extern PGDLLEXPORT PGDuckConnection * TryGetPGDuckConnectionWithConninfo(const char *conninfo,
const char *serverName,
char **errorMessage);
extern PGDLLEXPORT void SetQueryEngineTimeZone(PGDuckConnection * pgDuckConnection,
Oid serverId);
extern PGDLLEXPORT const char *GetQueryEngineTimeZoneName(Oid serverId);
extern PGDLLEXPORT void ReleasePGDuckConnection(PGDuckConnection * pgDuckConnection);
extern PGDLLEXPORT PGconn *ConnectToExternalQueryEngine(const char *conninfo);
extern PGDLLEXPORT void DisconnectFromExternalQueryEngine(PGconn *connection);
Expand Down
98 changes: 74 additions & 24 deletions pg_lake_engine/src/pgduck/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ static bool WaitForLastResultWithTimeout(PGconn *conn, TimestampTz endtime,
typedef struct PGDuckTimeZoneEntry PGDuckTimeZoneEntry;

static void LogPGDuckErrorClass(const char *errorClass);
static void SetPGDuckTimeZone(PGDuckConnection * pgDuckConnection);
static void SetPGDuckTimeZone(PGDuckConnection * pgDuckConnection, Oid serverId);
static void EnsurePGDuckDefaultTimeZone(PGDuckConnection * pgDuckConnection);
static bool PGDuckTimeZoneMatchesDefault(const char *timeZoneName);
static bool TrySetPGDuckTimeZone(PGDuckConnection * pgDuckConnection,
const char *timeZoneName, bool readBack);
static const char *GetPGDuckTimeZoneName(void);
static PGDuckTimeZoneEntry * FindRememberedPGDuckTimeZone(const char *sessionZoneName);
static void RememberPGDuckTimeZone(const char *sessionZoneName,
static PGDuckTimeZoneEntry * FindRememberedPGDuckTimeZone(Oid serverId,
const char *sessionZoneName);
static void RememberPGDuckTimeZone(Oid serverId, const char *sessionZoneName,
const char *engineZoneName);

/* query engine settings */
Expand Down Expand Up @@ -96,12 +97,14 @@ typedef struct PgDuckServerConnectionHashEntry
} PgDuckServerConnectionHashEntry;

/*
* Query engine zone to set for a session TimeZone this backend has used.
* A NULL engineZoneName means pgduck_server's default TimeZone already has
* the same meaning, so no SET is needed.
* Query engine zone to set for a session TimeZone this backend has used on a
* query engine. A NULL engineZoneName means the engine's default TimeZone
* already has the same meaning, so no SET is needed.
*/
struct PGDuckTimeZoneEntry
{
/* foreign server of the query engine, or InvalidOid for pgduck_server */
Oid serverId;
char *sessionZoneName;
char *engineZoneName;
};
Expand Down Expand Up @@ -203,7 +206,7 @@ GetPGDuckConnectionWithSessionTimeZone(void)

PG_TRY();
{
SetPGDuckTimeZone(pgDuckConnection);
SetPGDuckTimeZone(pgDuckConnection, InvalidOid);
}
PG_CATCH();
{
Expand Down Expand Up @@ -276,6 +279,40 @@ TryGetPGDuckConnectionWithConninfo(const char *conninfo, const char *serverName,
}


/*
* SetQueryEngineTimeZone sets the session's TimeZone on a connection to the
* external query engine of the given foreign server, as
* GetPGDuckConnectionWithSessionTimeZone does for pgduck_server. Whether an
* engine accepted a zone is kept per engine, since one engine's answer does
* not hold for another.
*/
void
SetQueryEngineTimeZone(PGDuckConnection * pgDuckConnection, Oid serverId)
{
SetPGDuckTimeZone(pgDuckConnection, serverId);
}


/*
* GetQueryEngineTimeZoneName returns the TimeZone that SetQueryEngineTimeZone
* would set on the external query engine of the given foreign server, for a
* new connection to ask for, or NULL if the engine cannot represent the
* session's TimeZone and SetQueryEngineTimeZone has not fallen back to UTC
* yet.
*/
const char *
GetQueryEngineTimeZoneName(Oid serverId)
{
PGDuckTimeZoneEntry *remembered =
FindRememberedPGDuckTimeZone(serverId, pg_get_timezone_name(session_timezone));

if (remembered != NULL)
return remembered->engineZoneName;

return GetPGDuckTimeZoneName();
}


/*
* TrackPGDuckConnection adds a new connection to the hash, so that it is
* closed when the (sub)transaction that opened it ends.
Expand Down Expand Up @@ -312,16 +349,20 @@ TrackPGDuckConnection(PGconn *connection, const char *serverName,
*
* pgduck_server's default TimeZone is read once per backend so connections in
* a matching session TimeZone (such as UTC against Etc/UTC) skip the SET.
* An external query engine, of the given server, does not skip the SET for
* a matching default: it is reached as the remote user of a user mapping,
* whose default zone can differ from another's.
* Because pgduck_server ignores failed SET commands, non-matching zones read
* the setting back the first time this backend uses them and send only the
* SET on later connections. Both caches assume pgduck_server's TimeZone
* configuration does not change during the backend's lifetime.
* the setting back the first time this backend uses them on an engine and
* send only the SET on later connections. Both caches assume an engine's
* TimeZone configuration does not change during the backend's lifetime.
*/
static void
SetPGDuckTimeZone(PGDuckConnection * pgDuckConnection)
SetPGDuckTimeZone(PGDuckConnection * pgDuckConnection, Oid serverId)
{
const char *sessionZoneName = pg_get_timezone_name(session_timezone);
PGDuckTimeZoneEntry *remembered = FindRememberedPGDuckTimeZone(sessionZoneName);
PGDuckTimeZoneEntry *remembered = FindRememberedPGDuckTimeZone(serverId,
sessionZoneName);

if (remembered != NULL)
{
Expand All @@ -330,26 +371,29 @@ SetPGDuckTimeZone(PGDuckConnection * pgDuckConnection)
return;
}

EnsurePGDuckDefaultTimeZone(pgDuckConnection);
bool useDefault = !OidIsValid(serverId);

if (useDefault)
EnsurePGDuckDefaultTimeZone(pgDuckConnection);

const char *engineZoneName = GetPGDuckTimeZoneName();

if (engineZoneName != NULL)
{
if (PGDuckTimeZoneMatchesDefault(engineZoneName))
if (useDefault && PGDuckTimeZoneMatchesDefault(engineZoneName))
{
RememberPGDuckTimeZone(sessionZoneName, NULL);
RememberPGDuckTimeZone(serverId, sessionZoneName, NULL);
return;
}

if (TrySetPGDuckTimeZone(pgDuckConnection, engineZoneName, true))
{
RememberPGDuckTimeZone(sessionZoneName, engineZoneName);
RememberPGDuckTimeZone(serverId, sessionZoneName, engineZoneName);
return;
}
}

bool utcMatchesDefault = PGDuckTimeZoneMatchesDefault("UTC");
bool utcMatchesDefault = useDefault && PGDuckTimeZoneMatchesDefault("UTC");

if (!utcMatchesDefault || engineZoneName != NULL)
{
Expand All @@ -367,7 +411,8 @@ SetPGDuckTimeZone(PGDuckConnection * pgDuckConnection)
errdetail("Time-zone-dependent results on lake tables may differ "
"from PostgreSQL.")));

RememberPGDuckTimeZone(sessionZoneName, utcMatchesDefault ? NULL : "UTC");
RememberPGDuckTimeZone(serverId, sessionZoneName,
utcMatchesDefault ? NULL : "UTC");
}


Expand Down Expand Up @@ -489,18 +534,20 @@ GetPGDuckTimeZoneName(void)

/*
* FindRememberedPGDuckTimeZone returns the cached entry for the given session
* TimeZone in this backend, or NULL if it has not been used yet.
* TimeZone on the query engine of the given server in this backend, or NULL
* if it has not been used there yet.
*/
static PGDuckTimeZoneEntry *
FindRememberedPGDuckTimeZone(const char *sessionZoneName)
FindRememberedPGDuckTimeZone(Oid serverId, const char *sessionZoneName)
{
ListCell *entryCell = NULL;

foreach(entryCell, PGDuckTimeZoneEntries)
{
PGDuckTimeZoneEntry *entry = lfirst(entryCell);

if (strcmp(entry->sessionZoneName, sessionZoneName) == 0)
if (entry->serverId == serverId &&
strcmp(entry->sessionZoneName, sessionZoneName) == 0)
return entry;
}

Expand All @@ -509,15 +556,18 @@ FindRememberedPGDuckTimeZone(const char *sessionZoneName)


/*
* RememberPGDuckTimeZone records the query engine zone to set for the given
* session TimeZone, or NULL when pgduck_server's default already matches it.
* RememberPGDuckTimeZone records the zone to set on the query engine of the
* given server for the given session TimeZone, or NULL when the engine's
* default already matches it.
*/
static void
RememberPGDuckTimeZone(const char *sessionZoneName, const char *engineZoneName)
RememberPGDuckTimeZone(Oid serverId, const char *sessionZoneName,
const char *engineZoneName)
{
MemoryContext oldContext = MemoryContextSwitchTo(TopMemoryContext);
PGDuckTimeZoneEntry *entry = palloc0(sizeof(PGDuckTimeZoneEntry));

entry->serverId = serverId;
entry->sessionZoneName = pstrdup(sessionZoneName);
entry->engineZoneName = engineZoneName != NULL ? pstrdup(engineZoneName) : NULL;

Expand Down
Loading
Loading