subselect.out 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. --
  2. -- SUBSELECT
  3. --
  4. SELECT 1 AS one WHERE 1 IN (SELECT 1);
  5. one
  6. -----
  7. 1
  8. (1 row)
  9. SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
  10. zero
  11. ------
  12. (0 rows)
  13. SELECT 1 AS zero WHERE 1 IN (SELECT 2);
  14. zero
  15. ------
  16. (0 rows)
  17. -- Check grammar's handling of extra parens in assorted contexts
  18. SELECT * FROM (SELECT 1 AS x) ss;
  19. x
  20. ---
  21. 1
  22. (1 row)
  23. SELECT * FROM ((SELECT 1 AS x)) ss;
  24. x
  25. ---
  26. 1
  27. (1 row)
  28. (SELECT 2) UNION SELECT 2;
  29. ?column?
  30. ----------
  31. 2
  32. (1 row)
  33. ((SELECT 2)) UNION SELECT 2;
  34. ?column?
  35. ----------
  36. 2
  37. (1 row)
  38. SELECT ((SELECT 2) UNION SELECT 2);
  39. ?column?
  40. ----------
  41. 2
  42. (1 row)
  43. SELECT (((SELECT 2)) UNION SELECT 2);
  44. ?column?
  45. ----------
  46. 2
  47. (1 row)
  48. SELECT (SELECT ARRAY[1,2,3])[1];
  49. array
  50. -------
  51. 1
  52. (1 row)
  53. SELECT ((SELECT ARRAY[1,2,3]))[2];
  54. array
  55. -------
  56. 2
  57. (1 row)
  58. SELECT (((SELECT ARRAY[1,2,3])))[3];
  59. array
  60. -------
  61. 3
  62. (1 row)
  63. -- Set up some simple test tables
  64. CREATE TABLE SUBSELECT_TBL (
  65. f1 integer,
  66. f2 integer,
  67. f3 float
  68. );
  69. INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
  70. INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
  71. INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
  72. INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
  73. INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
  74. INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
  75. INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
  76. INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
  77. SELECT * FROM SUBSELECT_TBL;
  78. f1 | f2 | f3
  79. ----+----+----
  80. 1 | 2 | 3
  81. 2 | 3 | 4
  82. 3 | 4 | 5
  83. 1 | 1 | 1
  84. 2 | 2 | 2
  85. 3 | 3 | 3
  86. 6 | 7 | 8
  87. 8 | 9 |
  88. (8 rows)
  89. -- Uncorrelated subselects
  90. SELECT f1 AS "Constant Select" FROM SUBSELECT_TBL
  91. WHERE f1 IN (SELECT 1);
  92. Constant Select
  93. -----------------
  94. 1
  95. 1
  96. (2 rows)
  97. SELECT f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
  98. WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL);
  99. Uncorrelated Field
  100. --------------------
  101. 1
  102. 2
  103. 3
  104. 1
  105. 2
  106. 3
  107. (6 rows)
  108. SELECT f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
  109. WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE
  110. f2 IN (SELECT f1 FROM SUBSELECT_TBL));
  111. Uncorrelated Field
  112. --------------------
  113. 1
  114. 2
  115. 3
  116. 1
  117. 2
  118. 3
  119. (6 rows)
  120. SELECT f1, f2
  121. FROM SUBSELECT_TBL
  122. WHERE (f1, f2) NOT IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
  123. WHERE f3 IS NOT NULL);
  124. f1 | f2
  125. ----+----
  126. 1 | 2
  127. 6 | 7
  128. 8 | 9
  129. (3 rows)
  130. -- Correlated subselects
  131. SELECT f1 AS "Correlated Field", f2 AS "Second Field"
  132. FROM SUBSELECT_TBL upper
  133. WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE f1 = upper.f1);
  134. Correlated Field | Second Field
  135. ------------------+--------------
  136. 1 | 2
  137. 2 | 3
  138. 3 | 4
  139. 1 | 1
  140. 2 | 2
  141. 3 | 3
  142. (6 rows)
  143. SELECT f1 AS "Correlated Field", f3 AS "Second Field"
  144. FROM SUBSELECT_TBL upper
  145. WHERE f1 IN
  146. (SELECT f2 FROM SUBSELECT_TBL WHERE CAST(upper.f2 AS float) = f3);
  147. Correlated Field | Second Field
  148. ------------------+--------------
  149. 2 | 4
  150. 3 | 5
  151. 1 | 1
  152. 2 | 2
  153. 3 | 3
  154. (5 rows)
  155. SELECT f1 AS "Correlated Field", f3 AS "Second Field"
  156. FROM SUBSELECT_TBL upper
  157. WHERE f3 IN (SELECT upper.f1 + f2 FROM SUBSELECT_TBL
  158. WHERE f2 = CAST(f3 AS integer));
  159. Correlated Field | Second Field
  160. ------------------+--------------
  161. 1 | 3
  162. 2 | 4
  163. 3 | 5
  164. 6 | 8
  165. (4 rows)
  166. SELECT f1 AS "Correlated Field"
  167. FROM SUBSELECT_TBL
  168. WHERE (f1, f2) IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
  169. WHERE f3 IS NOT NULL);
  170. Correlated Field
  171. ------------------
  172. 2
  173. 3
  174. 1
  175. 2
  176. 3
  177. (5 rows)
  178. --
  179. -- Use some existing tables in the regression test
  180. --
  181. SELECT ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
  182. FROM SUBSELECT_TBL ss
  183. WHERE f1 NOT IN (SELECT f1+1 FROM INT4_TBL
  184. WHERE f1 != ss.f1 AND f1 < 2147483647);
  185. Correlated Field | Second Field
  186. ------------------+--------------
  187. 2 | 4
  188. 3 | 5
  189. 2 | 2
  190. 3 | 3
  191. 6 | 8
  192. 8 |
  193. (6 rows)
  194. select q1, float8(count(*)) / (select count(*) from int8_tbl)
  195. from int8_tbl group by q1 order by q1;
  196. q1 | ?column?
  197. ------------------+----------
  198. 123 | 0.4
  199. 4567890123456789 | 0.6
  200. (2 rows)
  201. -- Unspecified-type literals in output columns should resolve as text
  202. SELECT *, pg_typeof(f1) FROM
  203. (SELECT 'foo' AS f1 FROM generate_series(1,3)) ss ORDER BY 1;
  204. f1 | pg_typeof
  205. -----+-----------
  206. foo | text
  207. foo | text
  208. foo | text
  209. (3 rows)
  210. -- ... unless there's context to suggest differently
  211. explain (verbose, costs off) select '42' union all select '43';
  212. QUERY PLAN
  213. ----------------------------
  214. Append
  215. -> Result
  216. Output: '42'::text
  217. -> Result
  218. Output: '43'::text
  219. (5 rows)
  220. explain (verbose, costs off) select '42' union all select 43;
  221. QUERY PLAN
  222. --------------------
  223. Append
  224. -> Result
  225. Output: 42
  226. -> Result
  227. Output: 43
  228. (5 rows)
  229. -- check materialization of an initplan reference (bug #14524)
  230. explain (verbose, costs off)
  231. select 1 = all (select (select 1));
  232. QUERY PLAN
  233. -----------------------------------
  234. Result
  235. Output: (SubPlan 2)
  236. SubPlan 2
  237. -> Materialize
  238. Output: ($0)
  239. InitPlan 1 (returns $0)
  240. -> Result
  241. Output: 1
  242. -> Result
  243. Output: $0
  244. (10 rows)
  245. select 1 = all (select (select 1));
  246. ?column?
  247. ----------
  248. t
  249. (1 row)
  250. --
  251. -- Check EXISTS simplification with LIMIT
  252. --
  253. explain (costs off)
  254. select * from int4_tbl o where exists
  255. (select 1 from int4_tbl i where i.f1=o.f1 limit null);
  256. QUERY PLAN
  257. ------------------------------------
  258. Hash Semi Join
  259. Hash Cond: (o.f1 = i.f1)
  260. -> Seq Scan on int4_tbl o
  261. -> Hash
  262. -> Seq Scan on int4_tbl i
  263. (5 rows)
  264. explain (costs off)
  265. select * from int4_tbl o where not exists
  266. (select 1 from int4_tbl i where i.f1=o.f1 limit 1);
  267. QUERY PLAN
  268. ------------------------------------
  269. Hash Anti Join
  270. Hash Cond: (o.f1 = i.f1)
  271. -> Seq Scan on int4_tbl o
  272. -> Hash
  273. -> Seq Scan on int4_tbl i
  274. (5 rows)
  275. explain (costs off)
  276. select * from int4_tbl o where exists
  277. (select 1 from int4_tbl i where i.f1=o.f1 limit 0);
  278. QUERY PLAN
  279. --------------------------------------
  280. Seq Scan on int4_tbl o
  281. Filter: (SubPlan 1)
  282. SubPlan 1
  283. -> Limit
  284. -> Seq Scan on int4_tbl i
  285. Filter: (f1 = o.f1)
  286. (6 rows)
  287. --
  288. -- Test cases to catch unpleasant interactions between IN-join processing
  289. -- and subquery pullup.
  290. --
  291. select count(*) from
  292. (select 1 from tenk1 a
  293. where unique1 IN (select hundred from tenk1 b)) ss;
  294. count
  295. -------
  296. 100
  297. (1 row)
  298. select count(distinct ss.ten) from
  299. (select ten from tenk1 a
  300. where unique1 IN (select hundred from tenk1 b)) ss;
  301. count
  302. -------
  303. 10
  304. (1 row)
  305. select count(*) from
  306. (select 1 from tenk1 a
  307. where unique1 IN (select distinct hundred from tenk1 b)) ss;
  308. count
  309. -------
  310. 100
  311. (1 row)
  312. select count(distinct ss.ten) from
  313. (select ten from tenk1 a
  314. where unique1 IN (select distinct hundred from tenk1 b)) ss;
  315. count
  316. -------
  317. 10
  318. (1 row)
  319. --
  320. -- Test cases to check for overenthusiastic optimization of
  321. -- "IN (SELECT DISTINCT ...)" and related cases. Per example from
  322. -- Luca Pireddu and Michael Fuhr.
  323. --
  324. CREATE TEMP TABLE foo (id integer);
  325. CREATE TEMP TABLE bar (id1 integer, id2 integer);
  326. INSERT INTO foo VALUES (1);
  327. INSERT INTO bar VALUES (1, 1);
  328. INSERT INTO bar VALUES (2, 2);
  329. INSERT INTO bar VALUES (3, 1);
  330. -- These cases require an extra level of distinct-ing above subquery s
  331. SELECT * FROM foo WHERE id IN
  332. (SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
  333. id
  334. ----
  335. 1
  336. (1 row)
  337. SELECT * FROM foo WHERE id IN
  338. (SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
  339. id
  340. ----
  341. 1
  342. (1 row)
  343. SELECT * FROM foo WHERE id IN
  344. (SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
  345. SELECT id1, id2 FROM bar) AS s);
  346. id
  347. ----
  348. 1
  349. (1 row)
  350. -- These cases do not
  351. SELECT * FROM foo WHERE id IN
  352. (SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
  353. id
  354. ----
  355. 1
  356. (1 row)
  357. SELECT * FROM foo WHERE id IN
  358. (SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
  359. id
  360. ----
  361. 1
  362. (1 row)
  363. SELECT * FROM foo WHERE id IN
  364. (SELECT id2 FROM (SELECT id2 FROM bar UNION
  365. SELECT id2 FROM bar) AS s);
  366. id
  367. ----
  368. 1
  369. (1 row)
  370. --
  371. -- Test case to catch problems with multiply nested sub-SELECTs not getting
  372. -- recalculated properly. Per bug report from Didier Moens.
  373. --
  374. CREATE TABLE orderstest (
  375. approver_ref integer,
  376. po_ref integer,
  377. ordercanceled boolean
  378. );
  379. INSERT INTO orderstest VALUES (1, 1, false);
  380. INSERT INTO orderstest VALUES (66, 5, false);
  381. INSERT INTO orderstest VALUES (66, 6, false);
  382. INSERT INTO orderstest VALUES (66, 7, false);
  383. INSERT INTO orderstest VALUES (66, 1, true);
  384. INSERT INTO orderstest VALUES (66, 8, false);
  385. INSERT INTO orderstest VALUES (66, 1, false);
  386. INSERT INTO orderstest VALUES (77, 1, false);
  387. INSERT INTO orderstest VALUES (1, 1, false);
  388. INSERT INTO orderstest VALUES (66, 1, false);
  389. INSERT INTO orderstest VALUES (1, 1, false);
  390. CREATE VIEW orders_view AS
  391. SELECT *,
  392. (SELECT CASE
  393. WHEN ord.approver_ref=1 THEN '---' ELSE 'Approved'
  394. END) AS "Approved",
  395. (SELECT CASE
  396. WHEN ord.ordercanceled
  397. THEN 'Canceled'
  398. ELSE
  399. (SELECT CASE
  400. WHEN ord.po_ref=1
  401. THEN
  402. (SELECT CASE
  403. WHEN ord.approver_ref=1
  404. THEN '---'
  405. ELSE 'Approved'
  406. END)
  407. ELSE 'PO'
  408. END)
  409. END) AS "Status",
  410. (CASE
  411. WHEN ord.ordercanceled
  412. THEN 'Canceled'
  413. ELSE
  414. (CASE
  415. WHEN ord.po_ref=1
  416. THEN
  417. (CASE
  418. WHEN ord.approver_ref=1
  419. THEN '---'
  420. ELSE 'Approved'
  421. END)
  422. ELSE 'PO'
  423. END)
  424. END) AS "Status_OK"
  425. FROM orderstest ord;
  426. SELECT * FROM orders_view;
  427. approver_ref | po_ref | ordercanceled | Approved | Status | Status_OK
  428. --------------+--------+---------------+----------+----------+-----------
  429. 1 | 1 | f | --- | --- | ---
  430. 66 | 5 | f | Approved | PO | PO
  431. 66 | 6 | f | Approved | PO | PO
  432. 66 | 7 | f | Approved | PO | PO
  433. 66 | 1 | t | Approved | Canceled | Canceled
  434. 66 | 8 | f | Approved | PO | PO
  435. 66 | 1 | f | Approved | Approved | Approved
  436. 77 | 1 | f | Approved | Approved | Approved
  437. 1 | 1 | f | --- | --- | ---
  438. 66 | 1 | f | Approved | Approved | Approved
  439. 1 | 1 | f | --- | --- | ---
  440. (11 rows)
  441. DROP TABLE orderstest cascade;
  442. NOTICE: drop cascades to view orders_view
  443. --
  444. -- Test cases to catch situations where rule rewriter fails to propagate
  445. -- hasSubLinks flag correctly. Per example from Kyle Bateman.
  446. --
  447. create temp table parts (
  448. partnum text,
  449. cost float8
  450. );
  451. create temp table shipped (
  452. ttype char(2),
  453. ordnum int4,
  454. partnum text,
  455. value float8
  456. );
  457. create temp view shipped_view as
  458. select * from shipped where ttype = 'wt';
  459. create rule shipped_view_insert as on insert to shipped_view do instead
  460. insert into shipped values('wt', new.ordnum, new.partnum, new.value);
  461. insert into parts (partnum, cost) values (1, 1234.56);
  462. insert into shipped_view (ordnum, partnum, value)
  463. values (0, 1, (select cost from parts where partnum = '1'));
  464. select * from shipped_view;
  465. ttype | ordnum | partnum | value
  466. -------+--------+---------+---------
  467. wt | 0 | 1 | 1234.56
  468. (1 row)
  469. create rule shipped_view_update as on update to shipped_view do instead
  470. update shipped set partnum = new.partnum, value = new.value
  471. where ttype = new.ttype and ordnum = new.ordnum;
  472. update shipped_view set value = 11
  473. from int4_tbl a join int4_tbl b
  474. on (a.f1 = (select f1 from int4_tbl c where c.f1=b.f1))
  475. where ordnum = a.f1;
  476. select * from shipped_view;
  477. ttype | ordnum | partnum | value
  478. -------+--------+---------+-------
  479. wt | 0 | 1 | 11
  480. (1 row)
  481. select f1, ss1 as relabel from
  482. (select *, (select sum(f1) from int4_tbl b where f1 >= a.f1) as ss1
  483. from int4_tbl a) ss;
  484. f1 | relabel
  485. -------------+------------
  486. 0 | 2147607103
  487. 123456 | 2147607103
  488. -123456 | 2147483647
  489. 2147483647 | 2147483647
  490. -2147483647 | 0
  491. (5 rows)
  492. --
  493. -- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
  494. -- Per bug report from David Sanchez i Gregori.
  495. --
  496. select * from (
  497. select max(unique1) from tenk1 as a
  498. where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
  499. ) ss;
  500. max
  501. ------
  502. 9997
  503. (1 row)
  504. select * from (
  505. select min(unique1) from tenk1 as a
  506. where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
  507. ) ss;
  508. min
  509. -----
  510. 0
  511. (1 row)
  512. --
  513. -- Test that an IN implemented using a UniquePath does unique-ification
  514. -- with the right semantics, as per bug #4113. (Unfortunately we have
  515. -- no simple way to ensure that this test case actually chooses that type
  516. -- of plan, but it does in releases 7.4-8.3. Note that an ordering difference
  517. -- here might mean that some other plan type is being used, rendering the test
  518. -- pointless.)
  519. --
  520. create temp table numeric_table (num_col numeric);
  521. insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
  522. create temp table float_table (float_col float8);
  523. insert into float_table values (1), (2), (3);
  524. select * from float_table
  525. where float_col in (select num_col from numeric_table);
  526. float_col
  527. -----------
  528. 1
  529. 2
  530. 3
  531. (3 rows)
  532. select * from numeric_table
  533. where num_col in (select float_col from float_table);
  534. num_col
  535. -------------------------
  536. 1
  537. 1.000000000000000000001
  538. 2
  539. 3
  540. (4 rows)
  541. --
  542. -- Test case for bug #4290: bogus calculation of subplan param sets
  543. --
  544. create temp table ta (id int primary key, val int);
  545. insert into ta values(1,1);
  546. insert into ta values(2,2);
  547. create temp table tb (id int primary key, aval int);
  548. insert into tb values(1,1);
  549. insert into tb values(2,1);
  550. insert into tb values(3,2);
  551. insert into tb values(4,2);
  552. create temp table tc (id int primary key, aid int);
  553. insert into tc values(1,1);
  554. insert into tc values(2,2);
  555. select
  556. ( select min(tb.id) from tb
  557. where tb.aval = (select ta.val from ta where ta.id = tc.aid) ) as min_tb_id
  558. from tc;
  559. min_tb_id
  560. -----------
  561. 1
  562. 3
  563. (2 rows)
  564. --
  565. -- Test case for 8.3 "failed to locate grouping columns" bug
  566. --
  567. create temp table t1 (f1 numeric(14,0), f2 varchar(30));
  568. select * from
  569. (select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
  570. from t1 up) ss
  571. group by f1,f2,fs;
  572. f1 | f2 | fs
  573. ----+----+----
  574. (0 rows)
  575. --
  576. -- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
  577. --
  578. create temp table table_a(id integer);
  579. insert into table_a values (42);
  580. create temp view view_a as select * from table_a;
  581. select view_a from view_a;
  582. view_a
  583. --------
  584. (42)
  585. (1 row)
  586. select (select view_a) from view_a;
  587. view_a
  588. --------
  589. (42)
  590. (1 row)
  591. select (select (select view_a)) from view_a;
  592. view_a
  593. --------
  594. (42)
  595. (1 row)
  596. select (select (a.*)::text) from view_a a;
  597. a
  598. ------
  599. (42)
  600. (1 row)
  601. --
  602. -- Check that whole-row Vars reading the result of a subselect don't include
  603. -- any junk columns therein
  604. --
  605. select q from (select max(f1) from int4_tbl group by f1 order by f1) q;
  606. q
  607. ---------------
  608. (-2147483647)
  609. (-123456)
  610. (0)
  611. (123456)
  612. (2147483647)
  613. (5 rows)
  614. with q as (select max(f1) from int4_tbl group by f1 order by f1)
  615. select q from q;
  616. q
  617. ---------------
  618. (-2147483647)
  619. (-123456)
  620. (0)
  621. (123456)
  622. (2147483647)
  623. (5 rows)
  624. --
  625. -- Test case for sublinks pulled up into joinaliasvars lists in an
  626. -- inherited update/delete query
  627. --
  628. begin; -- this shouldn't delete anything, but be safe
  629. delete from road
  630. where exists (
  631. select 1
  632. from
  633. int4_tbl cross join
  634. ( select f1, array(select q1 from int8_tbl) as arr
  635. from text_tbl ) ss
  636. where road.name = ss.f1 );
  637. rollback;
  638. --
  639. -- Test case for sublinks pushed down into subselects via join alias expansion
  640. --
  641. select
  642. (select sq1) as qq1
  643. from
  644. (select exists(select 1 from int4_tbl where f1 = q2) as sq1, 42 as dummy
  645. from int8_tbl) sq0
  646. join
  647. int4_tbl i4 on dummy = i4.f1;
  648. qq1
  649. -----
  650. (0 rows)
  651. --
  652. -- Test case for subselect within UPDATE of INSERT...ON CONFLICT DO UPDATE
  653. --
  654. create temp table upsert(key int4 primary key, val text);
  655. insert into upsert values(1, 'val') on conflict (key) do update set val = 'not seen';
  656. insert into upsert values(1, 'val') on conflict (key) do update set val = 'seen with subselect ' || (select f1 from int4_tbl where f1 != 0 limit 1)::text;
  657. select * from upsert;
  658. key | val
  659. -----+----------------------------
  660. 1 | seen with subselect 123456
  661. (1 row)
  662. with aa as (select 'int4_tbl' u from int4_tbl limit 1)
  663. insert into upsert values (1, 'x'), (999, 'y')
  664. on conflict (key) do update set val = (select u from aa)
  665. returning *;
  666. key | val
  667. -----+----------
  668. 1 | int4_tbl
  669. 999 | y
  670. (2 rows)
  671. --
  672. -- Test case for cross-type partial matching in hashed subplan (bug #7597)
  673. --
  674. create temp table outer_7597 (f1 int4, f2 int4);
  675. insert into outer_7597 values (0, 0);
  676. insert into outer_7597 values (1, 0);
  677. insert into outer_7597 values (0, null);
  678. insert into outer_7597 values (1, null);
  679. create temp table inner_7597(c1 int8, c2 int8);
  680. insert into inner_7597 values(0, null);
  681. select * from outer_7597 where (f1, f2) not in (select * from inner_7597);
  682. f1 | f2
  683. ----+----
  684. 1 | 0
  685. 1 |
  686. (2 rows)
  687. --
  688. -- Similar test case using text that verifies that collation
  689. -- information is passed through by execTuplesEqual() in nodeSubplan.c
  690. -- (otherwise it would error in texteq())
  691. --
  692. create temp table outer_text (f1 text, f2 text);
  693. insert into outer_text values ('a', 'a');
  694. insert into outer_text values ('b', 'a');
  695. insert into outer_text values ('a', null);
  696. insert into outer_text values ('b', null);
  697. create temp table inner_text (c1 text, c2 text);
  698. insert into inner_text values ('a', null);
  699. insert into inner_text values ('123', '456');
  700. select * from outer_text where (f1, f2) not in (select * from inner_text);
  701. f1 | f2
  702. ----+----
  703. b | a
  704. b |
  705. (2 rows)
  706. --
  707. -- Another test case for cross-type hashed subplans: comparison of
  708. -- inner-side values must be done with appropriate operator
  709. --
  710. explain (verbose, costs off)
  711. select 'foo'::text in (select 'bar'::name union all select 'bar'::name);
  712. QUERY PLAN
  713. -------------------------------------
  714. Result
  715. Output: (hashed SubPlan 1)
  716. SubPlan 1
  717. -> Append
  718. -> Result
  719. Output: 'bar'::name
  720. -> Result
  721. Output: 'bar'::name
  722. (8 rows)
  723. select 'foo'::text in (select 'bar'::name union all select 'bar'::name);
  724. ?column?
  725. ----------
  726. f
  727. (1 row)
  728. --
  729. -- Test that we don't try to hash nested records (bug #17363)
  730. -- (Hashing could be supported, but for now we don't)
  731. --
  732. explain (verbose, costs off)
  733. select row(row(row(1))) = any (select row(row(1)));
  734. QUERY PLAN
  735. -------------------------------------------
  736. Result
  737. Output: (SubPlan 1)
  738. SubPlan 1
  739. -> Materialize
  740. Output: '("(1)")'::record
  741. -> Result
  742. Output: '("(1)")'::record
  743. (7 rows)
  744. select row(row(row(1))) = any (select row(row(1)));
  745. ?column?
  746. ----------
  747. t
  748. (1 row)
  749. --
  750. -- Test case for premature memory release during hashing of subplan output
  751. --
  752. select '1'::text in (select '1'::name union all select '1'::name);
  753. ?column?
  754. ----------
  755. t
  756. (1 row)
  757. --
  758. -- Test that we don't try to use a hashed subplan if the simplified
  759. -- testexpr isn't of the right shape
  760. --
  761. -- this fails by default, of course
  762. select * from int8_tbl where q1 in (select c1 from inner_text);
  763. ERROR: operator does not exist: bigint = text
  764. LINE 1: select * from int8_tbl where q1 in (select c1 from inner_tex...
  765. ^
  766. HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
  767. begin;
  768. -- make an operator to allow it to succeed
  769. create function bogus_int8_text_eq(int8, text) returns boolean
  770. language sql as 'select $1::text = $2';
  771. create operator = (procedure=bogus_int8_text_eq, leftarg=int8, rightarg=text);
  772. explain (costs off)
  773. select * from int8_tbl where q1 in (select c1 from inner_text);
  774. QUERY PLAN
  775. --------------------------------
  776. Seq Scan on int8_tbl
  777. Filter: (hashed SubPlan 1)
  778. SubPlan 1
  779. -> Seq Scan on inner_text
  780. (4 rows)
  781. select * from int8_tbl where q1 in (select c1 from inner_text);
  782. q1 | q2
  783. -----+------------------
  784. 123 | 456
  785. 123 | 4567890123456789
  786. (2 rows)
  787. -- inlining of this function results in unusual number of hash clauses,
  788. -- which we can still cope with
  789. create or replace function bogus_int8_text_eq(int8, text) returns boolean
  790. language sql as 'select $1::text = $2 and $1::text = $2';
  791. explain (costs off)
  792. select * from int8_tbl where q1 in (select c1 from inner_text);
  793. QUERY PLAN
  794. --------------------------------
  795. Seq Scan on int8_tbl
  796. Filter: (hashed SubPlan 1)
  797. SubPlan 1
  798. -> Seq Scan on inner_text
  799. (4 rows)
  800. select * from int8_tbl where q1 in (select c1 from inner_text);
  801. q1 | q2
  802. -----+------------------
  803. 123 | 456
  804. 123 | 4567890123456789
  805. (2 rows)
  806. -- inlining of this function causes LHS and RHS to be switched,
  807. -- which we can't cope with, so hashing should be abandoned
  808. create or replace function bogus_int8_text_eq(int8, text) returns boolean
  809. language sql as 'select $2 = $1::text';
  810. explain (costs off)
  811. select * from int8_tbl where q1 in (select c1 from inner_text);
  812. QUERY PLAN
  813. --------------------------------------
  814. Seq Scan on int8_tbl
  815. Filter: (SubPlan 1)
  816. SubPlan 1
  817. -> Materialize
  818. -> Seq Scan on inner_text
  819. (5 rows)
  820. select * from int8_tbl where q1 in (select c1 from inner_text);
  821. q1 | q2
  822. -----+------------------
  823. 123 | 456
  824. 123 | 4567890123456789
  825. (2 rows)
  826. rollback; -- to get rid of the bogus operator
  827. --
  828. -- Test resolution of hashed vs non-hashed implementation of EXISTS subplan
  829. --
  830. explain (costs off)
  831. select count(*) from tenk1 t
  832. where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0);
  833. QUERY PLAN
  834. --------------------------------------------------------------
  835. Aggregate
  836. -> Seq Scan on tenk1 t
  837. Filter: ((hashed SubPlan 2) OR (ten < 0))
  838. SubPlan 2
  839. -> Index Only Scan using tenk1_unique1 on tenk1 k
  840. (5 rows)
  841. select count(*) from tenk1 t
  842. where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0);
  843. count
  844. -------
  845. 10000
  846. (1 row)
  847. explain (costs off)
  848. select count(*) from tenk1 t
  849. where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0)
  850. and thousand = 1;
  851. QUERY PLAN
  852. --------------------------------------------------------------
  853. Aggregate
  854. -> Bitmap Heap Scan on tenk1 t
  855. Recheck Cond: (thousand = 1)
  856. Filter: ((SubPlan 1) OR (ten < 0))
  857. -> Bitmap Index Scan on tenk1_thous_tenthous
  858. Index Cond: (thousand = 1)
  859. SubPlan 1
  860. -> Index Only Scan using tenk1_unique1 on tenk1 k
  861. Index Cond: (unique1 = t.unique2)
  862. (9 rows)
  863. select count(*) from tenk1 t
  864. where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0)
  865. and thousand = 1;
  866. count
  867. -------
  868. 10
  869. (1 row)
  870. -- It's possible for the same EXISTS to get resolved both ways
  871. create temp table exists_tbl (c1 int, c2 int, c3 int) partition by list (c1);
  872. create temp table exists_tbl_null partition of exists_tbl for values in (null);
  873. create temp table exists_tbl_def partition of exists_tbl default;
  874. insert into exists_tbl select x, x/2, x+1 from generate_series(0,10) x;
  875. analyze exists_tbl;
  876. explain (costs off)
  877. select * from exists_tbl t1
  878. where (exists(select 1 from exists_tbl t2 where t1.c1 = t2.c2) or c3 < 0);
  879. QUERY PLAN
  880. ------------------------------------------------------
  881. Append
  882. -> Seq Scan on exists_tbl_null t1_1
  883. Filter: ((SubPlan 1) OR (c3 < 0))
  884. SubPlan 1
  885. -> Append
  886. -> Seq Scan on exists_tbl_null t2_1
  887. Filter: (t1_1.c1 = c2)
  888. -> Seq Scan on exists_tbl_def t2_2
  889. Filter: (t1_1.c1 = c2)
  890. -> Seq Scan on exists_tbl_def t1_2
  891. Filter: ((hashed SubPlan 2) OR (c3 < 0))
  892. SubPlan 2
  893. -> Append
  894. -> Seq Scan on exists_tbl_null t2_4
  895. -> Seq Scan on exists_tbl_def t2_5
  896. (15 rows)
  897. select * from exists_tbl t1
  898. where (exists(select 1 from exists_tbl t2 where t1.c1 = t2.c2) or c3 < 0);
  899. c1 | c2 | c3
  900. ----+----+----
  901. 0 | 0 | 1
  902. 1 | 0 | 2
  903. 2 | 1 | 3
  904. 3 | 1 | 4
  905. 4 | 2 | 5
  906. 5 | 2 | 6
  907. (6 rows)
  908. --
  909. -- Test case for planner bug with nested EXISTS handling
  910. --
  911. select a.thousand from tenk1 a, tenk1 b
  912. where a.thousand = b.thousand
  913. and exists ( select 1 from tenk1 c where b.hundred = c.hundred
  914. and not exists ( select 1 from tenk1 d
  915. where a.thousand = d.thousand ) );
  916. thousand
  917. ----------
  918. (0 rows)
  919. --
  920. -- Check that nested sub-selects are not pulled up if they contain volatiles
  921. --
  922. explain (verbose, costs off)
  923. select x, x from
  924. (select (select now()) as x from (values(1),(2)) v(y)) ss;
  925. QUERY PLAN
  926. ---------------------------
  927. Values Scan on "*VALUES*"
  928. Output: $0, $1
  929. InitPlan 1 (returns $0)
  930. -> Result
  931. Output: now()
  932. InitPlan 2 (returns $1)
  933. -> Result
  934. Output: now()
  935. (8 rows)
  936. explain (verbose, costs off)
  937. select x, x from
  938. (select (select random()) as x from (values(1),(2)) v(y)) ss;
  939. QUERY PLAN
  940. ----------------------------------
  941. Subquery Scan on ss
  942. Output: ss.x, ss.x
  943. -> Values Scan on "*VALUES*"
  944. Output: $0
  945. InitPlan 1 (returns $0)
  946. -> Result
  947. Output: random()
  948. (7 rows)
  949. explain (verbose, costs off)
  950. select x, x from
  951. (select (select now() where y=y) as x from (values(1),(2)) v(y)) ss;
  952. QUERY PLAN
  953. ----------------------------------------------------------------------
  954. Values Scan on "*VALUES*"
  955. Output: (SubPlan 1), (SubPlan 2)
  956. SubPlan 1
  957. -> Result
  958. Output: now()
  959. One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
  960. SubPlan 2
  961. -> Result
  962. Output: now()
  963. One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
  964. (10 rows)
  965. explain (verbose, costs off)
  966. select x, x from
  967. (select (select random() where y=y) as x from (values(1),(2)) v(y)) ss;
  968. QUERY PLAN
  969. ----------------------------------------------------------------------------
  970. Subquery Scan on ss
  971. Output: ss.x, ss.x
  972. -> Values Scan on "*VALUES*"
  973. Output: (SubPlan 1)
  974. SubPlan 1
  975. -> Result
  976. Output: random()
  977. One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
  978. (8 rows)
  979. --
  980. -- Test rescan of a hashed subplan (the use of random() is to prevent the
  981. -- sub-select from being pulled up, which would result in not hashing)
  982. --
  983. explain (verbose, costs off)
  984. select sum(ss.tst::int) from
  985. onek o cross join lateral (
  986. select i.ten in (select f1 from int4_tbl where f1 <= o.hundred) as tst,
  987. random() as r
  988. from onek i where i.unique1 = o.unique1 ) ss
  989. where o.ten = 0;
  990. QUERY PLAN
  991. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  992. Aggregate
  993. Output: sum((((hashed SubPlan 1)))::integer)
  994. -> Nested Loop
  995. Output: ((hashed SubPlan 1))
  996. -> Seq Scan on public.onek o
  997. Output: o.unique1, o.unique2, o.two, o.four, o.ten, o.twenty, o.hundred, o.thousand, o.twothousand, o.fivethous, o.tenthous, o.odd, o.even, o.stringu1, o.stringu2, o.string4
  998. Filter: (o.ten = 0)
  999. -> Index Scan using onek_unique1 on public.onek i
  1000. Output: (hashed SubPlan 1), random()
  1001. Index Cond: (i.unique1 = o.unique1)
  1002. SubPlan 1
  1003. -> Seq Scan on public.int4_tbl
  1004. Output: int4_tbl.f1
  1005. Filter: (int4_tbl.f1 <= $0)
  1006. (14 rows)
  1007. select sum(ss.tst::int) from
  1008. onek o cross join lateral (
  1009. select i.ten in (select f1 from int4_tbl where f1 <= o.hundred) as tst,
  1010. random() as r
  1011. from onek i where i.unique1 = o.unique1 ) ss
  1012. where o.ten = 0;
  1013. sum
  1014. -----
  1015. 100
  1016. (1 row)
  1017. --
  1018. -- Test rescan of a SetOp node
  1019. --
  1020. explain (costs off)
  1021. select count(*) from
  1022. onek o cross join lateral (
  1023. select * from onek i1 where i1.unique1 = o.unique1
  1024. except
  1025. select * from onek i2 where i2.unique1 = o.unique2
  1026. ) ss
  1027. where o.ten = 1;
  1028. QUERY PLAN
  1029. ------------------------------------------------------------------------------
  1030. Aggregate
  1031. -> Nested Loop
  1032. -> Seq Scan on onek o
  1033. Filter: (ten = 1)
  1034. -> Subquery Scan on ss
  1035. -> HashSetOp Except
  1036. -> Append
  1037. -> Subquery Scan on "*SELECT* 1"
  1038. -> Index Scan using onek_unique1 on onek i1
  1039. Index Cond: (unique1 = o.unique1)
  1040. -> Subquery Scan on "*SELECT* 2"
  1041. -> Index Scan using onek_unique1 on onek i2
  1042. Index Cond: (unique1 = o.unique2)
  1043. (13 rows)
  1044. select count(*) from
  1045. onek o cross join lateral (
  1046. select * from onek i1 where i1.unique1 = o.unique1
  1047. except
  1048. select * from onek i2 where i2.unique1 = o.unique2
  1049. ) ss
  1050. where o.ten = 1;
  1051. count
  1052. -------
  1053. 100
  1054. (1 row)
  1055. --
  1056. -- Test rescan of a RecursiveUnion node
  1057. --
  1058. explain (costs off)
  1059. select sum(o.four), sum(ss.a) from
  1060. onek o cross join lateral (
  1061. with recursive x(a) as
  1062. (select o.four as a
  1063. union
  1064. select a + 1 from x
  1065. where a < 10)
  1066. select * from x
  1067. ) ss
  1068. where o.ten = 1;
  1069. QUERY PLAN
  1070. ---------------------------------------------------------
  1071. Aggregate
  1072. -> Nested Loop
  1073. -> Seq Scan on onek o
  1074. Filter: (ten = 1)
  1075. -> Memoize
  1076. Cache Key: o.four
  1077. Cache Mode: binary
  1078. -> CTE Scan on x
  1079. CTE x
  1080. -> Recursive Union
  1081. -> Result
  1082. -> WorkTable Scan on x x_1
  1083. Filter: (a < 10)
  1084. (13 rows)
  1085. select sum(o.four), sum(ss.a) from
  1086. onek o cross join lateral (
  1087. with recursive x(a) as
  1088. (select o.four as a
  1089. union
  1090. select a + 1 from x
  1091. where a < 10)
  1092. select * from x
  1093. ) ss
  1094. where o.ten = 1;
  1095. sum | sum
  1096. ------+------
  1097. 1700 | 5350
  1098. (1 row)
  1099. --
  1100. -- Check we don't misoptimize a NOT IN where the subquery returns no rows.
  1101. --
  1102. create temp table notinouter (a int);
  1103. create temp table notininner (b int not null);
  1104. insert into notinouter values (null), (1);
  1105. select * from notinouter where a not in (select b from notininner);
  1106. a
  1107. ---
  1108. 1
  1109. (2 rows)
  1110. --
  1111. -- Check we behave sanely in corner case of empty SELECT list (bug #8648)
  1112. --
  1113. create temp table nocolumns();
  1114. select exists(select * from nocolumns);
  1115. exists
  1116. --------
  1117. f
  1118. (1 row)
  1119. --
  1120. -- Check behavior with a SubPlan in VALUES (bug #14924)
  1121. --
  1122. select val.x
  1123. from generate_series(1,10) as s(i),
  1124. lateral (
  1125. values ((select s.i + 1)), (s.i + 101)
  1126. ) as val(x)
  1127. where s.i < 10 and (select val.x) < 110;
  1128. x
  1129. -----
  1130. 2
  1131. 102
  1132. 3
  1133. 103
  1134. 4
  1135. 104
  1136. 5
  1137. 105
  1138. 6
  1139. 106
  1140. 7
  1141. 107
  1142. 8
  1143. 108
  1144. 9
  1145. 109
  1146. 10
  1147. (17 rows)
  1148. -- another variant of that (bug #16213)
  1149. explain (verbose, costs off)
  1150. select * from
  1151. (values
  1152. (3 not in (select * from (values (1), (2)) ss1)),
  1153. (false)
  1154. ) ss;
  1155. QUERY PLAN
  1156. ----------------------------------------
  1157. Values Scan on "*VALUES*"
  1158. Output: "*VALUES*".column1
  1159. SubPlan 1
  1160. -> Values Scan on "*VALUES*_1"
  1161. Output: "*VALUES*_1".column1
  1162. (5 rows)
  1163. select * from
  1164. (values
  1165. (3 not in (select * from (values (1), (2)) ss1)),
  1166. (false)
  1167. ) ss;
  1168. column1
  1169. ---------
  1170. t
  1171. f
  1172. (2 rows)
  1173. --
  1174. -- Check sane behavior with nested IN SubLinks
  1175. --
  1176. explain (verbose, costs off)
  1177. select * from int4_tbl where
  1178. (case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
  1179. (select ten from tenk1 b);
  1180. QUERY PLAN
  1181. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1182. Nested Loop Semi Join
  1183. Output: int4_tbl.f1
  1184. Join Filter: (CASE WHEN (hashed SubPlan 1) THEN int4_tbl.f1 ELSE NULL::integer END = b.ten)
  1185. -> Seq Scan on public.int4_tbl
  1186. Output: int4_tbl.f1
  1187. -> Seq Scan on public.tenk1 b
  1188. Output: b.unique1, b.unique2, b.two, b.four, b.ten, b.twenty, b.hundred, b.thousand, b.twothousand, b.fivethous, b.tenthous, b.odd, b.even, b.stringu1, b.stringu2, b.string4
  1189. SubPlan 1
  1190. -> Index Only Scan using tenk1_unique1 on public.tenk1 a
  1191. Output: a.unique1
  1192. (10 rows)
  1193. select * from int4_tbl where
  1194. (case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
  1195. (select ten from tenk1 b);
  1196. f1
  1197. ----
  1198. 0
  1199. (1 row)
  1200. --
  1201. -- Check for incorrect optimization when IN subquery contains a SRF
  1202. --
  1203. explain (verbose, costs off)
  1204. select * from int4_tbl o where (f1, f1) in
  1205. (select f1, generate_series(1,50) / 10 g from int4_tbl i group by f1);
  1206. QUERY PLAN
  1207. -------------------------------------------------------------------
  1208. Nested Loop Semi Join
  1209. Output: o.f1
  1210. Join Filter: (o.f1 = "ANY_subquery".f1)
  1211. -> Seq Scan on public.int4_tbl o
  1212. Output: o.f1
  1213. -> Materialize
  1214. Output: "ANY_subquery".f1, "ANY_subquery".g
  1215. -> Subquery Scan on "ANY_subquery"
  1216. Output: "ANY_subquery".f1, "ANY_subquery".g
  1217. Filter: ("ANY_subquery".f1 = "ANY_subquery".g)
  1218. -> Result
  1219. Output: i.f1, ((generate_series(1, 50)) / 10)
  1220. -> ProjectSet
  1221. Output: generate_series(1, 50), i.f1
  1222. -> HashAggregate
  1223. Output: i.f1
  1224. Group Key: i.f1
  1225. -> Seq Scan on public.int4_tbl i
  1226. Output: i.f1
  1227. (19 rows)
  1228. select * from int4_tbl o where (f1, f1) in
  1229. (select f1, generate_series(1,50) / 10 g from int4_tbl i group by f1);
  1230. f1
  1231. ----
  1232. 0
  1233. (1 row)
  1234. --
  1235. -- check for over-optimization of whole-row Var referencing an Append plan
  1236. --
  1237. select (select q from
  1238. (select 1,2,3 where f1 > 0
  1239. union all
  1240. select 4,5,6.0 where f1 <= 0
  1241. ) q )
  1242. from int4_tbl;
  1243. q
  1244. -----------
  1245. (4,5,6.0)
  1246. (1,2,3)
  1247. (4,5,6.0)
  1248. (1,2,3)
  1249. (4,5,6.0)
  1250. (5 rows)
  1251. --
  1252. -- Check for sane handling of a lateral reference in a subquery's quals
  1253. -- (most of the complication here is to prevent the test case from being
  1254. -- flattened too much)
  1255. --
  1256. explain (verbose, costs off)
  1257. select * from
  1258. int4_tbl i4,
  1259. lateral (
  1260. select i4.f1 > 1 as b, 1 as id
  1261. from (select random() order by 1) as t1
  1262. union all
  1263. select true as b, 2 as id
  1264. ) as t2
  1265. where b and f1 >= 0;
  1266. QUERY PLAN
  1267. --------------------------------------------
  1268. Nested Loop
  1269. Output: i4.f1, ((i4.f1 > 1)), (1)
  1270. -> Seq Scan on public.int4_tbl i4
  1271. Output: i4.f1
  1272. Filter: (i4.f1 >= 0)
  1273. -> Append
  1274. -> Subquery Scan on t1
  1275. Output: (i4.f1 > 1), 1
  1276. Filter: (i4.f1 > 1)
  1277. -> Sort
  1278. Output: (random())
  1279. Sort Key: (random())
  1280. -> Result
  1281. Output: random()
  1282. -> Result
  1283. Output: true, 2
  1284. (16 rows)
  1285. select * from
  1286. int4_tbl i4,
  1287. lateral (
  1288. select i4.f1 > 1 as b, 1 as id
  1289. from (select random() order by 1) as t1
  1290. union all
  1291. select true as b, 2 as id
  1292. ) as t2
  1293. where b and f1 >= 0;
  1294. f1 | b | id
  1295. ------------+---+----
  1296. 0 | t | 2
  1297. 123456 | t | 1
  1298. 123456 | t | 2
  1299. 2147483647 | t | 1
  1300. 2147483647 | t | 2
  1301. (5 rows)
  1302. --
  1303. -- Check that volatile quals aren't pushed down past a DISTINCT:
  1304. -- nextval() should not be called more than the nominal number of times
  1305. --
  1306. create temp sequence ts1;
  1307. select * from
  1308. (select distinct ten from tenk1) ss
  1309. where ten < 10 + nextval('ts1')
  1310. order by 1;
  1311. ten
  1312. -----
  1313. 0
  1314. 1
  1315. 2
  1316. 3
  1317. 4
  1318. 5
  1319. 6
  1320. 7
  1321. 8
  1322. 9
  1323. (10 rows)
  1324. select nextval('ts1');
  1325. nextval
  1326. ---------
  1327. 11
  1328. (1 row)
  1329. --
  1330. -- Check that volatile quals aren't pushed down past a set-returning function;
  1331. -- while a nonvolatile qual can be, if it doesn't reference the SRF.
  1332. --
  1333. create function tattle(x int, y int) returns bool
  1334. volatile language plpgsql as $$
  1335. begin
  1336. raise notice 'x = %, y = %', x, y;
  1337. return x > y;
  1338. end$$;
  1339. explain (verbose, costs off)
  1340. select * from
  1341. (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
  1342. where tattle(x, 8);
  1343. QUERY PLAN
  1344. ----------------------------------------------------------
  1345. Subquery Scan on ss
  1346. Output: ss.x, ss.u
  1347. Filter: tattle(ss.x, 8)
  1348. -> ProjectSet
  1349. Output: 9, unnest('{1,2,3,11,12,13}'::integer[])
  1350. -> Result
  1351. (6 rows)
  1352. select * from
  1353. (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
  1354. where tattle(x, 8);
  1355. NOTICE: x = 9, y = 8
  1356. NOTICE: x = 9, y = 8
  1357. NOTICE: x = 9, y = 8
  1358. NOTICE: x = 9, y = 8
  1359. NOTICE: x = 9, y = 8
  1360. NOTICE: x = 9, y = 8
  1361. x | u
  1362. ---+----
  1363. 9 | 1
  1364. 9 | 2
  1365. 9 | 3
  1366. 9 | 11
  1367. 9 | 12
  1368. 9 | 13
  1369. (6 rows)
  1370. -- if we pretend it's stable, we get different results:
  1371. alter function tattle(x int, y int) stable;
  1372. explain (verbose, costs off)
  1373. select * from
  1374. (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
  1375. where tattle(x, 8);
  1376. QUERY PLAN
  1377. ----------------------------------------------------
  1378. ProjectSet
  1379. Output: 9, unnest('{1,2,3,11,12,13}'::integer[])
  1380. -> Result
  1381. One-Time Filter: tattle(9, 8)
  1382. (4 rows)
  1383. select * from
  1384. (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
  1385. where tattle(x, 8);
  1386. NOTICE: x = 9, y = 8
  1387. x | u
  1388. ---+----
  1389. 9 | 1
  1390. 9 | 2
  1391. 9 | 3
  1392. 9 | 11
  1393. 9 | 12
  1394. 9 | 13
  1395. (6 rows)
  1396. -- although even a stable qual should not be pushed down if it references SRF
  1397. explain (verbose, costs off)
  1398. select * from
  1399. (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
  1400. where tattle(x, u);
  1401. QUERY PLAN
  1402. ----------------------------------------------------------
  1403. Subquery Scan on ss
  1404. Output: ss.x, ss.u
  1405. Filter: tattle(ss.x, ss.u)
  1406. -> ProjectSet
  1407. Output: 9, unnest('{1,2,3,11,12,13}'::integer[])
  1408. -> Result
  1409. (6 rows)
  1410. select * from
  1411. (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
  1412. where tattle(x, u);
  1413. NOTICE: x = 9, y = 1
  1414. NOTICE: x = 9, y = 2
  1415. NOTICE: x = 9, y = 3
  1416. NOTICE: x = 9, y = 11
  1417. NOTICE: x = 9, y = 12
  1418. NOTICE: x = 9, y = 13
  1419. x | u
  1420. ---+---
  1421. 9 | 1
  1422. 9 | 2
  1423. 9 | 3
  1424. (3 rows)
  1425. drop function tattle(x int, y int);
  1426. --
  1427. -- Test that LIMIT can be pushed to SORT through a subquery that just projects
  1428. -- columns. We check for that having happened by looking to see if EXPLAIN
  1429. -- ANALYZE shows that a top-N sort was used. We must suppress or filter away
  1430. -- all the non-invariant parts of the EXPLAIN ANALYZE output.
  1431. --
  1432. create table sq_limit (pk int primary key, c1 int, c2 int);
  1433. insert into sq_limit values
  1434. (1, 1, 1),
  1435. (2, 2, 2),
  1436. (3, 3, 3),
  1437. (4, 4, 4),
  1438. (5, 1, 1),
  1439. (6, 2, 2),
  1440. (7, 3, 3),
  1441. (8, 4, 4);
  1442. create function explain_sq_limit() returns setof text language plpgsql as
  1443. $$
  1444. declare ln text;
  1445. begin
  1446. for ln in
  1447. explain (analyze, summary off, timing off, costs off)
  1448. select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3
  1449. loop
  1450. ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx');
  1451. return next ln;
  1452. end loop;
  1453. end;
  1454. $$;
  1455. select * from explain_sq_limit();
  1456. explain_sq_limit
  1457. ----------------------------------------------------------------
  1458. Limit (actual rows=3 loops=1)
  1459. -> Subquery Scan on x (actual rows=3 loops=1)
  1460. -> Sort (actual rows=3 loops=1)
  1461. Sort Key: sq_limit.c1, sq_limit.pk
  1462. Sort Method: top-N heapsort Memory: xxx
  1463. -> Seq Scan on sq_limit (actual rows=8 loops=1)
  1464. (6 rows)
  1465. select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3;
  1466. pk | c2
  1467. ----+----
  1468. 1 | 1
  1469. 5 | 1
  1470. 2 | 2
  1471. (3 rows)
  1472. drop function explain_sq_limit();
  1473. drop table sq_limit;
  1474. --
  1475. -- Ensure that backward scan direction isn't propagated into
  1476. -- expression subqueries (bug #15336)
  1477. --
  1478. begin;
  1479. declare c1 scroll cursor for
  1480. select * from generate_series(1,4) i
  1481. where i <> all (values (2),(3));
  1482. move forward all in c1;
  1483. fetch backward all in c1;
  1484. i
  1485. ---
  1486. 4
  1487. 1
  1488. (2 rows)
  1489. commit;
  1490. --
  1491. -- Tests for CTE inlining behavior
  1492. --
  1493. -- Basic subquery that can be inlined
  1494. explain (verbose, costs off)
  1495. with x as (select * from (select f1 from subselect_tbl) ss)
  1496. select * from x where f1 = 1;
  1497. QUERY PLAN
  1498. ----------------------------------
  1499. Seq Scan on public.subselect_tbl
  1500. Output: subselect_tbl.f1
  1501. Filter: (subselect_tbl.f1 = 1)
  1502. (3 rows)
  1503. -- Explicitly request materialization
  1504. explain (verbose, costs off)
  1505. with x as materialized (select * from (select f1 from subselect_tbl) ss)
  1506. select * from x where f1 = 1;
  1507. QUERY PLAN
  1508. ------------------------------------------
  1509. CTE Scan on x
  1510. Output: x.f1
  1511. Filter: (x.f1 = 1)
  1512. CTE x
  1513. -> Seq Scan on public.subselect_tbl
  1514. Output: subselect_tbl.f1
  1515. (6 rows)
  1516. -- Stable functions are safe to inline
  1517. explain (verbose, costs off)
  1518. with x as (select * from (select f1, now() from subselect_tbl) ss)
  1519. select * from x where f1 = 1;
  1520. QUERY PLAN
  1521. -----------------------------------
  1522. Seq Scan on public.subselect_tbl
  1523. Output: subselect_tbl.f1, now()
  1524. Filter: (subselect_tbl.f1 = 1)
  1525. (3 rows)
  1526. -- Volatile functions prevent inlining
  1527. explain (verbose, costs off)
  1528. with x as (select * from (select f1, random() from subselect_tbl) ss)
  1529. select * from x where f1 = 1;
  1530. QUERY PLAN
  1531. ----------------------------------------------
  1532. CTE Scan on x
  1533. Output: x.f1, x.random
  1534. Filter: (x.f1 = 1)
  1535. CTE x
  1536. -> Seq Scan on public.subselect_tbl
  1537. Output: subselect_tbl.f1, random()
  1538. (6 rows)
  1539. -- SELECT FOR UPDATE cannot be inlined
  1540. explain (verbose, costs off)
  1541. with x as (select * from (select f1 from subselect_tbl for update) ss)
  1542. select * from x where f1 = 1;
  1543. QUERY PLAN
  1544. --------------------------------------------------------------------
  1545. CTE Scan on x
  1546. Output: x.f1
  1547. Filter: (x.f1 = 1)
  1548. CTE x
  1549. -> Subquery Scan on ss
  1550. Output: ss.f1
  1551. -> LockRows
  1552. Output: subselect_tbl.f1, subselect_tbl.ctid
  1553. -> Seq Scan on public.subselect_tbl
  1554. Output: subselect_tbl.f1, subselect_tbl.ctid
  1555. (10 rows)
  1556. -- Multiply-referenced CTEs are inlined only when requested
  1557. explain (verbose, costs off)
  1558. with x as (select * from (select f1, now() as n from subselect_tbl) ss)
  1559. select * from x, x x2 where x.n = x2.n;
  1560. QUERY PLAN
  1561. -------------------------------------------
  1562. Merge Join
  1563. Output: x.f1, x.n, x2.f1, x2.n
  1564. Merge Cond: (x.n = x2.n)
  1565. CTE x
  1566. -> Seq Scan on public.subselect_tbl
  1567. Output: subselect_tbl.f1, now()
  1568. -> Sort
  1569. Output: x.f1, x.n
  1570. Sort Key: x.n
  1571. -> CTE Scan on x
  1572. Output: x.f1, x.n
  1573. -> Sort
  1574. Output: x2.f1, x2.n
  1575. Sort Key: x2.n
  1576. -> CTE Scan on x x2
  1577. Output: x2.f1, x2.n
  1578. (16 rows)
  1579. explain (verbose, costs off)
  1580. with x as not materialized (select * from (select f1, now() as n from subselect_tbl) ss)
  1581. select * from x, x x2 where x.n = x2.n;
  1582. QUERY PLAN
  1583. ----------------------------------------------------------------------------
  1584. Result
  1585. Output: subselect_tbl.f1, now(), subselect_tbl_1.f1, now()
  1586. One-Time Filter: (now() = now())
  1587. -> Nested Loop
  1588. Output: subselect_tbl.f1, subselect_tbl_1.f1
  1589. -> Seq Scan on public.subselect_tbl
  1590. Output: subselect_tbl.f1, subselect_tbl.f2, subselect_tbl.f3
  1591. -> Materialize
  1592. Output: subselect_tbl_1.f1
  1593. -> Seq Scan on public.subselect_tbl subselect_tbl_1
  1594. Output: subselect_tbl_1.f1
  1595. (11 rows)
  1596. -- Multiply-referenced CTEs can't be inlined if they contain outer self-refs
  1597. explain (verbose, costs off)
  1598. with recursive x(a) as
  1599. ((values ('a'), ('b'))
  1600. union all
  1601. (with z as not materialized (select * from x)
  1602. select z.a || z1.a as a from z cross join z as z1
  1603. where length(z.a || z1.a) < 5))
  1604. select * from x;
  1605. QUERY PLAN
  1606. ----------------------------------------------------------
  1607. CTE Scan on x
  1608. Output: x.a
  1609. CTE x
  1610. -> Recursive Union
  1611. -> Values Scan on "*VALUES*"
  1612. Output: "*VALUES*".column1
  1613. -> Nested Loop
  1614. Output: (z.a || z1.a)
  1615. Join Filter: (length((z.a || z1.a)) < 5)
  1616. CTE z
  1617. -> WorkTable Scan on x x_1
  1618. Output: x_1.a
  1619. -> CTE Scan on z
  1620. Output: z.a
  1621. -> CTE Scan on z z1
  1622. Output: z1.a
  1623. (16 rows)
  1624. with recursive x(a) as
  1625. ((values ('a'), ('b'))
  1626. union all
  1627. (with z as not materialized (select * from x)
  1628. select z.a || z1.a as a from z cross join z as z1
  1629. where length(z.a || z1.a) < 5))
  1630. select * from x;
  1631. a
  1632. ------
  1633. a
  1634. b
  1635. aa
  1636. ab
  1637. ba
  1638. bb
  1639. aaaa
  1640. aaab
  1641. aaba
  1642. aabb
  1643. abaa
  1644. abab
  1645. abba
  1646. abbb
  1647. baaa
  1648. baab
  1649. baba
  1650. babb
  1651. bbaa
  1652. bbab
  1653. bbba
  1654. bbbb
  1655. (22 rows)
  1656. explain (verbose, costs off)
  1657. with recursive x(a) as
  1658. ((values ('a'), ('b'))
  1659. union all
  1660. (with z as not materialized (select * from x)
  1661. select z.a || z.a as a from z
  1662. where length(z.a || z.a) < 5))
  1663. select * from x;
  1664. QUERY PLAN
  1665. --------------------------------------------------------
  1666. CTE Scan on x
  1667. Output: x.a
  1668. CTE x
  1669. -> Recursive Union
  1670. -> Values Scan on "*VALUES*"
  1671. Output: "*VALUES*".column1
  1672. -> WorkTable Scan on x x_1
  1673. Output: (x_1.a || x_1.a)
  1674. Filter: (length((x_1.a || x_1.a)) < 5)
  1675. (9 rows)
  1676. with recursive x(a) as
  1677. ((values ('a'), ('b'))
  1678. union all
  1679. (with z as not materialized (select * from x)
  1680. select z.a || z.a as a from z
  1681. where length(z.a || z.a) < 5))
  1682. select * from x;
  1683. a
  1684. ------
  1685. a
  1686. b
  1687. aa
  1688. bb
  1689. aaaa
  1690. bbbb
  1691. (6 rows)
  1692. -- Check handling of outer references
  1693. explain (verbose, costs off)
  1694. with x as (select * from int4_tbl)
  1695. select * from (with y as (select * from x) select * from y) ss;
  1696. QUERY PLAN
  1697. -----------------------------
  1698. Seq Scan on public.int4_tbl
  1699. Output: int4_tbl.f1
  1700. (2 rows)
  1701. explain (verbose, costs off)
  1702. with x as materialized (select * from int4_tbl)
  1703. select * from (with y as (select * from x) select * from y) ss;
  1704. QUERY PLAN
  1705. -------------------------------------
  1706. CTE Scan on x
  1707. Output: x.f1
  1708. CTE x
  1709. -> Seq Scan on public.int4_tbl
  1710. Output: int4_tbl.f1
  1711. (5 rows)
  1712. -- Ensure that we inline the currect CTE when there are
  1713. -- multiple CTEs with the same name
  1714. explain (verbose, costs off)
  1715. with x as (select 1 as y)
  1716. select * from (with x as (select 2 as y) select * from x) ss;
  1717. QUERY PLAN
  1718. -------------
  1719. Result
  1720. Output: 2
  1721. (2 rows)
  1722. -- Row marks are not pushed into CTEs
  1723. explain (verbose, costs off)
  1724. with x as (select * from subselect_tbl)
  1725. select * from x for update;
  1726. QUERY PLAN
  1727. ----------------------------------------------------------------
  1728. Seq Scan on public.subselect_tbl
  1729. Output: subselect_tbl.f1, subselect_tbl.f2, subselect_tbl.f3
  1730. (2 rows)