3 -- Virtual class definitions
4 -- (this also tests the query rewrite system)
6 -- directory paths and dlsuffix are passed to us in environment variables
7 \getenv abs_srcdir PG_ABS_SRCDIR
8 \getenv libdir PG_LIBDIR
9 \getenv dlsuffix PG_DLSUFFIX
10 \set regresslib :libdir '/regress' :dlsuffix
11 CREATE FUNCTION interpt_pp(path, path)
15 CREATE TABLE real_city (
20 \set filename :abs_srcdir '/data/real_city.data'
21 COPY real_city FROM :'filename';
26 WHERE name ~ '.*Ramp';
28 SELECT r.name, r.thepath, c.cname AS cname
29 FROM ONLY road r, real_city c
30 WHERE c.outline ?# r.thepath;
32 SELECT ih.name, ih.thepath,
33 interpt_pp(ih.thepath, r.thepath) AS exit
34 FROM ihighway ih, ramp r
35 WHERE ih.thepath ?# r.thepath;
37 SELECT name, age, location, 12*salary AS annualsal
40 COMMENT ON VIEW noview IS 'no view';
41 ERROR: relation "noview" does not exist
42 COMMENT ON VIEW toyemp IS 'is a view';
43 COMMENT ON VIEW toyemp IS NULL;
44 -- These views are left around mainly to exercise special cases in pg_dump.
45 CREATE TABLE view_base_table (key int PRIMARY KEY, data varchar(20));
46 CREATE VIEW key_dependent_view AS
47 SELECT * FROM view_base_table GROUP BY key;
48 ALTER TABLE view_base_table DROP CONSTRAINT view_base_table_pkey; -- fails
49 ERROR: cannot drop constraint view_base_table_pkey on table view_base_table because other objects depend on it
50 DETAIL: view key_dependent_view depends on constraint view_base_table_pkey on table view_base_table
51 HINT: Use DROP ... CASCADE to drop the dependent objects too.
52 CREATE VIEW key_dependent_view_no_cols AS
53 SELECT FROM view_base_table GROUP BY key HAVING length(data) > 0;
55 -- CREATE OR REPLACE VIEW
57 CREATE TABLE viewtest_tbl (a int, b int, c numeric(10,1), d text COLLATE "C");
58 COPY viewtest_tbl FROM stdin;
59 CREATE OR REPLACE VIEW viewtest AS
60 SELECT * FROM viewtest_tbl;
61 CREATE OR REPLACE VIEW viewtest AS
62 SELECT * FROM viewtest_tbl WHERE a > 10;
63 SELECT * FROM viewtest;
65 ----+----+-----+-------
70 CREATE OR REPLACE VIEW viewtest AS
71 SELECT a, b, c, d FROM viewtest_tbl WHERE a > 5 ORDER BY b DESC;
72 SELECT * FROM viewtest;
74 ----+----+-----+-------
81 CREATE OR REPLACE VIEW viewtest AS
82 SELECT a FROM viewtest_tbl WHERE a <> 20;
83 ERROR: cannot drop columns from view
85 CREATE OR REPLACE VIEW viewtest AS
86 SELECT 1, * FROM viewtest_tbl;
87 ERROR: cannot change name of view column "a" to "?column?"
88 HINT: Use ALTER VIEW ... RENAME COLUMN ... to change name of view column instead.
90 CREATE OR REPLACE VIEW viewtest AS
91 SELECT a, b::numeric, c, d FROM viewtest_tbl;
92 ERROR: cannot change data type of view column "b" from integer to numeric
94 CREATE OR REPLACE VIEW viewtest AS
95 SELECT a, b, c::numeric(10,2), d FROM viewtest_tbl;
96 ERROR: cannot change data type of view column "c" from numeric(10,1) to numeric(10,2)
98 CREATE OR REPLACE VIEW viewtest AS
99 SELECT a, b, c, d COLLATE "POSIX" FROM viewtest_tbl;
100 ERROR: cannot change collation of view column "d" from "C" to "POSIX"
102 CREATE OR REPLACE VIEW viewtest AS
103 SELECT a, b, c, d, 0 AS e FROM viewtest_tbl;
105 DROP TABLE viewtest_tbl;
106 -- tests for temporary views
107 CREATE SCHEMA temp_view_test
108 CREATE TABLE base_table (a int, id int)
109 CREATE TABLE base_table2 (a int, id int);
110 SET search_path TO temp_view_test, public;
111 CREATE TEMPORARY TABLE temp_table (a int, id int);
112 -- should be created in temp_view_test schema
113 CREATE VIEW v1 AS SELECT * FROM base_table;
114 -- should be created in temp object schema
115 CREATE VIEW v1_temp AS SELECT * FROM temp_table;
116 NOTICE: view "v1_temp" will be a temporary view
117 -- should be created in temp object schema
118 CREATE TEMP VIEW v2_temp AS SELECT * FROM base_table;
119 -- should be created in temp_views schema
120 CREATE VIEW temp_view_test.v2 AS SELECT * FROM base_table;
122 CREATE VIEW temp_view_test.v3_temp AS SELECT * FROM temp_table;
123 NOTICE: view "v3_temp" will be a temporary view
124 ERROR: cannot create temporary relation in non-temporary schema
126 CREATE SCHEMA test_view_schema
127 CREATE TEMP VIEW testview AS SELECT 1;
128 ERROR: cannot create temporary relation in non-temporary schema
129 -- joins: if any of the join relations are temporary, the view
130 -- should also be temporary
131 -- should be non-temp
133 SELECT t1.a AS t1_a, t2.a AS t2_a
134 FROM base_table t1, base_table2 t2
136 -- should be temp (one join rel is temp)
137 CREATE VIEW v4_temp AS
138 SELECT t1.a AS t1_a, t2.a AS t2_a
139 FROM base_table t1, temp_table t2
141 NOTICE: view "v4_temp" will be a temporary view
143 CREATE VIEW v5_temp AS
144 SELECT t1.a AS t1_a, t2.a AS t2_a, t3.a AS t3_a
145 FROM base_table t1, base_table2 t2, temp_table t3
146 WHERE t1.id = t2.id and t2.id = t3.id;
147 NOTICE: view "v5_temp" will be a temporary view
149 CREATE VIEW v4 AS SELECT * FROM base_table WHERE id IN (SELECT id FROM base_table2);
150 CREATE VIEW v5 AS SELECT t1.id, t2.a FROM base_table t1, (SELECT * FROM base_table2) t2;
151 CREATE VIEW v6 AS SELECT * FROM base_table WHERE EXISTS (SELECT 1 FROM base_table2);
152 CREATE VIEW v7 AS SELECT * FROM base_table WHERE NOT EXISTS (SELECT 1 FROM base_table2);
153 CREATE VIEW v8 AS SELECT * FROM base_table WHERE EXISTS (SELECT 1);
154 CREATE VIEW v6_temp AS SELECT * FROM base_table WHERE id IN (SELECT id FROM temp_table);
155 NOTICE: view "v6_temp" will be a temporary view
156 CREATE VIEW v7_temp AS SELECT t1.id, t2.a FROM base_table t1, (SELECT * FROM temp_table) t2;
157 NOTICE: view "v7_temp" will be a temporary view
158 CREATE VIEW v8_temp AS SELECT * FROM base_table WHERE EXISTS (SELECT 1 FROM temp_table);
159 NOTICE: view "v8_temp" will be a temporary view
160 CREATE VIEW v9_temp AS SELECT * FROM base_table WHERE NOT EXISTS (SELECT 1 FROM temp_table);
161 NOTICE: view "v9_temp" will be a temporary view
162 -- a view should also be temporary if it references a temporary view
163 CREATE VIEW v10_temp AS SELECT * FROM v7_temp;
164 NOTICE: view "v10_temp" will be a temporary view
165 CREATE VIEW v11_temp AS SELECT t1.id, t2.a FROM base_table t1, v10_temp t2;
166 NOTICE: view "v11_temp" will be a temporary view
167 CREATE VIEW v12_temp AS SELECT true FROM v11_temp;
168 NOTICE: view "v12_temp" will be a temporary view
169 -- a view should also be temporary if it references a temporary sequence
170 CREATE SEQUENCE seq1;
171 CREATE TEMPORARY SEQUENCE seq1_temp;
172 CREATE VIEW v9 AS SELECT seq1.is_called FROM seq1;
173 CREATE VIEW v13_temp AS SELECT seq1_temp.is_called FROM seq1_temp;
174 NOTICE: view "v13_temp" will be a temporary view
175 SELECT relname FROM pg_class
176 WHERE relname LIKE 'v_'
177 AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'temp_view_test')
192 SELECT relname FROM pg_class
193 WHERE relname LIKE 'v%'
194 AND relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname LIKE 'pg_temp%')
212 CREATE SCHEMA testviewschm2;
213 SET search_path TO testviewschm2, public;
214 CREATE TABLE t1 (num int, name text);
215 CREATE TABLE t2 (num2 int, value text);
216 CREATE TEMP TABLE tt (num2 int, value text);
217 CREATE VIEW nontemp1 AS SELECT * FROM t1 CROSS JOIN t2;
218 CREATE VIEW temporal1 AS SELECT * FROM t1 CROSS JOIN tt;
219 NOTICE: view "temporal1" will be a temporary view
220 CREATE VIEW nontemp2 AS SELECT * FROM t1 INNER JOIN t2 ON t1.num = t2.num2;
221 CREATE VIEW temporal2 AS SELECT * FROM t1 INNER JOIN tt ON t1.num = tt.num2;
222 NOTICE: view "temporal2" will be a temporary view
223 CREATE VIEW nontemp3 AS SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num2;
224 CREATE VIEW temporal3 AS SELECT * FROM t1 LEFT JOIN tt ON t1.num = tt.num2;
225 NOTICE: view "temporal3" will be a temporary view
226 CREATE VIEW nontemp4 AS SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num2 AND t2.value = 'xxx';
227 CREATE VIEW temporal4 AS SELECT * FROM t1 LEFT JOIN tt ON t1.num = tt.num2 AND tt.value = 'xxx';
228 NOTICE: view "temporal4" will be a temporary view
229 SELECT relname FROM pg_class
230 WHERE relname LIKE 'nontemp%'
231 AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'testviewschm2')
241 SELECT relname FROM pg_class
242 WHERE relname LIKE 'temporal%'
243 AND relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname LIKE 'pg_temp%')
253 CREATE TABLE tbl1 ( a int, b int);
254 CREATE TABLE tbl2 (c int, d int);
255 CREATE TABLE tbl3 (e int, f int);
256 CREATE TABLE tbl4 (g int, h int);
257 CREATE TEMP TABLE tmptbl (i int, j int);
258 --Should be in testviewschm2
259 CREATE VIEW pubview AS SELECT * FROM tbl1 WHERE tbl1.a
260 BETWEEN (SELECT d FROM tbl2 WHERE c = 1) AND (SELECT e FROM tbl3 WHERE f = 2)
261 AND EXISTS (SELECT g FROM tbl4 LEFT JOIN tbl3 ON tbl4.h = tbl3.f);
262 SELECT count(*) FROM pg_class where relname = 'pubview'
263 AND relnamespace IN (SELECT OID FROM pg_namespace WHERE nspname = 'testviewschm2');
269 --Should be in temp object schema
270 CREATE VIEW mytempview AS SELECT * FROM tbl1 WHERE tbl1.a
271 BETWEEN (SELECT d FROM tbl2 WHERE c = 1) AND (SELECT e FROM tbl3 WHERE f = 2)
272 AND EXISTS (SELECT g FROM tbl4 LEFT JOIN tbl3 ON tbl4.h = tbl3.f)
273 AND NOT EXISTS (SELECT g FROM tbl4 LEFT JOIN tmptbl ON tbl4.h = tmptbl.j);
274 NOTICE: view "mytempview" will be a temporary view
275 SELECT count(*) FROM pg_class where relname LIKE 'mytempview'
276 And relnamespace IN (SELECT OID FROM pg_namespace WHERE nspname LIKE 'pg_temp%');
283 -- CREATE VIEW and WITH(...) clause
285 CREATE VIEW mysecview1
286 AS SELECT * FROM tbl1 WHERE a = 0;
287 CREATE VIEW mysecview2 WITH (security_barrier=true)
288 AS SELECT * FROM tbl1 WHERE a > 0;
289 CREATE VIEW mysecview3 WITH (security_barrier=false)
290 AS SELECT * FROM tbl1 WHERE a < 0;
291 CREATE VIEW mysecview4 WITH (security_barrier)
292 AS SELECT * FROM tbl1 WHERE a <> 0;
293 CREATE VIEW mysecview5 WITH (security_barrier=100) -- Error
294 AS SELECT * FROM tbl1 WHERE a > 100;
295 ERROR: invalid value for boolean option "security_barrier": 100
296 CREATE VIEW mysecview6 WITH (invalid_option) -- Error
297 AS SELECT * FROM tbl1 WHERE a < 100;
298 ERROR: unrecognized parameter "invalid_option"
299 CREATE VIEW mysecview7 WITH (security_invoker=true)
300 AS SELECT * FROM tbl1 WHERE a = 100;
301 CREATE VIEW mysecview8 WITH (security_invoker=false, security_barrier=true)
302 AS SELECT * FROM tbl1 WHERE a > 100;
303 CREATE VIEW mysecview9 WITH (security_invoker)
304 AS SELECT * FROM tbl1 WHERE a < 100;
305 CREATE VIEW mysecview10 WITH (security_invoker=100) -- Error
306 AS SELECT * FROM tbl1 WHERE a <> 100;
307 ERROR: invalid value for boolean option "security_invoker": 100
308 SELECT relname, relkind, reloptions FROM pg_class
309 WHERE oid in ('mysecview1'::regclass, 'mysecview2'::regclass,
310 'mysecview3'::regclass, 'mysecview4'::regclass,
311 'mysecview7'::regclass, 'mysecview8'::regclass,
312 'mysecview9'::regclass)
314 relname | relkind | reloptions
315 ------------+---------+------------------------------------------------
317 mysecview2 | v | {security_barrier=true}
318 mysecview3 | v | {security_barrier=false}
319 mysecview4 | v | {security_barrier=true}
320 mysecview7 | v | {security_invoker=true}
321 mysecview8 | v | {security_invoker=false,security_barrier=true}
322 mysecview9 | v | {security_invoker=true}
325 CREATE OR REPLACE VIEW mysecview1
326 AS SELECT * FROM tbl1 WHERE a = 256;
327 CREATE OR REPLACE VIEW mysecview2
328 AS SELECT * FROM tbl1 WHERE a > 256;
329 CREATE OR REPLACE VIEW mysecview3 WITH (security_barrier=true)
330 AS SELECT * FROM tbl1 WHERE a < 256;
331 CREATE OR REPLACE VIEW mysecview4 WITH (security_barrier=false)
332 AS SELECT * FROM tbl1 WHERE a <> 256;
333 CREATE OR REPLACE VIEW mysecview7
334 AS SELECT * FROM tbl1 WHERE a > 256;
335 CREATE OR REPLACE VIEW mysecview8 WITH (security_invoker=true)
336 AS SELECT * FROM tbl1 WHERE a < 256;
337 CREATE OR REPLACE VIEW mysecview9 WITH (security_invoker=false, security_barrier=true)
338 AS SELECT * FROM tbl1 WHERE a <> 256;
339 SELECT relname, relkind, reloptions FROM pg_class
340 WHERE oid in ('mysecview1'::regclass, 'mysecview2'::regclass,
341 'mysecview3'::regclass, 'mysecview4'::regclass,
342 'mysecview7'::regclass, 'mysecview8'::regclass,
343 'mysecview9'::regclass)
345 relname | relkind | reloptions
346 ------------+---------+------------------------------------------------
349 mysecview3 | v | {security_barrier=true}
350 mysecview4 | v | {security_barrier=false}
352 mysecview8 | v | {security_invoker=true}
353 mysecview9 | v | {security_invoker=false,security_barrier=true}
356 -- Check that unknown literals are converted to "text" in CREATE VIEW,
357 -- so that we don't end up with unknown-type columns.
358 CREATE VIEW unspecified_types AS
359 SELECT 42 as i, 42.5 as num, 'foo' as u, 'foo'::unknown as u2, null as n;
360 \d+ unspecified_types
361 View "testviewschm2.unspecified_types"
362 Column | Type | Collation | Nullable | Default | Storage | Description
363 --------+---------+-----------+----------+---------+----------+-------------
364 i | integer | | | | plain |
365 num | numeric | | | | main |
366 u | text | | | | extended |
367 u2 | text | | | | extended |
368 n | text | | | | extended |
376 SELECT * FROM unspecified_types;
378 ----+------+-----+-----+---
379 42 | 42.5 | foo | foo |
382 -- This test checks that proper typmods are assigned in a multi-row VALUES
386 ('abc'::varchar(3), '0123456789', 42, 'abcd'::varchar(4)),
387 ('0123456789', 'abc'::varchar(3), 42.12, 'abc'::varchar(4))
390 View "testviewschm2.tt1"
391 Column | Type | Collation | Nullable | Default | Storage | Description
392 --------+----------------------+-----------+----------+---------+----------+-------------
393 a | character varying | | | | extended |
394 b | character varying | | | | extended |
395 c | numeric | | | | main |
396 d | character varying(4) | | | | extended |
402 FROM ( VALUES ('abc'::character varying(3),'0123456789'::character varying,42,'abcd'::character varying(4)), ('0123456789'::character varying,'abc'::character varying(3),42.12,'abc'::character varying(4))) vv(a, b, c, d);
406 ------------+------------+-------+------
407 abc | 0123456789 | 42 | abcd
408 0123456789 | abc | 42.12 | abc
411 SELECT a::varchar(3) FROM tt1;
419 -- Test view decompilation in the face of relation renaming conflicts
420 CREATE TABLE tt1 (f1 int, f2 int, f3 text);
421 CREATE TABLE tx1 (x1 int, x2 int, x3 text);
422 CREATE TABLE temp_view_test.tt1 (y1 int, f2 int, f3 text);
423 CREATE VIEW aliased_view_1 AS
425 where exists (select 1 from tx1 where tt1.f1 = tx1.x1);
426 CREATE VIEW aliased_view_2 AS
428 where exists (select 1 from tx1 where a1.f1 = tx1.x1);
429 CREATE VIEW aliased_view_3 AS
431 where exists (select 1 from tx1 a2 where tt1.f1 = a2.x1);
432 CREATE VIEW aliased_view_4 AS
433 select * from temp_view_test.tt1
434 where exists (select 1 from tt1 where temp_view_test.tt1.y1 = tt1.f1);
436 View "testviewschm2.aliased_view_1"
437 Column | Type | Collation | Nullable | Default | Storage | Description
438 --------+---------+-----------+----------+---------+----------+-------------
439 f1 | integer | | | | plain |
440 f2 | integer | | | | plain |
441 f3 | text | | | | extended |
447 WHERE (EXISTS ( SELECT 1
449 WHERE tt1.f1 = tx1.x1));
452 View "testviewschm2.aliased_view_2"
453 Column | Type | Collation | Nullable | Default | Storage | Description
454 --------+---------+-----------+----------+---------+----------+-------------
455 f1 | integer | | | | plain |
456 f2 | integer | | | | plain |
457 f3 | text | | | | extended |
463 WHERE (EXISTS ( SELECT 1
465 WHERE a1.f1 = tx1.x1));
468 View "testviewschm2.aliased_view_3"
469 Column | Type | Collation | Nullable | Default | Storage | Description
470 --------+---------+-----------+----------+---------+----------+-------------
471 f1 | integer | | | | plain |
472 f2 | integer | | | | plain |
473 f3 | text | | | | extended |
479 WHERE (EXISTS ( SELECT 1
481 WHERE tt1.f1 = a2.x1));
484 View "testviewschm2.aliased_view_4"
485 Column | Type | Collation | Nullable | Default | Storage | Description
486 --------+---------+-----------+----------+---------+----------+-------------
487 y1 | integer | | | | plain |
488 f2 | integer | | | | plain |
489 f3 | text | | | | extended |
494 FROM temp_view_test.tt1
495 WHERE (EXISTS ( SELECT 1
497 WHERE tt1.y1 = tt1_1.f1));
499 ALTER TABLE tx1 RENAME TO a1;
501 View "testviewschm2.aliased_view_1"
502 Column | Type | Collation | Nullable | Default | Storage | Description
503 --------+---------+-----------+----------+---------+----------+-------------
504 f1 | integer | | | | plain |
505 f2 | integer | | | | plain |
506 f3 | text | | | | extended |
512 WHERE (EXISTS ( SELECT 1
514 WHERE tt1.f1 = a1.x1));
517 View "testviewschm2.aliased_view_2"
518 Column | Type | Collation | Nullable | Default | Storage | Description
519 --------+---------+-----------+----------+---------+----------+-------------
520 f1 | integer | | | | plain |
521 f2 | integer | | | | plain |
522 f3 | text | | | | extended |
528 WHERE (EXISTS ( SELECT 1
530 WHERE a1.f1 = a1_1.x1));
533 View "testviewschm2.aliased_view_3"
534 Column | Type | Collation | Nullable | Default | Storage | Description
535 --------+---------+-----------+----------+---------+----------+-------------
536 f1 | integer | | | | plain |
537 f2 | integer | | | | plain |
538 f3 | text | | | | extended |
544 WHERE (EXISTS ( SELECT 1
546 WHERE tt1.f1 = a2.x1));
549 View "testviewschm2.aliased_view_4"
550 Column | Type | Collation | Nullable | Default | Storage | Description
551 --------+---------+-----------+----------+---------+----------+-------------
552 y1 | integer | | | | plain |
553 f2 | integer | | | | plain |
554 f3 | text | | | | extended |
559 FROM temp_view_test.tt1
560 WHERE (EXISTS ( SELECT 1
562 WHERE tt1.y1 = tt1_1.f1));
564 ALTER TABLE tt1 RENAME TO a2;
566 View "testviewschm2.aliased_view_1"
567 Column | Type | Collation | Nullable | Default | Storage | Description
568 --------+---------+-----------+----------+---------+----------+-------------
569 f1 | integer | | | | plain |
570 f2 | integer | | | | plain |
571 f3 | text | | | | extended |
577 WHERE (EXISTS ( SELECT 1
579 WHERE a2.f1 = a1.x1));
582 View "testviewschm2.aliased_view_2"
583 Column | Type | Collation | Nullable | Default | Storage | Description
584 --------+---------+-----------+----------+---------+----------+-------------
585 f1 | integer | | | | plain |
586 f2 | integer | | | | plain |
587 f3 | text | | | | extended |
593 WHERE (EXISTS ( SELECT 1
595 WHERE a1.f1 = a1_1.x1));
598 View "testviewschm2.aliased_view_3"
599 Column | Type | Collation | Nullable | Default | Storage | Description
600 --------+---------+-----------+----------+---------+----------+-------------
601 f1 | integer | | | | plain |
602 f2 | integer | | | | plain |
603 f3 | text | | | | extended |
609 WHERE (EXISTS ( SELECT 1
611 WHERE a2.f1 = a2_1.x1));
614 View "testviewschm2.aliased_view_4"
615 Column | Type | Collation | Nullable | Default | Storage | Description
616 --------+---------+-----------+----------+---------+----------+-------------
617 y1 | integer | | | | plain |
618 f2 | integer | | | | plain |
619 f3 | text | | | | extended |
624 FROM temp_view_test.tt1
625 WHERE (EXISTS ( SELECT 1
627 WHERE tt1.y1 = a2.f1));
629 ALTER TABLE a1 RENAME TO tt1;
631 View "testviewschm2.aliased_view_1"
632 Column | Type | Collation | Nullable | Default | Storage | Description
633 --------+---------+-----------+----------+---------+----------+-------------
634 f1 | integer | | | | plain |
635 f2 | integer | | | | plain |
636 f3 | text | | | | extended |
642 WHERE (EXISTS ( SELECT 1
644 WHERE a2.f1 = tt1.x1));
647 View "testviewschm2.aliased_view_2"
648 Column | Type | Collation | Nullable | Default | Storage | Description
649 --------+---------+-----------+----------+---------+----------+-------------
650 f1 | integer | | | | plain |
651 f2 | integer | | | | plain |
652 f3 | text | | | | extended |
658 WHERE (EXISTS ( SELECT 1
660 WHERE a1.f1 = tt1.x1));
663 View "testviewschm2.aliased_view_3"
664 Column | Type | Collation | Nullable | Default | Storage | Description
665 --------+---------+-----------+----------+---------+----------+-------------
666 f1 | integer | | | | plain |
667 f2 | integer | | | | plain |
668 f3 | text | | | | extended |
674 WHERE (EXISTS ( SELECT 1
676 WHERE a2.f1 = a2_1.x1));
679 View "testviewschm2.aliased_view_4"
680 Column | Type | Collation | Nullable | Default | Storage | Description
681 --------+---------+-----------+----------+---------+----------+-------------
682 y1 | integer | | | | plain |
683 f2 | integer | | | | plain |
684 f3 | text | | | | extended |
689 FROM temp_view_test.tt1
690 WHERE (EXISTS ( SELECT 1
692 WHERE tt1.y1 = a2.f1));
694 ALTER TABLE a2 RENAME TO tx1;
695 ALTER TABLE tx1 SET SCHEMA temp_view_test;
697 View "testviewschm2.aliased_view_1"
698 Column | Type | Collation | Nullable | Default | Storage | Description
699 --------+---------+-----------+----------+---------+----------+-------------
700 f1 | integer | | | | plain |
701 f2 | integer | | | | plain |
702 f3 | text | | | | extended |
707 FROM temp_view_test.tx1
708 WHERE (EXISTS ( SELECT 1
710 WHERE tx1.f1 = tt1.x1));
713 View "testviewschm2.aliased_view_2"
714 Column | Type | Collation | Nullable | Default | Storage | Description
715 --------+---------+-----------+----------+---------+----------+-------------
716 f1 | integer | | | | plain |
717 f2 | integer | | | | plain |
718 f3 | text | | | | extended |
723 FROM temp_view_test.tx1 a1
724 WHERE (EXISTS ( SELECT 1
726 WHERE a1.f1 = tt1.x1));
729 View "testviewschm2.aliased_view_3"
730 Column | Type | Collation | Nullable | Default | Storage | Description
731 --------+---------+-----------+----------+---------+----------+-------------
732 f1 | integer | | | | plain |
733 f2 | integer | | | | plain |
734 f3 | text | | | | extended |
739 FROM temp_view_test.tx1
740 WHERE (EXISTS ( SELECT 1
742 WHERE tx1.f1 = a2.x1));
745 View "testviewschm2.aliased_view_4"
746 Column | Type | Collation | Nullable | Default | Storage | Description
747 --------+---------+-----------+----------+---------+----------+-------------
748 y1 | integer | | | | plain |
749 f2 | integer | | | | plain |
750 f3 | text | | | | extended |
755 FROM temp_view_test.tt1
756 WHERE (EXISTS ( SELECT 1
757 FROM temp_view_test.tx1
758 WHERE tt1.y1 = tx1.f1));
760 ALTER TABLE temp_view_test.tt1 RENAME TO tmp1;
761 ALTER TABLE temp_view_test.tmp1 SET SCHEMA testviewschm2;
762 ALTER TABLE tmp1 RENAME TO tx1;
764 View "testviewschm2.aliased_view_1"
765 Column | Type | Collation | Nullable | Default | Storage | Description
766 --------+---------+-----------+----------+---------+----------+-------------
767 f1 | integer | | | | plain |
768 f2 | integer | | | | plain |
769 f3 | text | | | | extended |
774 FROM temp_view_test.tx1
775 WHERE (EXISTS ( SELECT 1
777 WHERE tx1.f1 = tt1.x1));
780 View "testviewschm2.aliased_view_2"
781 Column | Type | Collation | Nullable | Default | Storage | Description
782 --------+---------+-----------+----------+---------+----------+-------------
783 f1 | integer | | | | plain |
784 f2 | integer | | | | plain |
785 f3 | text | | | | extended |
790 FROM temp_view_test.tx1 a1
791 WHERE (EXISTS ( SELECT 1
793 WHERE a1.f1 = tt1.x1));
796 View "testviewschm2.aliased_view_3"
797 Column | Type | Collation | Nullable | Default | Storage | Description
798 --------+---------+-----------+----------+---------+----------+-------------
799 f1 | integer | | | | plain |
800 f2 | integer | | | | plain |
801 f3 | text | | | | extended |
806 FROM temp_view_test.tx1
807 WHERE (EXISTS ( SELECT 1
809 WHERE tx1.f1 = a2.x1));
812 View "testviewschm2.aliased_view_4"
813 Column | Type | Collation | Nullable | Default | Storage | Description
814 --------+---------+-----------+----------+---------+----------+-------------
815 y1 | integer | | | | plain |
816 f2 | integer | | | | plain |
817 f3 | text | | | | extended |
823 WHERE (EXISTS ( SELECT 1
824 FROM temp_view_test.tx1 tx1_1
825 WHERE tx1.y1 = tx1_1.f1));
827 -- Test correct deparsing of ORDER BY when there is an output name conflict
828 create view aliased_order_by as
829 select x1 as x2, x2 as x1, x3 from tt1
830 order by x2; -- this is interpreted per SQL92, so really ordering by x1
832 View "testviewschm2.aliased_order_by"
833 Column | Type | Collation | Nullable | Default | Storage | Description
834 --------+---------+-----------+----------+---------+----------+-------------
835 x2 | integer | | | | plain |
836 x1 | integer | | | | plain |
837 x3 | text | | | | extended |
845 alter view aliased_order_by rename column x1 to x0;
847 View "testviewschm2.aliased_order_by"
848 Column | Type | Collation | Nullable | Default | Storage | Description
849 --------+---------+-----------+----------+---------+----------+-------------
850 x2 | integer | | | | plain |
851 x0 | integer | | | | plain |
852 x3 | text | | | | extended |
860 alter view aliased_order_by rename column x3 to x1;
862 View "testviewschm2.aliased_order_by"
863 Column | Type | Collation | Nullable | Default | Storage | Description
864 --------+---------+-----------+----------+---------+----------+-------------
865 x2 | integer | | | | plain |
866 x0 | integer | | | | plain |
867 x1 | text | | | | extended |
875 -- Test aliasing of joins
876 create view view_of_joins as
878 (select * from (tbl1 cross join tbl2) same) ss,
879 (tbl3 cross join tbl4) same;
881 View "testviewschm2.view_of_joins"
882 Column | Type | Collation | Nullable | Default | Storage | Description
883 --------+---------+-----------+----------+---------+---------+-------------
884 a | integer | | | | plain |
885 b | integer | | | | plain |
886 c | integer | | | | plain |
887 d | integer | | | | plain |
888 e | integer | | | | plain |
889 f | integer | | | | plain |
890 g | integer | | | | plain |
891 h | integer | | | | plain |
901 FROM ( SELECT same_1.a,
906 CROSS JOIN tbl2) same_1) ss,
908 CROSS JOIN tbl4) same;
910 create table tbl1a (a int, c int);
911 create view view_of_joins_2a as select * from tbl1 join tbl1a using (a);
912 create view view_of_joins_2b as select * from tbl1 join tbl1a using (a) as x;
913 create view view_of_joins_2c as select * from (tbl1 join tbl1a using (a)) as y;
914 create view view_of_joins_2d as select * from (tbl1 join tbl1a using (a) as x) as y;
915 select pg_get_viewdef('view_of_joins_2a', true);
917 ----------------------------
922 JOIN tbl1a USING (a);
925 select pg_get_viewdef('view_of_joins_2b', true);
927 ---------------------------------
932 JOIN tbl1a USING (a) AS x;
935 select pg_get_viewdef('view_of_joins_2c', true);
937 -------------------------------
942 JOIN tbl1a USING (a)) y;
945 select pg_get_viewdef('view_of_joins_2d', true);
947 ------------------------------------
952 JOIN tbl1a USING (a) AS x) y;
955 -- Test view decompilation in the face of column addition/deletion/renaming
956 create table tt2 (a int, b int, c int);
957 create table tt3 (ax int8, b int2, c numeric);
958 create table tt4 (ay int, b int, q int);
959 create view v1 as select * from tt2 natural join tt3;
960 create view v1a as select * from (tt2 natural join tt3) j;
961 create view v2 as select * from tt2 join tt3 using (b,c) join tt4 using (b);
962 create view v2a as select * from (tt2 join tt3 using (b,c) join tt4 using (b)) j;
963 create view v3 as select * from tt2 join tt3 using (b,c) full join tt4 using (b);
964 select pg_get_viewdef('v1', true);
966 -----------------------------
972 JOIN tt3 USING (b, c);
975 select pg_get_viewdef('v1a', true);
977 --------------------------------
983 JOIN tt3 USING (b, c)) j;
986 select pg_get_viewdef('v2', true);
988 ----------------------------
996 JOIN tt3 USING (b, c)+
1000 select pg_get_viewdef('v2a', true);
1002 -----------------------------
1010 JOIN tt3 USING (b, c) +
1011 JOIN tt4 USING (b)) j;
1014 select pg_get_viewdef('v3', true);
1016 -------------------------------
1024 JOIN tt3 USING (b, c) +
1025 FULL JOIN tt4 USING (b);
1028 alter table tt2 add column d int;
1029 alter table tt2 add column e int;
1030 select pg_get_viewdef('v1', true);
1032 -----------------------------
1038 JOIN tt3 USING (b, c);
1041 select pg_get_viewdef('v1a', true);
1043 --------------------------------
1049 JOIN tt3 USING (b, c)) j;
1052 select pg_get_viewdef('v2', true);
1054 ----------------------------
1062 JOIN tt3 USING (b, c)+
1066 select pg_get_viewdef('v2a', true);
1068 -----------------------------
1076 JOIN tt3 USING (b, c) +
1077 JOIN tt4 USING (b)) j;
1080 select pg_get_viewdef('v3', true);
1082 -------------------------------
1090 JOIN tt3 USING (b, c) +
1091 FULL JOIN tt4 USING (b);
1094 alter table tt3 rename c to d;
1095 select pg_get_viewdef('v1', true);
1097 -------------------------------------------
1103 JOIN tt3 tt3(ax, b, c) USING (b, c);
1106 select pg_get_viewdef('v1a', true);
1108 ----------------------------------------------
1114 JOIN tt3 tt3(ax, b, c) USING (b, c)) j;
1117 select pg_get_viewdef('v2', true);
1119 ------------------------------------------
1127 JOIN tt3 tt3(ax, b, c) USING (b, c)+
1131 select pg_get_viewdef('v2a', true);
1133 ------------------------------------------
1141 JOIN tt3 tt3(ax, b, c) USING (b, c)+
1142 JOIN tt4 USING (b)) j;
1145 select pg_get_viewdef('v3', true);
1147 ------------------------------------------
1155 JOIN tt3 tt3(ax, b, c) USING (b, c)+
1156 FULL JOIN tt4 USING (b);
1159 alter table tt3 add column c int;
1160 alter table tt3 add column e int;
1161 select pg_get_viewdef('v1', true);
1163 ---------------------------------------------------
1169 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c);
1172 select pg_get_viewdef('v1a', true);
1174 -----------------------------------------------------------------------------------
1180 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c)) j(b, c, a, d, e, ax, c_1, e_1);
1183 select pg_get_viewdef('v2', true);
1185 --------------------------------------------------
1193 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c)+
1197 select pg_get_viewdef('v2a', true);
1199 -----------------------------------------------------------------
1207 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c) +
1208 JOIN tt4 USING (b)) j(b, c, a, d, e, ax, c_1, e_1, ay, q);
1211 select pg_get_viewdef('v3', true);
1213 --------------------------------------------------
1221 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c)+
1222 FULL JOIN tt4 USING (b);
1225 alter table tt2 drop column d;
1226 select pg_get_viewdef('v1', true);
1228 ---------------------------------------------------
1234 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c);
1237 select pg_get_viewdef('v1a', true);
1239 --------------------------------------------------------------------------------
1245 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c)) j(b, c, a, e, ax, c_1, e_1);
1248 select pg_get_viewdef('v2', true);
1250 --------------------------------------------------
1258 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c)+
1262 select pg_get_viewdef('v2a', true);
1264 --------------------------------------------------------------
1272 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c) +
1273 JOIN tt4 USING (b)) j(b, c, a, e, ax, c_1, e_1, ay, q);
1276 select pg_get_viewdef('v3', true);
1278 --------------------------------------------------
1286 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c)+
1287 FULL JOIN tt4 USING (b);
1290 create table tt5 (a int, b int);
1291 create table tt6 (c int, d int);
1292 create view vv1 as select * from (tt5 cross join tt6) j(aa,bb,cc,dd);
1293 select pg_get_viewdef('vv1', true);
1295 -----------------------------------------
1301 CROSS JOIN tt6) j(aa, bb, cc, dd);
1304 alter table tt5 add column c int;
1305 select pg_get_viewdef('vv1', true);
1307 --------------------------------------------
1313 CROSS JOIN tt6) j(aa, bb, c, cc, dd);
1316 alter table tt5 add column cc int;
1317 select pg_get_viewdef('vv1', true);
1319 --------------------------------------------------
1325 CROSS JOIN tt6) j(aa, bb, c, cc_1, cc, dd);
1328 alter table tt5 drop column c;
1329 select pg_get_viewdef('vv1', true);
1331 -----------------------------------------------
1337 CROSS JOIN tt6) j(aa, bb, cc_1, cc, dd);
1340 create view v4 as select * from v1;
1341 alter view v1 rename column a to x;
1342 select pg_get_viewdef('v1', true);
1344 ---------------------------------------------------
1350 JOIN tt3 tt3(ax, b, c, c_1, e) USING (b, c);
1353 select pg_get_viewdef('v4', true);
1363 -- Unnamed FULL JOIN USING is lots of fun too
1364 create table tt7 (x int, xx int, y int);
1365 alter table tt7 drop column xx;
1366 create table tt8 (x int, z int);
1368 select * from (values(1,2,3,4,5)) v(a,b,c,d,e)
1370 select * from tt7 full join tt8 using (x), tt8 tt8x;
1371 select pg_get_viewdef('vv2', true);
1373 ------------------------------------------------
1379 FROM ( VALUES (1,2,3,4,5)) v(a, b, c, d, e)+
1387 FULL JOIN tt8 USING (x), +
1392 select * from (values(1,2,3,4,5,6)) v(a,b,c,x,e,f)
1395 tt7 full join tt8 using (x),
1396 tt7 tt7x full join tt8 tt8x using (x);
1397 select pg_get_viewdef('vv3', true);
1399 -----------------------------------------------------
1406 FROM ( VALUES (1,2,3,4,5,6)) v(a, b, c, x, e, f)+
1415 FULL JOIN tt8 USING (x), +
1417 FULL JOIN tt8 tt8x(x_1, z) USING (x_1);
1421 select * from (values(1,2,3,4,5,6,7)) v(a,b,c,x,e,f,g)
1424 tt7 full join tt8 using (x),
1425 tt7 tt7x full join tt8 tt8x using (x) full join tt8 tt8y using (x);
1426 select pg_get_viewdef('vv4', true);
1428 ----------------------------------------------------------
1436 FROM ( VALUES (1,2,3,4,5,6,7)) v(a, b, c, x, e, f, g)+
1446 FULL JOIN tt8 USING (x), +
1448 FULL JOIN tt8 tt8x(x_1, z) USING (x_1) +
1449 FULL JOIN tt8 tt8y(x_1, z) USING (x_1);
1452 alter table tt7 add column zz int;
1453 alter table tt7 add column z int;
1454 alter table tt7 drop column zz;
1455 alter table tt8 add column z2 int;
1456 select pg_get_viewdef('vv2', true);
1458 ------------------------------------------------
1464 FROM ( VALUES (1,2,3,4,5)) v(a, b, c, d, e)+
1472 FULL JOIN tt8 USING (x), +
1473 tt8 tt8x(x_1, z, z2);
1476 select pg_get_viewdef('vv3', true);
1478 -----------------------------------------------------
1485 FROM ( VALUES (1,2,3,4,5,6)) v(a, b, c, x, e, f)+
1494 FULL JOIN tt8 USING (x), +
1495 tt7 tt7x(x_1, y, z) +
1496 FULL JOIN tt8 tt8x(x_1, z, z2) USING (x_1);
1499 select pg_get_viewdef('vv4', true);
1501 ----------------------------------------------------------
1509 FROM ( VALUES (1,2,3,4,5,6,7)) v(a, b, c, x, e, f, g)+
1519 FULL JOIN tt8 USING (x), +
1520 tt7 tt7x(x_1, y, z) +
1521 FULL JOIN tt8 tt8x(x_1, z, z2) USING (x_1) +
1522 FULL JOIN tt8 tt8y(x_1, z, z2) USING (x_1);
1525 -- Implicit coercions in a JOIN USING create issues similar to FULL JOIN
1526 create table tt7a (x date, xx int, y int);
1527 alter table tt7a drop column xx;
1528 create table tt8a (x timestamptz, z int);
1530 select * from (values(now(),2,3,now(),5)) v(a,b,c,d,e)
1532 select * from tt7a left join tt8a using (x), tt8a tt8ax;
1533 select pg_get_viewdef('vv2a', true);
1535 --------------------------------------------------------
1541 FROM ( VALUES (now(),2,3,now(),5)) v(a, b, c, d, e)+
1549 LEFT JOIN tt8a USING (x), +
1554 -- Also check dropping a column that existed when the view was made
1556 create table tt9 (x int, xx int, y int);
1557 create table tt10 (x int, z int);
1558 create view vv5 as select x,y,z from tt9 join tt10 using(x);
1559 select pg_get_viewdef('vv5', true);
1561 ---------------------------
1566 JOIN tt10 USING (x);
1569 alter table tt9 drop column xx;
1570 select pg_get_viewdef('vv5', true);
1572 ---------------------------
1577 JOIN tt10 USING (x);
1581 -- Another corner case is that we might add a column to a table below a
1582 -- JOIN USING, and thereby make the USING column name ambiguous
1584 create table tt11 (x int, y int);
1585 create table tt12 (x int, z int);
1586 create table tt13 (z int, q int);
1587 create view vv6 as select x,y,z,q from
1588 (tt11 join tt12 using(x)) join tt13 using(z);
1589 select pg_get_viewdef('vv6', true);
1591 ---------------------------
1597 JOIN tt12 USING (x) +
1598 JOIN tt13 USING (z);
1601 alter table tt11 add column z int;
1602 select pg_get_viewdef('vv6', true);
1604 ------------------------------
1609 FROM tt11 tt11(x, y, z_1)+
1610 JOIN tt12 USING (x) +
1611 JOIN tt13 USING (z);
1615 -- Check cases involving dropped/altered columns in a function's rowtype result
1617 create table tt14t (f1 text, f2 text, f3 text, f4 text);
1618 insert into tt14t values('foo', 'bar', 'baz', '42');
1619 alter table tt14t drop column f2;
1620 create function tt14f() returns setof tt14t as
1625 for rec1 in select * from tt14t
1632 create view tt14v as select t.* from tt14f() t;
1633 select pg_get_viewdef('tt14v', true);
1635 --------------------------------
1639 FROM tt14f() t(f1, f3, f4);
1642 select * from tt14v;
1648 alter table tt14t drop column f3; -- fail, view has explicit reference to f3
1649 ERROR: cannot drop column f3 of table tt14t because other objects depend on it
1650 DETAIL: view tt14v depends on column f3 of table tt14t
1651 HINT: Use DROP ... CASCADE to drop the dependent objects too.
1652 -- We used to have a bug that would allow the above to succeed, posing
1653 -- hazards for later execution of the view. Check that the internal
1654 -- defenses for those hazards haven't bit-rotted, in case some other
1655 -- bug with similar symptoms emerges.
1657 -- destroy the dependency entry that prevents the DROP:
1658 delete from pg_depend where
1659 objid = (select oid from pg_rewrite
1660 where ev_class = 'tt14v'::regclass and rulename = '_RETURN')
1662 returning pg_describe_object(classid, objid, objsubid) as obj,
1663 pg_describe_object(refclassid, refobjid, refobjsubid) as ref,
1666 ----------------------------+--------------------------+---------
1667 rule _RETURN on view tt14v | column f3 of table tt14t | n
1670 -- this will now succeed:
1671 alter table tt14t drop column f3;
1672 -- column f3 is still in the view, sort of ...
1673 select pg_get_viewdef('tt14v', true);
1675 -------------------------------
1677 "?dropped?column?" AS f3,+
1679 FROM tt14f() t(f1, f4);
1682 -- ... and you can even EXPLAIN it ...
1683 explain (verbose, costs off) select * from tt14v;
1685 ----------------------------------------
1686 Function Scan on testviewschm2.tt14f t
1687 Output: t.f1, t.f3, t.f4
1688 Function Call: tt14f()
1691 -- but it will fail at execution
1692 select f1, f4 from tt14v;
1698 select * from tt14v;
1699 ERROR: attribute 3 of type record has been dropped
1701 -- likewise, altering a referenced column's type is prohibited ...
1702 alter table tt14t alter column f4 type integer using f4::integer; -- fail
1703 ERROR: cannot alter type of a column used by a view or rule
1704 DETAIL: rule _RETURN on view tt14v depends on column "f4"
1705 -- ... but some bug might let it happen, so check defenses
1707 -- destroy the dependency entry that prevents the ALTER:
1708 delete from pg_depend where
1709 objid = (select oid from pg_rewrite
1710 where ev_class = 'tt14v'::regclass and rulename = '_RETURN')
1712 returning pg_describe_object(classid, objid, objsubid) as obj,
1713 pg_describe_object(refclassid, refobjid, refobjsubid) as ref,
1716 ----------------------------+--------------------------+---------
1717 rule _RETURN on view tt14v | column f4 of table tt14t | n
1720 -- this will now succeed:
1721 alter table tt14t alter column f4 type integer using f4::integer;
1722 -- f4 is still in the view ...
1723 select pg_get_viewdef('tt14v', true);
1725 --------------------------------
1729 FROM tt14f() t(f1, f3, f4);
1732 -- but will fail at execution
1733 select f1, f3 from tt14v;
1739 select * from tt14v;
1740 ERROR: attribute 4 of type record has wrong type
1741 DETAIL: Table has type integer, but query expects text.
1744 create view tt14v as select t.f1, t.f4 from tt14f() t;
1745 select pg_get_viewdef('tt14v', true);
1747 --------------------------------
1750 FROM tt14f() t(f1, f3, f4);
1753 select * from tt14v;
1759 alter table tt14t drop column f3; -- ok
1760 select pg_get_viewdef('tt14v', true);
1762 ----------------------------
1765 FROM tt14f() t(f1, f4);
1768 explain (verbose, costs off) select * from tt14v;
1770 ----------------------------------------
1771 Function Scan on testviewschm2.tt14f t
1773 Function Call: tt14f()
1776 select * from tt14v;
1782 -- check display of whole-row variables in some corner cases
1783 create type nestedcomposite as (x int8_tbl);
1784 create view tt15v as select row(i)::nestedcomposite from int8_tbl i;
1785 select * from tt15v;
1787 ------------------------------------------
1789 ("(123,4567890123456789)")
1790 ("(4567890123456789,123)")
1791 ("(4567890123456789,4567890123456789)")
1792 ("(4567890123456789,-4567890123456789)")
1795 select pg_get_viewdef('tt15v', true);
1797 ------------------------------------------------------
1798 SELECT ROW(i.*::int8_tbl)::nestedcomposite AS "row"+
1802 select row(i.*::int8_tbl)::nestedcomposite from int8_tbl i;
1804 ------------------------------------------
1806 ("(123,4567890123456789)")
1807 ("(4567890123456789,123)")
1808 ("(4567890123456789,4567890123456789)")
1809 ("(4567890123456789,-4567890123456789)")
1812 create view tt16v as select * from int8_tbl i, lateral(values(i)) ss;
1813 select * from tt16v;
1815 ------------------+-------------------+--------------------------------------
1816 123 | 456 | (123,456)
1817 123 | 4567890123456789 | (123,4567890123456789)
1818 4567890123456789 | 123 | (4567890123456789,123)
1819 4567890123456789 | 4567890123456789 | (4567890123456789,4567890123456789)
1820 4567890123456789 | -4567890123456789 | (4567890123456789,-4567890123456789)
1823 select pg_get_viewdef('tt16v', true);
1825 -------------------------------------------
1830 LATERAL ( VALUES (i.*::int8_tbl)) ss;
1833 select * from int8_tbl i, lateral(values(i.*::int8_tbl)) ss;
1835 ------------------+-------------------+--------------------------------------
1836 123 | 456 | (123,456)
1837 123 | 4567890123456789 | (123,4567890123456789)
1838 4567890123456789 | 123 | (4567890123456789,123)
1839 4567890123456789 | 4567890123456789 | (4567890123456789,4567890123456789)
1840 4567890123456789 | -4567890123456789 | (4567890123456789,-4567890123456789)
1843 create view tt17v as select * from int8_tbl i where i in (values(i));
1844 select * from tt17v;
1846 ------------------+-------------------
1848 123 | 4567890123456789
1849 4567890123456789 | 123
1850 4567890123456789 | 4567890123456789
1851 4567890123456789 | -4567890123456789
1854 select pg_get_viewdef('tt17v', true);
1856 ---------------------------------------------
1860 WHERE (i.* IN ( VALUES (i.*::int8_tbl)));
1863 select * from int8_tbl i where i.* in (values(i.*::int8_tbl));
1865 ------------------+-------------------
1867 123 | 4567890123456789
1868 4567890123456789 | 123
1869 4567890123456789 | 4567890123456789
1870 4567890123456789 | -4567890123456789
1873 create table tt15v_log(o tt15v, n tt15v, incr bool);
1874 create rule updlog as on update to tt15v do also
1875 insert into tt15v_log values(old, new, row(old,old) < row(new,new));
1877 View "testviewschm2.tt15v"
1878 Column | Type | Collation | Nullable | Default | Storage | Description
1879 --------+-----------------+-----------+----------+---------+----------+-------------
1880 row | nestedcomposite | | | | extended |
1882 SELECT ROW(i.*::int8_tbl)::nestedcomposite AS "row"
1886 ON UPDATE TO tt15v DO INSERT INTO tt15v_log (o, n, incr)
1887 VALUES (old.*::tt15v, new.*::tt15v, (ROW(old.*::tt15v, old.*::tt15v) < ROW(new.*::tt15v, new.*::tt15v)))
1889 -- check unique-ification of overlength names
1890 create view tt18v as
1891 select * from int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy
1893 select * from int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz;
1894 NOTICE: identifier "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy" will be truncated to "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
1895 NOTICE: identifier "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz" will be truncated to "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
1896 select pg_get_viewdef('tt18v', true);
1898 -----------------------------------------------------------------------------------
1899 SELECT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.q1, +
1900 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.q2 +
1901 FROM int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
1903 SELECT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.q1, +
1904 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.q2 +
1905 FROM int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
1908 explain (costs off) select * from tt18v;
1910 --------------------------------------------------------------------------------------------
1912 -> Seq Scan on int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1913 -> Seq Scan on int8_tbl xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_1
1916 -- check display of ScalarArrayOp with a sub-select
1917 select 'foo'::text = any(array['abc','def','foo']::text[]);
1923 select 'foo'::text = any((select array['abc','def','foo']::text[])); -- fail
1924 ERROR: operator does not exist: text = text[]
1925 LINE 1: select 'foo'::text = any((select array['abc','def','foo']::t...
1927 HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
1928 select 'foo'::text = any((select array['abc','def','foo']::text[])::text[]);
1934 create view tt19v as
1935 select 'foo'::text = any(array['abc','def','foo']::text[]) c1,
1936 'foo'::text = any((select array['abc','def','foo']::text[])::text[]) c2;
1937 select pg_get_viewdef('tt19v', true);
1939 ------------------------------------------------------------------------------------------------------------
1940 SELECT 'foo'::text = ANY (ARRAY['abc'::text, 'def'::text, 'foo'::text]) AS c1, +
1941 'foo'::text = ANY ((( SELECT ARRAY['abc'::text, 'def'::text, 'foo'::text] AS "array"))::text[]) AS c2;
1944 -- check display of assorted RTE_FUNCTION expressions
1945 create view tt20v as
1948 collation for ('x'::text) col,
1950 localtimestamp(3) as t,
1951 cast(1+2 as int4) as i4,
1952 cast(1+2 as int8) as i8;
1953 select pg_get_viewdef('tt20v', true);
1955 ---------------------------------------------
1962 FROM COALESCE(1, 2) c(c), +
1963 COLLATION FOR ('x'::text) col(col), +
1964 CURRENT_DATE d(d), +
1965 LOCALTIMESTAMP(3) t(t), +
1966 CAST(1 + 2 AS integer) i4(i4), +
1967 CAST((1 + 2)::bigint AS bigint) i8(i8);
1970 -- reverse-listing of various special function syntaxes required by SQL
1971 create view tt201v as
1973 ('2022-12-01'::date + '1 day'::interval) at time zone 'UTC' as atz,
1974 extract(day from now()) as extr,
1975 (now(), '1 day'::interval) overlaps
1976 (current_timestamp(2), '1 day'::interval) as o,
1977 'foo' is normalized isn,
1978 'foo' is nfkc normalized isnn,
1979 normalize('foo') as n,
1980 normalize('foo', nfkd) as nfkd,
1981 overlay('foo' placing 'bar' from 2) as ovl,
1982 overlay('foo' placing 'bar' from 2 for 3) as ovl2,
1983 position('foo' in 'foobar') as p,
1984 substring('foo' from 2 for 3) as s,
1985 substring('foo' similar 'f' escape '#') as ss,
1986 substring('foo' from 'oo') as ssf, -- historically-permitted abuse
1987 trim(' ' from ' foo ') as bt,
1988 trim(leading ' ' from ' foo ') as lt,
1989 trim(trailing ' foo ') as rt,
1990 trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea) as btb,
1991 trim(leading E'\\000'::bytea from E'\\000Tom\\000'::bytea) as ltb,
1992 trim(trailing E'\\000'::bytea from E'\\000Tom\\000'::bytea) as rtb,
1994 (select * from CURRENT_DATE) as cd2,
1996 (select * from CURRENT_TIME) as ct2,
1997 CURRENT_TIME (1) as ct3,
1998 (select * from CURRENT_TIME (1)) as ct4,
1999 CURRENT_TIMESTAMP as ct5,
2000 (select * from CURRENT_TIMESTAMP) as ct6,
2001 CURRENT_TIMESTAMP (1) as ct7,
2002 (select * from CURRENT_TIMESTAMP (1)) as ct8,
2004 (select * from LOCALTIME) as lt2,
2005 LOCALTIME (1) as lt3,
2006 (select * from LOCALTIME (1)) as lt4,
2007 LOCALTIMESTAMP as lt5,
2008 (select * from LOCALTIMESTAMP) as lt6,
2009 LOCALTIMESTAMP (1) as lt7,
2010 (select * from LOCALTIMESTAMP (1)) as lt8,
2011 CURRENT_CATALOG as ca,
2012 (select * from CURRENT_CATALOG) as ca2,
2014 (select * from CURRENT_ROLE) as cr2,
2015 CURRENT_SCHEMA as cs,
2016 (select * from CURRENT_SCHEMA) as cs2,
2018 (select * from CURRENT_USER) as cu2,
2020 (select * from USER) as us2,
2022 (select * from SESSION_USER) as seu2,
2024 (select * from SYSTEM_USER) as su2;
2025 select pg_get_viewdef('tt201v', true);
2027 -----------------------------------------------------------------------------------------------
2028 SELECT (('12-01-2022'::date + '@ 1 day'::interval) AT TIME ZONE 'UTC'::text) AS atz, +
2029 EXTRACT(day FROM now()) AS extr, +
2030 ((now(), '@ 1 day'::interval) OVERLAPS (CURRENT_TIMESTAMP(2), '@ 1 day'::interval)) AS o,+
2031 ('foo'::text IS NORMALIZED) AS isn, +
2032 ('foo'::text IS NFKC NORMALIZED) AS isnn, +
2033 NORMALIZE('foo'::text) AS n, +
2034 NORMALIZE('foo'::text, NFKD) AS nfkd, +
2035 OVERLAY('foo'::text PLACING 'bar'::text FROM 2) AS ovl, +
2036 OVERLAY('foo'::text PLACING 'bar'::text FROM 2 FOR 3) AS ovl2, +
2037 POSITION(('foo'::text) IN ('foobar'::text)) AS p, +
2038 SUBSTRING('foo'::text FROM 2 FOR 3) AS s, +
2039 SUBSTRING('foo'::text SIMILAR 'f'::text ESCAPE '#'::text) AS ss, +
2040 "substring"('foo'::text, 'oo'::text) AS ssf, +
2041 TRIM(BOTH ' '::text FROM ' foo '::text) AS bt, +
2042 TRIM(LEADING ' '::text FROM ' foo '::text) AS lt, +
2043 TRIM(TRAILING FROM ' foo '::text) AS rt, +
2044 TRIM(BOTH '\x00'::bytea FROM '\x00546f6d00'::bytea) AS btb, +
2045 TRIM(LEADING '\x00'::bytea FROM '\x00546f6d00'::bytea) AS ltb, +
2046 TRIM(TRAILING '\x00'::bytea FROM '\x00546f6d00'::bytea) AS rtb, +
2047 CURRENT_DATE AS cd, +
2048 ( SELECT "current_date"."current_date" +
2049 FROM CURRENT_DATE "current_date"("current_date")) AS cd2, +
2050 CURRENT_TIME AS ct, +
2051 ( SELECT "current_time"."current_time" +
2052 FROM CURRENT_TIME "current_time"("current_time")) AS ct2, +
2053 CURRENT_TIME(1) AS ct3, +
2054 ( SELECT "current_time"."current_time" +
2055 FROM CURRENT_TIME(1) "current_time"("current_time")) AS ct4, +
2056 CURRENT_TIMESTAMP AS ct5, +
2057 ( SELECT "current_timestamp"."current_timestamp" +
2058 FROM CURRENT_TIMESTAMP "current_timestamp"("current_timestamp")) AS ct6, +
2059 CURRENT_TIMESTAMP(1) AS ct7, +
2060 ( SELECT "current_timestamp"."current_timestamp" +
2061 FROM CURRENT_TIMESTAMP(1) "current_timestamp"("current_timestamp")) AS ct8, +
2063 ( SELECT "localtime"."localtime" +
2064 FROM LOCALTIME "localtime"("localtime")) AS lt2, +
2065 LOCALTIME(1) AS lt3, +
2066 ( SELECT "localtime"."localtime" +
2067 FROM LOCALTIME(1) "localtime"("localtime")) AS lt4, +
2068 LOCALTIMESTAMP AS lt5, +
2069 ( SELECT "localtimestamp"."localtimestamp" +
2070 FROM LOCALTIMESTAMP "localtimestamp"("localtimestamp")) AS lt6, +
2071 LOCALTIMESTAMP(1) AS lt7, +
2072 ( SELECT "localtimestamp"."localtimestamp" +
2073 FROM LOCALTIMESTAMP(1) "localtimestamp"("localtimestamp")) AS lt8, +
2074 CURRENT_CATALOG AS ca, +
2075 ( SELECT "current_catalog"."current_catalog" +
2076 FROM CURRENT_CATALOG "current_catalog"("current_catalog")) AS ca2, +
2077 CURRENT_ROLE AS cr, +
2078 ( SELECT "current_role"."current_role" +
2079 FROM CURRENT_ROLE "current_role"("current_role")) AS cr2, +
2080 CURRENT_SCHEMA AS cs, +
2081 ( SELECT "current_schema"."current_schema" +
2082 FROM CURRENT_SCHEMA "current_schema"("current_schema")) AS cs2, +
2083 CURRENT_USER AS cu, +
2084 ( SELECT "current_user"."current_user" +
2085 FROM CURRENT_USER "current_user"("current_user")) AS cu2, +
2087 ( SELECT "user"."user" +
2088 FROM USER "user"("user")) AS us2, +
2089 SESSION_USER AS seu, +
2090 ( SELECT "session_user"."session_user" +
2091 FROM SESSION_USER "session_user"("session_user")) AS seu2, +
2092 SYSTEM_USER AS su, +
2093 ( SELECT "system_user"."system_user" +
2094 FROM SYSTEM_USER "system_user"("system_user")) AS su2;
2097 -- corner cases with empty join conditions
2098 create view tt21v as
2099 select * from tt5 natural inner join tt6;
2100 select pg_get_viewdef('tt21v', true);
2102 ----------------------
2112 create view tt22v as
2113 select * from tt5 natural left join tt6;
2114 select pg_get_viewdef('tt22v', true);
2116 -----------------------------
2123 LEFT JOIN tt6 ON TRUE;
2126 -- check handling of views with immediately-renamed columns
2127 create view tt23v (col_a, col_b) as
2128 select q1 as other_name1, q2 as other_name2 from int8_tbl
2131 select pg_get_viewdef('tt23v', true);
2133 -------------------------------
2134 SELECT int8_tbl.q1 AS col_a,+
2135 int8_tbl.q2 AS col_b +
2138 SELECT 42 AS col_a, +
2142 select pg_get_ruledef(oid, true) from pg_rewrite
2143 where ev_class = 'tt23v'::regclass and ev_type = '1';
2145 -----------------------------------------------------------------
2146 CREATE RULE "_RETURN" AS +
2147 ON SELECT TO tt23v DO INSTEAD SELECT int8_tbl.q1 AS col_a,+
2148 int8_tbl.q2 AS col_b +
2151 SELECT 42 AS col_a, +
2155 -- test extraction of FieldSelect field names (get_name_for_var_field)
2156 create view tt24v as
2157 with cte as materialized (select r from (values(1,2),(3,4)) r)
2158 select (r).column2 as col_a, (rr).column2 as col_b from
2159 cte join (select rr from (values(1,7),(3,8)) rr limit 2) ss
2160 on (r).column1 = (rr).column1;
2161 select pg_get_viewdef('tt24v', true);
2163 ------------------------------------------------------------
2164 WITH cte AS MATERIALIZED ( +
2165 SELECT r.*::record AS r +
2166 FROM ( VALUES (1,2), (3,4)) r +
2168 SELECT (cte.r).column2 AS col_a, +
2169 (ss.rr).column2 AS col_b +
2171 JOIN ( SELECT rr.*::record AS rr +
2172 FROM ( VALUES (1,7), (3,8)) rr +
2173 LIMIT 2) ss ON (cte.r).column1 = (ss.rr).column1;
2176 create view tt25v as
2177 with cte as materialized (select pg_get_keywords() k)
2178 select (k).word from cte;
2179 select pg_get_viewdef('tt25v', true);
2181 ----------------------------------------
2182 WITH cte AS MATERIALIZED ( +
2183 SELECT pg_get_keywords() AS k+
2185 SELECT (k).word AS word +
2189 -- also check cases seen only in EXPLAIN
2190 explain (verbose, costs off)
2191 select * from tt24v;
2193 ------------------------------------------------------------------------------------------
2195 Output: (cte.r).column2, ((ROW("*VALUES*".column1, "*VALUES*".column2))).column2
2196 Hash Cond: ((cte.r).column1 = ((ROW("*VALUES*".column1, "*VALUES*".column2))).column1)
2198 -> Values Scan on "*VALUES*_1"
2199 Output: ROW("*VALUES*_1".column1, "*VALUES*_1".column2)
2203 Output: (ROW("*VALUES*".column1, "*VALUES*".column2))
2205 Output: (ROW("*VALUES*".column1, "*VALUES*".column2))
2206 -> Values Scan on "*VALUES*"
2207 Output: ROW("*VALUES*".column1, "*VALUES*".column2)
2210 explain (verbose, costs off)
2211 select (r).column2 from (select r from (values(1,2),(3,4)) r limit 1) ss;
2213 -------------------------------------------------------------------
2215 Output: (ss.r).column2
2217 Output: (ROW("*VALUES*".column1, "*VALUES*".column2))
2218 -> Values Scan on "*VALUES*"
2219 Output: ROW("*VALUES*".column1, "*VALUES*".column2)
2222 -- test pretty-print parenthesization rules, and SubLink deparsing
2223 create view tt26v as
2224 select x + y + z as c1,
2231 (x > y) AND (y > z OR x > z) as c8,
2232 (x > y) OR (y > z AND NOT (x > z)) as c9,
2233 (x,y) <> ALL (values(1,2),(3,4)) as c10,
2234 (x,y) <= ANY (values(1,2),(3,4)) as c11
2235 from (values(1,2,3)) v(x,y,z);
2236 select pg_get_viewdef('tt26v', true);
2238 ----------------------------------------------------
2239 SELECT x + y + z AS c1, +
2242 (x + y) * z AS c4, +
2243 x * (y + z) AS c5, +
2244 x + (y + z) AS c6, +
2245 x + (y # z) AS c7, +
2246 x > y AND (y > z OR x > z) AS c8, +
2247 x > y OR y > z AND NOT x > z AS c9, +
2248 ((x, y) <> ALL ( VALUES (1,2), (3,4))) AS c10,+
2249 ((x, y) <= ANY ( VALUES (1,2), (3,4))) AS c11 +
2250 FROM ( VALUES (1,2,3)) v(x, y, z);
2253 -- test restriction on non-system view expansion.
2254 create table tt27v_tbl (a int);
2255 create view tt27v as select a from tt27v_tbl;
2256 set restrict_nonsystem_relation_kind to 'view';
2257 select a from tt27v where a > 0; -- Error
2258 ERROR: access to non-system view "tt27v" is restricted
2259 insert into tt27v values (1); -- Error
2260 ERROR: access to non-system view "tt27v" is restricted
2261 select viewname from pg_views where viewname = 'tt27v'; -- Ok to access a system view.
2267 reset restrict_nonsystem_relation_kind;
2268 -- clean up all the random objects we made above
2269 DROP SCHEMA temp_view_test CASCADE;
2270 NOTICE: drop cascades to 27 other objects
2271 DETAIL: drop cascades to table temp_view_test.base_table
2272 drop cascades to view v2_temp
2273 drop cascades to view v4_temp
2274 drop cascades to view v6_temp
2275 drop cascades to view v7_temp
2276 drop cascades to view v10_temp
2277 drop cascades to view v8_temp
2278 drop cascades to view v9_temp
2279 drop cascades to view v11_temp
2280 drop cascades to view v12_temp
2281 drop cascades to table temp_view_test.base_table2
2282 drop cascades to view v5_temp
2283 drop cascades to view temp_view_test.v1
2284 drop cascades to view temp_view_test.v2
2285 drop cascades to view temp_view_test.v3
2286 drop cascades to view temp_view_test.v4
2287 drop cascades to view temp_view_test.v5
2288 drop cascades to view temp_view_test.v6
2289 drop cascades to view temp_view_test.v7
2290 drop cascades to view temp_view_test.v8
2291 drop cascades to sequence temp_view_test.seq1
2292 drop cascades to view temp_view_test.v9
2293 drop cascades to table temp_view_test.tx1
2294 drop cascades to view aliased_view_1
2295 drop cascades to view aliased_view_2
2296 drop cascades to view aliased_view_3
2297 drop cascades to view aliased_view_4
2298 DROP SCHEMA testviewschm2 CASCADE;
2299 NOTICE: drop cascades to 80 other objects
2300 DETAIL: drop cascades to table t1
2301 drop cascades to view temporal1
2302 drop cascades to view temporal2
2303 drop cascades to view temporal3
2304 drop cascades to view temporal4
2305 drop cascades to table t2
2306 drop cascades to view nontemp1
2307 drop cascades to view nontemp2
2308 drop cascades to view nontemp3
2309 drop cascades to view nontemp4
2310 drop cascades to table tbl1
2311 drop cascades to table tbl2
2312 drop cascades to table tbl3
2313 drop cascades to table tbl4
2314 drop cascades to view mytempview
2315 drop cascades to view pubview
2316 drop cascades to view mysecview1
2317 drop cascades to view mysecview2
2318 drop cascades to view mysecview3
2319 drop cascades to view mysecview4
2320 drop cascades to view mysecview7
2321 drop cascades to view mysecview8
2322 drop cascades to view mysecview9
2323 drop cascades to view unspecified_types
2324 drop cascades to table tt1
2325 drop cascades to table tx1
2326 drop cascades to view aliased_order_by
2327 drop cascades to view view_of_joins
2328 drop cascades to table tbl1a
2329 drop cascades to view view_of_joins_2a
2330 drop cascades to view view_of_joins_2b
2331 drop cascades to view view_of_joins_2c
2332 drop cascades to view view_of_joins_2d
2333 drop cascades to table tt2
2334 drop cascades to table tt3
2335 drop cascades to table tt4
2336 drop cascades to view v1
2337 drop cascades to view v1a
2338 drop cascades to view v2
2339 drop cascades to view v2a
2340 drop cascades to view v3
2341 drop cascades to table tt5
2342 drop cascades to table tt6
2343 drop cascades to view vv1
2344 drop cascades to view v4
2345 drop cascades to table tt7
2346 drop cascades to table tt8
2347 drop cascades to view vv2
2348 drop cascades to view vv3
2349 drop cascades to view vv4
2350 drop cascades to table tt7a
2351 drop cascades to table tt8a
2352 drop cascades to view vv2a
2353 drop cascades to table tt9
2354 drop cascades to table tt10
2355 drop cascades to view vv5
2356 drop cascades to table tt11
2357 drop cascades to table tt12
2358 drop cascades to table tt13
2359 drop cascades to view vv6
2360 drop cascades to table tt14t
2361 drop cascades to function tt14f()
2362 drop cascades to view tt14v
2363 drop cascades to type nestedcomposite
2364 drop cascades to view tt15v
2365 drop cascades to view tt16v
2366 drop cascades to view tt17v
2367 drop cascades to table tt15v_log
2368 drop cascades to view tt18v
2369 drop cascades to view tt19v
2370 drop cascades to view tt20v
2371 drop cascades to view tt201v
2372 drop cascades to view tt21v
2373 drop cascades to view tt22v
2374 drop cascades to view tt23v
2375 drop cascades to view tt24v
2376 drop cascades to view tt25v
2377 drop cascades to view tt26v
2378 drop cascades to table tt27v_tbl
2379 drop cascades to view tt27v