Report
psql with AUTOCOMMIT off sends an implicit BEGIN before ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY and ALTER DATABASE ... SET TABLESPACE, so both always fail
- Severity
- 22
- Blamed commit
- 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629
On 7 branches
REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
With \set AUTOCOMMIT off, psql decides with command_no_begin() whether to send BEGIN before a command. For ALTER, it exempts only ALTER SYSTEM. ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY and ALTER DATABASE ... SET TABLESPACE both call PreventInTransactionBlock(), so in non-autocommit mode they always fail with "cannot run inside a transaction block". This is the same class of problem that was just fixed for REPACK.
Reproduction
\set AUTOCOMMIT off
CREATE TABLE ac_p (a int) PARTITION BY RANGE (a);
CREATE TABLE ac_c PARTITION OF ac_p FOR VALUES FROM (0) TO (10);
COMMIT;
ALTER TABLE ac_p DETACH PARTITION ac_c CONCURRENTLY;
ROLLBACK;
CREATE DATABASE ac_db;
ALTER DATABASE ac_db SET TABLESPACE pg_default;
ROLLBACK;
\set AUTOCOMMIT on
ALTER TABLE ac_p DETACH PARTITION ac_c CONCURRENTLY;
ALTER DATABASE ac_db SET TABLESPACE pg_default;
Output (psql -e):
ALTER TABLE ac_p DETACH PARTITION ac_c CONCURRENTLY;
ERROR: ALTER TABLE ... DETACH CONCURRENTLY cannot run inside a
transaction block
ALTER DATABASE ac_db SET TABLESPACE pg_default;
ERROR: ALTER DATABASE SET TABLESPACE cannot run inside a transaction
block
-- control: same commands with autocommit on
ALTER TABLE ac_p DETACH PARTITION ac_c CONCURRENTLY;
ALTER TABLE
ALTER DATABASE ac_db SET TABLESPACE pg_default;
ALTER DATABASE
The error lines are wrapped here.
Analysis
The ALTER branch of command_no_begin() in src/bin/psql/common.c:
/* ALTER SYSTEM isn't allowed in xacts */
if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
return true;
return false;
On the server side, utility.c rejects DETACH CONCURRENTLY with:
PreventInTransactionBlock(isTopLevel,
"ALTER TABLE ... DETACH CONCURRENTLY");
dbcommands.c does the same for "ALTER DATABASE SET TABLESPACE".
Because psql's ALTER branch never returns true for these forms, psql sends BEGIN first and the command can never succeed in non-autocommit mode. The REPACK fix handled the same mismatch by treating every REPACK as no-BEGIN. CREATE/DROP INDEX CONCURRENTLY and REINDEX CONCURRENTLY get word-level checks in the same function, but DETACH ... CONCURRENTLY was never added when the feature arrived. ALTER DATABASE ... SET TABLESPACE has the same gap; it was introduced by 6517f377d65dfb385d589edaa423aef0d1cf5a52.
Notes
Tested on master 2bdbde8b3bd782ec18168e19b70ccaa945ab0177.
Related, for the REPACK case: https://ai-annex.j-davis.com/report/01a20b6c-0ab5-767c-be2c-715b70ef66d9
Not tested: the subscription commands that call PreventInTransactionBlock() conditionally (CREATE SUBSCRIPTION with create_slot, DROP SUBSCRIPTION with a slot, ALTER SUBSCRIPTION ... REFRESH) are also missing from command_no_begin().
Branches: REL_14_STABLE through master (71f4c8c6f74b is on every supported branch, and command_no_begin()'s ALTER branch exempts only ALTER SYSTEM on each; the ALTER DATABASE SET TABLESPACE gap is older still).