join.sql 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  1. --
  2. -- JOIN
  3. -- Test JOIN clauses
  4. --
  5. CREATE TABLE J1_TBL (
  6. i integer,
  7. j integer,
  8. t text
  9. );
  10. CREATE TABLE J2_TBL (
  11. i integer,
  12. k integer
  13. );
  14. INSERT INTO J1_TBL VALUES (1, 4, 'one');
  15. INSERT INTO J1_TBL VALUES (2, 3, 'two');
  16. INSERT INTO J1_TBL VALUES (3, 2, 'three');
  17. INSERT INTO J1_TBL VALUES (4, 1, 'four');
  18. INSERT INTO J1_TBL VALUES (5, 0, 'five');
  19. INSERT INTO J1_TBL VALUES (6, 6, 'six');
  20. INSERT INTO J1_TBL VALUES (7, 7, 'seven');
  21. INSERT INTO J1_TBL VALUES (8, 8, 'eight');
  22. INSERT INTO J1_TBL VALUES (0, NULL, 'zero');
  23. INSERT INTO J1_TBL VALUES (NULL, NULL, 'null');
  24. INSERT INTO J1_TBL VALUES (NULL, 0, 'zero');
  25. INSERT INTO J2_TBL VALUES (1, -1);
  26. INSERT INTO J2_TBL VALUES (2, 2);
  27. INSERT INTO J2_TBL VALUES (3, -3);
  28. INSERT INTO J2_TBL VALUES (2, 4);
  29. INSERT INTO J2_TBL VALUES (5, -5);
  30. INSERT INTO J2_TBL VALUES (5, -5);
  31. INSERT INTO J2_TBL VALUES (0, NULL);
  32. INSERT INTO J2_TBL VALUES (NULL, NULL);
  33. INSERT INTO J2_TBL VALUES (NULL, 0);
  34. -- useful in some tests below
  35. create temp table onerow();
  36. insert into onerow default values;
  37. analyze onerow;
  38. --
  39. -- CORRELATION NAMES
  40. -- Make sure that table/column aliases are supported
  41. -- before diving into more complex join syntax.
  42. --
  43. SELECT *
  44. FROM J1_TBL AS tx;
  45. SELECT *
  46. FROM J1_TBL tx;
  47. SELECT *
  48. FROM J1_TBL AS t1 (a, b, c);
  49. SELECT *
  50. FROM J1_TBL t1 (a, b, c);
  51. SELECT *
  52. FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
  53. SELECT t1.a, t2.e
  54. FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
  55. WHERE t1.a = t2.d;
  56. --
  57. -- CROSS JOIN
  58. -- Qualifications are not allowed on cross joins,
  59. -- which degenerate into a standard unqualified inner join.
  60. --
  61. SELECT *
  62. FROM J1_TBL CROSS JOIN J2_TBL;
  63. -- ambiguous column
  64. SELECT i, k, t
  65. FROM J1_TBL CROSS JOIN J2_TBL;
  66. -- resolve previous ambiguity by specifying the table name
  67. SELECT t1.i, k, t
  68. FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
  69. SELECT ii, tt, kk
  70. FROM (J1_TBL CROSS JOIN J2_TBL)
  71. AS tx (ii, jj, tt, ii2, kk);
  72. SELECT tx.ii, tx.jj, tx.kk
  73. FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
  74. AS tx (ii, jj, tt, ii2, kk);
  75. SELECT *
  76. FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
  77. --
  78. --
  79. -- Inner joins (equi-joins)
  80. --
  81. --
  82. --
  83. -- Inner joins (equi-joins) with USING clause
  84. -- The USING syntax changes the shape of the resulting table
  85. -- by including a column in the USING clause only once in the result.
  86. --
  87. -- Inner equi-join on specified column
  88. SELECT *
  89. FROM J1_TBL INNER JOIN J2_TBL USING (i);
  90. -- Same as above, slightly different syntax
  91. SELECT *
  92. FROM J1_TBL JOIN J2_TBL USING (i);
  93. SELECT *
  94. FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
  95. ORDER BY a, d;
  96. SELECT *
  97. FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b)
  98. ORDER BY b, t1.a;
  99. -- test join using aliases
  100. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) WHERE J1_TBL.t = 'one'; -- ok
  101. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one'; -- ok
  102. SELECT * FROM (J1_TBL JOIN J2_TBL USING (i)) AS x WHERE J1_TBL.t = 'one'; -- error
  103. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE x.i = 1; -- ok
  104. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE x.t = 'one'; -- error
  105. SELECT * FROM (J1_TBL JOIN J2_TBL USING (i) AS x) AS xx WHERE x.i = 1; -- error (XXX could use better hint)
  106. SELECT * FROM J1_TBL a1 JOIN J2_TBL a2 USING (i) AS a1; -- error
  107. SELECT x.* FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one';
  108. SELECT ROW(x.*) FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one';
  109. SELECT row_to_json(x.*) FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one';
  110. --
  111. -- NATURAL JOIN
  112. -- Inner equi-join on all columns with the same name
  113. --
  114. SELECT *
  115. FROM J1_TBL NATURAL JOIN J2_TBL;
  116. SELECT *
  117. FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
  118. SELECT *
  119. FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
  120. -- mismatch number of columns
  121. -- currently, Postgres will fill in with underlying names
  122. SELECT *
  123. FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
  124. --
  125. -- Inner joins (equi-joins)
  126. --
  127. SELECT *
  128. FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
  129. SELECT *
  130. FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
  131. --
  132. -- Non-equi-joins
  133. --
  134. SELECT *
  135. FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
  136. --
  137. -- Outer joins
  138. -- Note that OUTER is a noise word
  139. --
  140. SELECT *
  141. FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i)
  142. ORDER BY i, k, t;
  143. SELECT *
  144. FROM J1_TBL LEFT JOIN J2_TBL USING (i)
  145. ORDER BY i, k, t;
  146. SELECT *
  147. FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
  148. SELECT *
  149. FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
  150. SELECT *
  151. FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i)
  152. ORDER BY i, k, t;
  153. SELECT *
  154. FROM J1_TBL FULL JOIN J2_TBL USING (i)
  155. ORDER BY i, k, t;
  156. SELECT *
  157. FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
  158. SELECT *
  159. FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
  160. --
  161. -- semijoin selectivity for <>
  162. --
  163. explain (costs off)
  164. select * from int4_tbl i4, tenk1 a
  165. where exists(select * from tenk1 b
  166. where a.twothousand = b.twothousand and a.fivethous <> b.fivethous)
  167. and i4.f1 = a.tenthous;
  168. --
  169. -- More complicated constructs
  170. --
  171. --
  172. -- Multiway full join
  173. --
  174. CREATE TABLE t1 (name TEXT, n INTEGER);
  175. CREATE TABLE t2 (name TEXT, n INTEGER);
  176. CREATE TABLE t3 (name TEXT, n INTEGER);
  177. INSERT INTO t1 VALUES ( 'bb', 11 );
  178. INSERT INTO t2 VALUES ( 'bb', 12 );
  179. INSERT INTO t2 VALUES ( 'cc', 22 );
  180. INSERT INTO t2 VALUES ( 'ee', 42 );
  181. INSERT INTO t3 VALUES ( 'bb', 13 );
  182. INSERT INTO t3 VALUES ( 'cc', 23 );
  183. INSERT INTO t3 VALUES ( 'dd', 33 );
  184. SELECT * FROM t1 FULL JOIN t2 USING (name) FULL JOIN t3 USING (name);
  185. --
  186. -- Test interactions of join syntax and subqueries
  187. --
  188. -- Basic cases (we expect planner to pull up the subquery here)
  189. SELECT * FROM
  190. (SELECT * FROM t2) as s2
  191. INNER JOIN
  192. (SELECT * FROM t3) s3
  193. USING (name);
  194. SELECT * FROM
  195. (SELECT * FROM t2) as s2
  196. LEFT JOIN
  197. (SELECT * FROM t3) s3
  198. USING (name);
  199. SELECT * FROM
  200. (SELECT * FROM t2) as s2
  201. FULL JOIN
  202. (SELECT * FROM t3) s3
  203. USING (name);
  204. -- Cases with non-nullable expressions in subquery results;
  205. -- make sure these go to null as expected
  206. SELECT * FROM
  207. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  208. NATURAL INNER JOIN
  209. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  210. SELECT * FROM
  211. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  212. NATURAL LEFT JOIN
  213. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  214. SELECT * FROM
  215. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  216. NATURAL FULL JOIN
  217. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  218. SELECT * FROM
  219. (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
  220. NATURAL INNER JOIN
  221. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  222. NATURAL INNER JOIN
  223. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  224. SELECT * FROM
  225. (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
  226. NATURAL FULL JOIN
  227. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  228. NATURAL FULL JOIN
  229. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  230. SELECT * FROM
  231. (SELECT name, n as s1_n FROM t1) as s1
  232. NATURAL FULL JOIN
  233. (SELECT * FROM
  234. (SELECT name, n as s2_n FROM t2) as s2
  235. NATURAL FULL JOIN
  236. (SELECT name, n as s3_n FROM t3) as s3
  237. ) ss2;
  238. SELECT * FROM
  239. (SELECT name, n as s1_n FROM t1) as s1
  240. NATURAL FULL JOIN
  241. (SELECT * FROM
  242. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  243. NATURAL FULL JOIN
  244. (SELECT name, n as s3_n FROM t3) as s3
  245. ) ss2;
  246. -- Constants as join keys can also be problematic
  247. SELECT * FROM
  248. (SELECT name, n as s1_n FROM t1) as s1
  249. FULL JOIN
  250. (SELECT name, 2 as s2_n FROM t2) as s2
  251. ON (s1_n = s2_n);
  252. -- Test for propagation of nullability constraints into sub-joins
  253. create temp table x (x1 int, x2 int);
  254. insert into x values (1,11);
  255. insert into x values (2,22);
  256. insert into x values (3,null);
  257. insert into x values (4,44);
  258. insert into x values (5,null);
  259. create temp table y (y1 int, y2 int);
  260. insert into y values (1,111);
  261. insert into y values (2,222);
  262. insert into y values (3,333);
  263. insert into y values (4,null);
  264. select * from x;
  265. select * from y;
  266. select * from x left join y on (x1 = y1 and x2 is not null);
  267. select * from x left join y on (x1 = y1 and y2 is not null);
  268. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  269. on (x1 = xx1);
  270. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  271. on (x1 = xx1 and x2 is not null);
  272. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  273. on (x1 = xx1 and y2 is not null);
  274. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  275. on (x1 = xx1 and xx2 is not null);
  276. -- these should NOT give the same answers as above
  277. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  278. on (x1 = xx1) where (x2 is not null);
  279. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  280. on (x1 = xx1) where (y2 is not null);
  281. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  282. on (x1 = xx1) where (xx2 is not null);
  283. --
  284. -- regression test: check for bug with propagation of implied equality
  285. -- to outside an IN
  286. --
  287. select count(*) from tenk1 a where unique1 in
  288. (select unique1 from tenk1 b join tenk1 c using (unique1)
  289. where b.unique2 = 42);
  290. --
  291. -- regression test: check for failure to generate a plan with multiple
  292. -- degenerate IN clauses
  293. --
  294. select count(*) from tenk1 x where
  295. x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
  296. x.unique1 = 0 and
  297. x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
  298. -- try that with GEQO too
  299. begin;
  300. set geqo = on;
  301. set geqo_threshold = 2;
  302. select count(*) from tenk1 x where
  303. x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
  304. x.unique1 = 0 and
  305. x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
  306. rollback;
  307. --
  308. -- regression test: be sure we cope with proven-dummy append rels
  309. --
  310. explain (costs off)
  311. select aa, bb, unique1, unique1
  312. from tenk1 right join b on aa = unique1
  313. where bb < bb and bb is null;
  314. select aa, bb, unique1, unique1
  315. from tenk1 right join b on aa = unique1
  316. where bb < bb and bb is null;
  317. --
  318. -- regression test: check handling of empty-FROM subquery underneath outer join
  319. --
  320. explain (costs off)
  321. select * from int8_tbl i1 left join (int8_tbl i2 join
  322. (select 123 as x) ss on i2.q1 = x) on i1.q2 = i2.q2
  323. order by 1, 2;
  324. select * from int8_tbl i1 left join (int8_tbl i2 join
  325. (select 123 as x) ss on i2.q1 = x) on i1.q2 = i2.q2
  326. order by 1, 2;
  327. --
  328. -- regression test: check a case where join_clause_is_movable_into() gives
  329. -- an imprecise result, causing an assertion failure
  330. --
  331. select count(*)
  332. from
  333. (select t3.tenthous as x1, coalesce(t1.stringu1, t2.stringu1) as x2
  334. from tenk1 t1
  335. left join tenk1 t2 on t1.unique1 = t2.unique1
  336. join tenk1 t3 on t1.unique2 = t3.unique2) ss,
  337. tenk1 t4,
  338. tenk1 t5
  339. where t4.thousand = t5.unique1 and ss.x1 = t4.tenthous and ss.x2 = t5.stringu1;
  340. --
  341. -- regression test: check a case where we formerly missed including an EC
  342. -- enforcement clause because it was expected to be handled at scan level
  343. --
  344. explain (costs off)
  345. select a.f1, b.f1, t.thousand, t.tenthous from
  346. tenk1 t,
  347. (select sum(f1)+1 as f1 from int4_tbl i4a) a,
  348. (select sum(f1) as f1 from int4_tbl i4b) b
  349. where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
  350. select a.f1, b.f1, t.thousand, t.tenthous from
  351. tenk1 t,
  352. (select sum(f1)+1 as f1 from int4_tbl i4a) a,
  353. (select sum(f1) as f1 from int4_tbl i4b) b
  354. where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
  355. --
  356. -- check a case where we formerly got confused by conflicting sort orders
  357. -- in redundant merge join path keys
  358. --
  359. explain (costs off)
  360. select * from
  361. j1_tbl full join
  362. (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k asc) j2_tbl
  363. on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k;
  364. select * from
  365. j1_tbl full join
  366. (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k asc) j2_tbl
  367. on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k;
  368. --
  369. -- a different check for handling of redundant sort keys in merge joins
  370. --
  371. explain (costs off)
  372. select count(*) from
  373. (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
  374. left join
  375. (select * from tenk1 y order by y.unique2) y
  376. on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
  377. select count(*) from
  378. (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
  379. left join
  380. (select * from tenk1 y order by y.unique2) y
  381. on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
  382. --
  383. -- Clean up
  384. --
  385. DROP TABLE t1;
  386. DROP TABLE t2;
  387. DROP TABLE t3;
  388. DROP TABLE J1_TBL;
  389. DROP TABLE J2_TBL;
  390. -- Both DELETE and UPDATE allow the specification of additional tables
  391. -- to "join" against to determine which rows should be modified.
  392. CREATE TEMP TABLE t1 (a int, b int);
  393. CREATE TEMP TABLE t2 (a int, b int);
  394. CREATE TEMP TABLE t3 (x int, y int);
  395. INSERT INTO t1 VALUES (5, 10);
  396. INSERT INTO t1 VALUES (15, 20);
  397. INSERT INTO t1 VALUES (100, 100);
  398. INSERT INTO t1 VALUES (200, 1000);
  399. INSERT INTO t2 VALUES (200, 2000);
  400. INSERT INTO t3 VALUES (5, 20);
  401. INSERT INTO t3 VALUES (6, 7);
  402. INSERT INTO t3 VALUES (7, 8);
  403. INSERT INTO t3 VALUES (500, 100);
  404. DELETE FROM t3 USING t1 table1 WHERE t3.x = table1.a;
  405. SELECT * FROM t3;
  406. DELETE FROM t3 USING t1 JOIN t2 USING (a) WHERE t3.x > t1.a;
  407. SELECT * FROM t3;
  408. DELETE FROM t3 USING t3 t3_other WHERE t3.x = t3_other.x AND t3.y = t3_other.y;
  409. SELECT * FROM t3;
  410. -- Test join against inheritance tree
  411. create temp table t2a () inherits (t2);
  412. insert into t2a values (200, 2001);
  413. select * from t1 left join t2 on (t1.a = t2.a);
  414. -- Test matching of column name with wrong alias
  415. select t1.x from t1 join t3 on (t1.a = t3.x);
  416. -- Test matching of locking clause with wrong alias
  417. select t1.*, t2.*, unnamed_join.* from
  418. t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
  419. for update of unnamed_join;
  420. select foo.*, unnamed_join.* from
  421. t1 join t2 using (a) as foo, t3 as unnamed_join
  422. for update of unnamed_join;
  423. select foo.*, unnamed_join.* from
  424. t1 join t2 using (a) as foo, t3 as unnamed_join
  425. for update of foo;
  426. select bar.*, unnamed_join.* from
  427. (t1 join t2 using (a) as foo) as bar, t3 as unnamed_join
  428. for update of foo;
  429. select bar.*, unnamed_join.* from
  430. (t1 join t2 using (a) as foo) as bar, t3 as unnamed_join
  431. for update of bar;
  432. --
  433. -- regression test for 8.1 merge right join bug
  434. --
  435. CREATE TEMP TABLE tt1 ( tt1_id int4, joincol int4 );
  436. INSERT INTO tt1 VALUES (1, 11);
  437. INSERT INTO tt1 VALUES (2, NULL);
  438. CREATE TEMP TABLE tt2 ( tt2_id int4, joincol int4 );
  439. INSERT INTO tt2 VALUES (21, 11);
  440. INSERT INTO tt2 VALUES (22, 11);
  441. set enable_hashjoin to off;
  442. set enable_nestloop to off;
  443. -- these should give the same results
  444. select tt1.*, tt2.* from tt1 left join tt2 on tt1.joincol = tt2.joincol;
  445. select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
  446. reset enable_hashjoin;
  447. reset enable_nestloop;
  448. --
  449. -- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
  450. --
  451. set work_mem to '64kB';
  452. set enable_mergejoin to off;
  453. set enable_memoize to off;
  454. explain (costs off)
  455. select count(*) from tenk1 a, tenk1 b
  456. where a.hundred = b.thousand and (b.fivethous % 10) < 10;
  457. select count(*) from tenk1 a, tenk1 b
  458. where a.hundred = b.thousand and (b.fivethous % 10) < 10;
  459. reset work_mem;
  460. reset enable_mergejoin;
  461. reset enable_memoize;
  462. --
  463. -- regression test for 8.2 bug with improper re-ordering of left joins
  464. --
  465. create temp table tt3(f1 int, f2 text);
  466. insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
  467. create index tt3i on tt3(f1);
  468. analyze tt3;
  469. create temp table tt4(f1 int);
  470. insert into tt4 values (0),(1),(9999);
  471. analyze tt4;
  472. SELECT a.f1
  473. FROM tt4 a
  474. LEFT JOIN (
  475. SELECT b.f1
  476. FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
  477. WHERE c.f1 IS NULL
  478. ) AS d ON (a.f1 = d.f1)
  479. WHERE d.f1 IS NULL;
  480. --
  481. -- regression test for proper handling of outer joins within antijoins
  482. --
  483. create temp table tt4x(c1 int, c2 int, c3 int);
  484. explain (costs off)
  485. select * from tt4x t1
  486. where not exists (
  487. select 1 from tt4x t2
  488. left join tt4x t3 on t2.c3 = t3.c1
  489. left join ( select t5.c1 as c1
  490. from tt4x t4 left join tt4x t5 on t4.c2 = t5.c1
  491. ) a1 on t3.c2 = a1.c1
  492. where t1.c1 = t2.c2
  493. );
  494. --
  495. -- regression test for problems of the sort depicted in bug #3494
  496. --
  497. create temp table tt5(f1 int, f2 int);
  498. create temp table tt6(f1 int, f2 int);
  499. insert into tt5 values(1, 10);
  500. insert into tt5 values(1, 11);
  501. insert into tt6 values(1, 9);
  502. insert into tt6 values(1, 2);
  503. insert into tt6 values(2, 9);
  504. select * from tt5,tt6 where tt5.f1 = tt6.f1 and tt5.f1 = tt5.f2 - tt6.f2;
  505. --
  506. -- regression test for problems of the sort depicted in bug #3588
  507. --
  508. create temp table xx (pkxx int);
  509. create temp table yy (pkyy int, pkxx int);
  510. insert into xx values (1);
  511. insert into xx values (2);
  512. insert into xx values (3);
  513. insert into yy values (101, 1);
  514. insert into yy values (201, 2);
  515. insert into yy values (301, NULL);
  516. select yy.pkyy as yy_pkyy, yy.pkxx as yy_pkxx, yya.pkyy as yya_pkyy,
  517. xxa.pkxx as xxa_pkxx, xxb.pkxx as xxb_pkxx
  518. from yy
  519. left join (SELECT * FROM yy where pkyy = 101) as yya ON yy.pkyy = yya.pkyy
  520. left join xx xxa on yya.pkxx = xxa.pkxx
  521. left join xx xxb on coalesce (xxa.pkxx, 1) = xxb.pkxx;
  522. --
  523. -- regression test for improper pushing of constants across outer-join clauses
  524. -- (as seen in early 8.2.x releases)
  525. --
  526. create temp table zt1 (f1 int primary key);
  527. create temp table zt2 (f2 int primary key);
  528. create temp table zt3 (f3 int primary key);
  529. insert into zt1 values(53);
  530. insert into zt2 values(53);
  531. select * from
  532. zt2 left join zt3 on (f2 = f3)
  533. left join zt1 on (f3 = f1)
  534. where f2 = 53;
  535. create temp view zv1 as select *,'dummy'::text AS junk from zt1;
  536. select * from
  537. zt2 left join zt3 on (f2 = f3)
  538. left join zv1 on (f3 = f1)
  539. where f2 = 53;
  540. --
  541. -- regression test for improper extraction of OR indexqual conditions
  542. -- (as seen in early 8.3.x releases)
  543. --
  544. select a.unique2, a.ten, b.tenthous, b.unique2, b.hundred
  545. from tenk1 a left join tenk1 b on a.unique2 = b.tenthous
  546. where a.unique1 = 42 and
  547. ((b.unique2 is null and a.ten = 2) or b.hundred = 3);
  548. --
  549. -- test proper positioning of one-time quals in EXISTS (8.4devel bug)
  550. --
  551. prepare foo(bool) as
  552. select count(*) from tenk1 a left join tenk1 b
  553. on (a.unique2 = b.unique1 and exists
  554. (select 1 from tenk1 c where c.thousand = b.unique2 and $1));
  555. execute foo(true);
  556. execute foo(false);
  557. --
  558. -- test for sane behavior with noncanonical merge clauses, per bug #4926
  559. --
  560. begin;
  561. set enable_mergejoin = 1;
  562. set enable_hashjoin = 0;
  563. set enable_nestloop = 0;
  564. create temp table a (i integer);
  565. create temp table b (x integer, y integer);
  566. select * from a left join b on i = x and i = y and x = i;
  567. rollback;
  568. --
  569. -- test handling of merge clauses using record_ops
  570. --
  571. begin;
  572. create type mycomptype as (id int, v bigint);
  573. create temp table tidv (idv mycomptype);
  574. create index on tidv (idv);
  575. explain (costs off)
  576. select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
  577. set enable_mergejoin = 0;
  578. set enable_hashjoin = 0;
  579. explain (costs off)
  580. select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
  581. rollback;
  582. --
  583. -- test NULL behavior of whole-row Vars, per bug #5025
  584. --
  585. select t1.q2, count(t2.*)
  586. from int8_tbl t1 left join int8_tbl t2 on (t1.q2 = t2.q1)
  587. group by t1.q2 order by 1;
  588. select t1.q2, count(t2.*)
  589. from int8_tbl t1 left join (select * from int8_tbl) t2 on (t1.q2 = t2.q1)
  590. group by t1.q2 order by 1;
  591. select t1.q2, count(t2.*)
  592. from int8_tbl t1 left join (select * from int8_tbl offset 0) t2 on (t1.q2 = t2.q1)
  593. group by t1.q2 order by 1;
  594. select t1.q2, count(t2.*)
  595. from int8_tbl t1 left join
  596. (select q1, case when q2=1 then 1 else q2 end as q2 from int8_tbl) t2
  597. on (t1.q2 = t2.q1)
  598. group by t1.q2 order by 1;
  599. --
  600. -- test incorrect failure to NULL pulled-up subexpressions
  601. --
  602. begin;
  603. create temp table a (
  604. code char not null,
  605. constraint a_pk primary key (code)
  606. );
  607. create temp table b (
  608. a char not null,
  609. num integer not null,
  610. constraint b_pk primary key (a, num)
  611. );
  612. create temp table c (
  613. name char not null,
  614. a char,
  615. constraint c_pk primary key (name)
  616. );
  617. insert into a (code) values ('p');
  618. insert into a (code) values ('q');
  619. insert into b (a, num) values ('p', 1);
  620. insert into b (a, num) values ('p', 2);
  621. insert into c (name, a) values ('A', 'p');
  622. insert into c (name, a) values ('B', 'q');
  623. insert into c (name, a) values ('C', null);
  624. select c.name, ss.code, ss.b_cnt, ss.const
  625. from c left join
  626. (select a.code, coalesce(b_grp.cnt, 0) as b_cnt, -1 as const
  627. from a left join
  628. (select count(1) as cnt, b.a from b group by b.a) as b_grp
  629. on a.code = b_grp.a
  630. ) as ss
  631. on (c.a = ss.code)
  632. order by c.name;
  633. rollback;
  634. --
  635. -- test incorrect handling of placeholders that only appear in targetlists,
  636. -- per bug #6154
  637. --
  638. SELECT * FROM
  639. ( SELECT 1 as key1 ) sub1
  640. LEFT JOIN
  641. ( SELECT sub3.key3, sub4.value2, COALESCE(sub4.value2, 66) as value3 FROM
  642. ( SELECT 1 as key3 ) sub3
  643. LEFT JOIN
  644. ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
  645. ( SELECT 1 as key5 ) sub5
  646. LEFT JOIN
  647. ( SELECT 2 as key6, 42 as value1 ) sub6
  648. ON sub5.key5 = sub6.key6
  649. ) sub4
  650. ON sub4.key5 = sub3.key3
  651. ) sub2
  652. ON sub1.key1 = sub2.key3;
  653. -- test the path using join aliases, too
  654. SELECT * FROM
  655. ( SELECT 1 as key1 ) sub1
  656. LEFT JOIN
  657. ( SELECT sub3.key3, value2, COALESCE(value2, 66) as value3 FROM
  658. ( SELECT 1 as key3 ) sub3
  659. LEFT JOIN
  660. ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
  661. ( SELECT 1 as key5 ) sub5
  662. LEFT JOIN
  663. ( SELECT 2 as key6, 42 as value1 ) sub6
  664. ON sub5.key5 = sub6.key6
  665. ) sub4
  666. ON sub4.key5 = sub3.key3
  667. ) sub2
  668. ON sub1.key1 = sub2.key3;
  669. --
  670. -- test case where a PlaceHolderVar is used as a nestloop parameter
  671. --
  672. EXPLAIN (COSTS OFF)
  673. SELECT qq, unique1
  674. FROM
  675. ( SELECT COALESCE(q1, 0) AS qq FROM int8_tbl a ) AS ss1
  676. FULL OUTER JOIN
  677. ( SELECT COALESCE(q2, -1) AS qq FROM int8_tbl b ) AS ss2
  678. USING (qq)
  679. INNER JOIN tenk1 c ON qq = unique2;
  680. SELECT qq, unique1
  681. FROM
  682. ( SELECT COALESCE(q1, 0) AS qq FROM int8_tbl a ) AS ss1
  683. FULL OUTER JOIN
  684. ( SELECT COALESCE(q2, -1) AS qq FROM int8_tbl b ) AS ss2
  685. USING (qq)
  686. INNER JOIN tenk1 c ON qq = unique2;
  687. --
  688. -- nested nestloops can require nested PlaceHolderVars
  689. --
  690. create temp table nt1 (
  691. id int primary key,
  692. a1 boolean,
  693. a2 boolean
  694. );
  695. create temp table nt2 (
  696. id int primary key,
  697. nt1_id int,
  698. b1 boolean,
  699. b2 boolean,
  700. foreign key (nt1_id) references nt1(id)
  701. );
  702. create temp table nt3 (
  703. id int primary key,
  704. nt2_id int,
  705. c1 boolean,
  706. foreign key (nt2_id) references nt2(id)
  707. );
  708. insert into nt1 values (1,true,true);
  709. insert into nt1 values (2,true,false);
  710. insert into nt1 values (3,false,false);
  711. insert into nt2 values (1,1,true,true);
  712. insert into nt2 values (2,2,true,false);
  713. insert into nt2 values (3,3,false,false);
  714. insert into nt3 values (1,1,true);
  715. insert into nt3 values (2,2,false);
  716. insert into nt3 values (3,3,true);
  717. explain (costs off)
  718. select nt3.id
  719. from nt3 as nt3
  720. left join
  721. (select nt2.*, (nt2.b1 and ss1.a3) AS b3
  722. from nt2 as nt2
  723. left join
  724. (select nt1.*, (nt1.id is not null) as a3 from nt1) as ss1
  725. on ss1.id = nt2.nt1_id
  726. ) as ss2
  727. on ss2.id = nt3.nt2_id
  728. where nt3.id = 1 and ss2.b3;
  729. select nt3.id
  730. from nt3 as nt3
  731. left join
  732. (select nt2.*, (nt2.b1 and ss1.a3) AS b3
  733. from nt2 as nt2
  734. left join
  735. (select nt1.*, (nt1.id is not null) as a3 from nt1) as ss1
  736. on ss1.id = nt2.nt1_id
  737. ) as ss2
  738. on ss2.id = nt3.nt2_id
  739. where nt3.id = 1 and ss2.b3;
  740. --
  741. -- test case where a PlaceHolderVar is propagated into a subquery
  742. --
  743. explain (costs off)
  744. select * from
  745. int8_tbl t1 left join
  746. (select q1 as x, 42 as y from int8_tbl t2) ss
  747. on t1.q2 = ss.x
  748. where
  749. 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
  750. order by 1,2;
  751. select * from
  752. int8_tbl t1 left join
  753. (select q1 as x, 42 as y from int8_tbl t2) ss
  754. on t1.q2 = ss.x
  755. where
  756. 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
  757. order by 1,2;
  758. --
  759. -- variant where a PlaceHolderVar is needed at a join, but not above the join
  760. --
  761. explain (costs off)
  762. select * from
  763. int4_tbl as i41,
  764. lateral
  765. (select 1 as x from
  766. (select i41.f1 as lat,
  767. i42.f1 as loc from
  768. int8_tbl as i81, int4_tbl as i42) as ss1
  769. right join int4_tbl as i43 on (i43.f1 > 1)
  770. where ss1.loc = ss1.lat) as ss2
  771. where i41.f1 > 0;
  772. select * from
  773. int4_tbl as i41,
  774. lateral
  775. (select 1 as x from
  776. (select i41.f1 as lat,
  777. i42.f1 as loc from
  778. int8_tbl as i81, int4_tbl as i42) as ss1
  779. right join int4_tbl as i43 on (i43.f1 > 1)
  780. where ss1.loc = ss1.lat) as ss2
  781. where i41.f1 > 0;
  782. --
  783. -- test the corner cases FULL JOIN ON TRUE and FULL JOIN ON FALSE
  784. --
  785. select * from int4_tbl a full join int4_tbl b on true;
  786. select * from int4_tbl a full join int4_tbl b on false;
  787. --
  788. -- test for ability to use a cartesian join when necessary
  789. --
  790. create temp table q1 as select 1 as q1;
  791. create temp table q2 as select 0 as q2;
  792. analyze q1;
  793. analyze q2;
  794. explain (costs off)
  795. select * from
  796. tenk1 join int4_tbl on f1 = twothousand,
  797. q1, q2
  798. where q1 = thousand or q2 = thousand;
  799. explain (costs off)
  800. select * from
  801. tenk1 join int4_tbl on f1 = twothousand,
  802. q1, q2
  803. where thousand = (q1 + q2);
  804. --
  805. -- test ability to generate a suitable plan for a star-schema query
  806. --
  807. explain (costs off)
  808. select * from
  809. tenk1, int8_tbl a, int8_tbl b
  810. where thousand = a.q1 and tenthous = b.q1 and a.q2 = 1 and b.q2 = 2;
  811. --
  812. -- test a corner case in which we shouldn't apply the star-schema optimization
  813. --
  814. explain (costs off)
  815. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  816. tenk1 t1
  817. inner join int4_tbl i1
  818. left join (select v1.x2, v2.y1, 11 AS d1
  819. from (select 1,0 from onerow) v1(x1,x2)
  820. left join (select 3,1 from onerow) v2(y1,y2)
  821. on v1.x1 = v2.y2) subq1
  822. on (i1.f1 = subq1.x2)
  823. on (t1.unique2 = subq1.d1)
  824. left join tenk1 t2
  825. on (subq1.y1 = t2.unique1)
  826. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  827. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  828. tenk1 t1
  829. inner join int4_tbl i1
  830. left join (select v1.x2, v2.y1, 11 AS d1
  831. from (select 1,0 from onerow) v1(x1,x2)
  832. left join (select 3,1 from onerow) v2(y1,y2)
  833. on v1.x1 = v2.y2) subq1
  834. on (i1.f1 = subq1.x2)
  835. on (t1.unique2 = subq1.d1)
  836. left join tenk1 t2
  837. on (subq1.y1 = t2.unique1)
  838. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  839. -- variant that isn't quite a star-schema case
  840. select ss1.d1 from
  841. tenk1 as t1
  842. inner join tenk1 as t2
  843. on t1.tenthous = t2.ten
  844. inner join
  845. int8_tbl as i8
  846. left join int4_tbl as i4
  847. inner join (select 64::information_schema.cardinal_number as d1
  848. from tenk1 t3,
  849. lateral (select abs(t3.unique1) + random()) ss0(x)
  850. where t3.fivethous < 0) as ss1
  851. on i4.f1 = ss1.d1
  852. on i8.q1 = i4.f1
  853. on t1.tenthous = ss1.d1
  854. where t1.unique1 < i4.f1;
  855. -- this variant is foldable by the remove-useless-RESULT-RTEs code
  856. explain (costs off)
  857. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  858. tenk1 t1
  859. inner join int4_tbl i1
  860. left join (select v1.x2, v2.y1, 11 AS d1
  861. from (values(1,0)) v1(x1,x2)
  862. left join (values(3,1)) v2(y1,y2)
  863. on v1.x1 = v2.y2) subq1
  864. on (i1.f1 = subq1.x2)
  865. on (t1.unique2 = subq1.d1)
  866. left join tenk1 t2
  867. on (subq1.y1 = t2.unique1)
  868. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  869. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  870. tenk1 t1
  871. inner join int4_tbl i1
  872. left join (select v1.x2, v2.y1, 11 AS d1
  873. from (values(1,0)) v1(x1,x2)
  874. left join (values(3,1)) v2(y1,y2)
  875. on v1.x1 = v2.y2) subq1
  876. on (i1.f1 = subq1.x2)
  877. on (t1.unique2 = subq1.d1)
  878. left join tenk1 t2
  879. on (subq1.y1 = t2.unique1)
  880. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  881. -- Here's a variant that we can't fold too aggressively, though,
  882. -- or we end up with noplace to evaluate the lateral PHV
  883. explain (verbose, costs off)
  884. select * from
  885. (select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
  886. lateral (select ss2.y as z limit 1) ss3;
  887. select * from
  888. (select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
  889. lateral (select ss2.y as z limit 1) ss3;
  890. -- Test proper handling of appendrel PHVs during useless-RTE removal
  891. explain (costs off)
  892. select * from
  893. (select 0 as z) as t1
  894. left join
  895. (select true as a) as t2
  896. on true,
  897. lateral (select true as b
  898. union all
  899. select a as b) as t3
  900. where b;
  901. select * from
  902. (select 0 as z) as t1
  903. left join
  904. (select true as a) as t2
  905. on true,
  906. lateral (select true as b
  907. union all
  908. select a as b) as t3
  909. where b;
  910. --
  911. -- test inlining of immutable functions
  912. --
  913. create function f_immutable_int4(i integer) returns integer as
  914. $$ begin return i; end; $$ language plpgsql immutable;
  915. -- check optimization of function scan with join
  916. explain (costs off)
  917. select unique1 from tenk1, (select * from f_immutable_int4(1) x) x
  918. where x = unique1;
  919. explain (verbose, costs off)
  920. select unique1, x.*
  921. from tenk1, (select *, random() from f_immutable_int4(1) x) x
  922. where x = unique1;
  923. explain (costs off)
  924. select unique1 from tenk1, f_immutable_int4(1) x where x = unique1;
  925. explain (costs off)
  926. select unique1 from tenk1, lateral f_immutable_int4(1) x where x = unique1;
  927. explain (costs off)
  928. select unique1 from tenk1, lateral f_immutable_int4(1) x where x in (select 17);
  929. explain (costs off)
  930. select unique1, x from tenk1 join f_immutable_int4(1) x on unique1 = x;
  931. explain (costs off)
  932. select unique1, x from tenk1 left join f_immutable_int4(1) x on unique1 = x;
  933. explain (costs off)
  934. select unique1, x from tenk1 right join f_immutable_int4(1) x on unique1 = x;
  935. explain (costs off)
  936. select unique1, x from tenk1 full join f_immutable_int4(1) x on unique1 = x;
  937. -- check that pullup of a const function allows further const-folding
  938. explain (costs off)
  939. select unique1 from tenk1, f_immutable_int4(1) x where x = 42;
  940. -- test inlining of immutable functions with PlaceHolderVars
  941. explain (costs off)
  942. select nt3.id
  943. from nt3 as nt3
  944. left join
  945. (select nt2.*, (nt2.b1 or i4 = 42) AS b3
  946. from nt2 as nt2
  947. left join
  948. f_immutable_int4(0) i4
  949. on i4 = nt2.nt1_id
  950. ) as ss2
  951. on ss2.id = nt3.nt2_id
  952. where nt3.id = 1 and ss2.b3;
  953. drop function f_immutable_int4(int);
  954. -- test inlining when function returns composite
  955. create function mki8(bigint, bigint) returns int8_tbl as
  956. $$select row($1,$2)::int8_tbl$$ language sql;
  957. create function mki4(int) returns int4_tbl as
  958. $$select row($1)::int4_tbl$$ language sql;
  959. explain (verbose, costs off)
  960. select * from mki8(1,2);
  961. select * from mki8(1,2);
  962. explain (verbose, costs off)
  963. select * from mki4(42);
  964. select * from mki4(42);
  965. drop function mki8(bigint, bigint);
  966. drop function mki4(int);
  967. --
  968. -- test extraction of restriction OR clauses from join OR clause
  969. -- (we used to only do this for indexable clauses)
  970. --
  971. explain (costs off)
  972. select * from tenk1 a join tenk1 b on
  973. (a.unique1 = 1 and b.unique1 = 2) or (a.unique2 = 3 and b.hundred = 4);
  974. explain (costs off)
  975. select * from tenk1 a join tenk1 b on
  976. (a.unique1 = 1 and b.unique1 = 2) or (a.unique2 = 3 and b.ten = 4);
  977. explain (costs off)
  978. select * from tenk1 a join tenk1 b on
  979. (a.unique1 = 1 and b.unique1 = 2) or
  980. ((a.unique2 = 3 or a.unique2 = 7) and b.hundred = 4);
  981. --
  982. -- test placement of movable quals in a parameterized join tree
  983. --
  984. explain (costs off)
  985. select * from tenk1 t1 left join
  986. (tenk1 t2 join tenk1 t3 on t2.thousand = t3.unique2)
  987. on t1.hundred = t2.hundred and t1.ten = t3.ten
  988. where t1.unique1 = 1;
  989. explain (costs off)
  990. select * from tenk1 t1 left join
  991. (tenk1 t2 join tenk1 t3 on t2.thousand = t3.unique2)
  992. on t1.hundred = t2.hundred and t1.ten + t2.ten = t3.ten
  993. where t1.unique1 = 1;
  994. explain (costs off)
  995. select count(*) from
  996. tenk1 a join tenk1 b on a.unique1 = b.unique2
  997. left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
  998. join int4_tbl on b.thousand = f1;
  999. select count(*) from
  1000. tenk1 a join tenk1 b on a.unique1 = b.unique2
  1001. left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
  1002. join int4_tbl on b.thousand = f1;
  1003. explain (costs off)
  1004. select b.unique1 from
  1005. tenk1 a join tenk1 b on a.unique1 = b.unique2
  1006. left join tenk1 c on b.unique1 = 42 and c.thousand = a.thousand
  1007. join int4_tbl i1 on b.thousand = f1
  1008. right join int4_tbl i2 on i2.f1 = b.tenthous
  1009. order by 1;
  1010. select b.unique1 from
  1011. tenk1 a join tenk1 b on a.unique1 = b.unique2
  1012. left join tenk1 c on b.unique1 = 42 and c.thousand = a.thousand
  1013. join int4_tbl i1 on b.thousand = f1
  1014. right join int4_tbl i2 on i2.f1 = b.tenthous
  1015. order by 1;
  1016. explain (costs off)
  1017. select * from
  1018. (
  1019. select unique1, q1, coalesce(unique1, -1) + q1 as fault
  1020. from int8_tbl left join tenk1 on (q2 = unique2)
  1021. ) ss
  1022. where fault = 122
  1023. order by fault;
  1024. select * from
  1025. (
  1026. select unique1, q1, coalesce(unique1, -1) + q1 as fault
  1027. from int8_tbl left join tenk1 on (q2 = unique2)
  1028. ) ss
  1029. where fault = 122
  1030. order by fault;
  1031. explain (costs off)
  1032. select * from
  1033. (values (1, array[10,20]), (2, array[20,30])) as v1(v1x,v1ys)
  1034. left join (values (1, 10), (2, 20)) as v2(v2x,v2y) on v2x = v1x
  1035. left join unnest(v1ys) as u1(u1y) on u1y = v2y;
  1036. select * from
  1037. (values (1, array[10,20]), (2, array[20,30])) as v1(v1x,v1ys)
  1038. left join (values (1, 10), (2, 20)) as v2(v2x,v2y) on v2x = v1x
  1039. left join unnest(v1ys) as u1(u1y) on u1y = v2y;
  1040. --
  1041. -- test handling of potential equivalence clauses above outer joins
  1042. --
  1043. explain (costs off)
  1044. select q1, unique2, thousand, hundred
  1045. from int8_tbl a left join tenk1 b on q1 = unique2
  1046. where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
  1047. select q1, unique2, thousand, hundred
  1048. from int8_tbl a left join tenk1 b on q1 = unique2
  1049. where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
  1050. explain (costs off)
  1051. select f1, unique2, case when unique2 is null then f1 else 0 end
  1052. from int4_tbl a left join tenk1 b on f1 = unique2
  1053. where (case when unique2 is null then f1 else 0 end) = 0;
  1054. select f1, unique2, case when unique2 is null then f1 else 0 end
  1055. from int4_tbl a left join tenk1 b on f1 = unique2
  1056. where (case when unique2 is null then f1 else 0 end) = 0;
  1057. --
  1058. -- another case with equivalence clauses above outer joins (bug #8591)
  1059. --
  1060. explain (costs off)
  1061. select a.unique1, b.unique1, c.unique1, coalesce(b.twothousand, a.twothousand)
  1062. from tenk1 a left join tenk1 b on b.thousand = a.unique1 left join tenk1 c on c.unique2 = coalesce(b.twothousand, a.twothousand)
  1063. where a.unique2 < 10 and coalesce(b.twothousand, a.twothousand) = 44;
  1064. select a.unique1, b.unique1, c.unique1, coalesce(b.twothousand, a.twothousand)
  1065. from tenk1 a left join tenk1 b on b.thousand = a.unique1 left join tenk1 c on c.unique2 = coalesce(b.twothousand, a.twothousand)
  1066. where a.unique2 < 10 and coalesce(b.twothousand, a.twothousand) = 44;
  1067. --
  1068. -- check handling of join aliases when flattening multiple levels of subquery
  1069. --
  1070. explain (verbose, costs off)
  1071. select foo1.join_key as foo1_id, foo3.join_key AS foo3_id, bug_field from
  1072. (values (0),(1)) foo1(join_key)
  1073. left join
  1074. (select join_key, bug_field from
  1075. (select ss1.join_key, ss1.bug_field from
  1076. (select f1 as join_key, 666 as bug_field from int4_tbl i1) ss1
  1077. ) foo2
  1078. left join
  1079. (select unique2 as join_key from tenk1 i2) ss2
  1080. using (join_key)
  1081. ) foo3
  1082. using (join_key);
  1083. select foo1.join_key as foo1_id, foo3.join_key AS foo3_id, bug_field from
  1084. (values (0),(1)) foo1(join_key)
  1085. left join
  1086. (select join_key, bug_field from
  1087. (select ss1.join_key, ss1.bug_field from
  1088. (select f1 as join_key, 666 as bug_field from int4_tbl i1) ss1
  1089. ) foo2
  1090. left join
  1091. (select unique2 as join_key from tenk1 i2) ss2
  1092. using (join_key)
  1093. ) foo3
  1094. using (join_key);
  1095. --
  1096. -- test successful handling of nested outer joins with degenerate join quals
  1097. --
  1098. explain (verbose, costs off)
  1099. select t1.* from
  1100. text_tbl t1
  1101. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  1102. left join int8_tbl i8
  1103. left join (select *, null::int as d2 from int8_tbl i8b2) b2
  1104. on (i8.q1 = b2.q1)
  1105. on (b2.d2 = b1.q2)
  1106. on (t1.f1 = b1.d1)
  1107. left join int4_tbl i4
  1108. on (i8.q2 = i4.f1);
  1109. select t1.* from
  1110. text_tbl t1
  1111. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  1112. left join int8_tbl i8
  1113. left join (select *, null::int as d2 from int8_tbl i8b2) b2
  1114. on (i8.q1 = b2.q1)
  1115. on (b2.d2 = b1.q2)
  1116. on (t1.f1 = b1.d1)
  1117. left join int4_tbl i4
  1118. on (i8.q2 = i4.f1);
  1119. explain (verbose, costs off)
  1120. select t1.* from
  1121. text_tbl t1
  1122. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  1123. left join int8_tbl i8
  1124. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2) b2
  1125. on (i8.q1 = b2.q1)
  1126. on (b2.d2 = b1.q2)
  1127. on (t1.f1 = b1.d1)
  1128. left join int4_tbl i4
  1129. on (i8.q2 = i4.f1);
  1130. select t1.* from
  1131. text_tbl t1
  1132. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  1133. left join int8_tbl i8
  1134. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2) b2
  1135. on (i8.q1 = b2.q1)
  1136. on (b2.d2 = b1.q2)
  1137. on (t1.f1 = b1.d1)
  1138. left join int4_tbl i4
  1139. on (i8.q2 = i4.f1);
  1140. explain (verbose, costs off)
  1141. select t1.* from
  1142. text_tbl t1
  1143. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  1144. left join int8_tbl i8
  1145. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2
  1146. where q1 = f1) b2
  1147. on (i8.q1 = b2.q1)
  1148. on (b2.d2 = b1.q2)
  1149. on (t1.f1 = b1.d1)
  1150. left join int4_tbl i4
  1151. on (i8.q2 = i4.f1);
  1152. select t1.* from
  1153. text_tbl t1
  1154. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  1155. left join int8_tbl i8
  1156. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2
  1157. where q1 = f1) b2
  1158. on (i8.q1 = b2.q1)
  1159. on (b2.d2 = b1.q2)
  1160. on (t1.f1 = b1.d1)
  1161. left join int4_tbl i4
  1162. on (i8.q2 = i4.f1);
  1163. explain (verbose, costs off)
  1164. select * from
  1165. text_tbl t1
  1166. inner join int8_tbl i8
  1167. on i8.q2 = 456
  1168. right join text_tbl t2
  1169. on t1.f1 = 'doh!'
  1170. left join int4_tbl i4
  1171. on i8.q1 = i4.f1;
  1172. select * from
  1173. text_tbl t1
  1174. inner join int8_tbl i8
  1175. on i8.q2 = 456
  1176. right join text_tbl t2
  1177. on t1.f1 = 'doh!'
  1178. left join int4_tbl i4
  1179. on i8.q1 = i4.f1;
  1180. --
  1181. -- test for appropriate join order in the presence of lateral references
  1182. --
  1183. explain (verbose, costs off)
  1184. select * from
  1185. text_tbl t1
  1186. left join int8_tbl i8
  1187. on i8.q2 = 123,
  1188. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
  1189. where t1.f1 = ss.f1;
  1190. select * from
  1191. text_tbl t1
  1192. left join int8_tbl i8
  1193. on i8.q2 = 123,
  1194. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
  1195. where t1.f1 = ss.f1;
  1196. explain (verbose, costs off)
  1197. select * from
  1198. text_tbl t1
  1199. left join int8_tbl i8
  1200. on i8.q2 = 123,
  1201. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
  1202. lateral (select ss1.* from text_tbl t3 limit 1) as ss2
  1203. where t1.f1 = ss2.f1;
  1204. select * from
  1205. text_tbl t1
  1206. left join int8_tbl i8
  1207. on i8.q2 = 123,
  1208. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
  1209. lateral (select ss1.* from text_tbl t3 limit 1) as ss2
  1210. where t1.f1 = ss2.f1;
  1211. explain (verbose, costs off)
  1212. select 1 from
  1213. text_tbl as tt1
  1214. inner join text_tbl as tt2 on (tt1.f1 = 'foo')
  1215. left join text_tbl as tt3 on (tt3.f1 = 'foo')
  1216. left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
  1217. lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
  1218. where tt1.f1 = ss1.c0;
  1219. select 1 from
  1220. text_tbl as tt1
  1221. inner join text_tbl as tt2 on (tt1.f1 = 'foo')
  1222. left join text_tbl as tt3 on (tt3.f1 = 'foo')
  1223. left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
  1224. lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
  1225. where tt1.f1 = ss1.c0;
  1226. --
  1227. -- check a case in which a PlaceHolderVar forces join order
  1228. --
  1229. explain (verbose, costs off)
  1230. select ss2.* from
  1231. int4_tbl i41
  1232. left join int8_tbl i8
  1233. join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
  1234. from int4_tbl i42, int4_tbl i43) ss1
  1235. on i8.q1 = ss1.c2
  1236. on i41.f1 = ss1.c1,
  1237. lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
  1238. where ss1.c2 = 0;
  1239. select ss2.* from
  1240. int4_tbl i41
  1241. left join int8_tbl i8
  1242. join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
  1243. from int4_tbl i42, int4_tbl i43) ss1
  1244. on i8.q1 = ss1.c2
  1245. on i41.f1 = ss1.c1,
  1246. lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
  1247. where ss1.c2 = 0;
  1248. --
  1249. -- test successful handling of full join underneath left join (bug #14105)
  1250. --
  1251. explain (costs off)
  1252. select * from
  1253. (select 1 as id) as xx
  1254. left join
  1255. (tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
  1256. on (xx.id = coalesce(yy.id));
  1257. select * from
  1258. (select 1 as id) as xx
  1259. left join
  1260. (tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
  1261. on (xx.id = coalesce(yy.id));
  1262. --
  1263. -- test ability to push constants through outer join clauses
  1264. --
  1265. explain (costs off)
  1266. select * from int4_tbl a left join tenk1 b on f1 = unique2 where f1 = 0;
  1267. explain (costs off)
  1268. select * from tenk1 a full join tenk1 b using(unique2) where unique2 = 42;
  1269. --
  1270. -- test that quals attached to an outer join have correct semantics,
  1271. -- specifically that they don't re-use expressions computed below the join;
  1272. -- we force a mergejoin so that coalesce(b.q1, 1) appears as a join input
  1273. --
  1274. set enable_hashjoin to off;
  1275. set enable_nestloop to off;
  1276. explain (verbose, costs off)
  1277. select a.q2, b.q1
  1278. from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
  1279. where coalesce(b.q1, 1) > 0;
  1280. select a.q2, b.q1
  1281. from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
  1282. where coalesce(b.q1, 1) > 0;
  1283. reset enable_hashjoin;
  1284. reset enable_nestloop;
  1285. --
  1286. -- test join removal
  1287. --
  1288. begin;
  1289. CREATE TEMP TABLE a (id int PRIMARY KEY, b_id int);
  1290. CREATE TEMP TABLE b (id int PRIMARY KEY, c_id int);
  1291. CREATE TEMP TABLE c (id int PRIMARY KEY);
  1292. CREATE TEMP TABLE d (a int, b int);
  1293. INSERT INTO a VALUES (0, 0), (1, NULL);
  1294. INSERT INTO b VALUES (0, 0), (1, NULL);
  1295. INSERT INTO c VALUES (0), (1);
  1296. INSERT INTO d VALUES (1,3), (2,2), (3,1);
  1297. -- all three cases should be optimizable into a simple seqscan
  1298. explain (costs off) SELECT a.* FROM a LEFT JOIN b ON a.b_id = b.id;
  1299. explain (costs off) SELECT b.* FROM b LEFT JOIN c ON b.c_id = c.id;
  1300. explain (costs off)
  1301. SELECT a.* FROM a LEFT JOIN (b left join c on b.c_id = c.id)
  1302. ON (a.b_id = b.id);
  1303. -- check optimization of outer join within another special join
  1304. explain (costs off)
  1305. select id from a where id in (
  1306. select b.id from b left join c on b.id = c.id
  1307. );
  1308. -- check that join removal works for a left join when joining a subquery
  1309. -- that is guaranteed to be unique by its GROUP BY clause
  1310. explain (costs off)
  1311. select d.* from d left join (select * from b group by b.id, b.c_id) s
  1312. on d.a = s.id and d.b = s.c_id;
  1313. -- similarly, but keying off a DISTINCT clause
  1314. explain (costs off)
  1315. select d.* from d left join (select distinct * from b) s
  1316. on d.a = s.id and d.b = s.c_id;
  1317. -- join removal is not possible when the GROUP BY contains a column that is
  1318. -- not in the join condition. (Note: as of 9.6, we notice that b.id is a
  1319. -- primary key and so drop b.c_id from the GROUP BY of the resulting plan;
  1320. -- but this happens too late for join removal in the outer plan level.)
  1321. explain (costs off)
  1322. select d.* from d left join (select * from b group by b.id, b.c_id) s
  1323. on d.a = s.id;
  1324. -- similarly, but keying off a DISTINCT clause
  1325. explain (costs off)
  1326. select d.* from d left join (select distinct * from b) s
  1327. on d.a = s.id;
  1328. -- check join removal works when uniqueness of the join condition is enforced
  1329. -- by a UNION
  1330. explain (costs off)
  1331. select d.* from d left join (select id from a union select id from b) s
  1332. on d.a = s.id;
  1333. -- check join removal with a cross-type comparison operator
  1334. explain (costs off)
  1335. select i8.* from int8_tbl i8 left join (select f1 from int4_tbl group by f1) i4
  1336. on i8.q1 = i4.f1;
  1337. -- check join removal with lateral references
  1338. explain (costs off)
  1339. select 1 from (select a.id FROM a left join b on a.b_id = b.id) q,
  1340. lateral generate_series(1, q.id) gs(i) where q.id = gs.i;
  1341. rollback;
  1342. create temp table parent (k int primary key, pd int);
  1343. create temp table child (k int unique, cd int);
  1344. insert into parent values (1, 10), (2, 20), (3, 30);
  1345. insert into child values (1, 100), (4, 400);
  1346. -- this case is optimizable
  1347. select p.* from parent p left join child c on (p.k = c.k);
  1348. explain (costs off)
  1349. select p.* from parent p left join child c on (p.k = c.k);
  1350. -- this case is not
  1351. select p.*, linked from parent p
  1352. left join (select c.*, true as linked from child c) as ss
  1353. on (p.k = ss.k);
  1354. explain (costs off)
  1355. select p.*, linked from parent p
  1356. left join (select c.*, true as linked from child c) as ss
  1357. on (p.k = ss.k);
  1358. -- check for a 9.0rc1 bug: join removal breaks pseudoconstant qual handling
  1359. select p.* from
  1360. parent p left join child c on (p.k = c.k)
  1361. where p.k = 1 and p.k = 2;
  1362. explain (costs off)
  1363. select p.* from
  1364. parent p left join child c on (p.k = c.k)
  1365. where p.k = 1 and p.k = 2;
  1366. select p.* from
  1367. (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
  1368. where p.k = 1 and p.k = 2;
  1369. explain (costs off)
  1370. select p.* from
  1371. (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
  1372. where p.k = 1 and p.k = 2;
  1373. -- bug 5255: this is not optimizable by join removal
  1374. begin;
  1375. CREATE TEMP TABLE a (id int PRIMARY KEY);
  1376. CREATE TEMP TABLE b (id int PRIMARY KEY, a_id int);
  1377. INSERT INTO a VALUES (0), (1);
  1378. INSERT INTO b VALUES (0, 0), (1, NULL);
  1379. SELECT * FROM b LEFT JOIN a ON (b.a_id = a.id) WHERE (a.id IS NULL OR a.id > 0);
  1380. SELECT b.* FROM b LEFT JOIN a ON (b.a_id = a.id) WHERE (a.id IS NULL OR a.id > 0);
  1381. rollback;
  1382. -- another join removal bug: this is not optimizable, either
  1383. begin;
  1384. create temp table innertab (id int8 primary key, dat1 int8);
  1385. insert into innertab values(123, 42);
  1386. SELECT * FROM
  1387. (SELECT 1 AS x) ss1
  1388. LEFT JOIN
  1389. (SELECT q1, q2, COALESCE(dat1, q1) AS y
  1390. FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss2
  1391. ON true;
  1392. rollback;
  1393. -- another join removal bug: we must clean up correctly when removing a PHV
  1394. begin;
  1395. create temp table uniquetbl (f1 text unique);
  1396. explain (costs off)
  1397. select t1.* from
  1398. uniquetbl as t1
  1399. left join (select *, '***'::text as d1 from uniquetbl) t2
  1400. on t1.f1 = t2.f1
  1401. left join uniquetbl t3
  1402. on t2.d1 = t3.f1;
  1403. explain (costs off)
  1404. select t0.*
  1405. from
  1406. text_tbl t0
  1407. left join
  1408. (select case t1.ten when 0 then 'doh!'::text else null::text end as case1,
  1409. t1.stringu2
  1410. from tenk1 t1
  1411. join int4_tbl i4 ON i4.f1 = t1.unique2
  1412. left join uniquetbl u1 ON u1.f1 = t1.string4) ss
  1413. on t0.f1 = ss.case1
  1414. where ss.stringu2 !~* ss.case1;
  1415. select t0.*
  1416. from
  1417. text_tbl t0
  1418. left join
  1419. (select case t1.ten when 0 then 'doh!'::text else null::text end as case1,
  1420. t1.stringu2
  1421. from tenk1 t1
  1422. join int4_tbl i4 ON i4.f1 = t1.unique2
  1423. left join uniquetbl u1 ON u1.f1 = t1.string4) ss
  1424. on t0.f1 = ss.case1
  1425. where ss.stringu2 !~* ss.case1;
  1426. rollback;
  1427. -- test case to expose miscomputation of required relid set for a PHV
  1428. explain (verbose, costs off)
  1429. select i8.*, ss.v, t.unique2
  1430. from int8_tbl i8
  1431. left join int4_tbl i4 on i4.f1 = 1
  1432. left join lateral (select i4.f1 + 1 as v) as ss on true
  1433. left join tenk1 t on t.unique2 = ss.v
  1434. where q2 = 456;
  1435. select i8.*, ss.v, t.unique2
  1436. from int8_tbl i8
  1437. left join int4_tbl i4 on i4.f1 = 1
  1438. left join lateral (select i4.f1 + 1 as v) as ss on true
  1439. left join tenk1 t on t.unique2 = ss.v
  1440. where q2 = 456;
  1441. -- and check a related issue where we miscompute required relids for
  1442. -- a PHV that's been translated to a child rel
  1443. create temp table parttbl (a integer primary key) partition by range (a);
  1444. create temp table parttbl1 partition of parttbl for values from (1) to (100);
  1445. insert into parttbl values (11), (12);
  1446. explain (costs off)
  1447. select * from
  1448. (select *, 12 as phv from parttbl) as ss
  1449. right join int4_tbl on true
  1450. where ss.a = ss.phv and f1 = 0;
  1451. select * from
  1452. (select *, 12 as phv from parttbl) as ss
  1453. right join int4_tbl on true
  1454. where ss.a = ss.phv and f1 = 0;
  1455. -- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
  1456. select * from
  1457. int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
  1458. select * from
  1459. int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
  1460. select * from
  1461. int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
  1462. --
  1463. -- Test hints given on incorrect column references are useful
  1464. --
  1465. select t1.uunique1 from
  1466. tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t1" suggestion
  1467. select t2.uunique1 from
  1468. tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t2" suggestion
  1469. select uunique1 from
  1470. tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, suggest both at once
  1471. --
  1472. -- Take care to reference the correct RTE
  1473. --
  1474. select atts.relid::regclass, s.* from pg_stats s join
  1475. pg_attribute a on s.attname = a.attname and s.tablename =
  1476. a.attrelid::regclass::text join (select unnest(indkey) attnum,
  1477. indexrelid from pg_index i) atts on atts.attnum = a.attnum where
  1478. schemaname != 'pg_catalog';
  1479. --
  1480. -- Test LATERAL
  1481. --
  1482. select unique2, x.*
  1483. from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
  1484. explain (costs off)
  1485. select unique2, x.*
  1486. from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
  1487. select unique2, x.*
  1488. from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
  1489. explain (costs off)
  1490. select unique2, x.*
  1491. from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
  1492. explain (costs off)
  1493. select unique2, x.*
  1494. from int4_tbl x cross join lateral (select unique2 from tenk1 where f1 = unique1) ss;
  1495. select unique2, x.*
  1496. from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
  1497. explain (costs off)
  1498. select unique2, x.*
  1499. from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
  1500. -- check scoping of lateral versus parent references
  1501. -- the first of these should return int8_tbl.q2, the second int8_tbl.q1
  1502. select *, (select r from (select q1 as q2) x, (select q2 as r) y) from int8_tbl;
  1503. select *, (select r from (select q1 as q2) x, lateral (select q2 as r) y) from int8_tbl;
  1504. -- lateral with function in FROM
  1505. select count(*) from tenk1 a, lateral generate_series(1,two) g;
  1506. explain (costs off)
  1507. select count(*) from tenk1 a, lateral generate_series(1,two) g;
  1508. explain (costs off)
  1509. select count(*) from tenk1 a cross join lateral generate_series(1,two) g;
  1510. -- don't need the explicit LATERAL keyword for functions
  1511. explain (costs off)
  1512. select count(*) from tenk1 a, generate_series(1,two) g;
  1513. -- lateral with UNION ALL subselect
  1514. explain (costs off)
  1515. select * from generate_series(100,200) g,
  1516. lateral (select * from int8_tbl a where g = q1 union all
  1517. select * from int8_tbl b where g = q2) ss;
  1518. select * from generate_series(100,200) g,
  1519. lateral (select * from int8_tbl a where g = q1 union all
  1520. select * from int8_tbl b where g = q2) ss;
  1521. -- lateral with VALUES
  1522. explain (costs off)
  1523. select count(*) from tenk1 a,
  1524. tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
  1525. select count(*) from tenk1 a,
  1526. tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
  1527. -- lateral with VALUES, no flattening possible
  1528. explain (costs off)
  1529. select count(*) from tenk1 a,
  1530. tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
  1531. select count(*) from tenk1 a,
  1532. tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
  1533. -- lateral injecting a strange outer join condition
  1534. explain (costs off)
  1535. select * from int8_tbl a,
  1536. int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
  1537. on x.q2 = ss.z
  1538. order by a.q1, a.q2, x.q1, x.q2, ss.z;
  1539. select * from int8_tbl a,
  1540. int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
  1541. on x.q2 = ss.z
  1542. order by a.q1, a.q2, x.q1, x.q2, ss.z;
  1543. -- lateral reference to a join alias variable
  1544. select * from (select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
  1545. lateral (select x) ss2(y);
  1546. select * from (select f1 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
  1547. lateral (values(x)) ss2(y);
  1548. select * from ((select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1) j,
  1549. lateral (select x) ss2(y);
  1550. -- lateral references requiring pullup
  1551. select * from (values(1)) x(lb),
  1552. lateral generate_series(lb,4) x4;
  1553. select * from (select f1/1000000000 from int4_tbl) x(lb),
  1554. lateral generate_series(lb,4) x4;
  1555. select * from (values(1)) x(lb),
  1556. lateral (values(lb)) y(lbcopy);
  1557. select * from (values(1)) x(lb),
  1558. lateral (select lb from int4_tbl) y(lbcopy);
  1559. select * from
  1560. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  1561. lateral (values(x.q1,y.q1,y.q2)) v(xq1,yq1,yq2);
  1562. select * from
  1563. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  1564. lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
  1565. select x.* from
  1566. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  1567. lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
  1568. select v.* from
  1569. (int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1)
  1570. left join int4_tbl z on z.f1 = x.q2,
  1571. lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
  1572. select v.* from
  1573. (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
  1574. left join int4_tbl z on z.f1 = x.q2,
  1575. lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
  1576. select v.* from
  1577. (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
  1578. left join int4_tbl z on z.f1 = x.q2,
  1579. lateral (select x.q1,y.q1 from onerow union all select x.q2,y.q2 from onerow) v(vx,vy);
  1580. explain (verbose, costs off)
  1581. select * from
  1582. int8_tbl a left join
  1583. lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
  1584. select * from
  1585. int8_tbl a left join
  1586. lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
  1587. explain (verbose, costs off)
  1588. select * from
  1589. int8_tbl a left join
  1590. lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
  1591. select * from
  1592. int8_tbl a left join
  1593. lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
  1594. -- lateral can result in join conditions appearing below their
  1595. -- real semantic level
  1596. explain (verbose, costs off)
  1597. select * from int4_tbl i left join
  1598. lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
  1599. select * from int4_tbl i left join
  1600. lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
  1601. explain (verbose, costs off)
  1602. select * from int4_tbl i left join
  1603. lateral (select coalesce(i) from int2_tbl j where i.f1 = j.f1) k on true;
  1604. select * from int4_tbl i left join
  1605. lateral (select coalesce(i) from int2_tbl j where i.f1 = j.f1) k on true;
  1606. explain (verbose, costs off)
  1607. select * from int4_tbl a,
  1608. lateral (
  1609. select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
  1610. ) ss;
  1611. select * from int4_tbl a,
  1612. lateral (
  1613. select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
  1614. ) ss;
  1615. -- lateral reference in a PlaceHolderVar evaluated at join level
  1616. explain (verbose, costs off)
  1617. select * from
  1618. int8_tbl a left join lateral
  1619. (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
  1620. int8_tbl b cross join int8_tbl c) ss
  1621. on a.q2 = ss.bq1;
  1622. select * from
  1623. int8_tbl a left join lateral
  1624. (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
  1625. int8_tbl b cross join int8_tbl c) ss
  1626. on a.q2 = ss.bq1;
  1627. -- case requiring nested PlaceHolderVars
  1628. explain (verbose, costs off)
  1629. select * from
  1630. int8_tbl c left join (
  1631. int8_tbl a left join (select q1, coalesce(q2,42) as x from int8_tbl b) ss1
  1632. on a.q2 = ss1.q1
  1633. cross join
  1634. lateral (select q1, coalesce(ss1.x,q2) as y from int8_tbl d) ss2
  1635. ) on c.q2 = ss2.q1,
  1636. lateral (select ss2.y offset 0) ss3;
  1637. -- case that breaks the old ph_may_need optimization
  1638. explain (verbose, costs off)
  1639. select c.*,a.*,ss1.q1,ss2.q1,ss3.* from
  1640. int8_tbl c left join (
  1641. int8_tbl a left join
  1642. (select q1, coalesce(q2,f1) as x from int8_tbl b, int4_tbl b2
  1643. where q1 < f1) ss1
  1644. on a.q2 = ss1.q1
  1645. cross join
  1646. lateral (select q1, coalesce(ss1.x,q2) as y from int8_tbl d) ss2
  1647. ) on c.q2 = ss2.q1,
  1648. lateral (select * from int4_tbl i where ss2.y > f1) ss3;
  1649. -- check processing of postponed quals (bug #9041)
  1650. explain (verbose, costs off)
  1651. select * from
  1652. (select 1 as x offset 0) x cross join (select 2 as y offset 0) y
  1653. left join lateral (
  1654. select * from (select 3 as z offset 0) z where z.z = x.x
  1655. ) zz on zz.z = y.y;
  1656. -- check dummy rels with lateral references (bug #15694)
  1657. explain (verbose, costs off)
  1658. select * from int8_tbl i8 left join lateral
  1659. (select *, i8.q2 from int4_tbl where false) ss on true;
  1660. explain (verbose, costs off)
  1661. select * from int8_tbl i8 left join lateral
  1662. (select *, i8.q2 from int4_tbl i1, int4_tbl i2 where false) ss on true;
  1663. -- check handling of nested appendrels inside LATERAL
  1664. select * from
  1665. ((select 2 as v) union all (select 3 as v)) as q1
  1666. cross join lateral
  1667. ((select * from
  1668. ((select 4 as v) union all (select 5 as v)) as q3)
  1669. union all
  1670. (select q1.v)
  1671. ) as q2;
  1672. -- check the number of columns specified
  1673. SELECT * FROM (int8_tbl i cross join int4_tbl j) ss(a,b,c,d);
  1674. -- check we don't try to do a unique-ified semijoin with LATERAL
  1675. explain (verbose, costs off)
  1676. select * from
  1677. (values (0,9998), (1,1000)) v(id,x),
  1678. lateral (select f1 from int4_tbl
  1679. where f1 = any (select unique1 from tenk1
  1680. where unique2 = v.x offset 0)) ss;
  1681. select * from
  1682. (values (0,9998), (1,1000)) v(id,x),
  1683. lateral (select f1 from int4_tbl
  1684. where f1 = any (select unique1 from tenk1
  1685. where unique2 = v.x offset 0)) ss;
  1686. -- check proper extParam/allParam handling (this isn't exactly a LATERAL issue,
  1687. -- but we can make the test case much more compact with LATERAL)
  1688. explain (verbose, costs off)
  1689. select * from (values (0), (1)) v(id),
  1690. lateral (select * from int8_tbl t1,
  1691. lateral (select * from
  1692. (select * from int8_tbl t2
  1693. where q1 = any (select q2 from int8_tbl t3
  1694. where q2 = (select greatest(t1.q1,t2.q2))
  1695. and (select v.id=0)) offset 0) ss2) ss
  1696. where t1.q1 = ss.q2) ss0;
  1697. select * from (values (0), (1)) v(id),
  1698. lateral (select * from int8_tbl t1,
  1699. lateral (select * from
  1700. (select * from int8_tbl t2
  1701. where q1 = any (select q2 from int8_tbl t3
  1702. where q2 = (select greatest(t1.q1,t2.q2))
  1703. and (select v.id=0)) offset 0) ss2) ss
  1704. where t1.q1 = ss.q2) ss0;
  1705. -- test some error cases where LATERAL should have been used but wasn't
  1706. select f1,g from int4_tbl a, (select f1 as g) ss;
  1707. select f1,g from int4_tbl a, (select a.f1 as g) ss;
  1708. select f1,g from int4_tbl a cross join (select f1 as g) ss;
  1709. select f1,g from int4_tbl a cross join (select a.f1 as g) ss;
  1710. -- SQL:2008 says the left table is in scope but illegal to access here
  1711. select f1,g from int4_tbl a right join lateral generate_series(0, a.f1) g on true;
  1712. select f1,g from int4_tbl a full join lateral generate_series(0, a.f1) g on true;
  1713. -- check we complain about ambiguous table references
  1714. select * from
  1715. int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
  1716. -- LATERAL can be used to put an aggregate into the FROM clause of its query
  1717. select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;
  1718. -- check behavior of LATERAL in UPDATE/DELETE
  1719. create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl;
  1720. -- error, can't do this:
  1721. update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
  1722. update xx1 set x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss;
  1723. -- can't do it even with LATERAL:
  1724. update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss;
  1725. -- we might in future allow something like this, but for now it's an error:
  1726. update xx1 set x2 = f1 from xx1, lateral (select * from int4_tbl where f1 = x1) ss;
  1727. -- also errors:
  1728. delete from xx1 using (select * from int4_tbl where f1 = x1) ss;
  1729. delete from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss;
  1730. delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
  1731. --
  1732. -- test LATERAL reference propagation down a multi-level inheritance hierarchy
  1733. -- produced for a multi-level partitioned table hierarchy.
  1734. --
  1735. create table join_pt1 (a int, b int, c varchar) partition by range(a);
  1736. create table join_pt1p1 partition of join_pt1 for values from (0) to (100) partition by range(b);
  1737. create table join_pt1p2 partition of join_pt1 for values from (100) to (200);
  1738. create table join_pt1p1p1 partition of join_pt1p1 for values from (0) to (100);
  1739. insert into join_pt1 values (1, 1, 'x'), (101, 101, 'y');
  1740. create table join_ut1 (a int, b int, c varchar);
  1741. insert into join_ut1 values (101, 101, 'y'), (2, 2, 'z');
  1742. explain (verbose, costs off)
  1743. select t1.b, ss.phv from join_ut1 t1 left join lateral
  1744. (select t2.a as t2a, t3.a t3a, least(t1.a, t2.a, t3.a) phv
  1745. from join_pt1 t2 join join_ut1 t3 on t2.a = t3.b) ss
  1746. on t1.a = ss.t2a order by t1.a;
  1747. select t1.b, ss.phv from join_ut1 t1 left join lateral
  1748. (select t2.a as t2a, t3.a t3a, least(t1.a, t2.a, t3.a) phv
  1749. from join_pt1 t2 join join_ut1 t3 on t2.a = t3.b) ss
  1750. on t1.a = ss.t2a order by t1.a;
  1751. drop table join_pt1;
  1752. drop table join_ut1;
  1753. --
  1754. -- test estimation behavior with multi-column foreign key and constant qual
  1755. --
  1756. begin;
  1757. create table fkest (x integer, x10 integer, x10b integer, x100 integer);
  1758. insert into fkest select x, x/10, x/10, x/100 from generate_series(1,1000) x;
  1759. create unique index on fkest(x, x10, x100);
  1760. analyze fkest;
  1761. explain (costs off)
  1762. select * from fkest f1
  1763. join fkest f2 on (f1.x = f2.x and f1.x10 = f2.x10b and f1.x100 = f2.x100)
  1764. join fkest f3 on f1.x = f3.x
  1765. where f1.x100 = 2;
  1766. alter table fkest add constraint fk
  1767. foreign key (x, x10b, x100) references fkest (x, x10, x100);
  1768. explain (costs off)
  1769. select * from fkest f1
  1770. join fkest f2 on (f1.x = f2.x and f1.x10 = f2.x10b and f1.x100 = f2.x100)
  1771. join fkest f3 on f1.x = f3.x
  1772. where f1.x100 = 2;
  1773. rollback;
  1774. --
  1775. -- test that foreign key join estimation performs sanely for outer joins
  1776. --
  1777. begin;
  1778. create table fkest (a int, b int, c int unique, primary key(a,b));
  1779. create table fkest1 (a int, b int, primary key(a,b));
  1780. insert into fkest select x/10, x%10, x from generate_series(1,1000) x;
  1781. insert into fkest1 select x/10, x%10 from generate_series(1,1000) x;
  1782. alter table fkest1
  1783. add constraint fkest1_a_b_fkey foreign key (a,b) references fkest;
  1784. analyze fkest;
  1785. analyze fkest1;
  1786. explain (costs off)
  1787. select *
  1788. from fkest f
  1789. left join fkest1 f1 on f.a = f1.a and f.b = f1.b
  1790. left join fkest1 f2 on f.a = f2.a and f.b = f2.b
  1791. left join fkest1 f3 on f.a = f3.a and f.b = f3.b
  1792. where f.c = 1;
  1793. rollback;
  1794. --
  1795. -- test planner's ability to mark joins as unique
  1796. --
  1797. create table j1 (id int primary key);
  1798. create table j2 (id int primary key);
  1799. create table j3 (id int);
  1800. insert into j1 values(1),(2),(3);
  1801. insert into j2 values(1),(2),(3);
  1802. insert into j3 values(1),(1);
  1803. analyze j1;
  1804. analyze j2;
  1805. analyze j3;
  1806. -- ensure join is properly marked as unique
  1807. explain (verbose, costs off)
  1808. select * from j1 inner join j2 on j1.id = j2.id;
  1809. -- ensure join is not unique when not an equi-join
  1810. explain (verbose, costs off)
  1811. select * from j1 inner join j2 on j1.id > j2.id;
  1812. -- ensure non-unique rel is not chosen as inner
  1813. explain (verbose, costs off)
  1814. select * from j1 inner join j3 on j1.id = j3.id;
  1815. -- ensure left join is marked as unique
  1816. explain (verbose, costs off)
  1817. select * from j1 left join j2 on j1.id = j2.id;
  1818. -- ensure right join is marked as unique
  1819. explain (verbose, costs off)
  1820. select * from j1 right join j2 on j1.id = j2.id;
  1821. -- ensure full join is marked as unique
  1822. explain (verbose, costs off)
  1823. select * from j1 full join j2 on j1.id = j2.id;
  1824. -- a clauseless (cross) join can't be unique
  1825. explain (verbose, costs off)
  1826. select * from j1 cross join j2;
  1827. -- ensure a natural join is marked as unique
  1828. explain (verbose, costs off)
  1829. select * from j1 natural join j2;
  1830. -- ensure a distinct clause allows the inner to become unique
  1831. explain (verbose, costs off)
  1832. select * from j1
  1833. inner join (select distinct id from j3) j3 on j1.id = j3.id;
  1834. -- ensure group by clause allows the inner to become unique
  1835. explain (verbose, costs off)
  1836. select * from j1
  1837. inner join (select id from j3 group by id) j3 on j1.id = j3.id;
  1838. drop table j1;
  1839. drop table j2;
  1840. drop table j3;
  1841. -- test more complex permutations of unique joins
  1842. create table j1 (id1 int, id2 int, primary key(id1,id2));
  1843. create table j2 (id1 int, id2 int, primary key(id1,id2));
  1844. create table j3 (id1 int, id2 int, primary key(id1,id2));
  1845. insert into j1 values(1,1),(1,2);
  1846. insert into j2 values(1,1);
  1847. insert into j3 values(1,1);
  1848. analyze j1;
  1849. analyze j2;
  1850. analyze j3;
  1851. -- ensure there's no unique join when not all columns which are part of the
  1852. -- unique index are seen in the join clause
  1853. explain (verbose, costs off)
  1854. select * from j1
  1855. inner join j2 on j1.id1 = j2.id1;
  1856. -- ensure proper unique detection with multiple join quals
  1857. explain (verbose, costs off)
  1858. select * from j1
  1859. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2;
  1860. -- ensure we don't detect the join to be unique when quals are not part of the
  1861. -- join condition
  1862. explain (verbose, costs off)
  1863. select * from j1
  1864. inner join j2 on j1.id1 = j2.id1 where j1.id2 = 1;
  1865. -- as above, but for left joins.
  1866. explain (verbose, costs off)
  1867. select * from j1
  1868. left join j2 on j1.id1 = j2.id1 where j1.id2 = 1;
  1869. -- validate logic in merge joins which skips mark and restore.
  1870. -- it should only do this if all quals which were used to detect the unique
  1871. -- are present as join quals, and not plain quals.
  1872. set enable_nestloop to 0;
  1873. set enable_hashjoin to 0;
  1874. set enable_sort to 0;
  1875. -- create indexes that will be preferred over the PKs to perform the join
  1876. create index j1_id1_idx on j1 (id1) where id1 % 1000 = 1;
  1877. create index j2_id1_idx on j2 (id1) where id1 % 1000 = 1;
  1878. -- need an additional row in j2, if we want j2_id1_idx to be preferred
  1879. insert into j2 values(1,2);
  1880. analyze j2;
  1881. explain (costs off) select * from j1
  1882. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  1883. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1;
  1884. select * from j1
  1885. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  1886. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1;
  1887. -- Exercise array keys mark/restore B-Tree code
  1888. explain (costs off) select * from j1
  1889. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  1890. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 = any (array[1]);
  1891. select * from j1
  1892. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  1893. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 = any (array[1]);
  1894. -- Exercise array keys "find extreme element" B-Tree code
  1895. explain (costs off) select * from j1
  1896. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  1897. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 >= any (array[1,5]);
  1898. select * from j1
  1899. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  1900. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 >= any (array[1,5]);
  1901. reset enable_nestloop;
  1902. reset enable_hashjoin;
  1903. reset enable_sort;
  1904. drop table j1;
  1905. drop table j2;
  1906. drop table j3;
  1907. -- check that semijoin inner is not seen as unique for a portion of the outerrel
  1908. explain (verbose, costs off)
  1909. select t1.unique1, t2.hundred
  1910. from onek t1, tenk1 t2
  1911. where exists (select 1 from tenk1 t3
  1912. where t3.thousand = t1.unique1 and t3.tenthous = t2.hundred)
  1913. and t1.unique1 < 1;
  1914. -- ... unless it actually is unique
  1915. create table j3 as select unique1, tenthous from onek;
  1916. vacuum analyze j3;
  1917. create unique index on j3(unique1, tenthous);
  1918. explain (verbose, costs off)
  1919. select t1.unique1, t2.hundred
  1920. from onek t1, tenk1 t2
  1921. where exists (select 1 from j3
  1922. where j3.unique1 = t1.unique1 and j3.tenthous = t2.hundred)
  1923. and t1.unique1 < 1;
  1924. drop table j3;