Skip to content

Commit 3c49d46

Browse files
committed
Disallow NAMEDTUPLESTORE RTEs in stored views, rules, etc.
A named tuplestore is necessarily a transient object, so it makes no sense to reference one in a persistent object such as a view. We didn't previously prevent that, with the result that if you tried you would get some weird failure about how the executor couldn't find the tuplestore. We can mechanize a check for this case cheaply by making dependency extraction complain if it comes across such an RTE. This is a plausible way of dealing with it since part of the problem is that we have no way to make a pg_depend representation of a named tuplestore. Report and fix by Yugo Nagata. Although this is an old problem, it's a very weird corner case and there have been no reports from end users. So it seems sufficient to fix it in master. Discussion: https://postgr.es/m/20240726160714.e74d0db579f2c017e1ca0b7e@sraoss.co.jp
1 parent b20fe54 commit 3c49d46

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/backend/catalog/dependency.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,22 @@ find_expr_references_walker(Node *node,
21912191
}
21922192
context->rtables = list_delete_first(context->rtables);
21932193
break;
2194+
case RTE_NAMEDTUPLESTORE:
2195+
2196+
/*
2197+
* Cataloged objects cannot depend on tuplestores, because
2198+
* those have no cataloged representation. For now we can
2199+
* call the tuplestore a "transition table" because that's
2200+
* the only kind exposed to SQL, but someday we might have
2201+
* to work harder.
2202+
*/
2203+
ereport(ERROR,
2204+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2205+
errmsg("transition table \"%s\" cannot be referenced in a persistent object",
2206+
rte->eref->aliasname)));
2207+
break;
21942208
default:
2209+
/* Other RTE types can be ignored here */
21952210
break;
21962211
}
21972212
}

src/test/regress/expected/triggers.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,26 @@ create trigger my_table_col_update_trig
33153315
ERROR: transition tables cannot be specified for triggers with column lists
33163316
drop table my_table;
33173317
--
3318+
-- Verify that transition tables can't be used in, eg, a view.
3319+
--
3320+
create table my_table (a int);
3321+
create function make_bogus_matview() returns trigger as
3322+
$$ begin
3323+
create materialized view transition_test_mv as select * from new_table;
3324+
return new;
3325+
end $$
3326+
language plpgsql;
3327+
create trigger make_bogus_matview
3328+
after insert on my_table
3329+
referencing new table as new_table
3330+
for each statement execute function make_bogus_matview();
3331+
insert into my_table values (42); -- error
3332+
ERROR: transition table "new_table" cannot be referenced in a persistent object
3333+
CONTEXT: SQL statement "create materialized view transition_test_mv as select * from new_table"
3334+
PL/pgSQL function make_bogus_matview() line 2 at SQL statement
3335+
drop table my_table;
3336+
drop function make_bogus_matview();
3337+
--
33183338
-- Test firing of triggers with transition tables by foreign key cascades
33193339
--
33203340
create table refd_table (a int primary key, b text);

src/test/regress/sql/triggers.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,25 @@ create trigger my_table_col_update_trig
24342434

24352435
drop table my_table;
24362436

2437+
--
2438+
-- Verify that transition tables can't be used in, eg, a view.
2439+
--
2440+
2441+
create table my_table (a int);
2442+
create function make_bogus_matview() returns trigger as
2443+
$$ begin
2444+
create materialized view transition_test_mv as select * from new_table;
2445+
return new;
2446+
end $$
2447+
language plpgsql;
2448+
create trigger make_bogus_matview
2449+
after insert on my_table
2450+
referencing new table as new_table
2451+
for each statement execute function make_bogus_matview();
2452+
insert into my_table values (42); -- error
2453+
drop table my_table;
2454+
drop function make_bogus_matview();
2455+
24372456
--
24382457
-- Test firing of triggers with transition tables by foreign key cascades
24392458
--

0 commit comments

Comments
 (0)