Reject columns of permanent relations that depend on temporary objects

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>, Nikhil Sontakke <nikhil(at)planetscale(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Reject columns of permanent relations that depend on temporary objects
Date: 2026-11-29 05:25:23
Message-ID: 179592992325.510294.16785143669833910636@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

The attached patch rejects a column of a permanent table or composite
type whose type or collation depends on a temporary object. Today such
a column is dropped with its data at session exit, with no error and no
notice:

-- session 1
CREATE TEMP TABLE tt (x int);
CREATE TABLE perm (a int, b tt);
INSERT INTO perm VALUES (1, ROW(10));

-- session 2
SELECT * FROM perm; -- only column a is left

This was discussed in the thread that led to 572c40ba9 [1], and I'd
like to start from what was decided there. For functions, Tom first
wondered about forbidding them, then made it a notice, because several
regression tests relied on the old behavior and "I wonder if there
aren't plenty of end users depending on it too". 698fa924b11 then
kept an error for materialized views. When Jim showed this column
case, Tom answered that he could "recall approximately zero field
complaints about it", and that find_temp_object() "necessarily
involves at least two syscache lookups per dependency" [2].

What I measured on those two points:

- Tests: in a check-world run with the new check turned into a LOG
line instead of an error, no test outside the new ones created such
a column. Unlike the function case, nothing in our own tests depends
on it.
- Cost: the check runs only in DDL, and it stops at objects created
by initdb, which are never temporary, so a column of a built-in type
costs no catalog lookup. Creating 1000 domain columns, or a column
of a composite type nested 16 levels deep, showed no measurable
difference with and without the patch.

I don't know of a field complaint either. What I can add is that
Nikhil ran into the same loss from another side, a composite type
whose stored values lose an attribute [3], and that master already
refuses the neighbouring cases: a materialized view using a temporary
object, a foreign key to a temporary table, and a permanent partition
of a temporary table. Data is what is lost here, which is why I made
it an error; if a notice like the one for functions is preferred, that
is a small change in one function.

Following Tom's suggestion in [1] not to keep a list of object kinds,
the patch follows pg_depend down from the column's type, so domains,
arrays, ranges (including a temporary subtype_diff), multiranges,
composite types and collations are all covered. Temporary relations
and the transient heap of a rewrite are not affected. As it rejects
DDL that used to be accepted, it is for master only.

Nikhil, this is the separate thread I mentioned. The other cases you
listed (check constraints, column defaults, expression indexes and
partition keys) and a few more, such as policies, can be checked where
their dependencies are collected, with find_temp_object() as in
572c40ba9. I have that as a second patch, and I'll post it here if
this direction is acceptable.

[1] https://postgr.es/m/19cf6ae1-04cd-422c-a760-d7e75fe6cba9@uni-muenster.de
[2] https://postgr.es/m/1010769.1764018910@sss.pgh.pa.us
[3] https://postgr.es/m/CA+UBoq0bJZOU2_v65fdAqT2K6pmLbnaUvYSL8f00Ntbnfj+L+w@mail.gmail.com

Regards,
Manu

Attachment Content-Type Size
v1-0001-Reject-temporary-objects-in-columns-of-permanent-.patch text/x-patch 27.3 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Michael Paquier 2026-11-29 03:47:37 Re: Two fixes for parallel query cleanup: DSM detach order and a stray statement timeout