Skip to content

Commit b6263cd

Browse files
committed
Teach relation_is_updatable() about partitioned tables.
Table partitioning, introduced in commit f0e4475, added a new relkind - RELKIND_PARTITIONED_TABLE. Update relation_is_updatable() to handle it. Specifically, partitioned tables and simple views built on top of them are updatable. This affects the SQL-callable functions pg_relation_is_updatable() and pg_column_is_updatable(), and the views information_schema.views and information_schema.columns. Dean Rasheed, reviewed by Ashutosh Bapat. Discussion: https://postgr.es/m/CAEZATCXnbiFkMXgF4Ez1pmM2c-tS1z33bSq7OGbw7QQhHov%2B6Q%40mail.gmail.com
1 parent 2e3fc7a commit b6263cd

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/backend/rewrite/rewriteHandler.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,8 @@ relation_is_updatable(Oid reloid,
24542454
return 0;
24552455

24562456
/* If the relation is a table, it is always updatable */
2457-
if (rel->rd_rel->relkind == RELKIND_RELATION)
2457+
if (rel->rd_rel->relkind == RELKIND_RELATION ||
2458+
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
24582459
{
24592460
relation_close(rel, AccessShareLock);
24602461
return ALL_EVENTS;
@@ -2568,7 +2569,8 @@ relation_is_updatable(Oid reloid,
25682569
base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
25692570
Assert(base_rte->rtekind == RTE_RELATION);
25702571

2571-
if (base_rte->relkind != RELKIND_RELATION)
2572+
if (base_rte->relkind != RELKIND_RELATION &&
2573+
base_rte->relkind != RELKIND_PARTITIONED_TABLE)
25722574
{
25732575
baseoid = base_rte->relid;
25742576
include_cols = adjust_view_column_set(updatable_cols,

src/test/regress/expected/updatable_views.out

+36
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,42 @@ alter table pt11 add a int not null;
23782378
alter table pt1 attach partition pt11 for values from (2) to (5);
23792379
alter table pt attach partition pt1 for values from (1, 2) to (1, 10);
23802380
create view ptv as select * from pt;
2381+
select events & 4 != 0 AS upd,
2382+
events & 8 != 0 AS ins,
2383+
events & 16 != 0 AS del
2384+
from pg_catalog.pg_relation_is_updatable('pt'::regclass, false) t(events);
2385+
upd | ins | del
2386+
-----+-----+-----
2387+
t | t | t
2388+
(1 row)
2389+
2390+
select pg_catalog.pg_column_is_updatable('pt'::regclass, 1::smallint, false);
2391+
pg_column_is_updatable
2392+
------------------------
2393+
t
2394+
(1 row)
2395+
2396+
select pg_catalog.pg_column_is_updatable('pt'::regclass, 2::smallint, false);
2397+
pg_column_is_updatable
2398+
------------------------
2399+
t
2400+
(1 row)
2401+
2402+
select table_name, is_updatable, is_insertable_into
2403+
from information_schema.views where table_name = 'ptv';
2404+
table_name | is_updatable | is_insertable_into
2405+
------------+--------------+--------------------
2406+
ptv | YES | YES
2407+
(1 row)
2408+
2409+
select table_name, column_name, is_updatable
2410+
from information_schema.columns where table_name = 'ptv' order by column_name;
2411+
table_name | column_name | is_updatable
2412+
------------+-------------+--------------
2413+
ptv | a | YES
2414+
ptv | b | YES
2415+
(2 rows)
2416+
23812417
insert into ptv values (1, 2);
23822418
select tableoid::regclass, * from pt;
23832419
tableoid | a | b

src/test/regress/sql/updatable_views.sql

+10
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,16 @@ alter table pt1 attach partition pt11 for values from (2) to (5);
11251125
alter table pt attach partition pt1 for values from (1, 2) to (1, 10);
11261126

11271127
create view ptv as select * from pt;
1128+
select events & 4 != 0 AS upd,
1129+
events & 8 != 0 AS ins,
1130+
events & 16 != 0 AS del
1131+
from pg_catalog.pg_relation_is_updatable('pt'::regclass, false) t(events);
1132+
select pg_catalog.pg_column_is_updatable('pt'::regclass, 1::smallint, false);
1133+
select pg_catalog.pg_column_is_updatable('pt'::regclass, 2::smallint, false);
1134+
select table_name, is_updatable, is_insertable_into
1135+
from information_schema.views where table_name = 'ptv';
1136+
select table_name, column_name, is_updatable
1137+
from information_schema.columns where table_name = 'ptv' order by column_name;
11281138
insert into ptv values (1, 2);
11291139
select tableoid::regclass, * from pt;
11301140
create view ptv_wco as select * from pt where a = 0 with check option;

0 commit comments

Comments
 (0)