Re: Two bugs around ALTER TYPE

From: Andres Freund <andres(at)anarazel(dot)de>
To: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Cc: PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Subject: Re: Two bugs around ALTER TYPE
Date: 2026-11-16 19:14:32
Message-ID: qraomovaabqnuna6ublftvphngh4pccwqog5bzh6oapp27pybg@iw4s62l43qc4
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On 2026-11-10 16:32:14 +0500, Andrey Borodin wrote:
> Thank you for the substantial work on tuple descriptors and attribute
> versioning in the new release.
>
> Unfortunately, both bugs reported in this thread still reproduce on
> new minors.

Yea, since they weren't really security relevant, it didn't seem necessary to
go through the security process for them. They're also only partial fixes to
the problems they're trying to address, so we can't rely on them as a hard
barrier.

> From 36451d306ea25e97b431c9496744813bdaca9859 Mon Sep 17 00:00:00 2001
> From: Andrey Borodin <amborodin(at)acm(dot)org>
> Date: Tue, 7 Apr 2026 15:28:06 +0500
> Subject: [PATCH v2 2/5] Track RTE_FUNCTION composite rowtype dependencies
>
> Record typrelid dependencies for named composite outputs of RTE_FUNCTION
> nodes, so relcache invalidation from ALTER TYPE marks cached plans stale.
>
> This keeps prepared and SPI plans in sync with composite rowshape changes.
> ---
> doc/src/sgml/ref/alter_type.sgml | 9 +++++++
> src/backend/optimizer/plan/setrefs.c | 36 ++++++++++++++++++++++++++++
> 2 files changed, 45 insertions(+)
>
> diff --git a/doc/src/sgml/ref/alter_type.sgml b/doc/src/sgml/ref/alter_type.sgml
> index 025a3ee48f5..72b046afd98 100644
> --- a/doc/src/sgml/ref/alter_type.sgml
> +++ b/doc/src/sgml/ref/alter_type.sgml
> @@ -242,6 +242,15 @@ ALTER TYPE <replaceable class="parameter">name</replaceable> SET ( <replaceable
> and/or alter the type of several attributes in a single command.
> </para>
>
> + <para>
> + Changing a composite type's attributes also invalidates cached plans for
> + queries that scan <literal>SETOF</literal> functions returning that
> + composite type (including through a domain over the composite), even when
> + no <command>CREATE OR REPLACE FUNCTION</command> is run. This matches the
> + relcache invalidation already sent for the composite type's cataloged
> + rowtype.
> + </para>

That doesn't feel like the kind of detail we normally have in the docs.

> +/*
> + * Record relcache dependencies for RTE_FUNCTION entries whose declared result
> + * is a named composite type (or a domain over one). ALTER TYPE {ADD | DROP |
> + * ALTER} ATTRIBUTE updates the composite type's pg_class row without changing
> + * OIDs of dependent functions, so plans must be rebuilt when the composite
> + * rowtype changes. (Compare extract_query_dependencies_walker, which must
> + * stay in sync.)
> + */
> +static void
> +add_function_rte_relation_deps(PlannerGlobal *glob, const RangeTblEntry *rte)
> +{
> + ListCell *lc;
> +
> + Assert(rte->rtekind == RTE_FUNCTION);
> +
> + foreach(lc, rte->functions)
> + {
> + RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
> + Oid typid;
> + Oid typrelid;
> +
> + typid = exprType(rtfunc->funcexpr);
> + typrelid = typeOrDomainTypeRelid(typid);
> + if (OidIsValid(typrelid))
> + glob->relationOids = lappend_oid(glob->relationOids, typrelid);
> + }
> +}

Does this actually work right for things like arrays of named composite types?

Also, I'm pretty sure this won't reliably work for things like nested
composite types were only the inner type is changed.

Seems like this should have tests documenting cases in which we now do inval?
There should be plenty non-race paths?

> From 9557e372fcf7822afa4ef64dec498ceaeaefefb8 Mon Sep 17 00:00:00 2001
> From: Andrey Borodin <amborodin(at)acm(dot)org>
> Date: Tue, 14 Apr 2026 14:29:14 +0500
> Subject: [PATCH v2 3/5] Add TAP test for plpgsql RETURN QUERY DDL race
>
> Add an injection-point TAP reproducer for concurrent ALTER TYPE +
> CREATE OR REPLACE FUNCTION during an in-progress RETURN QUERY call.
>

> ---
> src/pl/plpgsql/src/pl_exec.c | 6 ++
> src/test/modules/test_misc/meson.build | 1 +
> .../t/012_plpgsql_composite_replan_race.pl | 96 +++++++++++++++++++

Adding dedicated tap tests for stuff like this does not scale. Each of them
does an initdb (or copies a cluster). That's a fair bit of IO.

I think this doesn't even need an injection point, it's probably quite easy to
repro with just an advisory lock acquisition? Then make it an isolation
test...

> From f298c979b11983851177cce465cce54772278cc7 Mon Sep 17 00:00:00 2001
> From: Andrey Borodin <amborodin(at)acm(dot)org>
> Date: Tue, 14 Apr 2026 14:29:21 +0500
> Subject: [PATCH v2 4/5] Lock SETOF composite return type during plpgsql
> execution
>
> For SETOF PL/pgSQL functions returning named composite tuples, acquire
> AccessShareLock on the return type's typrelid at function entry.
>
> This keeps rowshape stable for the whole execution under concurrent
> ALTER TYPE and avoids mid-statement mismatch errors.

I really doubt this makes sense. There's a lot of places that are affected by
concurrent rowtype changes. Why would we want to specifically fix this one
with locking?

Greetings,

Andres

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-11-16 19:28:59 Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
Previous Message Andrey Rachitskiy 2026-11-16 19:10:39 Re: BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails