2 -- UNION (also INTERSECT, EXCEPT)
4 -- Simple UNION constructs
5 SELECT 1 AS two UNION SELECT 2 ORDER BY 1;
12 SELECT 1 AS one UNION SELECT 1 ORDER BY 1;
18 SELECT 1 AS two UNION ALL SELECT 2;
25 SELECT 1 AS two UNION ALL SELECT 1;
32 SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
40 SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1;
47 SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
55 SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1;
63 SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1;
70 SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1;
77 SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1;
83 SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1;
90 SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1;
97 SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
105 SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1;
112 SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
120 SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1;
128 -- Try testing from tables...
130 SELECT f1 AS five FROM FLOAT8_TBL
132 SELECT f1 FROM FLOAT8_TBL
135 -----------------------
136 -1.2345678901234e+200
139 -1.2345678901234e-200
143 SELECT f1 AS ten FROM FLOAT8_TBL
145 SELECT f1 FROM FLOAT8_TBL;
147 -----------------------
151 -1.2345678901234e+200
152 -1.2345678901234e-200
156 -1.2345678901234e+200
157 -1.2345678901234e-200
160 SELECT f1 AS nine FROM FLOAT8_TBL
162 SELECT f1 FROM INT4_TBL
165 -----------------------
166 -1.2345678901234e+200
171 -1.2345678901234e-200
177 SELECT f1 AS ten FROM FLOAT8_TBL
179 SELECT f1 FROM INT4_TBL;
181 -----------------------
185 -1.2345678901234e+200
186 -1.2345678901234e-200
194 SELECT f1 AS five FROM FLOAT8_TBL
195 WHERE f1 BETWEEN -1e6 AND 1e6
197 SELECT f1 FROM INT4_TBL
198 WHERE f1 BETWEEN 0 AND 1000000
201 -----------------------
204 -1.2345678901234e-200
209 SELECT CAST(f1 AS char(4)) AS three FROM VARCHAR_TBL
211 SELECT f1 FROM CHAR_TBL
220 SELECT f1 AS three FROM VARCHAR_TBL
222 SELECT CAST(f1 AS varchar) FROM CHAR_TBL
231 SELECT f1 AS eight FROM VARCHAR_TBL
233 SELECT f1 FROM CHAR_TBL;
246 SELECT f1 AS five FROM TEXT_TBL
248 SELECT f1 FROM VARCHAR_TBL
250 SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
262 -- INTERSECT and EXCEPT
264 SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1;
271 SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
279 SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
286 SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
293 SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1;
301 SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY 1;
306 SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1;
313 SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1;
321 SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE;
322 ERROR: FOR NO KEY UPDATE is not allowed with UNION/INTERSECT/EXCEPT
324 (SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6;
325 ?column? | ?column? | ?column?
326 ----------+----------+----------
330 (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6;
331 ?column? | ?column? | ?column?
332 ----------+----------+----------
336 (SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6;
337 ?column? | ?column? | ?column?
338 ----------+----------+----------
342 (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6;
343 ?column? | ?column? | ?column?
344 ----------+----------+----------
348 -- exercise both hashed and sorted implementations of UNION/INTERSECT/EXCEPT
349 set enable_hashagg to on;
352 ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
354 ----------------------------------------------------------------
357 Group Key: tenk1.unique1
359 -> Index Only Scan using tenk1_unique1 on tenk1
360 -> Seq Scan on tenk1 tenk1_1
364 ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
372 ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
374 ------------------------------------------------------------------------------------
376 -> Subquery Scan on ss
377 -> HashSetOp Intersect
379 -> Subquery Scan on "*SELECT* 2"
381 -> Subquery Scan on "*SELECT* 1"
382 -> Index Only Scan using tenk1_unique1 on tenk1 tenk1_1
386 ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
393 select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
395 ------------------------------------------------------------------------
398 -> Subquery Scan on "*SELECT* 1"
399 -> Index Only Scan using tenk1_unique1 on tenk1
400 -> Subquery Scan on "*SELECT* 2"
401 -> Index Only Scan using tenk1_unique2 on tenk1 tenk1_1
402 Filter: (unique2 <> 10)
405 select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
411 set enable_hashagg to off;
414 ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
416 ----------------------------------------------------------------
420 Sort Key: tenk1.unique1
421 -> Index Only Scan using tenk1_unique1 on tenk1
423 Sort Key: tenk1_1.fivethous
424 -> Seq Scan on tenk1 tenk1_1
428 ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
436 ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
438 ------------------------------------------------------------------------------------------
440 -> Subquery Scan on ss
443 Sort Key: "*SELECT* 2".fivethous
445 -> Subquery Scan on "*SELECT* 2"
447 -> Subquery Scan on "*SELECT* 1"
448 -> Index Only Scan using tenk1_unique1 on tenk1 tenk1_1
452 ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
459 select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
461 ------------------------------------------------------------------------------
464 Sort Key: "*SELECT* 1".unique1
466 -> Subquery Scan on "*SELECT* 1"
467 -> Index Only Scan using tenk1_unique1 on tenk1
468 -> Subquery Scan on "*SELECT* 2"
469 -> Index Only Scan using tenk1_unique2 on tenk1 tenk1_1
470 Filter: (unique2 <> 10)
473 select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
479 reset enable_hashagg;
481 set enable_hashagg to on;
483 select x from (values ('11'::varbit), ('10'::varbit)) _(x) union select x from (values ('11'::varbit), ('10'::varbit)) _(x);
485 -----------------------------------------------
488 Sort Key: "*VALUES*".column1
490 -> Values Scan on "*VALUES*"
491 -> Values Scan on "*VALUES*_1"
494 set enable_hashagg to off;
496 select x from (values ('11'::varbit), ('10'::varbit)) _(x) union select x from (values ('11'::varbit), ('10'::varbit)) _(x);
498 -----------------------------------------------
501 Sort Key: "*VALUES*".column1
503 -> Values Scan on "*VALUES*"
504 -> Values Scan on "*VALUES*_1"
507 reset enable_hashagg;
509 set enable_hashagg to on;
511 select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
513 -----------------------------------------
515 Group Key: "*VALUES*".column1
517 -> Values Scan on "*VALUES*"
518 -> Values Scan on "*VALUES*_1"
521 select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
530 select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
532 -----------------------------------------------
535 -> Subquery Scan on "*SELECT* 1"
536 -> Values Scan on "*VALUES*"
537 -> Subquery Scan on "*SELECT* 2"
538 -> Values Scan on "*VALUES*_1"
541 select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
548 select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
550 -----------------------------------------------
553 -> Subquery Scan on "*SELECT* 1"
554 -> Values Scan on "*VALUES*"
555 -> Subquery Scan on "*SELECT* 2"
556 -> Values Scan on "*VALUES*_1"
559 select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
567 select x from (values (array['10'::varbit]), (array['11'::varbit])) _(x) union select x from (values (array['10'::varbit]), (array['01'::varbit])) _(x);
569 -----------------------------------------------
572 Sort Key: "*VALUES*".column1
574 -> Values Scan on "*VALUES*"
575 -> Values Scan on "*VALUES*_1"
578 select x from (values (array['10'::varbit]), (array['11'::varbit])) _(x) union select x from (values (array['10'::varbit]), (array['01'::varbit])) _(x);
586 set enable_hashagg to off;
588 select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
590 -----------------------------------------------
593 Sort Key: "*VALUES*".column1
595 -> Values Scan on "*VALUES*"
596 -> Values Scan on "*VALUES*_1"
599 select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
608 select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
610 -----------------------------------------------------
613 Sort Key: "*SELECT* 1".x
615 -> Subquery Scan on "*SELECT* 1"
616 -> Values Scan on "*VALUES*"
617 -> Subquery Scan on "*SELECT* 2"
618 -> Values Scan on "*VALUES*_1"
621 select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
628 select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
630 -----------------------------------------------------
633 Sort Key: "*SELECT* 1".x
635 -> Subquery Scan on "*SELECT* 1"
636 -> Values Scan on "*VALUES*"
637 -> Subquery Scan on "*SELECT* 2"
638 -> Values Scan on "*VALUES*_1"
641 select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
647 reset enable_hashagg;
649 set enable_hashagg to on;
651 select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
653 -----------------------------------------------
656 Sort Key: "*VALUES*".column1
658 -> Values Scan on "*VALUES*"
659 -> Values Scan on "*VALUES*_1"
662 select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
671 select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
673 -----------------------------------------------------
676 Sort Key: "*SELECT* 1".x
678 -> Subquery Scan on "*SELECT* 1"
679 -> Values Scan on "*VALUES*"
680 -> Subquery Scan on "*SELECT* 2"
681 -> Values Scan on "*VALUES*_1"
684 select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
691 select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
693 -----------------------------------------------------
696 Sort Key: "*SELECT* 1".x
698 -> Subquery Scan on "*SELECT* 1"
699 -> Values Scan on "*VALUES*"
700 -> Subquery Scan on "*SELECT* 2"
701 -> Values Scan on "*VALUES*_1"
704 select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
711 -- With an anonymous row type, the typcache does not report that the
712 -- type is hashable. (Otherwise, this would fail at execution time.)
714 select x from (values (row('10'::varbit)), (row('11'::varbit))) _(x) union select x from (values (row('10'::varbit)), (row('01'::varbit))) _(x);
716 -----------------------------------------------
719 Sort Key: "*VALUES*".column1
721 -> Values Scan on "*VALUES*"
722 -> Values Scan on "*VALUES*_1"
725 select x from (values (row('10'::varbit)), (row('11'::varbit))) _(x) union select x from (values (row('10'::varbit)), (row('01'::varbit))) _(x);
733 -- With a defined row type, the typcache can inspect the type's fields
735 create type ct1 as (f1 varbit);
737 select x from (values (row('10'::varbit)::ct1), (row('11'::varbit)::ct1)) _(x) union select x from (values (row('10'::varbit)::ct1), (row('01'::varbit)::ct1)) _(x);
739 -----------------------------------------------
742 Sort Key: "*VALUES*".column1
744 -> Values Scan on "*VALUES*"
745 -> Values Scan on "*VALUES*_1"
748 select x from (values (row('10'::varbit)::ct1), (row('11'::varbit)::ct1)) _(x) union select x from (values (row('10'::varbit)::ct1), (row('01'::varbit)::ct1)) _(x);
757 set enable_hashagg to off;
759 select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
761 -----------------------------------------------
764 Sort Key: "*VALUES*".column1
766 -> Values Scan on "*VALUES*"
767 -> Values Scan on "*VALUES*_1"
770 select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
779 select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
781 -----------------------------------------------------
784 Sort Key: "*SELECT* 1".x
786 -> Subquery Scan on "*SELECT* 1"
787 -> Values Scan on "*VALUES*"
788 -> Subquery Scan on "*SELECT* 2"
789 -> Values Scan on "*VALUES*_1"
792 select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
799 select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
801 -----------------------------------------------------
804 Sort Key: "*SELECT* 1".x
806 -> Subquery Scan on "*SELECT* 1"
807 -> Values Scan on "*VALUES*"
808 -> Subquery Scan on "*SELECT* 2"
809 -> Values Scan on "*VALUES*_1"
812 select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
819 -- Ensure we get a HashAggregate plan. Keep enable_hashagg=off to ensure
820 -- there's no chance of a sort.
821 explain (costs off) select '123'::xid union select '123'::xid;
823 ---------------------------
826 Group Key: ('123'::xid)
832 reset enable_hashagg;
836 SELECT f1 FROM float8_tbl INTERSECT SELECT f1 FROM int4_tbl ORDER BY 1;
842 SELECT f1 FROM float8_tbl EXCEPT SELECT f1 FROM int4_tbl ORDER BY 1;
844 -----------------------
845 -1.2345678901234e+200
848 -1.2345678901234e-200
852 -- Operator precedence and (((((extra))))) parentheses
854 SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl ORDER BY 1;
866 SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1;
873 (((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl;
885 SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
892 SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)));
904 (((SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
912 -- Subqueries with ORDER BY & LIMIT clauses
914 -- In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
915 SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
918 ------------------+-------------------
919 4567890123456789 | -4567890123456789
923 -- This should fail, because q2 isn't a name of an EXCEPT output column
924 SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
925 ERROR: column "q2" does not exist
926 LINE 1: ... int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1...
928 DETAIL: There is a column named "q2" in table "*SELECT* 2", but it cannot be referenced from this part of the query.
929 -- But this should work:
930 SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
938 -- New syntaxes (7.1) permit new tests
940 (((((select * from int8_tbl)))));
942 ------------------+-------------------
944 123 | 4567890123456789
945 4567890123456789 | 123
946 4567890123456789 | 4567890123456789
947 4567890123456789 | -4567890123456789
951 -- Check behavior with empty select list (allowed since 9.4)
957 select intersect select;
961 select except select;
965 -- check hashed implementation
966 set enable_hashagg = true;
967 set enable_sort = false;
968 -- We've no way to check hashed UNION as the empty pathkeys in the Append are
969 -- fine to make use of Unique, which is cheaper than HashAggregate and we've
970 -- no means to disable Unique.
972 select from generate_series(1,5) intersect select from generate_series(1,3);
974 ----------------------------------------------------------------------
977 -> Subquery Scan on "*SELECT* 1"
978 -> Function Scan on generate_series
979 -> Subquery Scan on "*SELECT* 2"
980 -> Function Scan on generate_series generate_series_1
983 select from generate_series(1,5) union all select from generate_series(1,3);
987 select from generate_series(1,5) intersect select from generate_series(1,3);
991 select from generate_series(1,5) intersect all select from generate_series(1,3);
995 select from generate_series(1,5) except select from generate_series(1,3);
999 select from generate_series(1,5) except all select from generate_series(1,3);
1003 -- check sorted implementation
1004 set enable_hashagg = false;
1005 set enable_sort = true;
1007 select from generate_series(1,5) union select from generate_series(1,3);
1009 ----------------------------------------------------------------
1012 -> Function Scan on generate_series
1013 -> Function Scan on generate_series generate_series_1
1017 select from generate_series(1,5) intersect select from generate_series(1,3);
1019 ----------------------------------------------------------------------
1022 -> Subquery Scan on "*SELECT* 1"
1023 -> Function Scan on generate_series
1024 -> Subquery Scan on "*SELECT* 2"
1025 -> Function Scan on generate_series generate_series_1
1028 select from generate_series(1,5) union select from generate_series(1,3);
1032 select from generate_series(1,5) union all select from generate_series(1,3);
1036 select from generate_series(1,5) intersect select from generate_series(1,3);
1040 select from generate_series(1,5) intersect all select from generate_series(1,3);
1044 select from generate_series(1,5) except select from generate_series(1,3);
1048 select from generate_series(1,5) except all select from generate_series(1,3);
1052 -- Try a variation of the above but with a CTE which contains a column, again
1053 -- with an empty final select list.
1054 -- Ensure we get the expected 1 row with 0 columns
1055 with cte as materialized (select s from generate_series(1,5) s)
1056 select from cte union select from cte;
1060 -- Ensure we get the same result as the above.
1061 with cte as not materialized (select s from generate_series(1,5) s)
1062 select from cte union select from cte;
1066 reset enable_hashagg;
1069 -- Check handling of a case with unknown constants. We don't guarantee
1070 -- an undecorated constant will work in all cases, but historically this
1071 -- usage has worked, so test we don't break it.
1073 SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
1075 SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
1085 -- This should fail, but it should produce an error cursor
1086 SELECT '3.4'::numeric UNION SELECT 'foo';
1087 ERROR: invalid input syntax for type numeric: "foo"
1088 LINE 1: SELECT '3.4'::numeric UNION SELECT 'foo';
1091 -- Test that expression-index constraints can be pushed down through
1092 -- UNION or UNION ALL
1094 CREATE TEMP TABLE t1 (a text, b text);
1095 CREATE INDEX t1_ab_idx on t1 ((a || b));
1096 CREATE TEMP TABLE t2 (ab text primary key);
1097 INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
1098 INSERT INTO t2 VALUES ('ab'), ('xy');
1099 set enable_seqscan = off;
1100 set enable_indexscan = on;
1101 set enable_bitmapscan = off;
1102 set enable_sort = off;
1105 (SELECT a || b AS ab FROM t1
1110 ---------------------------------------------
1112 -> Index Scan using t1_ab_idx on t1
1113 Index Cond: ((a || b) = 'ab'::text)
1114 -> Index Only Scan using t2_pkey on t2
1115 Index Cond: (ab = 'ab'::text)
1120 (SELECT a || b AS ab FROM t1
1125 ---------------------------------------------------
1127 Group Key: ((t1.a || t1.b))
1129 -> Index Scan using t1_ab_idx on t1
1130 Index Cond: ((a || b) = 'ab'::text)
1131 -> Index Only Scan using t2_pkey on t2
1132 Index Cond: (ab = 'ab'::text)
1136 -- Test that ORDER BY for UNION ALL can be pushed down to inheritance
1139 CREATE TEMP TABLE t1c (b text, a text);
1140 ALTER TABLE t1c INHERIT t1;
1141 CREATE TEMP TABLE t2c (primary key (ab)) INHERITS (t2);
1142 INSERT INTO t1c VALUES ('v', 'w'), ('c', 'd'), ('m', 'n'), ('e', 'f');
1143 INSERT INTO t2c VALUES ('vw'), ('cd'), ('mn'), ('ef');
1144 CREATE INDEX t1c_ab_idx on t1c ((a || b));
1145 set enable_seqscan = on;
1146 set enable_indexonlyscan = off;
1149 (SELECT a || b AS ab FROM t1
1151 SELECT ab FROM t2) t
1154 -----------------------------------------------------
1157 Sort Key: ((t1.a || t1.b))
1158 -> Index Scan using t1_ab_idx on t1
1159 -> Index Scan using t1c_ab_idx on t1c t1_1
1160 -> Index Scan using t2_pkey on t2
1161 -> Index Scan using t2c_pkey on t2c t2_1
1165 (SELECT a || b AS ab FROM t1
1167 SELECT ab FROM t2) t
1181 reset enable_seqscan;
1182 reset enable_indexscan;
1183 reset enable_bitmapscan;
1185 -- This simpler variant of the above test has been observed to fail differently
1186 create table events (event_id int primary key);
1187 create table other_events (event_id int primary key);
1188 create table events_child () inherits (events);
1191 from (select event_id from events
1193 select event_id from other_events) ss
1196 ----------------------------------------------------------
1198 Sort Key: events.event_id
1199 -> Index Scan using events_pkey on events
1201 Sort Key: events_1.event_id
1202 -> Seq Scan on events_child events_1
1203 -> Index Scan using other_events_pkey on other_events
1206 drop table events_child, events, other_events;
1207 reset enable_indexonlyscan;
1208 -- Test constraint exclusion of UNION ALL subqueries
1211 (SELECT 1 AS t, * FROM tenk1 a
1213 SELECT 2 AS t, * FROM tenk1 b) c
1216 ---------------------
1220 -- Test that we push quals into UNION sub-selects only when it's safe
1223 (SELECT 1 AS t, 2 AS x
1225 SELECT 2 AS t, 4 AS x) ss
1229 --------------------------------------------------
1238 One-Time Filter: false
1242 (SELECT 1 AS t, 2 AS x
1244 SELECT 2 AS t, 4 AS x) ss
1254 (SELECT 1 AS t, generate_series(1,10) AS x
1256 SELECT 2 AS t, 4 AS x) ss
1260 --------------------------------------------------------
1263 -> Subquery Scan on ss
1266 Group Key: (1), (generate_series(1, 10))
1274 (SELECT 1 AS t, generate_series(1,10) AS x
1276 SELECT 2 AS t, 4 AS x) ss
1288 (SELECT 1 AS t, (random()*3)::int AS x
1290 SELECT 2 AS t, 4 AS x) ss
1294 ------------------------------------------------------------------------------------
1297 -> Subquery Scan on ss
1301 Sort Key: (1), (((random() * '3'::double precision))::integer)
1308 (SELECT 1 AS t, (random()*3)::int AS x
1310 SELECT 2 AS t, 4 AS x) ss
1318 -- Test cases where the native ordering of a sub-select has more pathkeys
1319 -- than the outer query cares about
1321 select distinct q1 from
1322 (select distinct * from int8_tbl i81
1324 select distinct * from int8_tbl i82) ss
1327 ----------------------------------------------------------
1330 Sort Key: "*SELECT* 1".q1
1331 -> Subquery Scan on "*SELECT* 1"
1334 Sort Key: i81.q1, i81.q2
1335 -> Seq Scan on int8_tbl i81
1336 Filter: (q2 IS NOT NULL)
1337 -> Subquery Scan on "*SELECT* 2"
1340 Sort Key: i82.q1, i82.q2
1341 -> Seq Scan on int8_tbl i82
1342 Filter: (q2 IS NOT NULL)
1345 select distinct q1 from
1346 (select distinct * from int8_tbl i81
1348 select distinct * from int8_tbl i82) ss
1357 select distinct q1 from
1358 (select distinct * from int8_tbl i81
1360 select distinct * from int8_tbl i82) ss
1363 --------------------------------------------------------
1366 Sort Key: "*SELECT* 1".q1
1367 -> Subquery Scan on "*SELECT* 1"
1370 Sort Key: i81.q1, i81.q2
1371 -> Seq Scan on int8_tbl i81
1372 Filter: ((- q1) = q2)
1373 -> Subquery Scan on "*SELECT* 2"
1376 Sort Key: i82.q1, i82.q2
1377 -> Seq Scan on int8_tbl i82
1378 Filter: ((- q1) = q2)
1381 select distinct q1 from
1382 (select distinct * from int8_tbl i81
1384 select distinct * from int8_tbl i82) ss
1391 -- Test proper handling of parameterized appendrel paths when the
1392 -- potential join qual is expensive
1393 create function expensivefunc(int) returns int
1394 language plpgsql immutable strict cost 10000
1395 as $$begin return $1; end$$;
1396 create temp table t3 as select generate_series(-1000,1000) as x;
1397 create index t3i on t3 (expensivefunc(x));
1401 (select * from t3 a union all select * from t3 b) ss
1402 join int4_tbl on f1 = expensivefunc(x);
1404 ------------------------------------------------------------
1406 -> Seq Scan on int4_tbl
1408 -> Index Scan using t3i on t3 a
1409 Index Cond: (expensivefunc(x) = int4_tbl.f1)
1410 -> Index Scan using t3i on t3 b
1411 Index Cond: (expensivefunc(x) = int4_tbl.f1)
1415 (select * from t3 a union all select * from t3 b) ss
1416 join int4_tbl on f1 = expensivefunc(x);
1424 drop function expensivefunc(int);
1425 -- Test handling of appendrel quals that const-simplify into an AND
1428 (select *, 0 as x from int8_tbl a
1430 select *, 1 as x from int8_tbl b) ss
1431 where (x = 0) or (q1 >= q2 and q1 <= q2);
1433 ---------------------------------------------
1435 -> Seq Scan on int8_tbl a
1436 -> Seq Scan on int8_tbl b
1437 Filter: ((q1 >= q2) AND (q1 <= q2))
1441 (select *, 0 as x from int8_tbl a
1443 select *, 1 as x from int8_tbl b) ss
1444 where (x = 0) or (q1 >= q2 and q1 <= q2);
1446 ------------------+-------------------+---
1448 123 | 4567890123456789 | 0
1449 4567890123456789 | 123 | 0
1450 4567890123456789 | 4567890123456789 | 0
1451 4567890123456789 | -4567890123456789 | 0
1452 4567890123456789 | 4567890123456789 | 1
1456 -- Test the planner's ability to produce cheap startup plans with Append nodes
1458 -- Ensure we get a Nested Loop join between tenk1 and tenk2
1460 select t1.unique1 from tenk1 t1
1461 inner join tenk2 t2 on t1.tenthous = t2.tenthous and t2.thousand = 0
1463 (values(1)) limit 1;
1465 --------------------------------------------------------
1469 Join Filter: (t1.tenthous = t2.tenthous)
1470 -> Seq Scan on tenk1 t1
1472 -> Seq Scan on tenk2 t2
1473 Filter: (thousand = 0)
1477 -- Ensure there is no problem if cheapest_startup_path is NULL
1479 select * from tenk1 t1
1481 (select t1.tenthous from tenk2 t2 union all (values(1)))
1484 -------------------------------------------------------------------
1486 -> Nested Loop Left Join
1487 -> Seq Scan on tenk1 t1
1489 -> Index Only Scan using tenk2_hundred on tenk2 t2