union.out 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. --
  2. -- UNION (also INTERSECT, EXCEPT)
  3. --
  4. -- Simple UNION constructs
  5. SELECT 1 AS two UNION SELECT 2 ORDER BY 1;
  6. two
  7. -----
  8. 1
  9. 2
  10. (2 rows)
  11. SELECT 1 AS one UNION SELECT 1 ORDER BY 1;
  12. one
  13. -----
  14. 1
  15. (1 row)
  16. SELECT 1 AS two UNION ALL SELECT 2;
  17. two
  18. -----
  19. 1
  20. 2
  21. (2 rows)
  22. SELECT 1 AS two UNION ALL SELECT 1;
  23. two
  24. -----
  25. 1
  26. 1
  27. (2 rows)
  28. SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
  29. three
  30. -------
  31. 1
  32. 2
  33. 3
  34. (3 rows)
  35. SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1;
  36. two
  37. -----
  38. 1
  39. 2
  40. (2 rows)
  41. SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
  42. three
  43. -------
  44. 1
  45. 2
  46. 2
  47. (3 rows)
  48. SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1;
  49. two
  50. -----
  51. 1.1
  52. 2.2
  53. (2 rows)
  54. -- Mixed types
  55. SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1;
  56. two
  57. -----
  58. 1.1
  59. 2
  60. (2 rows)
  61. SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1;
  62. two
  63. -----
  64. 1
  65. 2.2
  66. (2 rows)
  67. SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1;
  68. one
  69. -----
  70. 1
  71. (1 row)
  72. SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1;
  73. two
  74. -----
  75. 1.1
  76. 2
  77. (2 rows)
  78. SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1;
  79. two
  80. -----
  81. 1
  82. 1
  83. (2 rows)
  84. SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
  85. three
  86. -------
  87. 1.1
  88. 2
  89. 3
  90. (3 rows)
  91. SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1;
  92. two
  93. -----
  94. 1.1
  95. 2
  96. (2 rows)
  97. SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
  98. three
  99. -------
  100. 1.1
  101. 2
  102. 2
  103. (3 rows)
  104. SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1;
  105. two
  106. -----
  107. 1.1
  108. 2
  109. (2 rows)
  110. --
  111. -- Try testing from tables...
  112. --
  113. SELECT f1 AS five FROM FLOAT8_TBL
  114. UNION
  115. SELECT f1 FROM FLOAT8_TBL
  116. ORDER BY 1;
  117. five
  118. -----------------------
  119. -1.2345678901234e+200
  120. -1004.3
  121. -34.84
  122. -1.2345678901234e-200
  123. 0
  124. (5 rows)
  125. SELECT f1 AS ten FROM FLOAT8_TBL
  126. UNION ALL
  127. SELECT f1 FROM FLOAT8_TBL;
  128. ten
  129. -----------------------
  130. 0
  131. -34.84
  132. -1004.3
  133. -1.2345678901234e+200
  134. -1.2345678901234e-200
  135. 0
  136. -34.84
  137. -1004.3
  138. -1.2345678901234e+200
  139. -1.2345678901234e-200
  140. (10 rows)
  141. SELECT f1 AS nine FROM FLOAT8_TBL
  142. UNION
  143. SELECT f1 FROM INT4_TBL
  144. ORDER BY 1;
  145. nine
  146. -----------------------
  147. -1.2345678901234e+200
  148. -2147483647
  149. -123456
  150. -1004.3
  151. -34.84
  152. -1.2345678901234e-200
  153. 0
  154. 123456
  155. 2147483647
  156. (9 rows)
  157. SELECT f1 AS ten FROM FLOAT8_TBL
  158. UNION ALL
  159. SELECT f1 FROM INT4_TBL;
  160. ten
  161. -----------------------
  162. 0
  163. -34.84
  164. -1004.3
  165. -1.2345678901234e+200
  166. -1.2345678901234e-200
  167. 0
  168. 123456
  169. -123456
  170. 2147483647
  171. -2147483647
  172. (10 rows)
  173. SELECT f1 AS five FROM FLOAT8_TBL
  174. WHERE f1 BETWEEN -1e6 AND 1e6
  175. UNION
  176. SELECT f1 FROM INT4_TBL
  177. WHERE f1 BETWEEN 0 AND 1000000
  178. ORDER BY 1;
  179. five
  180. -----------------------
  181. -1004.3
  182. -34.84
  183. -1.2345678901234e-200
  184. 0
  185. 123456
  186. (5 rows)
  187. SELECT CAST(f1 AS char(4)) AS three FROM VARCHAR_TBL
  188. UNION
  189. SELECT f1 FROM CHAR_TBL
  190. ORDER BY 1;
  191. three
  192. -------
  193. a
  194. ab
  195. abcd
  196. (3 rows)
  197. SELECT f1 AS three FROM VARCHAR_TBL
  198. UNION
  199. SELECT CAST(f1 AS varchar) FROM CHAR_TBL
  200. ORDER BY 1;
  201. three
  202. -------
  203. a
  204. ab
  205. abcd
  206. (3 rows)
  207. SELECT f1 AS eight FROM VARCHAR_TBL
  208. UNION ALL
  209. SELECT f1 FROM CHAR_TBL;
  210. eight
  211. -------
  212. a
  213. ab
  214. abcd
  215. abcd
  216. a
  217. ab
  218. abcd
  219. abcd
  220. (8 rows)
  221. SELECT f1 AS five FROM TEXT_TBL
  222. UNION
  223. SELECT f1 FROM VARCHAR_TBL
  224. UNION
  225. SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
  226. ORDER BY 1;
  227. five
  228. -------------------
  229. a
  230. ab
  231. abcd
  232. doh!
  233. hi de ho neighbor
  234. (5 rows)
  235. --
  236. -- INTERSECT and EXCEPT
  237. --
  238. SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1;
  239. q2
  240. ------------------
  241. 123
  242. 4567890123456789
  243. (2 rows)
  244. SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
  245. q2
  246. ------------------
  247. 123
  248. 4567890123456789
  249. 4567890123456789
  250. (3 rows)
  251. SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
  252. q2
  253. -------------------
  254. -4567890123456789
  255. 456
  256. (2 rows)
  257. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
  258. q2
  259. -------------------
  260. -4567890123456789
  261. 456
  262. (2 rows)
  263. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1;
  264. q2
  265. -------------------
  266. -4567890123456789
  267. 456
  268. 4567890123456789
  269. (3 rows)
  270. SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY 1;
  271. q1
  272. ----
  273. (0 rows)
  274. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1;
  275. q1
  276. ------------------
  277. 123
  278. 4567890123456789
  279. (2 rows)
  280. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1;
  281. q1
  282. ------------------
  283. 123
  284. 4567890123456789
  285. 4567890123456789
  286. (3 rows)
  287. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE;
  288. ERROR: FOR NO KEY UPDATE is not allowed with UNION/INTERSECT/EXCEPT
  289. -- nested cases
  290. (SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6;
  291. ?column? | ?column? | ?column?
  292. ----------+----------+----------
  293. 4 | 5 | 6
  294. (1 row)
  295. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6;
  296. ?column? | ?column? | ?column?
  297. ----------+----------+----------
  298. 4 | 5 | 6
  299. (1 row)
  300. (SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6;
  301. ?column? | ?column? | ?column?
  302. ----------+----------+----------
  303. 1 | 2 | 3
  304. (1 row)
  305. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6;
  306. ?column? | ?column? | ?column?
  307. ----------+----------+----------
  308. 1 | 2 | 3
  309. (1 row)
  310. -- exercise both hashed and sorted implementations of UNION/INTERSECT/EXCEPT
  311. set enable_hashagg to on;
  312. explain (costs off)
  313. select count(*) from
  314. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  315. QUERY PLAN
  316. ----------------------------------------------------------------
  317. Aggregate
  318. -> HashAggregate
  319. Group Key: tenk1.unique1
  320. -> Append
  321. -> Index Only Scan using tenk1_unique1 on tenk1
  322. -> Seq Scan on tenk1 tenk1_1
  323. (6 rows)
  324. select count(*) from
  325. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  326. count
  327. -------
  328. 10000
  329. (1 row)
  330. explain (costs off)
  331. select count(*) from
  332. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  333. QUERY PLAN
  334. ------------------------------------------------------------------------------------
  335. Aggregate
  336. -> Subquery Scan on ss
  337. -> HashSetOp Intersect
  338. -> Append
  339. -> Subquery Scan on "*SELECT* 2"
  340. -> Seq Scan on tenk1
  341. -> Subquery Scan on "*SELECT* 1"
  342. -> Index Only Scan using tenk1_unique1 on tenk1 tenk1_1
  343. (8 rows)
  344. select count(*) from
  345. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  346. count
  347. -------
  348. 5000
  349. (1 row)
  350. explain (costs off)
  351. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  352. QUERY PLAN
  353. ------------------------------------------------------------------------
  354. HashSetOp Except
  355. -> Append
  356. -> Subquery Scan on "*SELECT* 1"
  357. -> Index Only Scan using tenk1_unique1 on tenk1
  358. -> Subquery Scan on "*SELECT* 2"
  359. -> Index Only Scan using tenk1_unique2 on tenk1 tenk1_1
  360. Filter: (unique2 <> 10)
  361. (7 rows)
  362. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  363. unique1
  364. ---------
  365. 10
  366. (1 row)
  367. set enable_hashagg to off;
  368. explain (costs off)
  369. select count(*) from
  370. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  371. QUERY PLAN
  372. ----------------------------------------------------------------------
  373. Aggregate
  374. -> Unique
  375. -> Sort
  376. Sort Key: tenk1.unique1
  377. -> Append
  378. -> Index Only Scan using tenk1_unique1 on tenk1
  379. -> Seq Scan on tenk1 tenk1_1
  380. (7 rows)
  381. select count(*) from
  382. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  383. count
  384. -------
  385. 10000
  386. (1 row)
  387. explain (costs off)
  388. select count(*) from
  389. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  390. QUERY PLAN
  391. ------------------------------------------------------------------------------------------
  392. Aggregate
  393. -> Subquery Scan on ss
  394. -> SetOp Intersect
  395. -> Sort
  396. Sort Key: "*SELECT* 2".fivethous
  397. -> Append
  398. -> Subquery Scan on "*SELECT* 2"
  399. -> Seq Scan on tenk1
  400. -> Subquery Scan on "*SELECT* 1"
  401. -> Index Only Scan using tenk1_unique1 on tenk1 tenk1_1
  402. (10 rows)
  403. select count(*) from
  404. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  405. count
  406. -------
  407. 5000
  408. (1 row)
  409. explain (costs off)
  410. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  411. QUERY PLAN
  412. ------------------------------------------------------------------------------
  413. SetOp Except
  414. -> Sort
  415. Sort Key: "*SELECT* 1".unique1
  416. -> Append
  417. -> Subquery Scan on "*SELECT* 1"
  418. -> Index Only Scan using tenk1_unique1 on tenk1
  419. -> Subquery Scan on "*SELECT* 2"
  420. -> Index Only Scan using tenk1_unique2 on tenk1 tenk1_1
  421. Filter: (unique2 <> 10)
  422. (9 rows)
  423. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  424. unique1
  425. ---------
  426. 10
  427. (1 row)
  428. reset enable_hashagg;
  429. -- non-hashable type
  430. set enable_hashagg to on;
  431. explain (costs off)
  432. select x from (values (100::money), (200::money)) _(x) union select x from (values (100::money), (300::money)) _(x);
  433. QUERY PLAN
  434. -----------------------------------------------
  435. Unique
  436. -> Sort
  437. Sort Key: "*VALUES*".column1
  438. -> Append
  439. -> Values Scan on "*VALUES*"
  440. -> Values Scan on "*VALUES*_1"
  441. (6 rows)
  442. set enable_hashagg to off;
  443. explain (costs off)
  444. select x from (values (100::money), (200::money)) _(x) union select x from (values (100::money), (300::money)) _(x);
  445. QUERY PLAN
  446. -----------------------------------------------
  447. Unique
  448. -> Sort
  449. Sort Key: "*VALUES*".column1
  450. -> Append
  451. -> Values Scan on "*VALUES*"
  452. -> Values Scan on "*VALUES*_1"
  453. (6 rows)
  454. reset enable_hashagg;
  455. -- arrays
  456. set enable_hashagg to on;
  457. explain (costs off)
  458. select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
  459. QUERY PLAN
  460. -----------------------------------------
  461. HashAggregate
  462. Group Key: "*VALUES*".column1
  463. -> Append
  464. -> Values Scan on "*VALUES*"
  465. -> Values Scan on "*VALUES*_1"
  466. (5 rows)
  467. select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
  468. x
  469. -------
  470. {1,4}
  471. {1,2}
  472. {1,3}
  473. (3 rows)
  474. explain (costs off)
  475. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  476. QUERY PLAN
  477. -----------------------------------------------
  478. HashSetOp Intersect
  479. -> Append
  480. -> Subquery Scan on "*SELECT* 1"
  481. -> Values Scan on "*VALUES*"
  482. -> Subquery Scan on "*SELECT* 2"
  483. -> Values Scan on "*VALUES*_1"
  484. (6 rows)
  485. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  486. x
  487. -------
  488. {1,2}
  489. (1 row)
  490. explain (costs off)
  491. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  492. QUERY PLAN
  493. -----------------------------------------------
  494. HashSetOp Except
  495. -> Append
  496. -> Subquery Scan on "*SELECT* 1"
  497. -> Values Scan on "*VALUES*"
  498. -> Subquery Scan on "*SELECT* 2"
  499. -> Values Scan on "*VALUES*_1"
  500. (6 rows)
  501. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  502. x
  503. -------
  504. {1,3}
  505. (1 row)
  506. -- non-hashable type
  507. explain (costs off)
  508. select x from (values (array[100::money]), (array[200::money])) _(x) union select x from (values (array[100::money]), (array[300::money])) _(x);
  509. QUERY PLAN
  510. -----------------------------------------------
  511. Unique
  512. -> Sort
  513. Sort Key: "*VALUES*".column1
  514. -> Append
  515. -> Values Scan on "*VALUES*"
  516. -> Values Scan on "*VALUES*_1"
  517. (6 rows)
  518. select x from (values (array[100::money]), (array[200::money])) _(x) union select x from (values (array[100::money]), (array[300::money])) _(x);
  519. x
  520. -----------
  521. {$100.00}
  522. {$200.00}
  523. {$300.00}
  524. (3 rows)
  525. set enable_hashagg to off;
  526. explain (costs off)
  527. select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
  528. QUERY PLAN
  529. -----------------------------------------------
  530. Unique
  531. -> Sort
  532. Sort Key: "*VALUES*".column1
  533. -> Append
  534. -> Values Scan on "*VALUES*"
  535. -> Values Scan on "*VALUES*_1"
  536. (6 rows)
  537. select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (values (array[1, 2]), (array[1, 4])) _(x);
  538. x
  539. -------
  540. {1,2}
  541. {1,3}
  542. {1,4}
  543. (3 rows)
  544. explain (costs off)
  545. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  546. QUERY PLAN
  547. -----------------------------------------------------
  548. SetOp Intersect
  549. -> Sort
  550. Sort Key: "*SELECT* 1".x
  551. -> Append
  552. -> Subquery Scan on "*SELECT* 1"
  553. -> Values Scan on "*VALUES*"
  554. -> Subquery Scan on "*SELECT* 2"
  555. -> Values Scan on "*VALUES*_1"
  556. (8 rows)
  557. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  558. x
  559. -------
  560. {1,2}
  561. (1 row)
  562. explain (costs off)
  563. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  564. QUERY PLAN
  565. -----------------------------------------------------
  566. SetOp Except
  567. -> Sort
  568. Sort Key: "*SELECT* 1".x
  569. -> Append
  570. -> Subquery Scan on "*SELECT* 1"
  571. -> Values Scan on "*VALUES*"
  572. -> Subquery Scan on "*SELECT* 2"
  573. -> Values Scan on "*VALUES*_1"
  574. (8 rows)
  575. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  576. x
  577. -------
  578. {1,3}
  579. (1 row)
  580. reset enable_hashagg;
  581. -- records
  582. set enable_hashagg to on;
  583. explain (costs off)
  584. select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
  585. QUERY PLAN
  586. -----------------------------------------------
  587. Unique
  588. -> Sort
  589. Sort Key: "*VALUES*".column1
  590. -> Append
  591. -> Values Scan on "*VALUES*"
  592. -> Values Scan on "*VALUES*_1"
  593. (6 rows)
  594. select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
  595. x
  596. -------
  597. (1,2)
  598. (1,3)
  599. (1,4)
  600. (3 rows)
  601. explain (costs off)
  602. select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
  603. QUERY PLAN
  604. -----------------------------------------------------
  605. SetOp Intersect
  606. -> Sort
  607. Sort Key: "*SELECT* 1".x
  608. -> Append
  609. -> Subquery Scan on "*SELECT* 1"
  610. -> Values Scan on "*VALUES*"
  611. -> Subquery Scan on "*SELECT* 2"
  612. -> Values Scan on "*VALUES*_1"
  613. (8 rows)
  614. select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
  615. x
  616. -------
  617. (1,2)
  618. (1 row)
  619. explain (costs off)
  620. select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
  621. QUERY PLAN
  622. -----------------------------------------------------
  623. SetOp Except
  624. -> Sort
  625. Sort Key: "*SELECT* 1".x
  626. -> Append
  627. -> Subquery Scan on "*SELECT* 1"
  628. -> Values Scan on "*VALUES*"
  629. -> Subquery Scan on "*SELECT* 2"
  630. -> Values Scan on "*VALUES*_1"
  631. (8 rows)
  632. select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
  633. x
  634. -------
  635. (1,3)
  636. (1 row)
  637. -- non-hashable type
  638. -- With an anonymous row type, the typcache does not report that the
  639. -- type is hashable. (Otherwise, this would fail at execution time.)
  640. explain (costs off)
  641. select x from (values (row(100::money)), (row(200::money))) _(x) union select x from (values (row(100::money)), (row(300::money))) _(x);
  642. QUERY PLAN
  643. -----------------------------------------------
  644. Unique
  645. -> Sort
  646. Sort Key: "*VALUES*".column1
  647. -> Append
  648. -> Values Scan on "*VALUES*"
  649. -> Values Scan on "*VALUES*_1"
  650. (6 rows)
  651. select x from (values (row(100::money)), (row(200::money))) _(x) union select x from (values (row(100::money)), (row(300::money))) _(x);
  652. x
  653. -----------
  654. ($100.00)
  655. ($200.00)
  656. ($300.00)
  657. (3 rows)
  658. -- With a defined row type, the typcache can inspect the type's fields
  659. -- for hashability.
  660. create type ct1 as (f1 money);
  661. explain (costs off)
  662. select x from (values (row(100::money)::ct1), (row(200::money)::ct1)) _(x) union select x from (values (row(100::money)::ct1), (row(300::money)::ct1)) _(x);
  663. QUERY PLAN
  664. -----------------------------------------------
  665. Unique
  666. -> Sort
  667. Sort Key: "*VALUES*".column1
  668. -> Append
  669. -> Values Scan on "*VALUES*"
  670. -> Values Scan on "*VALUES*_1"
  671. (6 rows)
  672. select x from (values (row(100::money)::ct1), (row(200::money)::ct1)) _(x) union select x from (values (row(100::money)::ct1), (row(300::money)::ct1)) _(x);
  673. x
  674. -----------
  675. ($100.00)
  676. ($200.00)
  677. ($300.00)
  678. (3 rows)
  679. drop type ct1;
  680. set enable_hashagg to off;
  681. explain (costs off)
  682. select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
  683. QUERY PLAN
  684. -----------------------------------------------
  685. Unique
  686. -> Sort
  687. Sort Key: "*VALUES*".column1
  688. -> Append
  689. -> Values Scan on "*VALUES*"
  690. -> Values Scan on "*VALUES*_1"
  691. (6 rows)
  692. select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values (row(1, 2)), (row(1, 4))) _(x);
  693. x
  694. -------
  695. (1,2)
  696. (1,3)
  697. (1,4)
  698. (3 rows)
  699. explain (costs off)
  700. select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
  701. QUERY PLAN
  702. -----------------------------------------------------
  703. SetOp Intersect
  704. -> Sort
  705. Sort Key: "*SELECT* 1".x
  706. -> Append
  707. -> Subquery Scan on "*SELECT* 1"
  708. -> Values Scan on "*VALUES*"
  709. -> Subquery Scan on "*SELECT* 2"
  710. -> Values Scan on "*VALUES*_1"
  711. (8 rows)
  712. select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
  713. x
  714. -------
  715. (1,2)
  716. (1 row)
  717. explain (costs off)
  718. select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
  719. QUERY PLAN
  720. -----------------------------------------------------
  721. SetOp Except
  722. -> Sort
  723. Sort Key: "*SELECT* 1".x
  724. -> Append
  725. -> Subquery Scan on "*SELECT* 1"
  726. -> Values Scan on "*VALUES*"
  727. -> Subquery Scan on "*SELECT* 2"
  728. -> Values Scan on "*VALUES*_1"
  729. (8 rows)
  730. select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
  731. x
  732. -------
  733. (1,3)
  734. (1 row)
  735. reset enable_hashagg;
  736. --
  737. -- Mixed types
  738. --
  739. SELECT f1 FROM float8_tbl INTERSECT SELECT f1 FROM int4_tbl ORDER BY 1;
  740. f1
  741. ----
  742. 0
  743. (1 row)
  744. SELECT f1 FROM float8_tbl EXCEPT SELECT f1 FROM int4_tbl ORDER BY 1;
  745. f1
  746. -----------------------
  747. -1.2345678901234e+200
  748. -1004.3
  749. -34.84
  750. -1.2345678901234e-200
  751. (4 rows)
  752. --
  753. -- Operator precedence and (((((extra))))) parentheses
  754. --
  755. SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl ORDER BY 1;
  756. q1
  757. -------------------
  758. -4567890123456789
  759. 123
  760. 123
  761. 456
  762. 4567890123456789
  763. 4567890123456789
  764. 4567890123456789
  765. (7 rows)
  766. SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1;
  767. q1
  768. ------------------
  769. 123
  770. 4567890123456789
  771. (2 rows)
  772. (((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl;
  773. q1
  774. -------------------
  775. 123
  776. 4567890123456789
  777. 456
  778. 4567890123456789
  779. 123
  780. 4567890123456789
  781. -4567890123456789
  782. (7 rows)
  783. SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
  784. q1
  785. -------------------
  786. -4567890123456789
  787. 456
  788. (2 rows)
  789. SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)));
  790. q1
  791. -------------------
  792. 123
  793. 123
  794. 4567890123456789
  795. 4567890123456789
  796. 4567890123456789
  797. -4567890123456789
  798. 456
  799. (7 rows)
  800. (((SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
  801. q1
  802. -------------------
  803. -4567890123456789
  804. 456
  805. (2 rows)
  806. --
  807. -- Subqueries with ORDER BY & LIMIT clauses
  808. --
  809. -- In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
  810. SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
  811. ORDER BY q2,q1;
  812. q1 | q2
  813. ------------------+-------------------
  814. 4567890123456789 | -4567890123456789
  815. 123 | 456
  816. (2 rows)
  817. -- This should fail, because q2 isn't a name of an EXCEPT output column
  818. SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
  819. ERROR: column "q2" does not exist
  820. LINE 1: ... int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1...
  821. ^
  822. HINT: There is a column named "q2" in table "*SELECT* 2", but it cannot be referenced from this part of the query.
  823. -- But this should work:
  824. SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
  825. q1
  826. ------------------
  827. 123
  828. 4567890123456789
  829. (2 rows)
  830. --
  831. -- New syntaxes (7.1) permit new tests
  832. --
  833. (((((select * from int8_tbl)))));
  834. q1 | q2
  835. ------------------+-------------------
  836. 123 | 456
  837. 123 | 4567890123456789
  838. 4567890123456789 | 123
  839. 4567890123456789 | 4567890123456789
  840. 4567890123456789 | -4567890123456789
  841. (5 rows)
  842. --
  843. -- Check behavior with empty select list (allowed since 9.4)
  844. --
  845. select union select;
  846. --
  847. (1 row)
  848. select intersect select;
  849. --
  850. (1 row)
  851. select except select;
  852. --
  853. (0 rows)
  854. -- check hashed implementation
  855. set enable_hashagg = true;
  856. set enable_sort = false;
  857. explain (costs off)
  858. select from generate_series(1,5) union select from generate_series(1,3);
  859. QUERY PLAN
  860. ----------------------------------------------------------------
  861. HashAggregate
  862. -> Append
  863. -> Function Scan on generate_series
  864. -> Function Scan on generate_series generate_series_1
  865. (4 rows)
  866. explain (costs off)
  867. select from generate_series(1,5) intersect select from generate_series(1,3);
  868. QUERY PLAN
  869. ----------------------------------------------------------------------
  870. HashSetOp Intersect
  871. -> Append
  872. -> Subquery Scan on "*SELECT* 1"
  873. -> Function Scan on generate_series
  874. -> Subquery Scan on "*SELECT* 2"
  875. -> Function Scan on generate_series generate_series_1
  876. (6 rows)
  877. select from generate_series(1,5) union select from generate_series(1,3);
  878. --
  879. (1 row)
  880. select from generate_series(1,5) union all select from generate_series(1,3);
  881. --
  882. (8 rows)
  883. select from generate_series(1,5) intersect select from generate_series(1,3);
  884. --
  885. (1 row)
  886. select from generate_series(1,5) intersect all select from generate_series(1,3);
  887. --
  888. (3 rows)
  889. select from generate_series(1,5) except select from generate_series(1,3);
  890. --
  891. (0 rows)
  892. select from generate_series(1,5) except all select from generate_series(1,3);
  893. --
  894. (2 rows)
  895. -- check sorted implementation
  896. set enable_hashagg = false;
  897. set enable_sort = true;
  898. explain (costs off)
  899. select from generate_series(1,5) union select from generate_series(1,3);
  900. QUERY PLAN
  901. ----------------------------------------------------------------
  902. Unique
  903. -> Append
  904. -> Function Scan on generate_series
  905. -> Function Scan on generate_series generate_series_1
  906. (4 rows)
  907. explain (costs off)
  908. select from generate_series(1,5) intersect select from generate_series(1,3);
  909. QUERY PLAN
  910. ----------------------------------------------------------------------
  911. SetOp Intersect
  912. -> Append
  913. -> Subquery Scan on "*SELECT* 1"
  914. -> Function Scan on generate_series
  915. -> Subquery Scan on "*SELECT* 2"
  916. -> Function Scan on generate_series generate_series_1
  917. (6 rows)
  918. select from generate_series(1,5) union select from generate_series(1,3);
  919. --
  920. (1 row)
  921. select from generate_series(1,5) union all select from generate_series(1,3);
  922. --
  923. (8 rows)
  924. select from generate_series(1,5) intersect select from generate_series(1,3);
  925. --
  926. (1 row)
  927. select from generate_series(1,5) intersect all select from generate_series(1,3);
  928. --
  929. (3 rows)
  930. select from generate_series(1,5) except select from generate_series(1,3);
  931. --
  932. (0 rows)
  933. select from generate_series(1,5) except all select from generate_series(1,3);
  934. --
  935. (2 rows)
  936. reset enable_hashagg;
  937. reset enable_sort;
  938. --
  939. -- Check handling of a case with unknown constants. We don't guarantee
  940. -- an undecorated constant will work in all cases, but historically this
  941. -- usage has worked, so test we don't break it.
  942. --
  943. SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
  944. UNION
  945. SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
  946. ORDER BY 1;
  947. f1
  948. ------
  949. a
  950. ab
  951. abcd
  952. test
  953. (4 rows)
  954. -- This should fail, but it should produce an error cursor
  955. SELECT '3.4'::numeric UNION SELECT 'foo';
  956. ERROR: invalid input syntax for type numeric: "foo"
  957. LINE 1: SELECT '3.4'::numeric UNION SELECT 'foo';
  958. ^
  959. --
  960. -- Test that expression-index constraints can be pushed down through
  961. -- UNION or UNION ALL
  962. --
  963. CREATE TEMP TABLE t1 (a text, b text);
  964. CREATE INDEX t1_ab_idx on t1 ((a || b));
  965. CREATE TEMP TABLE t2 (ab text primary key);
  966. INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
  967. INSERT INTO t2 VALUES ('ab'), ('xy');
  968. set enable_seqscan = off;
  969. set enable_indexscan = on;
  970. set enable_bitmapscan = off;
  971. explain (costs off)
  972. SELECT * FROM
  973. (SELECT a || b AS ab FROM t1
  974. UNION ALL
  975. SELECT * FROM t2) t
  976. WHERE ab = 'ab';
  977. QUERY PLAN
  978. ---------------------------------------------
  979. Append
  980. -> Index Scan using t1_ab_idx on t1
  981. Index Cond: ((a || b) = 'ab'::text)
  982. -> Index Only Scan using t2_pkey on t2
  983. Index Cond: (ab = 'ab'::text)
  984. (5 rows)
  985. explain (costs off)
  986. SELECT * FROM
  987. (SELECT a || b AS ab FROM t1
  988. UNION
  989. SELECT * FROM t2) t
  990. WHERE ab = 'ab';
  991. QUERY PLAN
  992. ---------------------------------------------------
  993. HashAggregate
  994. Group Key: ((t1.a || t1.b))
  995. -> Append
  996. -> Index Scan using t1_ab_idx on t1
  997. Index Cond: ((a || b) = 'ab'::text)
  998. -> Index Only Scan using t2_pkey on t2
  999. Index Cond: (ab = 'ab'::text)
  1000. (7 rows)
  1001. --
  1002. -- Test that ORDER BY for UNION ALL can be pushed down to inheritance
  1003. -- children.
  1004. --
  1005. CREATE TEMP TABLE t1c (b text, a text);
  1006. ALTER TABLE t1c INHERIT t1;
  1007. CREATE TEMP TABLE t2c (primary key (ab)) INHERITS (t2);
  1008. INSERT INTO t1c VALUES ('v', 'w'), ('c', 'd'), ('m', 'n'), ('e', 'f');
  1009. INSERT INTO t2c VALUES ('vw'), ('cd'), ('mn'), ('ef');
  1010. CREATE INDEX t1c_ab_idx on t1c ((a || b));
  1011. set enable_seqscan = on;
  1012. set enable_indexonlyscan = off;
  1013. explain (costs off)
  1014. SELECT * FROM
  1015. (SELECT a || b AS ab FROM t1
  1016. UNION ALL
  1017. SELECT ab FROM t2) t
  1018. ORDER BY 1 LIMIT 8;
  1019. QUERY PLAN
  1020. -----------------------------------------------------
  1021. Limit
  1022. -> Merge Append
  1023. Sort Key: ((t1.a || t1.b))
  1024. -> Index Scan using t1_ab_idx on t1
  1025. -> Index Scan using t1c_ab_idx on t1c t1_1
  1026. -> Index Scan using t2_pkey on t2
  1027. -> Index Scan using t2c_pkey on t2c t2_1
  1028. (7 rows)
  1029. SELECT * FROM
  1030. (SELECT a || b AS ab FROM t1
  1031. UNION ALL
  1032. SELECT ab FROM t2) t
  1033. ORDER BY 1 LIMIT 8;
  1034. ab
  1035. ----
  1036. ab
  1037. ab
  1038. cd
  1039. dc
  1040. ef
  1041. fe
  1042. mn
  1043. nm
  1044. (8 rows)
  1045. reset enable_seqscan;
  1046. reset enable_indexscan;
  1047. reset enable_bitmapscan;
  1048. -- This simpler variant of the above test has been observed to fail differently
  1049. create table events (event_id int primary key);
  1050. create table other_events (event_id int primary key);
  1051. create table events_child () inherits (events);
  1052. explain (costs off)
  1053. select event_id
  1054. from (select event_id from events
  1055. union all
  1056. select event_id from other_events) ss
  1057. order by event_id;
  1058. QUERY PLAN
  1059. ----------------------------------------------------------
  1060. Merge Append
  1061. Sort Key: events.event_id
  1062. -> Index Scan using events_pkey on events
  1063. -> Sort
  1064. Sort Key: events_1.event_id
  1065. -> Seq Scan on events_child events_1
  1066. -> Index Scan using other_events_pkey on other_events
  1067. (7 rows)
  1068. drop table events_child, events, other_events;
  1069. reset enable_indexonlyscan;
  1070. -- Test constraint exclusion of UNION ALL subqueries
  1071. explain (costs off)
  1072. SELECT * FROM
  1073. (SELECT 1 AS t, * FROM tenk1 a
  1074. UNION ALL
  1075. SELECT 2 AS t, * FROM tenk1 b) c
  1076. WHERE t = 2;
  1077. QUERY PLAN
  1078. ---------------------
  1079. Seq Scan on tenk1 b
  1080. (1 row)
  1081. -- Test that we push quals into UNION sub-selects only when it's safe
  1082. explain (costs off)
  1083. SELECT * FROM
  1084. (SELECT 1 AS t, 2 AS x
  1085. UNION
  1086. SELECT 2 AS t, 4 AS x) ss
  1087. WHERE x < 4
  1088. ORDER BY x;
  1089. QUERY PLAN
  1090. --------------------------------------------------
  1091. Sort
  1092. Sort Key: (2)
  1093. -> Unique
  1094. -> Sort
  1095. Sort Key: (1), (2)
  1096. -> Append
  1097. -> Result
  1098. -> Result
  1099. One-Time Filter: false
  1100. (9 rows)
  1101. SELECT * FROM
  1102. (SELECT 1 AS t, 2 AS x
  1103. UNION
  1104. SELECT 2 AS t, 4 AS x) ss
  1105. WHERE x < 4
  1106. ORDER BY x;
  1107. t | x
  1108. ---+---
  1109. 1 | 2
  1110. (1 row)
  1111. explain (costs off)
  1112. SELECT * FROM
  1113. (SELECT 1 AS t, generate_series(1,10) AS x
  1114. UNION
  1115. SELECT 2 AS t, 4 AS x) ss
  1116. WHERE x < 4
  1117. ORDER BY x;
  1118. QUERY PLAN
  1119. --------------------------------------------------------
  1120. Sort
  1121. Sort Key: ss.x
  1122. -> Subquery Scan on ss
  1123. Filter: (ss.x < 4)
  1124. -> HashAggregate
  1125. Group Key: (1), (generate_series(1, 10))
  1126. -> Append
  1127. -> ProjectSet
  1128. -> Result
  1129. -> Result
  1130. (10 rows)
  1131. SELECT * FROM
  1132. (SELECT 1 AS t, generate_series(1,10) AS x
  1133. UNION
  1134. SELECT 2 AS t, 4 AS x) ss
  1135. WHERE x < 4
  1136. ORDER BY x;
  1137. t | x
  1138. ---+---
  1139. 1 | 1
  1140. 1 | 2
  1141. 1 | 3
  1142. (3 rows)
  1143. explain (costs off)
  1144. SELECT * FROM
  1145. (SELECT 1 AS t, (random()*3)::int AS x
  1146. UNION
  1147. SELECT 2 AS t, 4 AS x) ss
  1148. WHERE x > 3
  1149. ORDER BY x;
  1150. QUERY PLAN
  1151. ------------------------------------------------------------------------------------
  1152. Sort
  1153. Sort Key: ss.x
  1154. -> Subquery Scan on ss
  1155. Filter: (ss.x > 3)
  1156. -> Unique
  1157. -> Sort
  1158. Sort Key: (1), (((random() * '3'::double precision))::integer)
  1159. -> Append
  1160. -> Result
  1161. -> Result
  1162. (10 rows)
  1163. SELECT * FROM
  1164. (SELECT 1 AS t, (random()*3)::int AS x
  1165. UNION
  1166. SELECT 2 AS t, 4 AS x) ss
  1167. WHERE x > 3
  1168. ORDER BY x;
  1169. t | x
  1170. ---+---
  1171. 2 | 4
  1172. (1 row)
  1173. -- Test cases where the native ordering of a sub-select has more pathkeys
  1174. -- than the outer query cares about
  1175. explain (costs off)
  1176. select distinct q1 from
  1177. (select distinct * from int8_tbl i81
  1178. union all
  1179. select distinct * from int8_tbl i82) ss
  1180. where q2 = q2;
  1181. QUERY PLAN
  1182. ----------------------------------------------------------
  1183. Unique
  1184. -> Merge Append
  1185. Sort Key: "*SELECT* 1".q1
  1186. -> Subquery Scan on "*SELECT* 1"
  1187. -> Unique
  1188. -> Sort
  1189. Sort Key: i81.q1, i81.q2
  1190. -> Seq Scan on int8_tbl i81
  1191. Filter: (q2 IS NOT NULL)
  1192. -> Subquery Scan on "*SELECT* 2"
  1193. -> Unique
  1194. -> Sort
  1195. Sort Key: i82.q1, i82.q2
  1196. -> Seq Scan on int8_tbl i82
  1197. Filter: (q2 IS NOT NULL)
  1198. (15 rows)
  1199. select distinct q1 from
  1200. (select distinct * from int8_tbl i81
  1201. union all
  1202. select distinct * from int8_tbl i82) ss
  1203. where q2 = q2;
  1204. q1
  1205. ------------------
  1206. 123
  1207. 4567890123456789
  1208. (2 rows)
  1209. explain (costs off)
  1210. select distinct q1 from
  1211. (select distinct * from int8_tbl i81
  1212. union all
  1213. select distinct * from int8_tbl i82) ss
  1214. where -q1 = q2;
  1215. QUERY PLAN
  1216. --------------------------------------------------------
  1217. Unique
  1218. -> Merge Append
  1219. Sort Key: "*SELECT* 1".q1
  1220. -> Subquery Scan on "*SELECT* 1"
  1221. -> Unique
  1222. -> Sort
  1223. Sort Key: i81.q1, i81.q2
  1224. -> Seq Scan on int8_tbl i81
  1225. Filter: ((- q1) = q2)
  1226. -> Subquery Scan on "*SELECT* 2"
  1227. -> Unique
  1228. -> Sort
  1229. Sort Key: i82.q1, i82.q2
  1230. -> Seq Scan on int8_tbl i82
  1231. Filter: ((- q1) = q2)
  1232. (15 rows)
  1233. select distinct q1 from
  1234. (select distinct * from int8_tbl i81
  1235. union all
  1236. select distinct * from int8_tbl i82) ss
  1237. where -q1 = q2;
  1238. q1
  1239. ------------------
  1240. 4567890123456789
  1241. (1 row)
  1242. -- Test proper handling of parameterized appendrel paths when the
  1243. -- potential join qual is expensive
  1244. create function expensivefunc(int) returns int
  1245. language plpgsql immutable strict cost 10000
  1246. as $$begin return $1; end$$;
  1247. create temp table t3 as select generate_series(-1000,1000) as x;
  1248. create index t3i on t3 (expensivefunc(x));
  1249. analyze t3;
  1250. explain (costs off)
  1251. select * from
  1252. (select * from t3 a union all select * from t3 b) ss
  1253. join int4_tbl on f1 = expensivefunc(x);
  1254. QUERY PLAN
  1255. ------------------------------------------------------------
  1256. Nested Loop
  1257. -> Seq Scan on int4_tbl
  1258. -> Append
  1259. -> Index Scan using t3i on t3 a
  1260. Index Cond: (expensivefunc(x) = int4_tbl.f1)
  1261. -> Index Scan using t3i on t3 b
  1262. Index Cond: (expensivefunc(x) = int4_tbl.f1)
  1263. (7 rows)
  1264. select * from
  1265. (select * from t3 a union all select * from t3 b) ss
  1266. join int4_tbl on f1 = expensivefunc(x);
  1267. x | f1
  1268. ---+----
  1269. 0 | 0
  1270. 0 | 0
  1271. (2 rows)
  1272. drop table t3;
  1273. drop function expensivefunc(int);
  1274. -- Test handling of appendrel quals that const-simplify into an AND
  1275. explain (costs off)
  1276. select * from
  1277. (select *, 0 as x from int8_tbl a
  1278. union all
  1279. select *, 1 as x from int8_tbl b) ss
  1280. where (x = 0) or (q1 >= q2 and q1 <= q2);
  1281. QUERY PLAN
  1282. ---------------------------------------------
  1283. Append
  1284. -> Seq Scan on int8_tbl a
  1285. -> Seq Scan on int8_tbl b
  1286. Filter: ((q1 >= q2) AND (q1 <= q2))
  1287. (4 rows)
  1288. select * from
  1289. (select *, 0 as x from int8_tbl a
  1290. union all
  1291. select *, 1 as x from int8_tbl b) ss
  1292. where (x = 0) or (q1 >= q2 and q1 <= q2);
  1293. q1 | q2 | x
  1294. ------------------+-------------------+---
  1295. 123 | 456 | 0
  1296. 123 | 4567890123456789 | 0
  1297. 4567890123456789 | 123 | 0
  1298. 4567890123456789 | 4567890123456789 | 0
  1299. 4567890123456789 | -4567890123456789 | 0
  1300. 4567890123456789 | 4567890123456789 | 1
  1301. (6 rows)