Skip to content

Commit d364a23

Browse files
authored
Fix CREATE TYPE in namespace pg_ext_aux (#380)
Creating relation in namespace pg_ext_aux will not create the array type. The relations under pg_ext_aux are considered as auxiliary tables, and used by some extensions. While creating type is different, the new type will also have its array type like other normal types, which follows behavior of pg_catalog.
1 parent d02a01d commit d364a23

4 files changed

Lines changed: 152 additions & 1 deletion

File tree

src/backend/catalog/heap.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,10 @@ heap_create_with_catalog(const char *relname,
16541654
* NAMEDATALEN and gets truncated. Then the name may same with other child
16551655
* table's.
16561656
*
1657+
* Auxiliary relations in pg_ext_aux don't need array type. Relations in
1658+
* pg_ext_aux are assumed simple relation, their array types are unexpected
1659+
* to be used in the extension code.
1660+
*
16571661
* The below code is different from upstream since we preassign type
16581662
* OID first on QD and use the name as key to retrieve the pre-assigned
16591663
* OID from QE.
@@ -1666,7 +1670,10 @@ heap_create_with_catalog(const char *relname,
16661670
relkind == RELKIND_AOBLOCKDIR ||
16671671
relkind == RELKIND_AOVISIMAP ||
16681672
relnamespace == PG_BITMAPINDEX_NAMESPACE ||
1669-
relnamespace == PG_EXTAUX_NAMESPACE))
1673+
(relnamespace == PG_EXTAUX_NAMESPACE &&
1674+
(relkind == RELKIND_RELATION ||
1675+
relkind == RELKIND_VIEW ||
1676+
relkind == RELKIND_MATVIEW))))
16701677
{
16711678
/* OK, so pre-assign a type OID for the array type */
16721679
Oid new_array_oid;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-- Test use of namespace pg_ext_aux
2+
-- Test 1: Create database object in pg_ext_aux should fail normally
3+
create table pg_ext_aux.extaux_t(a int, b int);
4+
ERROR: permission denied to create "pg_ext_aux.extaux_t"
5+
DETAIL: System catalog modifications are currently disallowed.
6+
create view pg_ext_aux.extaux_v as select oid, relname from pg_class;
7+
ERROR: permission denied to create "pg_ext_aux.extaux_v"
8+
DETAIL: System catalog modifications are currently disallowed.
9+
create materialized view pg_ext_aux.extaux_mv as select oid, relname from pg_class;
10+
ERROR: permission denied to create "pg_ext_aux.extaux_mv"
11+
DETAIL: System catalog modifications are currently disallowed.
12+
-- shell type is created successfully
13+
create type pg_ext_aux.extaux_shell_type;
14+
-- concreate type will fail
15+
create type pg_ext_aux.extaux_concreat_type AS (sum int, count int);
16+
ERROR: permission denied to create "pg_ext_aux.extaux_concreat_type"
17+
DETAIL: System catalog modifications are currently disallowed.
18+
-- create function will success
19+
create function pg_ext_aux.extaux_add1(i integer) returns integer AS $$
20+
begin
21+
return i + 1;
22+
end;
23+
$$ language plpgsql;
24+
-- Test 2: Create database object in pg_ext_aux should success
25+
-- if allow_system_table_mods is on
26+
set allow_system_table_mods = on;
27+
create table pg_ext_aux.extaux_t(a int, b text);
28+
create view pg_ext_aux.extaux_v as select * from pg_ext_aux.extaux_t;
29+
create materialized view pg_ext_aux.extaux_mv as select * from pg_ext_aux.extaux_t;
30+
create type pg_ext_aux.extaux_concreat_type AS (sum int, count int);
31+
set allow_system_table_mods = off;
32+
-- Test 3: Check that the array type is not created
33+
-- these relations have its reltype, but have no array type
34+
select typname, typarray from pg_type where oid = (select reltype from pg_class where relname='extaux_t');
35+
typname | typarray
36+
---------+----------
37+
(0 rows)
38+
39+
select typname, typarray from pg_type where oid = (select reltype from pg_class where relname='extaux_v');
40+
typname | typarray
41+
---------+----------
42+
(0 rows)
43+
44+
select typname, typarray from pg_type where oid = (select reltype from pg_class where relname='extaux_mv');
45+
typname | typarray
46+
---------+----------
47+
(0 rows)
48+
49+
-- Test 4: check function works
50+
select pg_ext_aux.extaux_add1(7);
51+
extaux_add1
52+
-------------
53+
8
54+
(1 row)
55+
56+
-- Test 5: Check that these relations are protected
57+
-- fail: should not allowed to insert by user
58+
insert into pg_ext_aux.extaux_t values(1,'hello');
59+
ERROR: permission denied: "extaux_t" is a system catalog
60+
-- fail: should not allowed to refresh by user
61+
refresh materialized view pg_ext_aux.extaux_mv;
62+
ERROR: cannot swap toast files by links for system catalogs (cluster.c:1424)
63+
-- fail: should not allow to be dropped by user
64+
drop view pg_ext_aux.extaux_v;
65+
ERROR: permission denied: "extaux_v" is a system catalog
66+
drop materialized view pg_ext_aux.extaux_mv;
67+
ERROR: permission denied: "extaux_mv" is a system catalog
68+
drop table pg_ext_aux.extaux_t;
69+
ERROR: permission denied: "extaux_t" is a system catalog
70+
-- Test 6: type and functions may be dropped normally
71+
drop type pg_ext_aux.extaux_shell_type;
72+
drop type pg_ext_aux.extaux_concreat_type;
73+
drop function pg_ext_aux.extaux_add1(i integer);
74+
-- Clean up
75+
set allow_system_table_mods = on;
76+
drop view pg_ext_aux.extaux_v;
77+
drop materialized view pg_ext_aux.extaux_mv;
78+
drop table pg_ext_aux.extaux_t;
79+
reset allow_system_table_mods;

src/test/regress/greenplum_schedule

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# hitting max_connections limit on segments.
1616
#
1717

18+
# test for builtin namespace pg_ext_aux
19+
test: pg_ext_aux
20+
1821
test: gp_dispatch_keepalives
1922

2023
# copy command
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- Test use of namespace pg_ext_aux
2+
3+
-- Test 1: Create database object in pg_ext_aux should fail normally
4+
create table pg_ext_aux.extaux_t(a int, b int);
5+
create view pg_ext_aux.extaux_v as select oid, relname from pg_class;
6+
create materialized view pg_ext_aux.extaux_mv as select oid, relname from pg_class;
7+
8+
-- shell type is created successfully
9+
create type pg_ext_aux.extaux_shell_type;
10+
-- concreate type will fail
11+
create type pg_ext_aux.extaux_concreat_type AS (sum int, count int);
12+
13+
-- create function will success
14+
create function pg_ext_aux.extaux_add1(i integer) returns integer AS $$
15+
begin
16+
return i + 1;
17+
end;
18+
$$ language plpgsql;
19+
20+
21+
-- Test 2: Create database object in pg_ext_aux should success
22+
-- if allow_system_table_mods is on
23+
set allow_system_table_mods = on;
24+
create table pg_ext_aux.extaux_t(a int, b text);
25+
create view pg_ext_aux.extaux_v as select * from pg_ext_aux.extaux_t;
26+
create materialized view pg_ext_aux.extaux_mv as select * from pg_ext_aux.extaux_t;
27+
create type pg_ext_aux.extaux_concreat_type AS (sum int, count int);
28+
set allow_system_table_mods = off;
29+
30+
-- Test 3: Check that the array type is not created
31+
-- these relations have its reltype, but have no array type
32+
select typname, typarray from pg_type where oid = (select reltype from pg_class where relname='extaux_t');
33+
select typname, typarray from pg_type where oid = (select reltype from pg_class where relname='extaux_v');
34+
select typname, typarray from pg_type where oid = (select reltype from pg_class where relname='extaux_mv');
35+
36+
-- Test 4: check function works
37+
select pg_ext_aux.extaux_add1(7);
38+
39+
-- Test 5: Check that these relations are protected
40+
-- fail: should not allowed to insert by user
41+
insert into pg_ext_aux.extaux_t values(1,'hello');
42+
-- fail: should not allowed to refresh by user
43+
refresh materialized view pg_ext_aux.extaux_mv;
44+
45+
-- fail: should not allow to be dropped by user
46+
drop view pg_ext_aux.extaux_v;
47+
drop materialized view pg_ext_aux.extaux_mv;
48+
drop table pg_ext_aux.extaux_t;
49+
50+
-- Test 6: type and functions may be dropped normally
51+
drop type pg_ext_aux.extaux_shell_type;
52+
drop type pg_ext_aux.extaux_concreat_type;
53+
drop function pg_ext_aux.extaux_add1(i integer);
54+
55+
56+
-- Clean up
57+
set allow_system_table_mods = on;
58+
drop view pg_ext_aux.extaux_v;
59+
drop materialized view pg_ext_aux.extaux_mv;
60+
drop table pg_ext_aux.extaux_t;
61+
62+
reset allow_system_table_mods;

0 commit comments

Comments
 (0)