Re: Two bugs around ALTER TYPE

From: Rui Zhao <zhaorui126(at)gmail(dot)com>
To: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Cc: surya poondla <suryapoondla4(at)gmail(dot)com>, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Two bugs around ALTER TYPE
Date: 2026-11-16 07:47:27
Message-ID: CAHWVJhHxw8m_eyiFTcV8ch4sHGDBs9jaQgi2kzh8mkQ_DEHuhA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Andrey,

Thanks for working on these fixes. 0002 fixes the cached plan issue you
reported after ALTER TYPE changes a composite return type. I also
tested adding and dropping attributes and changing attribute types.

However, 0004 still leaves a window before the function acquires its
lock. A cursor can save the old result descriptor at DECLARE, then
enter the function after another session has changed the type.
This fails on both master at 2e0fe6d5ace and the full v2 series:

CREATE TYPE planinv_ct AS (a int, b int);
CREATE TABLE planinv_tbl (a int, b int);
INSERT INTO planinv_tbl VALUES (1, 2);
CREATE FUNCTION planinv_caller() RETURNS SETOF planinv_ct
LANGUAGE plpgsql AS $$
BEGIN
RETURN QUERY SELECT * FROM planinv_tbl;
END;
$$;

-- Session 1:
BEGIN;
DECLARE c CURSOR FOR SELECT * FROM planinv_caller();

-- Session 2: completes without waiting for session 1.
BEGIN;
ALTER TYPE planinv_ct ADD ATTRIBUTE c int;
ALTER TABLE planinv_tbl ADD COLUMN c int DEFAULT 99;
COMMIT;

-- Session 1:
FETCH ALL FROM c;
ERROR: structure of query does not match function result type
DETAIL: Number of returned columns (3) does not match expected
column count (2).
ROLLBACK;

The error occurs because ExecInitFunctionScan() copies the two-column
descriptor during DECLARE.
At FETCH, ExecMakeTableFunctionResult() passes it as rsi->expectedDesc.
Only then does plpgsql_estate_setup() acquire the new lock.
exec_init_tuple_store() still uses that old descriptor, while RETURN
QUERY reads three columns. The lock needs to protect the caller's
descriptor setup too. Acquiring it at PL/pgSQL function entry is
too late.

I chose init_sexpr() because both SRF call paths reach it before
copying the result descriptor. The attached patch, on top of v2,
moves the lock there. It covers SETOF functions returning named
composites or domains over them. The lock still lasts until
transaction end, but now protects DECLARE before the first FETCH too.

I also considered these places for the lock:

1. ExecInitFunctionScan() covers FROM f(), but not SELECT f().
The latter would need a separate lock in ExecInitFunctionResultSet().
2. get_expr_result_type() is also used by the parser and planner.
Adding a lock there would affect more than SRF execution.

So I settled on init_sexpr().

I extended rowtypes-alter.spec, reusing its type, table and cursor
steps. The four added schedules cover both call forms, a domain, and
reuse of a cached SPI plan. All four fail on v2 and pass with the patch.
Core regression, isolation, and your TAP test passed.

I also synchronized alter_table.out with the comments changed by
0005; that mismatch was the only core regression failure on v2.

Regards,
Rui

Attachment Content-Type Size
nocfbot-0001-Lock-composite-SRF-result-types-before-copying-their.patch application/octet-stream 12.2 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2026-11-16 08:01:04 RE: BUG #19892: Logical replication skips UPDATE/DELETE of rows with NULL in a hash-indexed subscriber column
Previous Message Raja Sai pranav 2026-11-16 06:03:01 Re: Re: Re: BUG #19776: pg_combinebackup ignores the truncation_block_length of an intermediate incremental backup