Skip to content

Commit 75df0a6

Browse files
authored
Fix crash in timezone assign hook (#981)
The timezone assign hook could sometimes be run outside of a Postgres transaction context and this would then cause a crash. This fixes that. Fixes #975 Fixes #938
1 parent 3a7c054 commit 75df0a6

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/pgduckdb_guc.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
extern "C" {
1111
#include "postgres.h"
12+
#include "access/xact.h"
1213
#include "utils/guc.h"
1314
#include "utils/guc_tables.h"
1415
#include "miscadmin.h" // DataDir
@@ -272,8 +273,12 @@ DuckAssignTimezone_Cpp(const char *tz) {
272273
return;
273274
}
274275

275-
// update duckdb tz
276-
auto connection = pgduckdb::DuckDBManager::GetConnection(false);
276+
// Update timezone in DuckDB too.
277+
// This uses GetConnectionUnsafe because GetConnection requires a
278+
// transaction context to be active in Postgres to be able to call
279+
// RefreshConnectionState, and GUCs can be changed outside of transaction
280+
// blocks (for instance by being reverted due to SET LOCAL or by SIGHUP)
281+
auto connection = pgduckdb::DuckDBManager::GetConnectionUnsafe();
277282
pgduckdb::DuckDBQueryOrThrow(*connection, "SET TimeZone =" + duckdb::KeywordHelper::WriteQuoted(tz));
278283
elog(DEBUG2, "[PGDuckDB] Set DuckDB option: 'TimeZone'=%s", tz);
279284
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Initialize duckdb in this session
2+
SELECT * FROM duckdb.query($$ SELECT 1 $$);
3+
1
4+
---
5+
1
6+
(1 row)
7+
8+
-- Have the timezone change be rolled back at the end transaction
9+
BEGIN;
10+
SET TimeZone TO 'UTC';
11+
ROLLBACK;

test/regression/schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test: issue_789
2929
test: issue_796
3030
test: issue_802
3131
test: issue_813
32+
test: issue_975
3233
test: json_functions_duckdb
3334
test: materialized_view
3435
test: non_superuser

test/regression/sql/issue_975.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Initialize duckdb in this session
2+
SELECT * FROM duckdb.query($$ SELECT 1 $$);
3+
-- Have the timezone change be rolled back at the end transaction
4+
BEGIN;
5+
SET TimeZone TO 'UTC';
6+
ROLLBACK;

0 commit comments

Comments
 (0)