| From: | hi(at)joel(dot)id |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | EXCEPTION block in a PARALLEL SAFE function fails CREATE TABLE AS |
| Date: | 2026-12-04 09:24:06 |
| Message-ID: | 18ee02aa82bb60b4.9773c90d0c97c59d.9b1479a7615ca4d9@neubau |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
PostgreSQL 18.6 on aarch64 Linux; the code below is unchanged on master.
A PARALLEL SAFE PL/pgSQL function with an EXCEPTION block fails when the
leader of a parallel plan runs it inside CREATE TABLE AS, SELECT INTO or
CREATE MATERIALIZED VIEW. The same SELECT on its own succeeds. The docs
allow this: functions must be marked PARALLEL UNSAFE if they "change the
transaction state (other than by using a subtransaction for error
recovery)".
Reproducer:
CREATE FUNCTION g(x int) RETURNS int LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE AS $$
BEGIN
RETURN x + 1;
EXCEPTION WHEN division_by_zero THEN RETURN NULL;
END $$;
CREATE TABLE big AS SELECT g AS x FROM generate_series(1, 1000000) g;
ANALYZE big;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SELECT count(g(x)) FROM big; -- works
CREATE TABLE c1 AS SELECT g(x) FROM big; -- fails
WARNING: AbortSubTransaction while in COMMIT state
ERROR: cannot start commands during a parallel operation
CONTEXT: PL/pgSQL function g(integer) line 2 during statement block exit
SELECT g(x) INTO c2 FROM big and CREATE MATERIALIZED VIEW m AS SELECT
g(x) FROM big fail the same way. With debug_parallel_query = on and
max_parallel_workers = 0 it fails on a 10-row table.
What I believe happens: CommitSubTransaction calls
CommandCounterIncrement ("Must CCI to ensure commands of subtransaction
are seen as done"). In the leader of a writing statement,
currentCommandIdUsed is already true from the rows it has inserted, so
CommandCounterIncrement raises because IsInParallelMode(). The comment
added with internal subtransactions in parallel mode says they are
allowed "so long as no new XIDs or command IDs are assigned"; this
subtransaction assigns none itself, but its commit still increments the
counter. The error is then raised in COMMIT state, hence the warning.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | hi | 2026-12-04 10:08:32 | Large regex fails with XX000 instead of a regex error |
| Previous Message | PG Bug reporting form | 2026-12-04 09:15:19 | BUG #19978: synchronized_standby_slots: stale ss_oldest_flush_lsn cache bypasses slot validation |