Debian Codegraph @

src/test/modules/unsafe_tests/sql/alter_system_table.sql

raw
1 --
2 -- Tests for things affected by allow_system_table_mods
3 --
4 -- We run the same set of commands once with allow_system_table_mods
5 -- off and then again with on.
6 --
7 -- The "on" tests should where possible be wrapped in BEGIN/ROLLBACK
8 -- blocks so as to not leave a mess around.
9
10 CREATE USER regress_user_ast;
11
12 SET allow_system_table_mods = off;
13
14 -- create new table in pg_catalog
15 CREATE TABLE pg_catalog.test (a int);
16
17 -- anyarray column
18 CREATE TABLE t1x (a int, b anyarray);
19
20 -- index on system catalog
21 ALTER TABLE pg_namespace ADD CONSTRAINT foo UNIQUE USING INDEX pg_namespace_nspname_index;
22
23 -- write to system catalog table as superuser
24 -- (allowed even without allow_system_table_mods)
25 INSERT INTO pg_description (objoid, classoid, objsubid, description) VALUES (0, 0, 0, 'foo');
26
27 -- write to system catalog table as normal user
28 GRANT INSERT ON pg_description TO regress_user_ast;
29 SET ROLE regress_user_ast;
30 INSERT INTO pg_description (objoid, classoid, objsubid, description) VALUES (0, 0, 1, 'foo');
31 RESET ROLE;
32
33 -- policy on system catalog
34 CREATE POLICY foo ON pg_description FOR SELECT USING (description NOT LIKE 'secret%');
35
36 -- reserved schema name
37 CREATE SCHEMA pg_foo;
38
39 -- drop system table
40 DROP TABLE pg_description;
41
42 -- truncate of system table
43 TRUNCATE pg_description;
44
45 -- rename column of system table
46 ALTER TABLE pg_description RENAME COLUMN description TO comment;
47
48 -- ATSimplePermissions()
49 ALTER TABLE pg_description ALTER COLUMN description SET NOT NULL;
50
51 -- SET STATISTICS
52 ALTER TABLE pg_description ALTER COLUMN description SET STATISTICS -1;
53
54 -- foreign key referencing catalog
55 CREATE TABLE foo (a oid, b oid, c int, FOREIGN KEY (a, b, c) REFERENCES pg_description);
56
57 -- RangeVarCallbackOwnsRelation()
58 CREATE INDEX pg_description_test_index ON pg_description (description);
59
60 -- RangeVarCallbackForAlterRelation()
61 ALTER TABLE pg_description RENAME TO pg_comment;
62 ALTER TABLE pg_description SET SCHEMA public;
63
64 -- reserved tablespace name
65 CREATE TABLESPACE pg_foo LOCATION '/no/such/location';
66
67 -- triggers
68 CREATE FUNCTION tf1() RETURNS trigger
69 LANGUAGE plpgsql
70 AS $$
71 BEGIN
72 RETURN NULL;
73 END $$;
74
75 CREATE TRIGGER t1 BEFORE INSERT ON pg_description EXECUTE FUNCTION tf1();
76 ALTER TRIGGER t1 ON pg_description RENAME TO t2;
77 --DROP TRIGGER t2 ON pg_description;
78
79 -- rules
80 CREATE RULE r1 AS ON INSERT TO pg_description DO INSTEAD NOTHING;
81 ALTER RULE r1 ON pg_description RENAME TO r2;
82 -- now make one to test dropping:
83 SET allow_system_table_mods TO on;
84 CREATE RULE r2 AS ON INSERT TO pg_description DO INSTEAD NOTHING;
85 RESET allow_system_table_mods;
86 DROP RULE r2 ON pg_description;
87 -- cleanup:
88 SET allow_system_table_mods TO on;
89 DROP RULE r2 ON pg_description;
90 RESET allow_system_table_mods;
91
92 -- Reloptions on TOAST tables
93 ALTER TABLE pg_toast.pg_toast_2615 SET (fillfactor = '90');
94
95 SET allow_system_table_mods = on;
96
97 -- create new table in pg_catalog
98 BEGIN;
99 CREATE TABLE pg_catalog.test (a int);
100 ROLLBACK;
101
102 -- anyarray column
103 BEGIN;
104 CREATE TABLE t1 (a int, b anyarray);
105 ROLLBACK;
106
107 -- index on system catalog
108 BEGIN;
109 ALTER TABLE pg_namespace ADD CONSTRAINT foo UNIQUE USING INDEX pg_namespace_nspname_index;
110 ROLLBACK;
111
112 -- write to system catalog table as superuser
113 BEGIN;
114 INSERT INTO pg_description (objoid, classoid, objsubid, description) VALUES (0, 0, 2, 'foo');
115 ROLLBACK;
116
117 -- write to system catalog table as normal user
118 -- (not allowed)
119 SET ROLE regress_user_ast;
120 INSERT INTO pg_description (objoid, classoid, objsubid, description) VALUES (0, 0, 3, 'foo');
121 RESET ROLE;
122
123 -- policy on system catalog
124 BEGIN;
125 CREATE POLICY foo ON pg_description FOR SELECT USING (description NOT LIKE 'secret%');
126 ROLLBACK;
127
128 -- reserved schema name
129 BEGIN;
130 CREATE SCHEMA pg_foo;
131 ROLLBACK;
132
133 -- drop system table
134 -- (This will fail anyway because it's pinned.)
135 BEGIN;
136 DROP TABLE pg_description;
137 ROLLBACK;
138
139 -- truncate of system table
140 BEGIN;
141 TRUNCATE pg_description;
142 ROLLBACK;
143
144 -- rename column of system table
145 BEGIN;
146 ALTER TABLE pg_description RENAME COLUMN description TO comment;
147 ROLLBACK;
148
149 -- ATSimplePermissions()
150 BEGIN;
151 ALTER TABLE pg_description ALTER COLUMN description SET NOT NULL;
152 ROLLBACK;
153
154 -- SET STATISTICS
155 BEGIN;
156 ALTER TABLE pg_description ALTER COLUMN description SET STATISTICS -1;
157 ROLLBACK;
158
159 -- foreign key referencing catalog
160 BEGIN;
161 CREATE TABLE foo (a oid, b oid, c int, FOREIGN KEY (a, b, c) REFERENCES pg_description);
162 ROLLBACK;
163
164 -- RangeVarCallbackOwnsRelation()
165 BEGIN;
166 CREATE INDEX pg_description_test_index ON pg_description (description);
167 ROLLBACK;
168
169 -- RangeVarCallbackForAlterRelation()
170 BEGIN;
171 ALTER TABLE pg_description RENAME TO pg_comment;
172 ROLLBACK;
173 BEGIN;
174 ALTER TABLE pg_description SET SCHEMA public;
175 ROLLBACK;
176
177 -- reserved tablespace name
178 SET client_min_messages = error; -- disable ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS warning
179 CREATE TABLESPACE pg_foo LOCATION '/no/such/location';
180 RESET client_min_messages;
181
182 -- triggers
183 CREATE TRIGGER t1 BEFORE INSERT ON pg_description EXECUTE FUNCTION tf1();
184 ALTER TRIGGER t1 ON pg_description RENAME TO t2;
185 DROP TRIGGER t2 ON pg_description;
186
187 -- rules
188 CREATE RULE r1 AS ON INSERT TO pg_description DO INSTEAD NOTHING;
189 ALTER RULE r1 ON pg_description RENAME TO r2;
190 DROP RULE r2 ON pg_description;
191
192 -- Reloptions on TOAST tables
193 ALTER TABLE pg_toast.pg_toast_2615 SET (fillfactor = '90');
194
195 -- cleanup
196 REVOKE ALL ON pg_description FROM regress_user_ast;
197 DROP USER regress_user_ast;
198 DROP FUNCTION tf1;