Skip to content

Commit 900d6af

Browse files
committed
Allow ALTER EXTENSION UPDATE inside a transaction block
Opening a transaction with BEGIN or START TRANSACTION used to load the extension. That made ALTER EXTENSION timescaledb UPDATE fail inside a transaction block because the old version was already loaded before the update ran. The loader now skips loading on BEGIN and START TRANSACTION, so the update can run as the first extension command in the transaction.
1 parent 5d11033 commit 900d6af

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #XXXX Allow running ALTER EXTENSION timescaledb UPDATE inside a transaction block

src/loader/loader.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,27 @@ should_load_on_create_extension(Node const *const utility_stmt, TsExtension cons
392392
return false;
393393
}
394394

395+
static bool
396+
should_load_on_transaction(Node const *const utility_stmt, TsExtension const *const /*ext*/)
397+
{
398+
TransactionStmt *stmt = (TransactionStmt *) utility_stmt;
399+
400+
/*
401+
* Do not load the extension just to open a transaction. This lets ALTER
402+
* EXTENSION ... UPDATE run as the first extension-touching command inside
403+
* a transaction block instead of failing because BEGIN already loaded the
404+
* old version.
405+
*/
406+
switch (stmt->kind)
407+
{
408+
case TRANS_STMT_BEGIN:
409+
case TRANS_STMT_START:
410+
return false;
411+
default:
412+
return true;
413+
}
414+
}
415+
395416
static bool
396417
load_utility_cmd(Node const *const utility_stmt, TsExtension const *const ext)
397418
{
@@ -403,6 +424,8 @@ load_utility_cmd(Node const *const utility_stmt, TsExtension const *const ext)
403424
return should_load_on_alter_extension(utility_stmt, ext);
404425
case T_CreateExtensionStmt:
405426
return should_load_on_create_extension(utility_stmt, ext);
427+
case T_TransactionStmt:
428+
return should_load_on_transaction(utility_stmt, ext);
406429
case T_DropStmt:
407430
return !drop_statement_drops_extension((DropStmt *) utility_stmt, ext);
408431
default:

test/expected/loader-tsl.out

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,28 @@ SELECT * FROM test.extension;
191191
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
192192
timescaledb | mock-2 | public | Enables scalable inserts and complex queries for time-series data
193193

194+
--ALTER EXTENSION UPDATE works inside a transaction block; BEGIN must not
195+
--load the old version before UPDATE runs as the first command.
196+
DROP EXTENSION timescaledb;
197+
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
198+
CREATE EXTENSION timescaledb VERSION 'mock-1';
199+
WARNING: mock init "mock-1"
200+
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
201+
BEGIN;
202+
ALTER EXTENSION timescaledb UPDATE TO 'mock-2';
203+
WARNING: mock init "mock-2"
204+
COMMIT;
205+
SELECT 1;
206+
?column?
207+
----------
208+
1
209+
210+
SELECT * FROM test.extension;
211+
Name | Version | Schema | Description
212+
-------------+---------+------------+-------------------------------------------------------------------
213+
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
214+
timescaledb | mock-2 | public | Enables scalable inserts and complex queries for time-series data
215+
194216
--drop extension
195217
DROP EXTENSION timescaledb;
196218
SELECT 1;

test/sql/loader.sql.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ ALTER EXTENSION timescaledb UPDATE TO 'mock-2';
9393
SELECT 1;
9494
SELECT * FROM test.extension;
9595

96+
--ALTER EXTENSION UPDATE works inside a transaction block; BEGIN must not
97+
--load the old version before UPDATE runs as the first command.
98+
DROP EXTENSION timescaledb;
99+
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
100+
CREATE EXTENSION timescaledb VERSION 'mock-1';
101+
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
102+
BEGIN;
103+
ALTER EXTENSION timescaledb UPDATE TO 'mock-2';
104+
COMMIT;
105+
SELECT 1;
106+
SELECT * FROM test.extension;
107+
96108
--drop extension
97109
DROP EXTENSION timescaledb;
98110
SELECT 1;

0 commit comments

Comments
 (0)