Skip to content

Commit

Permalink
Allow EXPLAIN in read-only mode
Browse files Browse the repository at this point in the history
Using `EXPLAIN` when `transaction_read_only` is set to `on` fails with
an error. This is fixed by explicitly setting the `check_read_only`
flag.
  • Loading branch information
mkindahl committed Jan 30, 2025
1 parent 64e5ffc commit 15365a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/process_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -4859,6 +4859,7 @@ process_ddl_command_start(ProcessUtilityArgs *args)
handler = preprocess_execute;
break;
case T_ExplainStmt:
check_read_only = false;
handler = process_explain_start;
break;

Expand Down
19 changes: 19 additions & 0 deletions tsl/test/expected/read_only.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,26 @@
-- create_hypertable()
--
CREATE TABLE test_table(time bigint NOT NULL, device int);
-- EXPLAIN should work in read-only mode, when enabling in transaction.
START TRANSACTION;
SET transaction_read_only TO on;
EXPLAIN (COSTS OFF) SELECT * FROM test_table;
QUERY PLAN
------------------------
Seq Scan on test_table
(1 row)

ROLLBACK;
SET default_transaction_read_only TO on;
-- EXPLAIN should work in read-only mode, even when using the default.
START TRANSACTION;
EXPLAIN (COSTS OFF) SELECT * FROM test_table;
QUERY PLAN
------------------------
Seq Scan on test_table
(1 row)

ROLLBACK;
\set ON_ERROR_STOP 0
SELECT * FROM create_hypertable('test_table', 'time');
ERROR: cannot execute create_hypertable() in a read-only transaction
Expand Down
12 changes: 12 additions & 0 deletions tsl/test/sql/read_only.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@
--
CREATE TABLE test_table(time bigint NOT NULL, device int);

-- EXPLAIN should work in read-only mode, when enabling in transaction.
START TRANSACTION;
SET transaction_read_only TO on;
EXPLAIN (COSTS OFF) SELECT * FROM test_table;
ROLLBACK;

SET default_transaction_read_only TO on;

-- EXPLAIN should work in read-only mode, even when using the default.
START TRANSACTION;
EXPLAIN (COSTS OFF) SELECT * FROM test_table;
ROLLBACK;

\set ON_ERROR_STOP 0
SELECT * FROM create_hypertable('test_table', 'time');
\set ON_ERROR_STOP 1
Expand Down Expand Up @@ -213,3 +224,4 @@ SELECT remove_retention_policy('test_table');
SELECT add_job('now','12h');
SELECT alter_job(1,scheduled:=false);
SELECT delete_job(1);

0 comments on commit 15365a3

Please sign in to comment.