2 -- Regression tests for schemas (namespaces)
4 -- set the whitespace-only search_path to test that the
5 -- GUC list syntax is preserved during a schema creation
6 SELECT pg_catalog.set_config('search_path', ' ', false);
12 CREATE SCHEMA test_ns_schema_1
13 CREATE UNIQUE INDEX abc_a_idx ON abc (a)
14 CREATE VIEW abc_view AS
15 SELECT a+1 AS a, b+1 AS b FROM abc
20 -- verify that the correct search_path restored on abort
21 SET search_path to public;
23 SET search_path to public, test_ns_schema_1;
24 CREATE SCHEMA test_ns_schema_2
25 CREATE VIEW abc_view AS SELECT c FROM abc;
26 ERROR: column "c" does not exist
27 LINE 2: CREATE VIEW abc_view AS SELECT c FROM abc;
36 -- verify that the correct search_path preserved
37 -- after creating the schema and on commit
39 SET search_path to public, test_ns_schema_1;
40 CREATE SCHEMA test_ns_schema_2
41 CREATE VIEW abc_view AS SELECT a FROM abc;
44 --------------------------
45 public, test_ns_schema_1
51 --------------------------
52 public, test_ns_schema_1
55 DROP SCHEMA test_ns_schema_2 CASCADE;
56 NOTICE: drop cascades to view test_ns_schema_2.abc_view
57 -- verify that the objects were created
58 SELECT COUNT(*) FROM pg_class WHERE relnamespace =
59 (SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1');
65 INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
66 INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
67 INSERT INTO test_ns_schema_1.abc DEFAULT VALUES;
68 SELECT * FROM test_ns_schema_1.abc;
76 SELECT * FROM test_ns_schema_1.abc_view;
84 ALTER SCHEMA test_ns_schema_1 RENAME TO test_ns_schema_renamed;
85 SELECT COUNT(*) FROM pg_class WHERE relnamespace =
86 (SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1');
92 -- test IF NOT EXISTS cases
93 CREATE SCHEMA test_ns_schema_renamed; -- fail, already exists
94 ERROR: schema "test_ns_schema_renamed" already exists
95 CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed; -- ok with notice
96 NOTICE: schema "test_ns_schema_renamed" already exists, skipping
97 CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed -- fail, disallowed
102 ERROR: CREATE SCHEMA IF NOT EXISTS cannot include schema elements
103 LINE 2: CREATE TABLE abc (
105 DROP SCHEMA test_ns_schema_renamed CASCADE;
106 NOTICE: drop cascades to 2 other objects
107 DETAIL: drop cascades to table test_ns_schema_renamed.abc
108 drop cascades to view test_ns_schema_renamed.abc_view
109 -- verify that the objects were dropped
110 SELECT COUNT(*) FROM pg_class WHERE relnamespace =
111 (SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_renamed');
118 -- Verify that search_path is set to a safe value during maintenance
121 CREATE SCHEMA test_maint_search_path;
122 SET search_path = test_maint_search_path;
123 CREATE FUNCTION fn(INT) RETURNS INT IMMUTABLE LANGUAGE plpgsql AS $$
125 RAISE NOTICE 'current search_path: %', current_setting('search_path');
129 CREATE TABLE test_maint(i INT);
130 INSERT INTO test_maint VALUES (1), (2);
131 CREATE MATERIALIZED VIEW test_maint_mv AS SELECT fn(i) FROM test_maint;
132 NOTICE: current search_path: pg_catalog, pg_temp
133 NOTICE: current search_path: pg_catalog, pg_temp
134 -- the following commands should see search_path as pg_catalog, pg_temp
135 CREATE INDEX test_maint_idx ON test_maint_search_path.test_maint (fn(i));
136 NOTICE: current search_path: pg_catalog, pg_temp
137 NOTICE: current search_path: pg_catalog, pg_temp
138 REINDEX TABLE test_maint_search_path.test_maint;
139 NOTICE: current search_path: pg_catalog, pg_temp
140 NOTICE: current search_path: pg_catalog, pg_temp
141 ANALYZE test_maint_search_path.test_maint;
142 NOTICE: current search_path: pg_catalog, pg_temp
143 NOTICE: current search_path: pg_catalog, pg_temp
144 VACUUM FULL test_maint_search_path.test_maint;
145 NOTICE: current search_path: pg_catalog, pg_temp
146 NOTICE: current search_path: pg_catalog, pg_temp
147 CLUSTER test_maint_search_path.test_maint USING test_maint_idx;
148 NOTICE: current search_path: pg_catalog, pg_temp
149 NOTICE: current search_path: pg_catalog, pg_temp
150 NOTICE: current search_path: pg_catalog, pg_temp
151 NOTICE: current search_path: pg_catalog, pg_temp
152 REFRESH MATERIALIZED VIEW test_maint_search_path.test_maint_mv;
153 NOTICE: current search_path: pg_catalog, pg_temp
154 NOTICE: current search_path: pg_catalog, pg_temp
156 DROP SCHEMA test_maint_search_path CASCADE;
157 NOTICE: drop cascades to 3 other objects
158 DETAIL: drop cascades to function test_maint_search_path.fn(integer)
159 drop cascades to table test_maint_search_path.test_maint
160 drop cascades to materialized view test_maint_search_path.test_maint_mv