BUG #19940: Permanent partitioned table silently dropped on DISCARD TEMP when partition key uses temp row type
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
The following bug has been logged on the website:
Bug reference: 19940
Logged by: Zheng Hacker
Email address: hackerzheng666@gmail.com
PostgreSQL version: 18.7
Operating system: Ubuntu 22.04 host, Debian 13 container
Description:
Also reproduced on PostgreSQL master at commit
70c4a57b64d39c526eb4c44a925eb92a6e56f3c5.
Summary: A permanent partitioned table whose partition key uses the row type
of a temp table is silently dropped, together with all of its partitions and
rows, by DISCARD TEMP or when the session ends. CREATE TABLE accepts it, and
no NOTICE or WARNING is printed at the default client_min_messages. Found
while testing.
Reproduction: each run used a fresh container from the official postgres:18
image (initdb by its entrypoint), with psql run inside it as -q -t -A. Two
runs gave identical output. Session A is the DISCARD TEMP path:
CREATE TEMP TABLE tt (a int);
CREATE TABLE pt (k int NOT NULL, v int) PARTITION BY RANGE
(((ROW(k)::tt).a));
CREATE TABLE pt_lo PARTITION OF pt FOR VALUES FROM (1) TO (51);
CREATE TABLE pt_hi PARTITION OF pt FOR VALUES FROM (51) TO (101);
INSERT INTO pt SELECT g, g FROM generate_series(1, 100) AS g;
SELECT 'before DISCARD TEMP: count(*)=' || count(*) FROM pt;
SELECT 'before DISCARD TEMP: pt, pt_lo, pt_hi=' ||
coalesce(to_regclass('public.pt')::text, 'NULL') || ', ' ||
coalesce(to_regclass('public.pt_lo')::text, 'NULL') || ', ' ||
coalesce(to_regclass('public.pt_hi')::text, 'NULL');
DISCARD TEMP;
SELECT 'after DISCARD TEMP: pt, pt_lo, pt_hi=' ||
coalesce(to_regclass('public.pt')::text, 'NULL') || ', ' ||
coalesce(to_regclass('public.pt_lo')::text, 'NULL') || ', ' ||
coalesce(to_regclass('public.pt_hi')::text, 'NULL');
SELECT 'after DISCARD TEMP: count(*)=' || count(*) FROM pt;
Output:
before DISCARD TEMP: count(*)=100
before DISCARD TEMP: pt, pt_lo, pt_hi=pt, pt_lo, pt_hi
after DISCARD TEMP: pt, pt_lo, pt_hi=NULL, NULL, NULL
ERROR: relation "pt" does not exist
LINE 1: SELECT 'after DISCARD TEMP: count(*)=' || count(*) FROM pt;
^
Session exit: session s1 runs SET application_name = 's1'; then the same
first five statements as session A (tt, pt, pt_lo, pt_hi and the INSERT),
then SELECT count(*) FROM pt, and it exits without DISCARD TEMP. After s1
has gone, session 2 runs:
SELECT 'session 2: pt, pt_lo, pt_hi=' ||
coalesce(to_regclass('public.pt')::text, 'NULL') || ', ' ||
coalesce(to_regclass('public.pt_lo')::text, 'NULL') || ', ' ||
coalesce(to_regclass('public.pt_hi')::text, 'NULL');
SELECT 'session 2: count(*)=' || count(*) FROM pt;
Output (session s1, then session 2):
session s1 before exit: count(*)=100
session 2: pt, pt_lo, pt_hi=NULL, NULL, NULL
ERROR: relation "pt" does not exist
LINE 1: SELECT 'session 2: count(*)=' || count(*) FROM pt;
^
Column variant: a permanent table p2 whose column y has the temp row type
(same psql options):
CREATE TEMP TABLE tt (a int);
CREATE TABLE p2 (id int, y tt);
INSERT INTO p2 VALUES (1, ROW(11)::tt), (2, ROW(22)::tt), (3, ROW(33)::tt);
SELECT 'before DISCARD TEMP: columns=' || string_agg(attname, ',' ORDER BY
attnum) FROM pg_attribute WHERE attrelid = 'public.p2'::regclass AND attnum
0 AND NOT attisdropped;
SELECT 'before DISCARD TEMP: count(*)=' || count(*) || ' y.a=' ||
string_agg((y).a::text, ',' ORDER BY id) FROM p2;
DISCARD TEMP;
SELECT 'after DISCARD TEMP: columns=' || string_agg(attname, ',' ORDER BY
attnum) FROM pg_attribute WHERE attrelid = 'public.p2'::regclass AND attnum
0 AND NOT attisdropped;
SELECT 'after DISCARD TEMP: count(*)=' || count(*) FROM p2;
SELECT * FROM p2 ORDER BY id;
Output:
before DISCARD TEMP: columns=id,y
before DISCARD TEMP: count(*)=3 y.a=11,22,33
after DISCARD TEMP: columns=id
after DISCARD TEMP: count(*)=3
1
2
3
Variants (same DDL pattern, same DISCARD TEMP or session end; all checked on
18.7 unless marked master):
- Refused at CREATE: a FOREIGN KEY from a permanent table to a temp table
(tablecmds.c: "constraints on permanent tables may reference only permanent
tables"); a materialized view over a temp object (18.7: "materialized views
must not use temporary tables or views"; master, parser/analyze.c:
"materialized views must not use temporary objects").
- NOTICE at CREATE, object removed later: a view over a temp table (view.c:
"will be a temporary view").
- Removed with no message on 18.7: a function with a temp argument type
(master 70c4a57 prints a NOTICE instead, pg_proc.c: "will be effectively
temporary"); a column of a permanent table (its data is lost; the other
column and all rows stay); a CHECK constraint; an expression index; a column
default; a stored generated column.
- CREATE TYPE pty AS (a tt) gives no message. After session end, pty is
still in pg_type (typtype c) but has no attributes.
Earlier discussion: the reply to BUG #19023
(/messages/by-id/347682.1755525955@sss.pgh.pa.us)
says: "Appears to me to operate as intended: the DEFAULT will be dropped at
session exit, just as if you'd done DROP SEQUENCE s1 CASCADE." The
2025-11-24 message in the thread "Add notification on BEGIN ATOMIC SQL
functions using temp relations"
(/messages/by-id/1010769.1764018910@sss.pgh.pa.us)
says: "It's been like that for a very long time, yet I can recall
approximately zero field complaints about it." This report is a different
case: the cascade removes a whole permanent table with all of its rows, not
only a default or a column, and it happens on a plain DISCARD TEMP.
Suggestion, for the maintainers to decide: refuse a permanent object's
dependency on a temp row type at CREATE, the way FOREIGN KEY and
materialized view dependencies are refused now, or at least raise a WARNING
when such a dependency is created.