join.out 210 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628
  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. i | j | t
  46. ---+---+-------
  47. 1 | 4 | one
  48. 2 | 3 | two
  49. 3 | 2 | three
  50. 4 | 1 | four
  51. 5 | 0 | five
  52. 6 | 6 | six
  53. 7 | 7 | seven
  54. 8 | 8 | eight
  55. 0 | | zero
  56. | | null
  57. | 0 | zero
  58. (11 rows)
  59. SELECT *
  60. FROM J1_TBL tx;
  61. i | j | t
  62. ---+---+-------
  63. 1 | 4 | one
  64. 2 | 3 | two
  65. 3 | 2 | three
  66. 4 | 1 | four
  67. 5 | 0 | five
  68. 6 | 6 | six
  69. 7 | 7 | seven
  70. 8 | 8 | eight
  71. 0 | | zero
  72. | | null
  73. | 0 | zero
  74. (11 rows)
  75. SELECT *
  76. FROM J1_TBL AS t1 (a, b, c);
  77. a | b | c
  78. ---+---+-------
  79. 1 | 4 | one
  80. 2 | 3 | two
  81. 3 | 2 | three
  82. 4 | 1 | four
  83. 5 | 0 | five
  84. 6 | 6 | six
  85. 7 | 7 | seven
  86. 8 | 8 | eight
  87. 0 | | zero
  88. | | null
  89. | 0 | zero
  90. (11 rows)
  91. SELECT *
  92. FROM J1_TBL t1 (a, b, c);
  93. a | b | c
  94. ---+---+-------
  95. 1 | 4 | one
  96. 2 | 3 | two
  97. 3 | 2 | three
  98. 4 | 1 | four
  99. 5 | 0 | five
  100. 6 | 6 | six
  101. 7 | 7 | seven
  102. 8 | 8 | eight
  103. 0 | | zero
  104. | | null
  105. | 0 | zero
  106. (11 rows)
  107. SELECT *
  108. FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
  109. a | b | c | d | e
  110. ---+---+-------+---+----
  111. 1 | 4 | one | 1 | -1
  112. 2 | 3 | two | 1 | -1
  113. 3 | 2 | three | 1 | -1
  114. 4 | 1 | four | 1 | -1
  115. 5 | 0 | five | 1 | -1
  116. 6 | 6 | six | 1 | -1
  117. 7 | 7 | seven | 1 | -1
  118. 8 | 8 | eight | 1 | -1
  119. 0 | | zero | 1 | -1
  120. | | null | 1 | -1
  121. | 0 | zero | 1 | -1
  122. 1 | 4 | one | 2 | 2
  123. 2 | 3 | two | 2 | 2
  124. 3 | 2 | three | 2 | 2
  125. 4 | 1 | four | 2 | 2
  126. 5 | 0 | five | 2 | 2
  127. 6 | 6 | six | 2 | 2
  128. 7 | 7 | seven | 2 | 2
  129. 8 | 8 | eight | 2 | 2
  130. 0 | | zero | 2 | 2
  131. | | null | 2 | 2
  132. | 0 | zero | 2 | 2
  133. 1 | 4 | one | 3 | -3
  134. 2 | 3 | two | 3 | -3
  135. 3 | 2 | three | 3 | -3
  136. 4 | 1 | four | 3 | -3
  137. 5 | 0 | five | 3 | -3
  138. 6 | 6 | six | 3 | -3
  139. 7 | 7 | seven | 3 | -3
  140. 8 | 8 | eight | 3 | -3
  141. 0 | | zero | 3 | -3
  142. | | null | 3 | -3
  143. | 0 | zero | 3 | -3
  144. 1 | 4 | one | 2 | 4
  145. 2 | 3 | two | 2 | 4
  146. 3 | 2 | three | 2 | 4
  147. 4 | 1 | four | 2 | 4
  148. 5 | 0 | five | 2 | 4
  149. 6 | 6 | six | 2 | 4
  150. 7 | 7 | seven | 2 | 4
  151. 8 | 8 | eight | 2 | 4
  152. 0 | | zero | 2 | 4
  153. | | null | 2 | 4
  154. | 0 | zero | 2 | 4
  155. 1 | 4 | one | 5 | -5
  156. 2 | 3 | two | 5 | -5
  157. 3 | 2 | three | 5 | -5
  158. 4 | 1 | four | 5 | -5
  159. 5 | 0 | five | 5 | -5
  160. 6 | 6 | six | 5 | -5
  161. 7 | 7 | seven | 5 | -5
  162. 8 | 8 | eight | 5 | -5
  163. 0 | | zero | 5 | -5
  164. | | null | 5 | -5
  165. | 0 | zero | 5 | -5
  166. 1 | 4 | one | 5 | -5
  167. 2 | 3 | two | 5 | -5
  168. 3 | 2 | three | 5 | -5
  169. 4 | 1 | four | 5 | -5
  170. 5 | 0 | five | 5 | -5
  171. 6 | 6 | six | 5 | -5
  172. 7 | 7 | seven | 5 | -5
  173. 8 | 8 | eight | 5 | -5
  174. 0 | | zero | 5 | -5
  175. | | null | 5 | -5
  176. | 0 | zero | 5 | -5
  177. 1 | 4 | one | 0 |
  178. 2 | 3 | two | 0 |
  179. 3 | 2 | three | 0 |
  180. 4 | 1 | four | 0 |
  181. 5 | 0 | five | 0 |
  182. 6 | 6 | six | 0 |
  183. 7 | 7 | seven | 0 |
  184. 8 | 8 | eight | 0 |
  185. 0 | | zero | 0 |
  186. | | null | 0 |
  187. | 0 | zero | 0 |
  188. 1 | 4 | one | |
  189. 2 | 3 | two | |
  190. 3 | 2 | three | |
  191. 4 | 1 | four | |
  192. 5 | 0 | five | |
  193. 6 | 6 | six | |
  194. 7 | 7 | seven | |
  195. 8 | 8 | eight | |
  196. 0 | | zero | |
  197. | | null | |
  198. | 0 | zero | |
  199. 1 | 4 | one | | 0
  200. 2 | 3 | two | | 0
  201. 3 | 2 | three | | 0
  202. 4 | 1 | four | | 0
  203. 5 | 0 | five | | 0
  204. 6 | 6 | six | | 0
  205. 7 | 7 | seven | | 0
  206. 8 | 8 | eight | | 0
  207. 0 | | zero | | 0
  208. | | null | | 0
  209. | 0 | zero | | 0
  210. (99 rows)
  211. SELECT t1.a, t2.e
  212. FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
  213. WHERE t1.a = t2.d;
  214. a | e
  215. ---+----
  216. 0 |
  217. 1 | -1
  218. 2 | 2
  219. 2 | 4
  220. 3 | -3
  221. 5 | -5
  222. 5 | -5
  223. (7 rows)
  224. --
  225. -- CROSS JOIN
  226. -- Qualifications are not allowed on cross joins,
  227. -- which degenerate into a standard unqualified inner join.
  228. --
  229. SELECT *
  230. FROM J1_TBL CROSS JOIN J2_TBL;
  231. i | j | t | i | k
  232. ---+---+-------+---+----
  233. 1 | 4 | one | 1 | -1
  234. 2 | 3 | two | 1 | -1
  235. 3 | 2 | three | 1 | -1
  236. 4 | 1 | four | 1 | -1
  237. 5 | 0 | five | 1 | -1
  238. 6 | 6 | six | 1 | -1
  239. 7 | 7 | seven | 1 | -1
  240. 8 | 8 | eight | 1 | -1
  241. 0 | | zero | 1 | -1
  242. | | null | 1 | -1
  243. | 0 | zero | 1 | -1
  244. 1 | 4 | one | 2 | 2
  245. 2 | 3 | two | 2 | 2
  246. 3 | 2 | three | 2 | 2
  247. 4 | 1 | four | 2 | 2
  248. 5 | 0 | five | 2 | 2
  249. 6 | 6 | six | 2 | 2
  250. 7 | 7 | seven | 2 | 2
  251. 8 | 8 | eight | 2 | 2
  252. 0 | | zero | 2 | 2
  253. | | null | 2 | 2
  254. | 0 | zero | 2 | 2
  255. 1 | 4 | one | 3 | -3
  256. 2 | 3 | two | 3 | -3
  257. 3 | 2 | three | 3 | -3
  258. 4 | 1 | four | 3 | -3
  259. 5 | 0 | five | 3 | -3
  260. 6 | 6 | six | 3 | -3
  261. 7 | 7 | seven | 3 | -3
  262. 8 | 8 | eight | 3 | -3
  263. 0 | | zero | 3 | -3
  264. | | null | 3 | -3
  265. | 0 | zero | 3 | -3
  266. 1 | 4 | one | 2 | 4
  267. 2 | 3 | two | 2 | 4
  268. 3 | 2 | three | 2 | 4
  269. 4 | 1 | four | 2 | 4
  270. 5 | 0 | five | 2 | 4
  271. 6 | 6 | six | 2 | 4
  272. 7 | 7 | seven | 2 | 4
  273. 8 | 8 | eight | 2 | 4
  274. 0 | | zero | 2 | 4
  275. | | null | 2 | 4
  276. | 0 | zero | 2 | 4
  277. 1 | 4 | one | 5 | -5
  278. 2 | 3 | two | 5 | -5
  279. 3 | 2 | three | 5 | -5
  280. 4 | 1 | four | 5 | -5
  281. 5 | 0 | five | 5 | -5
  282. 6 | 6 | six | 5 | -5
  283. 7 | 7 | seven | 5 | -5
  284. 8 | 8 | eight | 5 | -5
  285. 0 | | zero | 5 | -5
  286. | | null | 5 | -5
  287. | 0 | zero | 5 | -5
  288. 1 | 4 | one | 5 | -5
  289. 2 | 3 | two | 5 | -5
  290. 3 | 2 | three | 5 | -5
  291. 4 | 1 | four | 5 | -5
  292. 5 | 0 | five | 5 | -5
  293. 6 | 6 | six | 5 | -5
  294. 7 | 7 | seven | 5 | -5
  295. 8 | 8 | eight | 5 | -5
  296. 0 | | zero | 5 | -5
  297. | | null | 5 | -5
  298. | 0 | zero | 5 | -5
  299. 1 | 4 | one | 0 |
  300. 2 | 3 | two | 0 |
  301. 3 | 2 | three | 0 |
  302. 4 | 1 | four | 0 |
  303. 5 | 0 | five | 0 |
  304. 6 | 6 | six | 0 |
  305. 7 | 7 | seven | 0 |
  306. 8 | 8 | eight | 0 |
  307. 0 | | zero | 0 |
  308. | | null | 0 |
  309. | 0 | zero | 0 |
  310. 1 | 4 | one | |
  311. 2 | 3 | two | |
  312. 3 | 2 | three | |
  313. 4 | 1 | four | |
  314. 5 | 0 | five | |
  315. 6 | 6 | six | |
  316. 7 | 7 | seven | |
  317. 8 | 8 | eight | |
  318. 0 | | zero | |
  319. | | null | |
  320. | 0 | zero | |
  321. 1 | 4 | one | | 0
  322. 2 | 3 | two | | 0
  323. 3 | 2 | three | | 0
  324. 4 | 1 | four | | 0
  325. 5 | 0 | five | | 0
  326. 6 | 6 | six | | 0
  327. 7 | 7 | seven | | 0
  328. 8 | 8 | eight | | 0
  329. 0 | | zero | | 0
  330. | | null | | 0
  331. | 0 | zero | | 0
  332. (99 rows)
  333. -- ambiguous column
  334. SELECT i, k, t
  335. FROM J1_TBL CROSS JOIN J2_TBL;
  336. ERROR: column reference "i" is ambiguous
  337. LINE 1: SELECT i, k, t
  338. ^
  339. -- resolve previous ambiguity by specifying the table name
  340. SELECT t1.i, k, t
  341. FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
  342. i | k | t
  343. ---+----+-------
  344. 1 | -1 | one
  345. 2 | -1 | two
  346. 3 | -1 | three
  347. 4 | -1 | four
  348. 5 | -1 | five
  349. 6 | -1 | six
  350. 7 | -1 | seven
  351. 8 | -1 | eight
  352. 0 | -1 | zero
  353. | -1 | null
  354. | -1 | zero
  355. 1 | 2 | one
  356. 2 | 2 | two
  357. 3 | 2 | three
  358. 4 | 2 | four
  359. 5 | 2 | five
  360. 6 | 2 | six
  361. 7 | 2 | seven
  362. 8 | 2 | eight
  363. 0 | 2 | zero
  364. | 2 | null
  365. | 2 | zero
  366. 1 | -3 | one
  367. 2 | -3 | two
  368. 3 | -3 | three
  369. 4 | -3 | four
  370. 5 | -3 | five
  371. 6 | -3 | six
  372. 7 | -3 | seven
  373. 8 | -3 | eight
  374. 0 | -3 | zero
  375. | -3 | null
  376. | -3 | zero
  377. 1 | 4 | one
  378. 2 | 4 | two
  379. 3 | 4 | three
  380. 4 | 4 | four
  381. 5 | 4 | five
  382. 6 | 4 | six
  383. 7 | 4 | seven
  384. 8 | 4 | eight
  385. 0 | 4 | zero
  386. | 4 | null
  387. | 4 | zero
  388. 1 | -5 | one
  389. 2 | -5 | two
  390. 3 | -5 | three
  391. 4 | -5 | four
  392. 5 | -5 | five
  393. 6 | -5 | six
  394. 7 | -5 | seven
  395. 8 | -5 | eight
  396. 0 | -5 | zero
  397. | -5 | null
  398. | -5 | zero
  399. 1 | -5 | one
  400. 2 | -5 | two
  401. 3 | -5 | three
  402. 4 | -5 | four
  403. 5 | -5 | five
  404. 6 | -5 | six
  405. 7 | -5 | seven
  406. 8 | -5 | eight
  407. 0 | -5 | zero
  408. | -5 | null
  409. | -5 | zero
  410. 1 | | one
  411. 2 | | two
  412. 3 | | three
  413. 4 | | four
  414. 5 | | five
  415. 6 | | six
  416. 7 | | seven
  417. 8 | | eight
  418. 0 | | zero
  419. | | null
  420. | | zero
  421. 1 | | one
  422. 2 | | two
  423. 3 | | three
  424. 4 | | four
  425. 5 | | five
  426. 6 | | six
  427. 7 | | seven
  428. 8 | | eight
  429. 0 | | zero
  430. | | null
  431. | | zero
  432. 1 | 0 | one
  433. 2 | 0 | two
  434. 3 | 0 | three
  435. 4 | 0 | four
  436. 5 | 0 | five
  437. 6 | 0 | six
  438. 7 | 0 | seven
  439. 8 | 0 | eight
  440. 0 | 0 | zero
  441. | 0 | null
  442. | 0 | zero
  443. (99 rows)
  444. SELECT ii, tt, kk
  445. FROM (J1_TBL CROSS JOIN J2_TBL)
  446. AS tx (ii, jj, tt, ii2, kk);
  447. ii | tt | kk
  448. ----+-------+----
  449. 1 | one | -1
  450. 2 | two | -1
  451. 3 | three | -1
  452. 4 | four | -1
  453. 5 | five | -1
  454. 6 | six | -1
  455. 7 | seven | -1
  456. 8 | eight | -1
  457. 0 | zero | -1
  458. | null | -1
  459. | zero | -1
  460. 1 | one | 2
  461. 2 | two | 2
  462. 3 | three | 2
  463. 4 | four | 2
  464. 5 | five | 2
  465. 6 | six | 2
  466. 7 | seven | 2
  467. 8 | eight | 2
  468. 0 | zero | 2
  469. | null | 2
  470. | zero | 2
  471. 1 | one | -3
  472. 2 | two | -3
  473. 3 | three | -3
  474. 4 | four | -3
  475. 5 | five | -3
  476. 6 | six | -3
  477. 7 | seven | -3
  478. 8 | eight | -3
  479. 0 | zero | -3
  480. | null | -3
  481. | zero | -3
  482. 1 | one | 4
  483. 2 | two | 4
  484. 3 | three | 4
  485. 4 | four | 4
  486. 5 | five | 4
  487. 6 | six | 4
  488. 7 | seven | 4
  489. 8 | eight | 4
  490. 0 | zero | 4
  491. | null | 4
  492. | zero | 4
  493. 1 | one | -5
  494. 2 | two | -5
  495. 3 | three | -5
  496. 4 | four | -5
  497. 5 | five | -5
  498. 6 | six | -5
  499. 7 | seven | -5
  500. 8 | eight | -5
  501. 0 | zero | -5
  502. | null | -5
  503. | zero | -5
  504. 1 | one | -5
  505. 2 | two | -5
  506. 3 | three | -5
  507. 4 | four | -5
  508. 5 | five | -5
  509. 6 | six | -5
  510. 7 | seven | -5
  511. 8 | eight | -5
  512. 0 | zero | -5
  513. | null | -5
  514. | zero | -5
  515. 1 | one |
  516. 2 | two |
  517. 3 | three |
  518. 4 | four |
  519. 5 | five |
  520. 6 | six |
  521. 7 | seven |
  522. 8 | eight |
  523. 0 | zero |
  524. | null |
  525. | zero |
  526. 1 | one |
  527. 2 | two |
  528. 3 | three |
  529. 4 | four |
  530. 5 | five |
  531. 6 | six |
  532. 7 | seven |
  533. 8 | eight |
  534. 0 | zero |
  535. | null |
  536. | zero |
  537. 1 | one | 0
  538. 2 | two | 0
  539. 3 | three | 0
  540. 4 | four | 0
  541. 5 | five | 0
  542. 6 | six | 0
  543. 7 | seven | 0
  544. 8 | eight | 0
  545. 0 | zero | 0
  546. | null | 0
  547. | zero | 0
  548. (99 rows)
  549. SELECT tx.ii, tx.jj, tx.kk
  550. FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
  551. AS tx (ii, jj, tt, ii2, kk);
  552. ii | jj | kk
  553. ----+----+----
  554. 1 | 4 | -1
  555. 2 | 3 | -1
  556. 3 | 2 | -1
  557. 4 | 1 | -1
  558. 5 | 0 | -1
  559. 6 | 6 | -1
  560. 7 | 7 | -1
  561. 8 | 8 | -1
  562. 0 | | -1
  563. | | -1
  564. | 0 | -1
  565. 1 | 4 | 2
  566. 2 | 3 | 2
  567. 3 | 2 | 2
  568. 4 | 1 | 2
  569. 5 | 0 | 2
  570. 6 | 6 | 2
  571. 7 | 7 | 2
  572. 8 | 8 | 2
  573. 0 | | 2
  574. | | 2
  575. | 0 | 2
  576. 1 | 4 | -3
  577. 2 | 3 | -3
  578. 3 | 2 | -3
  579. 4 | 1 | -3
  580. 5 | 0 | -3
  581. 6 | 6 | -3
  582. 7 | 7 | -3
  583. 8 | 8 | -3
  584. 0 | | -3
  585. | | -3
  586. | 0 | -3
  587. 1 | 4 | 4
  588. 2 | 3 | 4
  589. 3 | 2 | 4
  590. 4 | 1 | 4
  591. 5 | 0 | 4
  592. 6 | 6 | 4
  593. 7 | 7 | 4
  594. 8 | 8 | 4
  595. 0 | | 4
  596. | | 4
  597. | 0 | 4
  598. 1 | 4 | -5
  599. 2 | 3 | -5
  600. 3 | 2 | -5
  601. 4 | 1 | -5
  602. 5 | 0 | -5
  603. 6 | 6 | -5
  604. 7 | 7 | -5
  605. 8 | 8 | -5
  606. 0 | | -5
  607. | | -5
  608. | 0 | -5
  609. 1 | 4 | -5
  610. 2 | 3 | -5
  611. 3 | 2 | -5
  612. 4 | 1 | -5
  613. 5 | 0 | -5
  614. 6 | 6 | -5
  615. 7 | 7 | -5
  616. 8 | 8 | -5
  617. 0 | | -5
  618. | | -5
  619. | 0 | -5
  620. 1 | 4 |
  621. 2 | 3 |
  622. 3 | 2 |
  623. 4 | 1 |
  624. 5 | 0 |
  625. 6 | 6 |
  626. 7 | 7 |
  627. 8 | 8 |
  628. 0 | |
  629. | |
  630. | 0 |
  631. 1 | 4 |
  632. 2 | 3 |
  633. 3 | 2 |
  634. 4 | 1 |
  635. 5 | 0 |
  636. 6 | 6 |
  637. 7 | 7 |
  638. 8 | 8 |
  639. 0 | |
  640. | |
  641. | 0 |
  642. 1 | 4 | 0
  643. 2 | 3 | 0
  644. 3 | 2 | 0
  645. 4 | 1 | 0
  646. 5 | 0 | 0
  647. 6 | 6 | 0
  648. 7 | 7 | 0
  649. 8 | 8 | 0
  650. 0 | | 0
  651. | | 0
  652. | 0 | 0
  653. (99 rows)
  654. SELECT *
  655. FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
  656. i | j | t | i | k | i | k
  657. ---+---+-------+---+----+---+----
  658. 1 | 4 | one | 1 | -1 | 1 | -1
  659. 1 | 4 | one | 1 | -1 | 2 | 2
  660. 1 | 4 | one | 1 | -1 | 3 | -3
  661. 1 | 4 | one | 1 | -1 | 2 | 4
  662. 1 | 4 | one | 1 | -1 | 5 | -5
  663. 1 | 4 | one | 1 | -1 | 5 | -5
  664. 1 | 4 | one | 1 | -1 | 0 |
  665. 1 | 4 | one | 1 | -1 | |
  666. 1 | 4 | one | 1 | -1 | | 0
  667. 2 | 3 | two | 1 | -1 | 1 | -1
  668. 2 | 3 | two | 1 | -1 | 2 | 2
  669. 2 | 3 | two | 1 | -1 | 3 | -3
  670. 2 | 3 | two | 1 | -1 | 2 | 4
  671. 2 | 3 | two | 1 | -1 | 5 | -5
  672. 2 | 3 | two | 1 | -1 | 5 | -5
  673. 2 | 3 | two | 1 | -1 | 0 |
  674. 2 | 3 | two | 1 | -1 | |
  675. 2 | 3 | two | 1 | -1 | | 0
  676. 3 | 2 | three | 1 | -1 | 1 | -1
  677. 3 | 2 | three | 1 | -1 | 2 | 2
  678. 3 | 2 | three | 1 | -1 | 3 | -3
  679. 3 | 2 | three | 1 | -1 | 2 | 4
  680. 3 | 2 | three | 1 | -1 | 5 | -5
  681. 3 | 2 | three | 1 | -1 | 5 | -5
  682. 3 | 2 | three | 1 | -1 | 0 |
  683. 3 | 2 | three | 1 | -1 | |
  684. 3 | 2 | three | 1 | -1 | | 0
  685. 4 | 1 | four | 1 | -1 | 1 | -1
  686. 4 | 1 | four | 1 | -1 | 2 | 2
  687. 4 | 1 | four | 1 | -1 | 3 | -3
  688. 4 | 1 | four | 1 | -1 | 2 | 4
  689. 4 | 1 | four | 1 | -1 | 5 | -5
  690. 4 | 1 | four | 1 | -1 | 5 | -5
  691. 4 | 1 | four | 1 | -1 | 0 |
  692. 4 | 1 | four | 1 | -1 | |
  693. 4 | 1 | four | 1 | -1 | | 0
  694. 5 | 0 | five | 1 | -1 | 1 | -1
  695. 5 | 0 | five | 1 | -1 | 2 | 2
  696. 5 | 0 | five | 1 | -1 | 3 | -3
  697. 5 | 0 | five | 1 | -1 | 2 | 4
  698. 5 | 0 | five | 1 | -1 | 5 | -5
  699. 5 | 0 | five | 1 | -1 | 5 | -5
  700. 5 | 0 | five | 1 | -1 | 0 |
  701. 5 | 0 | five | 1 | -1 | |
  702. 5 | 0 | five | 1 | -1 | | 0
  703. 6 | 6 | six | 1 | -1 | 1 | -1
  704. 6 | 6 | six | 1 | -1 | 2 | 2
  705. 6 | 6 | six | 1 | -1 | 3 | -3
  706. 6 | 6 | six | 1 | -1 | 2 | 4
  707. 6 | 6 | six | 1 | -1 | 5 | -5
  708. 6 | 6 | six | 1 | -1 | 5 | -5
  709. 6 | 6 | six | 1 | -1 | 0 |
  710. 6 | 6 | six | 1 | -1 | |
  711. 6 | 6 | six | 1 | -1 | | 0
  712. 7 | 7 | seven | 1 | -1 | 1 | -1
  713. 7 | 7 | seven | 1 | -1 | 2 | 2
  714. 7 | 7 | seven | 1 | -1 | 3 | -3
  715. 7 | 7 | seven | 1 | -1 | 2 | 4
  716. 7 | 7 | seven | 1 | -1 | 5 | -5
  717. 7 | 7 | seven | 1 | -1 | 5 | -5
  718. 7 | 7 | seven | 1 | -1 | 0 |
  719. 7 | 7 | seven | 1 | -1 | |
  720. 7 | 7 | seven | 1 | -1 | | 0
  721. 8 | 8 | eight | 1 | -1 | 1 | -1
  722. 8 | 8 | eight | 1 | -1 | 2 | 2
  723. 8 | 8 | eight | 1 | -1 | 3 | -3
  724. 8 | 8 | eight | 1 | -1 | 2 | 4
  725. 8 | 8 | eight | 1 | -1 | 5 | -5
  726. 8 | 8 | eight | 1 | -1 | 5 | -5
  727. 8 | 8 | eight | 1 | -1 | 0 |
  728. 8 | 8 | eight | 1 | -1 | |
  729. 8 | 8 | eight | 1 | -1 | | 0
  730. 0 | | zero | 1 | -1 | 1 | -1
  731. 0 | | zero | 1 | -1 | 2 | 2
  732. 0 | | zero | 1 | -1 | 3 | -3
  733. 0 | | zero | 1 | -1 | 2 | 4
  734. 0 | | zero | 1 | -1 | 5 | -5
  735. 0 | | zero | 1 | -1 | 5 | -5
  736. 0 | | zero | 1 | -1 | 0 |
  737. 0 | | zero | 1 | -1 | |
  738. 0 | | zero | 1 | -1 | | 0
  739. | | null | 1 | -1 | 1 | -1
  740. | | null | 1 | -1 | 2 | 2
  741. | | null | 1 | -1 | 3 | -3
  742. | | null | 1 | -1 | 2 | 4
  743. | | null | 1 | -1 | 5 | -5
  744. | | null | 1 | -1 | 5 | -5
  745. | | null | 1 | -1 | 0 |
  746. | | null | 1 | -1 | |
  747. | | null | 1 | -1 | | 0
  748. | 0 | zero | 1 | -1 | 1 | -1
  749. | 0 | zero | 1 | -1 | 2 | 2
  750. | 0 | zero | 1 | -1 | 3 | -3
  751. | 0 | zero | 1 | -1 | 2 | 4
  752. | 0 | zero | 1 | -1 | 5 | -5
  753. | 0 | zero | 1 | -1 | 5 | -5
  754. | 0 | zero | 1 | -1 | 0 |
  755. | 0 | zero | 1 | -1 | |
  756. | 0 | zero | 1 | -1 | | 0
  757. 1 | 4 | one | 2 | 2 | 1 | -1
  758. 1 | 4 | one | 2 | 2 | 2 | 2
  759. 1 | 4 | one | 2 | 2 | 3 | -3
  760. 1 | 4 | one | 2 | 2 | 2 | 4
  761. 1 | 4 | one | 2 | 2 | 5 | -5
  762. 1 | 4 | one | 2 | 2 | 5 | -5
  763. 1 | 4 | one | 2 | 2 | 0 |
  764. 1 | 4 | one | 2 | 2 | |
  765. 1 | 4 | one | 2 | 2 | | 0
  766. 2 | 3 | two | 2 | 2 | 1 | -1
  767. 2 | 3 | two | 2 | 2 | 2 | 2
  768. 2 | 3 | two | 2 | 2 | 3 | -3
  769. 2 | 3 | two | 2 | 2 | 2 | 4
  770. 2 | 3 | two | 2 | 2 | 5 | -5
  771. 2 | 3 | two | 2 | 2 | 5 | -5
  772. 2 | 3 | two | 2 | 2 | 0 |
  773. 2 | 3 | two | 2 | 2 | |
  774. 2 | 3 | two | 2 | 2 | | 0
  775. 3 | 2 | three | 2 | 2 | 1 | -1
  776. 3 | 2 | three | 2 | 2 | 2 | 2
  777. 3 | 2 | three | 2 | 2 | 3 | -3
  778. 3 | 2 | three | 2 | 2 | 2 | 4
  779. 3 | 2 | three | 2 | 2 | 5 | -5
  780. 3 | 2 | three | 2 | 2 | 5 | -5
  781. 3 | 2 | three | 2 | 2 | 0 |
  782. 3 | 2 | three | 2 | 2 | |
  783. 3 | 2 | three | 2 | 2 | | 0
  784. 4 | 1 | four | 2 | 2 | 1 | -1
  785. 4 | 1 | four | 2 | 2 | 2 | 2
  786. 4 | 1 | four | 2 | 2 | 3 | -3
  787. 4 | 1 | four | 2 | 2 | 2 | 4
  788. 4 | 1 | four | 2 | 2 | 5 | -5
  789. 4 | 1 | four | 2 | 2 | 5 | -5
  790. 4 | 1 | four | 2 | 2 | 0 |
  791. 4 | 1 | four | 2 | 2 | |
  792. 4 | 1 | four | 2 | 2 | | 0
  793. 5 | 0 | five | 2 | 2 | 1 | -1
  794. 5 | 0 | five | 2 | 2 | 2 | 2
  795. 5 | 0 | five | 2 | 2 | 3 | -3
  796. 5 | 0 | five | 2 | 2 | 2 | 4
  797. 5 | 0 | five | 2 | 2 | 5 | -5
  798. 5 | 0 | five | 2 | 2 | 5 | -5
  799. 5 | 0 | five | 2 | 2 | 0 |
  800. 5 | 0 | five | 2 | 2 | |
  801. 5 | 0 | five | 2 | 2 | | 0
  802. 6 | 6 | six | 2 | 2 | 1 | -1
  803. 6 | 6 | six | 2 | 2 | 2 | 2
  804. 6 | 6 | six | 2 | 2 | 3 | -3
  805. 6 | 6 | six | 2 | 2 | 2 | 4
  806. 6 | 6 | six | 2 | 2 | 5 | -5
  807. 6 | 6 | six | 2 | 2 | 5 | -5
  808. 6 | 6 | six | 2 | 2 | 0 |
  809. 6 | 6 | six | 2 | 2 | |
  810. 6 | 6 | six | 2 | 2 | | 0
  811. 7 | 7 | seven | 2 | 2 | 1 | -1
  812. 7 | 7 | seven | 2 | 2 | 2 | 2
  813. 7 | 7 | seven | 2 | 2 | 3 | -3
  814. 7 | 7 | seven | 2 | 2 | 2 | 4
  815. 7 | 7 | seven | 2 | 2 | 5 | -5
  816. 7 | 7 | seven | 2 | 2 | 5 | -5
  817. 7 | 7 | seven | 2 | 2 | 0 |
  818. 7 | 7 | seven | 2 | 2 | |
  819. 7 | 7 | seven | 2 | 2 | | 0
  820. 8 | 8 | eight | 2 | 2 | 1 | -1
  821. 8 | 8 | eight | 2 | 2 | 2 | 2
  822. 8 | 8 | eight | 2 | 2 | 3 | -3
  823. 8 | 8 | eight | 2 | 2 | 2 | 4
  824. 8 | 8 | eight | 2 | 2 | 5 | -5
  825. 8 | 8 | eight | 2 | 2 | 5 | -5
  826. 8 | 8 | eight | 2 | 2 | 0 |
  827. 8 | 8 | eight | 2 | 2 | |
  828. 8 | 8 | eight | 2 | 2 | | 0
  829. 0 | | zero | 2 | 2 | 1 | -1
  830. 0 | | zero | 2 | 2 | 2 | 2
  831. 0 | | zero | 2 | 2 | 3 | -3
  832. 0 | | zero | 2 | 2 | 2 | 4
  833. 0 | | zero | 2 | 2 | 5 | -5
  834. 0 | | zero | 2 | 2 | 5 | -5
  835. 0 | | zero | 2 | 2 | 0 |
  836. 0 | | zero | 2 | 2 | |
  837. 0 | | zero | 2 | 2 | | 0
  838. | | null | 2 | 2 | 1 | -1
  839. | | null | 2 | 2 | 2 | 2
  840. | | null | 2 | 2 | 3 | -3
  841. | | null | 2 | 2 | 2 | 4
  842. | | null | 2 | 2 | 5 | -5
  843. | | null | 2 | 2 | 5 | -5
  844. | | null | 2 | 2 | 0 |
  845. | | null | 2 | 2 | |
  846. | | null | 2 | 2 | | 0
  847. | 0 | zero | 2 | 2 | 1 | -1
  848. | 0 | zero | 2 | 2 | 2 | 2
  849. | 0 | zero | 2 | 2 | 3 | -3
  850. | 0 | zero | 2 | 2 | 2 | 4
  851. | 0 | zero | 2 | 2 | 5 | -5
  852. | 0 | zero | 2 | 2 | 5 | -5
  853. | 0 | zero | 2 | 2 | 0 |
  854. | 0 | zero | 2 | 2 | |
  855. | 0 | zero | 2 | 2 | | 0
  856. 1 | 4 | one | 3 | -3 | 1 | -1
  857. 1 | 4 | one | 3 | -3 | 2 | 2
  858. 1 | 4 | one | 3 | -3 | 3 | -3
  859. 1 | 4 | one | 3 | -3 | 2 | 4
  860. 1 | 4 | one | 3 | -3 | 5 | -5
  861. 1 | 4 | one | 3 | -3 | 5 | -5
  862. 1 | 4 | one | 3 | -3 | 0 |
  863. 1 | 4 | one | 3 | -3 | |
  864. 1 | 4 | one | 3 | -3 | | 0
  865. 2 | 3 | two | 3 | -3 | 1 | -1
  866. 2 | 3 | two | 3 | -3 | 2 | 2
  867. 2 | 3 | two | 3 | -3 | 3 | -3
  868. 2 | 3 | two | 3 | -3 | 2 | 4
  869. 2 | 3 | two | 3 | -3 | 5 | -5
  870. 2 | 3 | two | 3 | -3 | 5 | -5
  871. 2 | 3 | two | 3 | -3 | 0 |
  872. 2 | 3 | two | 3 | -3 | |
  873. 2 | 3 | two | 3 | -3 | | 0
  874. 3 | 2 | three | 3 | -3 | 1 | -1
  875. 3 | 2 | three | 3 | -3 | 2 | 2
  876. 3 | 2 | three | 3 | -3 | 3 | -3
  877. 3 | 2 | three | 3 | -3 | 2 | 4
  878. 3 | 2 | three | 3 | -3 | 5 | -5
  879. 3 | 2 | three | 3 | -3 | 5 | -5
  880. 3 | 2 | three | 3 | -3 | 0 |
  881. 3 | 2 | three | 3 | -3 | |
  882. 3 | 2 | three | 3 | -3 | | 0
  883. 4 | 1 | four | 3 | -3 | 1 | -1
  884. 4 | 1 | four | 3 | -3 | 2 | 2
  885. 4 | 1 | four | 3 | -3 | 3 | -3
  886. 4 | 1 | four | 3 | -3 | 2 | 4
  887. 4 | 1 | four | 3 | -3 | 5 | -5
  888. 4 | 1 | four | 3 | -3 | 5 | -5
  889. 4 | 1 | four | 3 | -3 | 0 |
  890. 4 | 1 | four | 3 | -3 | |
  891. 4 | 1 | four | 3 | -3 | | 0
  892. 5 | 0 | five | 3 | -3 | 1 | -1
  893. 5 | 0 | five | 3 | -3 | 2 | 2
  894. 5 | 0 | five | 3 | -3 | 3 | -3
  895. 5 | 0 | five | 3 | -3 | 2 | 4
  896. 5 | 0 | five | 3 | -3 | 5 | -5
  897. 5 | 0 | five | 3 | -3 | 5 | -5
  898. 5 | 0 | five | 3 | -3 | 0 |
  899. 5 | 0 | five | 3 | -3 | |
  900. 5 | 0 | five | 3 | -3 | | 0
  901. 6 | 6 | six | 3 | -3 | 1 | -1
  902. 6 | 6 | six | 3 | -3 | 2 | 2
  903. 6 | 6 | six | 3 | -3 | 3 | -3
  904. 6 | 6 | six | 3 | -3 | 2 | 4
  905. 6 | 6 | six | 3 | -3 | 5 | -5
  906. 6 | 6 | six | 3 | -3 | 5 | -5
  907. 6 | 6 | six | 3 | -3 | 0 |
  908. 6 | 6 | six | 3 | -3 | |
  909. 6 | 6 | six | 3 | -3 | | 0
  910. 7 | 7 | seven | 3 | -3 | 1 | -1
  911. 7 | 7 | seven | 3 | -3 | 2 | 2
  912. 7 | 7 | seven | 3 | -3 | 3 | -3
  913. 7 | 7 | seven | 3 | -3 | 2 | 4
  914. 7 | 7 | seven | 3 | -3 | 5 | -5
  915. 7 | 7 | seven | 3 | -3 | 5 | -5
  916. 7 | 7 | seven | 3 | -3 | 0 |
  917. 7 | 7 | seven | 3 | -3 | |
  918. 7 | 7 | seven | 3 | -3 | | 0
  919. 8 | 8 | eight | 3 | -3 | 1 | -1
  920. 8 | 8 | eight | 3 | -3 | 2 | 2
  921. 8 | 8 | eight | 3 | -3 | 3 | -3
  922. 8 | 8 | eight | 3 | -3 | 2 | 4
  923. 8 | 8 | eight | 3 | -3 | 5 | -5
  924. 8 | 8 | eight | 3 | -3 | 5 | -5
  925. 8 | 8 | eight | 3 | -3 | 0 |
  926. 8 | 8 | eight | 3 | -3 | |
  927. 8 | 8 | eight | 3 | -3 | | 0
  928. 0 | | zero | 3 | -3 | 1 | -1
  929. 0 | | zero | 3 | -3 | 2 | 2
  930. 0 | | zero | 3 | -3 | 3 | -3
  931. 0 | | zero | 3 | -3 | 2 | 4
  932. 0 | | zero | 3 | -3 | 5 | -5
  933. 0 | | zero | 3 | -3 | 5 | -5
  934. 0 | | zero | 3 | -3 | 0 |
  935. 0 | | zero | 3 | -3 | |
  936. 0 | | zero | 3 | -3 | | 0
  937. | | null | 3 | -3 | 1 | -1
  938. | | null | 3 | -3 | 2 | 2
  939. | | null | 3 | -3 | 3 | -3
  940. | | null | 3 | -3 | 2 | 4
  941. | | null | 3 | -3 | 5 | -5
  942. | | null | 3 | -3 | 5 | -5
  943. | | null | 3 | -3 | 0 |
  944. | | null | 3 | -3 | |
  945. | | null | 3 | -3 | | 0
  946. | 0 | zero | 3 | -3 | 1 | -1
  947. | 0 | zero | 3 | -3 | 2 | 2
  948. | 0 | zero | 3 | -3 | 3 | -3
  949. | 0 | zero | 3 | -3 | 2 | 4
  950. | 0 | zero | 3 | -3 | 5 | -5
  951. | 0 | zero | 3 | -3 | 5 | -5
  952. | 0 | zero | 3 | -3 | 0 |
  953. | 0 | zero | 3 | -3 | |
  954. | 0 | zero | 3 | -3 | | 0
  955. 1 | 4 | one | 2 | 4 | 1 | -1
  956. 1 | 4 | one | 2 | 4 | 2 | 2
  957. 1 | 4 | one | 2 | 4 | 3 | -3
  958. 1 | 4 | one | 2 | 4 | 2 | 4
  959. 1 | 4 | one | 2 | 4 | 5 | -5
  960. 1 | 4 | one | 2 | 4 | 5 | -5
  961. 1 | 4 | one | 2 | 4 | 0 |
  962. 1 | 4 | one | 2 | 4 | |
  963. 1 | 4 | one | 2 | 4 | | 0
  964. 2 | 3 | two | 2 | 4 | 1 | -1
  965. 2 | 3 | two | 2 | 4 | 2 | 2
  966. 2 | 3 | two | 2 | 4 | 3 | -3
  967. 2 | 3 | two | 2 | 4 | 2 | 4
  968. 2 | 3 | two | 2 | 4 | 5 | -5
  969. 2 | 3 | two | 2 | 4 | 5 | -5
  970. 2 | 3 | two | 2 | 4 | 0 |
  971. 2 | 3 | two | 2 | 4 | |
  972. 2 | 3 | two | 2 | 4 | | 0
  973. 3 | 2 | three | 2 | 4 | 1 | -1
  974. 3 | 2 | three | 2 | 4 | 2 | 2
  975. 3 | 2 | three | 2 | 4 | 3 | -3
  976. 3 | 2 | three | 2 | 4 | 2 | 4
  977. 3 | 2 | three | 2 | 4 | 5 | -5
  978. 3 | 2 | three | 2 | 4 | 5 | -5
  979. 3 | 2 | three | 2 | 4 | 0 |
  980. 3 | 2 | three | 2 | 4 | |
  981. 3 | 2 | three | 2 | 4 | | 0
  982. 4 | 1 | four | 2 | 4 | 1 | -1
  983. 4 | 1 | four | 2 | 4 | 2 | 2
  984. 4 | 1 | four | 2 | 4 | 3 | -3
  985. 4 | 1 | four | 2 | 4 | 2 | 4
  986. 4 | 1 | four | 2 | 4 | 5 | -5
  987. 4 | 1 | four | 2 | 4 | 5 | -5
  988. 4 | 1 | four | 2 | 4 | 0 |
  989. 4 | 1 | four | 2 | 4 | |
  990. 4 | 1 | four | 2 | 4 | | 0
  991. 5 | 0 | five | 2 | 4 | 1 | -1
  992. 5 | 0 | five | 2 | 4 | 2 | 2
  993. 5 | 0 | five | 2 | 4 | 3 | -3
  994. 5 | 0 | five | 2 | 4 | 2 | 4
  995. 5 | 0 | five | 2 | 4 | 5 | -5
  996. 5 | 0 | five | 2 | 4 | 5 | -5
  997. 5 | 0 | five | 2 | 4 | 0 |
  998. 5 | 0 | five | 2 | 4 | |
  999. 5 | 0 | five | 2 | 4 | | 0
  1000. 6 | 6 | six | 2 | 4 | 1 | -1
  1001. 6 | 6 | six | 2 | 4 | 2 | 2
  1002. 6 | 6 | six | 2 | 4 | 3 | -3
  1003. 6 | 6 | six | 2 | 4 | 2 | 4
  1004. 6 | 6 | six | 2 | 4 | 5 | -5
  1005. 6 | 6 | six | 2 | 4 | 5 | -5
  1006. 6 | 6 | six | 2 | 4 | 0 |
  1007. 6 | 6 | six | 2 | 4 | |
  1008. 6 | 6 | six | 2 | 4 | | 0
  1009. 7 | 7 | seven | 2 | 4 | 1 | -1
  1010. 7 | 7 | seven | 2 | 4 | 2 | 2
  1011. 7 | 7 | seven | 2 | 4 | 3 | -3
  1012. 7 | 7 | seven | 2 | 4 | 2 | 4
  1013. 7 | 7 | seven | 2 | 4 | 5 | -5
  1014. 7 | 7 | seven | 2 | 4 | 5 | -5
  1015. 7 | 7 | seven | 2 | 4 | 0 |
  1016. 7 | 7 | seven | 2 | 4 | |
  1017. 7 | 7 | seven | 2 | 4 | | 0
  1018. 8 | 8 | eight | 2 | 4 | 1 | -1
  1019. 8 | 8 | eight | 2 | 4 | 2 | 2
  1020. 8 | 8 | eight | 2 | 4 | 3 | -3
  1021. 8 | 8 | eight | 2 | 4 | 2 | 4
  1022. 8 | 8 | eight | 2 | 4 | 5 | -5
  1023. 8 | 8 | eight | 2 | 4 | 5 | -5
  1024. 8 | 8 | eight | 2 | 4 | 0 |
  1025. 8 | 8 | eight | 2 | 4 | |
  1026. 8 | 8 | eight | 2 | 4 | | 0
  1027. 0 | | zero | 2 | 4 | 1 | -1
  1028. 0 | | zero | 2 | 4 | 2 | 2
  1029. 0 | | zero | 2 | 4 | 3 | -3
  1030. 0 | | zero | 2 | 4 | 2 | 4
  1031. 0 | | zero | 2 | 4 | 5 | -5
  1032. 0 | | zero | 2 | 4 | 5 | -5
  1033. 0 | | zero | 2 | 4 | 0 |
  1034. 0 | | zero | 2 | 4 | |
  1035. 0 | | zero | 2 | 4 | | 0
  1036. | | null | 2 | 4 | 1 | -1
  1037. | | null | 2 | 4 | 2 | 2
  1038. | | null | 2 | 4 | 3 | -3
  1039. | | null | 2 | 4 | 2 | 4
  1040. | | null | 2 | 4 | 5 | -5
  1041. | | null | 2 | 4 | 5 | -5
  1042. | | null | 2 | 4 | 0 |
  1043. | | null | 2 | 4 | |
  1044. | | null | 2 | 4 | | 0
  1045. | 0 | zero | 2 | 4 | 1 | -1
  1046. | 0 | zero | 2 | 4 | 2 | 2
  1047. | 0 | zero | 2 | 4 | 3 | -3
  1048. | 0 | zero | 2 | 4 | 2 | 4
  1049. | 0 | zero | 2 | 4 | 5 | -5
  1050. | 0 | zero | 2 | 4 | 5 | -5
  1051. | 0 | zero | 2 | 4 | 0 |
  1052. | 0 | zero | 2 | 4 | |
  1053. | 0 | zero | 2 | 4 | | 0
  1054. 1 | 4 | one | 5 | -5 | 1 | -1
  1055. 1 | 4 | one | 5 | -5 | 2 | 2
  1056. 1 | 4 | one | 5 | -5 | 3 | -3
  1057. 1 | 4 | one | 5 | -5 | 2 | 4
  1058. 1 | 4 | one | 5 | -5 | 5 | -5
  1059. 1 | 4 | one | 5 | -5 | 5 | -5
  1060. 1 | 4 | one | 5 | -5 | 0 |
  1061. 1 | 4 | one | 5 | -5 | |
  1062. 1 | 4 | one | 5 | -5 | | 0
  1063. 2 | 3 | two | 5 | -5 | 1 | -1
  1064. 2 | 3 | two | 5 | -5 | 2 | 2
  1065. 2 | 3 | two | 5 | -5 | 3 | -3
  1066. 2 | 3 | two | 5 | -5 | 2 | 4
  1067. 2 | 3 | two | 5 | -5 | 5 | -5
  1068. 2 | 3 | two | 5 | -5 | 5 | -5
  1069. 2 | 3 | two | 5 | -5 | 0 |
  1070. 2 | 3 | two | 5 | -5 | |
  1071. 2 | 3 | two | 5 | -5 | | 0
  1072. 3 | 2 | three | 5 | -5 | 1 | -1
  1073. 3 | 2 | three | 5 | -5 | 2 | 2
  1074. 3 | 2 | three | 5 | -5 | 3 | -3
  1075. 3 | 2 | three | 5 | -5 | 2 | 4
  1076. 3 | 2 | three | 5 | -5 | 5 | -5
  1077. 3 | 2 | three | 5 | -5 | 5 | -5
  1078. 3 | 2 | three | 5 | -5 | 0 |
  1079. 3 | 2 | three | 5 | -5 | |
  1080. 3 | 2 | three | 5 | -5 | | 0
  1081. 4 | 1 | four | 5 | -5 | 1 | -1
  1082. 4 | 1 | four | 5 | -5 | 2 | 2
  1083. 4 | 1 | four | 5 | -5 | 3 | -3
  1084. 4 | 1 | four | 5 | -5 | 2 | 4
  1085. 4 | 1 | four | 5 | -5 | 5 | -5
  1086. 4 | 1 | four | 5 | -5 | 5 | -5
  1087. 4 | 1 | four | 5 | -5 | 0 |
  1088. 4 | 1 | four | 5 | -5 | |
  1089. 4 | 1 | four | 5 | -5 | | 0
  1090. 5 | 0 | five | 5 | -5 | 1 | -1
  1091. 5 | 0 | five | 5 | -5 | 2 | 2
  1092. 5 | 0 | five | 5 | -5 | 3 | -3
  1093. 5 | 0 | five | 5 | -5 | 2 | 4
  1094. 5 | 0 | five | 5 | -5 | 5 | -5
  1095. 5 | 0 | five | 5 | -5 | 5 | -5
  1096. 5 | 0 | five | 5 | -5 | 0 |
  1097. 5 | 0 | five | 5 | -5 | |
  1098. 5 | 0 | five | 5 | -5 | | 0
  1099. 6 | 6 | six | 5 | -5 | 1 | -1
  1100. 6 | 6 | six | 5 | -5 | 2 | 2
  1101. 6 | 6 | six | 5 | -5 | 3 | -3
  1102. 6 | 6 | six | 5 | -5 | 2 | 4
  1103. 6 | 6 | six | 5 | -5 | 5 | -5
  1104. 6 | 6 | six | 5 | -5 | 5 | -5
  1105. 6 | 6 | six | 5 | -5 | 0 |
  1106. 6 | 6 | six | 5 | -5 | |
  1107. 6 | 6 | six | 5 | -5 | | 0
  1108. 7 | 7 | seven | 5 | -5 | 1 | -1
  1109. 7 | 7 | seven | 5 | -5 | 2 | 2
  1110. 7 | 7 | seven | 5 | -5 | 3 | -3
  1111. 7 | 7 | seven | 5 | -5 | 2 | 4
  1112. 7 | 7 | seven | 5 | -5 | 5 | -5
  1113. 7 | 7 | seven | 5 | -5 | 5 | -5
  1114. 7 | 7 | seven | 5 | -5 | 0 |
  1115. 7 | 7 | seven | 5 | -5 | |
  1116. 7 | 7 | seven | 5 | -5 | | 0
  1117. 8 | 8 | eight | 5 | -5 | 1 | -1
  1118. 8 | 8 | eight | 5 | -5 | 2 | 2
  1119. 8 | 8 | eight | 5 | -5 | 3 | -3
  1120. 8 | 8 | eight | 5 | -5 | 2 | 4
  1121. 8 | 8 | eight | 5 | -5 | 5 | -5
  1122. 8 | 8 | eight | 5 | -5 | 5 | -5
  1123. 8 | 8 | eight | 5 | -5 | 0 |
  1124. 8 | 8 | eight | 5 | -5 | |
  1125. 8 | 8 | eight | 5 | -5 | | 0
  1126. 0 | | zero | 5 | -5 | 1 | -1
  1127. 0 | | zero | 5 | -5 | 2 | 2
  1128. 0 | | zero | 5 | -5 | 3 | -3
  1129. 0 | | zero | 5 | -5 | 2 | 4
  1130. 0 | | zero | 5 | -5 | 5 | -5
  1131. 0 | | zero | 5 | -5 | 5 | -5
  1132. 0 | | zero | 5 | -5 | 0 |
  1133. 0 | | zero | 5 | -5 | |
  1134. 0 | | zero | 5 | -5 | | 0
  1135. | | null | 5 | -5 | 1 | -1
  1136. | | null | 5 | -5 | 2 | 2
  1137. | | null | 5 | -5 | 3 | -3
  1138. | | null | 5 | -5 | 2 | 4
  1139. | | null | 5 | -5 | 5 | -5
  1140. | | null | 5 | -5 | 5 | -5
  1141. | | null | 5 | -5 | 0 |
  1142. | | null | 5 | -5 | |
  1143. | | null | 5 | -5 | | 0
  1144. | 0 | zero | 5 | -5 | 1 | -1
  1145. | 0 | zero | 5 | -5 | 2 | 2
  1146. | 0 | zero | 5 | -5 | 3 | -3
  1147. | 0 | zero | 5 | -5 | 2 | 4
  1148. | 0 | zero | 5 | -5 | 5 | -5
  1149. | 0 | zero | 5 | -5 | 5 | -5
  1150. | 0 | zero | 5 | -5 | 0 |
  1151. | 0 | zero | 5 | -5 | |
  1152. | 0 | zero | 5 | -5 | | 0
  1153. 1 | 4 | one | 5 | -5 | 1 | -1
  1154. 1 | 4 | one | 5 | -5 | 2 | 2
  1155. 1 | 4 | one | 5 | -5 | 3 | -3
  1156. 1 | 4 | one | 5 | -5 | 2 | 4
  1157. 1 | 4 | one | 5 | -5 | 5 | -5
  1158. 1 | 4 | one | 5 | -5 | 5 | -5
  1159. 1 | 4 | one | 5 | -5 | 0 |
  1160. 1 | 4 | one | 5 | -5 | |
  1161. 1 | 4 | one | 5 | -5 | | 0
  1162. 2 | 3 | two | 5 | -5 | 1 | -1
  1163. 2 | 3 | two | 5 | -5 | 2 | 2
  1164. 2 | 3 | two | 5 | -5 | 3 | -3
  1165. 2 | 3 | two | 5 | -5 | 2 | 4
  1166. 2 | 3 | two | 5 | -5 | 5 | -5
  1167. 2 | 3 | two | 5 | -5 | 5 | -5
  1168. 2 | 3 | two | 5 | -5 | 0 |
  1169. 2 | 3 | two | 5 | -5 | |
  1170. 2 | 3 | two | 5 | -5 | | 0
  1171. 3 | 2 | three | 5 | -5 | 1 | -1
  1172. 3 | 2 | three | 5 | -5 | 2 | 2
  1173. 3 | 2 | three | 5 | -5 | 3 | -3
  1174. 3 | 2 | three | 5 | -5 | 2 | 4
  1175. 3 | 2 | three | 5 | -5 | 5 | -5
  1176. 3 | 2 | three | 5 | -5 | 5 | -5
  1177. 3 | 2 | three | 5 | -5 | 0 |
  1178. 3 | 2 | three | 5 | -5 | |
  1179. 3 | 2 | three | 5 | -5 | | 0
  1180. 4 | 1 | four | 5 | -5 | 1 | -1
  1181. 4 | 1 | four | 5 | -5 | 2 | 2
  1182. 4 | 1 | four | 5 | -5 | 3 | -3
  1183. 4 | 1 | four | 5 | -5 | 2 | 4
  1184. 4 | 1 | four | 5 | -5 | 5 | -5
  1185. 4 | 1 | four | 5 | -5 | 5 | -5
  1186. 4 | 1 | four | 5 | -5 | 0 |
  1187. 4 | 1 | four | 5 | -5 | |
  1188. 4 | 1 | four | 5 | -5 | | 0
  1189. 5 | 0 | five | 5 | -5 | 1 | -1
  1190. 5 | 0 | five | 5 | -5 | 2 | 2
  1191. 5 | 0 | five | 5 | -5 | 3 | -3
  1192. 5 | 0 | five | 5 | -5 | 2 | 4
  1193. 5 | 0 | five | 5 | -5 | 5 | -5
  1194. 5 | 0 | five | 5 | -5 | 5 | -5
  1195. 5 | 0 | five | 5 | -5 | 0 |
  1196. 5 | 0 | five | 5 | -5 | |
  1197. 5 | 0 | five | 5 | -5 | | 0
  1198. 6 | 6 | six | 5 | -5 | 1 | -1
  1199. 6 | 6 | six | 5 | -5 | 2 | 2
  1200. 6 | 6 | six | 5 | -5 | 3 | -3
  1201. 6 | 6 | six | 5 | -5 | 2 | 4
  1202. 6 | 6 | six | 5 | -5 | 5 | -5
  1203. 6 | 6 | six | 5 | -5 | 5 | -5
  1204. 6 | 6 | six | 5 | -5 | 0 |
  1205. 6 | 6 | six | 5 | -5 | |
  1206. 6 | 6 | six | 5 | -5 | | 0
  1207. 7 | 7 | seven | 5 | -5 | 1 | -1
  1208. 7 | 7 | seven | 5 | -5 | 2 | 2
  1209. 7 | 7 | seven | 5 | -5 | 3 | -3
  1210. 7 | 7 | seven | 5 | -5 | 2 | 4
  1211. 7 | 7 | seven | 5 | -5 | 5 | -5
  1212. 7 | 7 | seven | 5 | -5 | 5 | -5
  1213. 7 | 7 | seven | 5 | -5 | 0 |
  1214. 7 | 7 | seven | 5 | -5 | |
  1215. 7 | 7 | seven | 5 | -5 | | 0
  1216. 8 | 8 | eight | 5 | -5 | 1 | -1
  1217. 8 | 8 | eight | 5 | -5 | 2 | 2
  1218. 8 | 8 | eight | 5 | -5 | 3 | -3
  1219. 8 | 8 | eight | 5 | -5 | 2 | 4
  1220. 8 | 8 | eight | 5 | -5 | 5 | -5
  1221. 8 | 8 | eight | 5 | -5 | 5 | -5
  1222. 8 | 8 | eight | 5 | -5 | 0 |
  1223. 8 | 8 | eight | 5 | -5 | |
  1224. 8 | 8 | eight | 5 | -5 | | 0
  1225. 0 | | zero | 5 | -5 | 1 | -1
  1226. 0 | | zero | 5 | -5 | 2 | 2
  1227. 0 | | zero | 5 | -5 | 3 | -3
  1228. 0 | | zero | 5 | -5 | 2 | 4
  1229. 0 | | zero | 5 | -5 | 5 | -5
  1230. 0 | | zero | 5 | -5 | 5 | -5
  1231. 0 | | zero | 5 | -5 | 0 |
  1232. 0 | | zero | 5 | -5 | |
  1233. 0 | | zero | 5 | -5 | | 0
  1234. | | null | 5 | -5 | 1 | -1
  1235. | | null | 5 | -5 | 2 | 2
  1236. | | null | 5 | -5 | 3 | -3
  1237. | | null | 5 | -5 | 2 | 4
  1238. | | null | 5 | -5 | 5 | -5
  1239. | | null | 5 | -5 | 5 | -5
  1240. | | null | 5 | -5 | 0 |
  1241. | | null | 5 | -5 | |
  1242. | | null | 5 | -5 | | 0
  1243. | 0 | zero | 5 | -5 | 1 | -1
  1244. | 0 | zero | 5 | -5 | 2 | 2
  1245. | 0 | zero | 5 | -5 | 3 | -3
  1246. | 0 | zero | 5 | -5 | 2 | 4
  1247. | 0 | zero | 5 | -5 | 5 | -5
  1248. | 0 | zero | 5 | -5 | 5 | -5
  1249. | 0 | zero | 5 | -5 | 0 |
  1250. | 0 | zero | 5 | -5 | |
  1251. | 0 | zero | 5 | -5 | | 0
  1252. 1 | 4 | one | 0 | | 1 | -1
  1253. 1 | 4 | one | 0 | | 2 | 2
  1254. 1 | 4 | one | 0 | | 3 | -3
  1255. 1 | 4 | one | 0 | | 2 | 4
  1256. 1 | 4 | one | 0 | | 5 | -5
  1257. 1 | 4 | one | 0 | | 5 | -5
  1258. 1 | 4 | one | 0 | | 0 |
  1259. 1 | 4 | one | 0 | | |
  1260. 1 | 4 | one | 0 | | | 0
  1261. 2 | 3 | two | 0 | | 1 | -1
  1262. 2 | 3 | two | 0 | | 2 | 2
  1263. 2 | 3 | two | 0 | | 3 | -3
  1264. 2 | 3 | two | 0 | | 2 | 4
  1265. 2 | 3 | two | 0 | | 5 | -5
  1266. 2 | 3 | two | 0 | | 5 | -5
  1267. 2 | 3 | two | 0 | | 0 |
  1268. 2 | 3 | two | 0 | | |
  1269. 2 | 3 | two | 0 | | | 0
  1270. 3 | 2 | three | 0 | | 1 | -1
  1271. 3 | 2 | three | 0 | | 2 | 2
  1272. 3 | 2 | three | 0 | | 3 | -3
  1273. 3 | 2 | three | 0 | | 2 | 4
  1274. 3 | 2 | three | 0 | | 5 | -5
  1275. 3 | 2 | three | 0 | | 5 | -5
  1276. 3 | 2 | three | 0 | | 0 |
  1277. 3 | 2 | three | 0 | | |
  1278. 3 | 2 | three | 0 | | | 0
  1279. 4 | 1 | four | 0 | | 1 | -1
  1280. 4 | 1 | four | 0 | | 2 | 2
  1281. 4 | 1 | four | 0 | | 3 | -3
  1282. 4 | 1 | four | 0 | | 2 | 4
  1283. 4 | 1 | four | 0 | | 5 | -5
  1284. 4 | 1 | four | 0 | | 5 | -5
  1285. 4 | 1 | four | 0 | | 0 |
  1286. 4 | 1 | four | 0 | | |
  1287. 4 | 1 | four | 0 | | | 0
  1288. 5 | 0 | five | 0 | | 1 | -1
  1289. 5 | 0 | five | 0 | | 2 | 2
  1290. 5 | 0 | five | 0 | | 3 | -3
  1291. 5 | 0 | five | 0 | | 2 | 4
  1292. 5 | 0 | five | 0 | | 5 | -5
  1293. 5 | 0 | five | 0 | | 5 | -5
  1294. 5 | 0 | five | 0 | | 0 |
  1295. 5 | 0 | five | 0 | | |
  1296. 5 | 0 | five | 0 | | | 0
  1297. 6 | 6 | six | 0 | | 1 | -1
  1298. 6 | 6 | six | 0 | | 2 | 2
  1299. 6 | 6 | six | 0 | | 3 | -3
  1300. 6 | 6 | six | 0 | | 2 | 4
  1301. 6 | 6 | six | 0 | | 5 | -5
  1302. 6 | 6 | six | 0 | | 5 | -5
  1303. 6 | 6 | six | 0 | | 0 |
  1304. 6 | 6 | six | 0 | | |
  1305. 6 | 6 | six | 0 | | | 0
  1306. 7 | 7 | seven | 0 | | 1 | -1
  1307. 7 | 7 | seven | 0 | | 2 | 2
  1308. 7 | 7 | seven | 0 | | 3 | -3
  1309. 7 | 7 | seven | 0 | | 2 | 4
  1310. 7 | 7 | seven | 0 | | 5 | -5
  1311. 7 | 7 | seven | 0 | | 5 | -5
  1312. 7 | 7 | seven | 0 | | 0 |
  1313. 7 | 7 | seven | 0 | | |
  1314. 7 | 7 | seven | 0 | | | 0
  1315. 8 | 8 | eight | 0 | | 1 | -1
  1316. 8 | 8 | eight | 0 | | 2 | 2
  1317. 8 | 8 | eight | 0 | | 3 | -3
  1318. 8 | 8 | eight | 0 | | 2 | 4
  1319. 8 | 8 | eight | 0 | | 5 | -5
  1320. 8 | 8 | eight | 0 | | 5 | -5
  1321. 8 | 8 | eight | 0 | | 0 |
  1322. 8 | 8 | eight | 0 | | |
  1323. 8 | 8 | eight | 0 | | | 0
  1324. 0 | | zero | 0 | | 1 | -1
  1325. 0 | | zero | 0 | | 2 | 2
  1326. 0 | | zero | 0 | | 3 | -3
  1327. 0 | | zero | 0 | | 2 | 4
  1328. 0 | | zero | 0 | | 5 | -5
  1329. 0 | | zero | 0 | | 5 | -5
  1330. 0 | | zero | 0 | | 0 |
  1331. 0 | | zero | 0 | | |
  1332. 0 | | zero | 0 | | | 0
  1333. | | null | 0 | | 1 | -1
  1334. | | null | 0 | | 2 | 2
  1335. | | null | 0 | | 3 | -3
  1336. | | null | 0 | | 2 | 4
  1337. | | null | 0 | | 5 | -5
  1338. | | null | 0 | | 5 | -5
  1339. | | null | 0 | | 0 |
  1340. | | null | 0 | | |
  1341. | | null | 0 | | | 0
  1342. | 0 | zero | 0 | | 1 | -1
  1343. | 0 | zero | 0 | | 2 | 2
  1344. | 0 | zero | 0 | | 3 | -3
  1345. | 0 | zero | 0 | | 2 | 4
  1346. | 0 | zero | 0 | | 5 | -5
  1347. | 0 | zero | 0 | | 5 | -5
  1348. | 0 | zero | 0 | | 0 |
  1349. | 0 | zero | 0 | | |
  1350. | 0 | zero | 0 | | | 0
  1351. 1 | 4 | one | | | 1 | -1
  1352. 1 | 4 | one | | | 2 | 2
  1353. 1 | 4 | one | | | 3 | -3
  1354. 1 | 4 | one | | | 2 | 4
  1355. 1 | 4 | one | | | 5 | -5
  1356. 1 | 4 | one | | | 5 | -5
  1357. 1 | 4 | one | | | 0 |
  1358. 1 | 4 | one | | | |
  1359. 1 | 4 | one | | | | 0
  1360. 2 | 3 | two | | | 1 | -1
  1361. 2 | 3 | two | | | 2 | 2
  1362. 2 | 3 | two | | | 3 | -3
  1363. 2 | 3 | two | | | 2 | 4
  1364. 2 | 3 | two | | | 5 | -5
  1365. 2 | 3 | two | | | 5 | -5
  1366. 2 | 3 | two | | | 0 |
  1367. 2 | 3 | two | | | |
  1368. 2 | 3 | two | | | | 0
  1369. 3 | 2 | three | | | 1 | -1
  1370. 3 | 2 | three | | | 2 | 2
  1371. 3 | 2 | three | | | 3 | -3
  1372. 3 | 2 | three | | | 2 | 4
  1373. 3 | 2 | three | | | 5 | -5
  1374. 3 | 2 | three | | | 5 | -5
  1375. 3 | 2 | three | | | 0 |
  1376. 3 | 2 | three | | | |
  1377. 3 | 2 | three | | | | 0
  1378. 4 | 1 | four | | | 1 | -1
  1379. 4 | 1 | four | | | 2 | 2
  1380. 4 | 1 | four | | | 3 | -3
  1381. 4 | 1 | four | | | 2 | 4
  1382. 4 | 1 | four | | | 5 | -5
  1383. 4 | 1 | four | | | 5 | -5
  1384. 4 | 1 | four | | | 0 |
  1385. 4 | 1 | four | | | |
  1386. 4 | 1 | four | | | | 0
  1387. 5 | 0 | five | | | 1 | -1
  1388. 5 | 0 | five | | | 2 | 2
  1389. 5 | 0 | five | | | 3 | -3
  1390. 5 | 0 | five | | | 2 | 4
  1391. 5 | 0 | five | | | 5 | -5
  1392. 5 | 0 | five | | | 5 | -5
  1393. 5 | 0 | five | | | 0 |
  1394. 5 | 0 | five | | | |
  1395. 5 | 0 | five | | | | 0
  1396. 6 | 6 | six | | | 1 | -1
  1397. 6 | 6 | six | | | 2 | 2
  1398. 6 | 6 | six | | | 3 | -3
  1399. 6 | 6 | six | | | 2 | 4
  1400. 6 | 6 | six | | | 5 | -5
  1401. 6 | 6 | six | | | 5 | -5
  1402. 6 | 6 | six | | | 0 |
  1403. 6 | 6 | six | | | |
  1404. 6 | 6 | six | | | | 0
  1405. 7 | 7 | seven | | | 1 | -1
  1406. 7 | 7 | seven | | | 2 | 2
  1407. 7 | 7 | seven | | | 3 | -3
  1408. 7 | 7 | seven | | | 2 | 4
  1409. 7 | 7 | seven | | | 5 | -5
  1410. 7 | 7 | seven | | | 5 | -5
  1411. 7 | 7 | seven | | | 0 |
  1412. 7 | 7 | seven | | | |
  1413. 7 | 7 | seven | | | | 0
  1414. 8 | 8 | eight | | | 1 | -1
  1415. 8 | 8 | eight | | | 2 | 2
  1416. 8 | 8 | eight | | | 3 | -3
  1417. 8 | 8 | eight | | | 2 | 4
  1418. 8 | 8 | eight | | | 5 | -5
  1419. 8 | 8 | eight | | | 5 | -5
  1420. 8 | 8 | eight | | | 0 |
  1421. 8 | 8 | eight | | | |
  1422. 8 | 8 | eight | | | | 0
  1423. 0 | | zero | | | 1 | -1
  1424. 0 | | zero | | | 2 | 2
  1425. 0 | | zero | | | 3 | -3
  1426. 0 | | zero | | | 2 | 4
  1427. 0 | | zero | | | 5 | -5
  1428. 0 | | zero | | | 5 | -5
  1429. 0 | | zero | | | 0 |
  1430. 0 | | zero | | | |
  1431. 0 | | zero | | | | 0
  1432. | | null | | | 1 | -1
  1433. | | null | | | 2 | 2
  1434. | | null | | | 3 | -3
  1435. | | null | | | 2 | 4
  1436. | | null | | | 5 | -5
  1437. | | null | | | 5 | -5
  1438. | | null | | | 0 |
  1439. | | null | | | |
  1440. | | null | | | | 0
  1441. | 0 | zero | | | 1 | -1
  1442. | 0 | zero | | | 2 | 2
  1443. | 0 | zero | | | 3 | -3
  1444. | 0 | zero | | | 2 | 4
  1445. | 0 | zero | | | 5 | -5
  1446. | 0 | zero | | | 5 | -5
  1447. | 0 | zero | | | 0 |
  1448. | 0 | zero | | | |
  1449. | 0 | zero | | | | 0
  1450. 1 | 4 | one | | 0 | 1 | -1
  1451. 1 | 4 | one | | 0 | 2 | 2
  1452. 1 | 4 | one | | 0 | 3 | -3
  1453. 1 | 4 | one | | 0 | 2 | 4
  1454. 1 | 4 | one | | 0 | 5 | -5
  1455. 1 | 4 | one | | 0 | 5 | -5
  1456. 1 | 4 | one | | 0 | 0 |
  1457. 1 | 4 | one | | 0 | |
  1458. 1 | 4 | one | | 0 | | 0
  1459. 2 | 3 | two | | 0 | 1 | -1
  1460. 2 | 3 | two | | 0 | 2 | 2
  1461. 2 | 3 | two | | 0 | 3 | -3
  1462. 2 | 3 | two | | 0 | 2 | 4
  1463. 2 | 3 | two | | 0 | 5 | -5
  1464. 2 | 3 | two | | 0 | 5 | -5
  1465. 2 | 3 | two | | 0 | 0 |
  1466. 2 | 3 | two | | 0 | |
  1467. 2 | 3 | two | | 0 | | 0
  1468. 3 | 2 | three | | 0 | 1 | -1
  1469. 3 | 2 | three | | 0 | 2 | 2
  1470. 3 | 2 | three | | 0 | 3 | -3
  1471. 3 | 2 | three | | 0 | 2 | 4
  1472. 3 | 2 | three | | 0 | 5 | -5
  1473. 3 | 2 | three | | 0 | 5 | -5
  1474. 3 | 2 | three | | 0 | 0 |
  1475. 3 | 2 | three | | 0 | |
  1476. 3 | 2 | three | | 0 | | 0
  1477. 4 | 1 | four | | 0 | 1 | -1
  1478. 4 | 1 | four | | 0 | 2 | 2
  1479. 4 | 1 | four | | 0 | 3 | -3
  1480. 4 | 1 | four | | 0 | 2 | 4
  1481. 4 | 1 | four | | 0 | 5 | -5
  1482. 4 | 1 | four | | 0 | 5 | -5
  1483. 4 | 1 | four | | 0 | 0 |
  1484. 4 | 1 | four | | 0 | |
  1485. 4 | 1 | four | | 0 | | 0
  1486. 5 | 0 | five | | 0 | 1 | -1
  1487. 5 | 0 | five | | 0 | 2 | 2
  1488. 5 | 0 | five | | 0 | 3 | -3
  1489. 5 | 0 | five | | 0 | 2 | 4
  1490. 5 | 0 | five | | 0 | 5 | -5
  1491. 5 | 0 | five | | 0 | 5 | -5
  1492. 5 | 0 | five | | 0 | 0 |
  1493. 5 | 0 | five | | 0 | |
  1494. 5 | 0 | five | | 0 | | 0
  1495. 6 | 6 | six | | 0 | 1 | -1
  1496. 6 | 6 | six | | 0 | 2 | 2
  1497. 6 | 6 | six | | 0 | 3 | -3
  1498. 6 | 6 | six | | 0 | 2 | 4
  1499. 6 | 6 | six | | 0 | 5 | -5
  1500. 6 | 6 | six | | 0 | 5 | -5
  1501. 6 | 6 | six | | 0 | 0 |
  1502. 6 | 6 | six | | 0 | |
  1503. 6 | 6 | six | | 0 | | 0
  1504. 7 | 7 | seven | | 0 | 1 | -1
  1505. 7 | 7 | seven | | 0 | 2 | 2
  1506. 7 | 7 | seven | | 0 | 3 | -3
  1507. 7 | 7 | seven | | 0 | 2 | 4
  1508. 7 | 7 | seven | | 0 | 5 | -5
  1509. 7 | 7 | seven | | 0 | 5 | -5
  1510. 7 | 7 | seven | | 0 | 0 |
  1511. 7 | 7 | seven | | 0 | |
  1512. 7 | 7 | seven | | 0 | | 0
  1513. 8 | 8 | eight | | 0 | 1 | -1
  1514. 8 | 8 | eight | | 0 | 2 | 2
  1515. 8 | 8 | eight | | 0 | 3 | -3
  1516. 8 | 8 | eight | | 0 | 2 | 4
  1517. 8 | 8 | eight | | 0 | 5 | -5
  1518. 8 | 8 | eight | | 0 | 5 | -5
  1519. 8 | 8 | eight | | 0 | 0 |
  1520. 8 | 8 | eight | | 0 | |
  1521. 8 | 8 | eight | | 0 | | 0
  1522. 0 | | zero | | 0 | 1 | -1
  1523. 0 | | zero | | 0 | 2 | 2
  1524. 0 | | zero | | 0 | 3 | -3
  1525. 0 | | zero | | 0 | 2 | 4
  1526. 0 | | zero | | 0 | 5 | -5
  1527. 0 | | zero | | 0 | 5 | -5
  1528. 0 | | zero | | 0 | 0 |
  1529. 0 | | zero | | 0 | |
  1530. 0 | | zero | | 0 | | 0
  1531. | | null | | 0 | 1 | -1
  1532. | | null | | 0 | 2 | 2
  1533. | | null | | 0 | 3 | -3
  1534. | | null | | 0 | 2 | 4
  1535. | | null | | 0 | 5 | -5
  1536. | | null | | 0 | 5 | -5
  1537. | | null | | 0 | 0 |
  1538. | | null | | 0 | |
  1539. | | null | | 0 | | 0
  1540. | 0 | zero | | 0 | 1 | -1
  1541. | 0 | zero | | 0 | 2 | 2
  1542. | 0 | zero | | 0 | 3 | -3
  1543. | 0 | zero | | 0 | 2 | 4
  1544. | 0 | zero | | 0 | 5 | -5
  1545. | 0 | zero | | 0 | 5 | -5
  1546. | 0 | zero | | 0 | 0 |
  1547. | 0 | zero | | 0 | |
  1548. | 0 | zero | | 0 | | 0
  1549. (891 rows)
  1550. --
  1551. --
  1552. -- Inner joins (equi-joins)
  1553. --
  1554. --
  1555. --
  1556. -- Inner joins (equi-joins) with USING clause
  1557. -- The USING syntax changes the shape of the resulting table
  1558. -- by including a column in the USING clause only once in the result.
  1559. --
  1560. -- Inner equi-join on specified column
  1561. SELECT *
  1562. FROM J1_TBL INNER JOIN J2_TBL USING (i);
  1563. i | j | t | k
  1564. ---+---+-------+----
  1565. 0 | | zero |
  1566. 1 | 4 | one | -1
  1567. 2 | 3 | two | 2
  1568. 2 | 3 | two | 4
  1569. 3 | 2 | three | -3
  1570. 5 | 0 | five | -5
  1571. 5 | 0 | five | -5
  1572. (7 rows)
  1573. -- Same as above, slightly different syntax
  1574. SELECT *
  1575. FROM J1_TBL JOIN J2_TBL USING (i);
  1576. i | j | t | k
  1577. ---+---+-------+----
  1578. 0 | | zero |
  1579. 1 | 4 | one | -1
  1580. 2 | 3 | two | 2
  1581. 2 | 3 | two | 4
  1582. 3 | 2 | three | -3
  1583. 5 | 0 | five | -5
  1584. 5 | 0 | five | -5
  1585. (7 rows)
  1586. SELECT *
  1587. FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
  1588. ORDER BY a, d;
  1589. a | b | c | d
  1590. ---+---+-------+----
  1591. 0 | | zero |
  1592. 1 | 4 | one | -1
  1593. 2 | 3 | two | 2
  1594. 2 | 3 | two | 4
  1595. 3 | 2 | three | -3
  1596. 5 | 0 | five | -5
  1597. 5 | 0 | five | -5
  1598. (7 rows)
  1599. SELECT *
  1600. FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b)
  1601. ORDER BY b, t1.a;
  1602. b | a | c | a
  1603. ---+---+-------+---
  1604. 0 | 5 | five |
  1605. 0 | | zero |
  1606. 2 | 3 | three | 2
  1607. 4 | 1 | one | 2
  1608. (4 rows)
  1609. -- test join using aliases
  1610. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) WHERE J1_TBL.t = 'one'; -- ok
  1611. i | j | t | k
  1612. ---+---+-----+----
  1613. 1 | 4 | one | -1
  1614. (1 row)
  1615. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one'; -- ok
  1616. i | j | t | k
  1617. ---+---+-----+----
  1618. 1 | 4 | one | -1
  1619. (1 row)
  1620. SELECT * FROM (J1_TBL JOIN J2_TBL USING (i)) AS x WHERE J1_TBL.t = 'one'; -- error
  1621. ERROR: invalid reference to FROM-clause entry for table "j1_tbl"
  1622. LINE 1: ... * FROM (J1_TBL JOIN J2_TBL USING (i)) AS x WHERE J1_TBL.t =...
  1623. ^
  1624. HINT: There is an entry for table "j1_tbl", but it cannot be referenced from this part of the query.
  1625. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE x.i = 1; -- ok
  1626. i | j | t | k
  1627. ---+---+-----+----
  1628. 1 | 4 | one | -1
  1629. (1 row)
  1630. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE x.t = 'one'; -- error
  1631. ERROR: column x.t does not exist
  1632. LINE 1: ...CT * FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE x.t = 'one...
  1633. ^
  1634. SELECT * FROM (J1_TBL JOIN J2_TBL USING (i) AS x) AS xx WHERE x.i = 1; -- error (XXX could use better hint)
  1635. ERROR: missing FROM-clause entry for table "x"
  1636. LINE 1: ...ROM (J1_TBL JOIN J2_TBL USING (i) AS x) AS xx WHERE x.i = 1;
  1637. ^
  1638. SELECT * FROM J1_TBL a1 JOIN J2_TBL a2 USING (i) AS a1; -- error
  1639. ERROR: table name "a1" specified more than once
  1640. SELECT x.* FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one';
  1641. i
  1642. ---
  1643. 1
  1644. (1 row)
  1645. SELECT ROW(x.*) FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one';
  1646. row
  1647. -----
  1648. (1)
  1649. (1 row)
  1650. SELECT row_to_json(x.*) FROM J1_TBL JOIN J2_TBL USING (i) AS x WHERE J1_TBL.t = 'one';
  1651. row_to_json
  1652. -------------
  1653. {"i":1}
  1654. (1 row)
  1655. --
  1656. -- NATURAL JOIN
  1657. -- Inner equi-join on all columns with the same name
  1658. --
  1659. SELECT *
  1660. FROM J1_TBL NATURAL JOIN J2_TBL;
  1661. i | j | t | k
  1662. ---+---+-------+----
  1663. 0 | | zero |
  1664. 1 | 4 | one | -1
  1665. 2 | 3 | two | 2
  1666. 2 | 3 | two | 4
  1667. 3 | 2 | three | -3
  1668. 5 | 0 | five | -5
  1669. 5 | 0 | five | -5
  1670. (7 rows)
  1671. SELECT *
  1672. FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
  1673. a | b | c | d
  1674. ---+---+-------+----
  1675. 0 | | zero |
  1676. 1 | 4 | one | -1
  1677. 2 | 3 | two | 2
  1678. 2 | 3 | two | 4
  1679. 3 | 2 | three | -3
  1680. 5 | 0 | five | -5
  1681. 5 | 0 | five | -5
  1682. (7 rows)
  1683. SELECT *
  1684. FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
  1685. a | b | c | d
  1686. ---+---+------+---
  1687. 0 | | zero |
  1688. 2 | 3 | two | 2
  1689. 4 | 1 | four | 2
  1690. (3 rows)
  1691. -- mismatch number of columns
  1692. -- currently, Postgres will fill in with underlying names
  1693. SELECT *
  1694. FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
  1695. a | b | t | k
  1696. ---+---+-------+----
  1697. 0 | | zero |
  1698. 1 | 4 | one | -1
  1699. 2 | 3 | two | 2
  1700. 2 | 3 | two | 4
  1701. 3 | 2 | three | -3
  1702. 5 | 0 | five | -5
  1703. 5 | 0 | five | -5
  1704. (7 rows)
  1705. --
  1706. -- Inner joins (equi-joins)
  1707. --
  1708. SELECT *
  1709. FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
  1710. i | j | t | i | k
  1711. ---+---+-------+---+----
  1712. 0 | | zero | 0 |
  1713. 1 | 4 | one | 1 | -1
  1714. 2 | 3 | two | 2 | 2
  1715. 2 | 3 | two | 2 | 4
  1716. 3 | 2 | three | 3 | -3
  1717. 5 | 0 | five | 5 | -5
  1718. 5 | 0 | five | 5 | -5
  1719. (7 rows)
  1720. SELECT *
  1721. FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
  1722. i | j | t | i | k
  1723. ---+---+------+---+---
  1724. 0 | | zero | | 0
  1725. 2 | 3 | two | 2 | 2
  1726. 4 | 1 | four | 2 | 4
  1727. (3 rows)
  1728. --
  1729. -- Non-equi-joins
  1730. --
  1731. SELECT *
  1732. FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
  1733. i | j | t | i | k
  1734. ---+---+-------+---+---
  1735. 1 | 4 | one | 2 | 2
  1736. 2 | 3 | two | 2 | 2
  1737. 0 | | zero | 2 | 2
  1738. 1 | 4 | one | 2 | 4
  1739. 2 | 3 | two | 2 | 4
  1740. 3 | 2 | three | 2 | 4
  1741. 4 | 1 | four | 2 | 4
  1742. 0 | | zero | 2 | 4
  1743. 0 | | zero | | 0
  1744. (9 rows)
  1745. --
  1746. -- Outer joins
  1747. -- Note that OUTER is a noise word
  1748. --
  1749. SELECT *
  1750. FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i)
  1751. ORDER BY i, k, t;
  1752. i | j | t | k
  1753. ---+---+-------+----
  1754. 0 | | zero |
  1755. 1 | 4 | one | -1
  1756. 2 | 3 | two | 2
  1757. 2 | 3 | two | 4
  1758. 3 | 2 | three | -3
  1759. 4 | 1 | four |
  1760. 5 | 0 | five | -5
  1761. 5 | 0 | five | -5
  1762. 6 | 6 | six |
  1763. 7 | 7 | seven |
  1764. 8 | 8 | eight |
  1765. | | null |
  1766. | 0 | zero |
  1767. (13 rows)
  1768. SELECT *
  1769. FROM J1_TBL LEFT JOIN J2_TBL USING (i)
  1770. ORDER BY i, k, t;
  1771. i | j | t | k
  1772. ---+---+-------+----
  1773. 0 | | zero |
  1774. 1 | 4 | one | -1
  1775. 2 | 3 | two | 2
  1776. 2 | 3 | two | 4
  1777. 3 | 2 | three | -3
  1778. 4 | 1 | four |
  1779. 5 | 0 | five | -5
  1780. 5 | 0 | five | -5
  1781. 6 | 6 | six |
  1782. 7 | 7 | seven |
  1783. 8 | 8 | eight |
  1784. | | null |
  1785. | 0 | zero |
  1786. (13 rows)
  1787. SELECT *
  1788. FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
  1789. i | j | t | k
  1790. ---+---+-------+----
  1791. 0 | | zero |
  1792. 1 | 4 | one | -1
  1793. 2 | 3 | two | 2
  1794. 2 | 3 | two | 4
  1795. 3 | 2 | three | -3
  1796. 5 | 0 | five | -5
  1797. 5 | 0 | five | -5
  1798. | | |
  1799. | | | 0
  1800. (9 rows)
  1801. SELECT *
  1802. FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
  1803. i | j | t | k
  1804. ---+---+-------+----
  1805. 0 | | zero |
  1806. 1 | 4 | one | -1
  1807. 2 | 3 | two | 2
  1808. 2 | 3 | two | 4
  1809. 3 | 2 | three | -3
  1810. 5 | 0 | five | -5
  1811. 5 | 0 | five | -5
  1812. | | |
  1813. | | | 0
  1814. (9 rows)
  1815. SELECT *
  1816. FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i)
  1817. ORDER BY i, k, t;
  1818. i | j | t | k
  1819. ---+---+-------+----
  1820. 0 | | zero |
  1821. 1 | 4 | one | -1
  1822. 2 | 3 | two | 2
  1823. 2 | 3 | two | 4
  1824. 3 | 2 | three | -3
  1825. 4 | 1 | four |
  1826. 5 | 0 | five | -5
  1827. 5 | 0 | five | -5
  1828. 6 | 6 | six |
  1829. 7 | 7 | seven |
  1830. 8 | 8 | eight |
  1831. | | | 0
  1832. | | null |
  1833. | 0 | zero |
  1834. | | |
  1835. (15 rows)
  1836. SELECT *
  1837. FROM J1_TBL FULL JOIN J2_TBL USING (i)
  1838. ORDER BY i, k, t;
  1839. i | j | t | k
  1840. ---+---+-------+----
  1841. 0 | | zero |
  1842. 1 | 4 | one | -1
  1843. 2 | 3 | two | 2
  1844. 2 | 3 | two | 4
  1845. 3 | 2 | three | -3
  1846. 4 | 1 | four |
  1847. 5 | 0 | five | -5
  1848. 5 | 0 | five | -5
  1849. 6 | 6 | six |
  1850. 7 | 7 | seven |
  1851. 8 | 8 | eight |
  1852. | | | 0
  1853. | | null |
  1854. | 0 | zero |
  1855. | | |
  1856. (15 rows)
  1857. SELECT *
  1858. FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
  1859. i | j | t | k
  1860. ---+---+---+---
  1861. (0 rows)
  1862. SELECT *
  1863. FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
  1864. i | j | t | k
  1865. ---+---+-----+----
  1866. 1 | 4 | one | -1
  1867. (1 row)
  1868. --
  1869. -- semijoin selectivity for <>
  1870. --
  1871. explain (costs off)
  1872. select * from int4_tbl i4, tenk1 a
  1873. where exists(select * from tenk1 b
  1874. where a.twothousand = b.twothousand and a.fivethous <> b.fivethous)
  1875. and i4.f1 = a.tenthous;
  1876. QUERY PLAN
  1877. ----------------------------------------------
  1878. Hash Semi Join
  1879. Hash Cond: (a.twothousand = b.twothousand)
  1880. Join Filter: (a.fivethous <> b.fivethous)
  1881. -> Hash Join
  1882. Hash Cond: (a.tenthous = i4.f1)
  1883. -> Seq Scan on tenk1 a
  1884. -> Hash
  1885. -> Seq Scan on int4_tbl i4
  1886. -> Hash
  1887. -> Seq Scan on tenk1 b
  1888. (10 rows)
  1889. --
  1890. -- More complicated constructs
  1891. --
  1892. --
  1893. -- Multiway full join
  1894. --
  1895. CREATE TABLE t1 (name TEXT, n INTEGER);
  1896. CREATE TABLE t2 (name TEXT, n INTEGER);
  1897. CREATE TABLE t3 (name TEXT, n INTEGER);
  1898. INSERT INTO t1 VALUES ( 'bb', 11 );
  1899. INSERT INTO t2 VALUES ( 'bb', 12 );
  1900. INSERT INTO t2 VALUES ( 'cc', 22 );
  1901. INSERT INTO t2 VALUES ( 'ee', 42 );
  1902. INSERT INTO t3 VALUES ( 'bb', 13 );
  1903. INSERT INTO t3 VALUES ( 'cc', 23 );
  1904. INSERT INTO t3 VALUES ( 'dd', 33 );
  1905. SELECT * FROM t1 FULL JOIN t2 USING (name) FULL JOIN t3 USING (name);
  1906. name | n | n | n
  1907. ------+----+----+----
  1908. bb | 11 | 12 | 13
  1909. cc | | 22 | 23
  1910. dd | | | 33
  1911. ee | | 42 |
  1912. (4 rows)
  1913. --
  1914. -- Test interactions of join syntax and subqueries
  1915. --
  1916. -- Basic cases (we expect planner to pull up the subquery here)
  1917. SELECT * FROM
  1918. (SELECT * FROM t2) as s2
  1919. INNER JOIN
  1920. (SELECT * FROM t3) s3
  1921. USING (name);
  1922. name | n | n
  1923. ------+----+----
  1924. bb | 12 | 13
  1925. cc | 22 | 23
  1926. (2 rows)
  1927. SELECT * FROM
  1928. (SELECT * FROM t2) as s2
  1929. LEFT JOIN
  1930. (SELECT * FROM t3) s3
  1931. USING (name);
  1932. name | n | n
  1933. ------+----+----
  1934. bb | 12 | 13
  1935. cc | 22 | 23
  1936. ee | 42 |
  1937. (3 rows)
  1938. SELECT * FROM
  1939. (SELECT * FROM t2) as s2
  1940. FULL JOIN
  1941. (SELECT * FROM t3) s3
  1942. USING (name);
  1943. name | n | n
  1944. ------+----+----
  1945. bb | 12 | 13
  1946. cc | 22 | 23
  1947. dd | | 33
  1948. ee | 42 |
  1949. (4 rows)
  1950. -- Cases with non-nullable expressions in subquery results;
  1951. -- make sure these go to null as expected
  1952. SELECT * FROM
  1953. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  1954. NATURAL INNER JOIN
  1955. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  1956. name | s2_n | s2_2 | s3_n | s3_2
  1957. ------+------+------+------+------
  1958. bb | 12 | 2 | 13 | 3
  1959. cc | 22 | 2 | 23 | 3
  1960. (2 rows)
  1961. SELECT * FROM
  1962. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  1963. NATURAL LEFT JOIN
  1964. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  1965. name | s2_n | s2_2 | s3_n | s3_2
  1966. ------+------+------+------+------
  1967. bb | 12 | 2 | 13 | 3
  1968. cc | 22 | 2 | 23 | 3
  1969. ee | 42 | 2 | |
  1970. (3 rows)
  1971. SELECT * FROM
  1972. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  1973. NATURAL FULL JOIN
  1974. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  1975. name | s2_n | s2_2 | s3_n | s3_2
  1976. ------+------+------+------+------
  1977. bb | 12 | 2 | 13 | 3
  1978. cc | 22 | 2 | 23 | 3
  1979. dd | | | 33 | 3
  1980. ee | 42 | 2 | |
  1981. (4 rows)
  1982. SELECT * FROM
  1983. (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
  1984. NATURAL INNER JOIN
  1985. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  1986. NATURAL INNER JOIN
  1987. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  1988. name | s1_n | s1_1 | s2_n | s2_2 | s3_n | s3_2
  1989. ------+------+------+------+------+------+------
  1990. bb | 11 | 1 | 12 | 2 | 13 | 3
  1991. (1 row)
  1992. SELECT * FROM
  1993. (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
  1994. NATURAL FULL JOIN
  1995. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  1996. NATURAL FULL JOIN
  1997. (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
  1998. name | s1_n | s1_1 | s2_n | s2_2 | s3_n | s3_2
  1999. ------+------+------+------+------+------+------
  2000. bb | 11 | 1 | 12 | 2 | 13 | 3
  2001. cc | | | 22 | 2 | 23 | 3
  2002. dd | | | | | 33 | 3
  2003. ee | | | 42 | 2 | |
  2004. (4 rows)
  2005. SELECT * FROM
  2006. (SELECT name, n as s1_n FROM t1) as s1
  2007. NATURAL FULL JOIN
  2008. (SELECT * FROM
  2009. (SELECT name, n as s2_n FROM t2) as s2
  2010. NATURAL FULL JOIN
  2011. (SELECT name, n as s3_n FROM t3) as s3
  2012. ) ss2;
  2013. name | s1_n | s2_n | s3_n
  2014. ------+------+------+------
  2015. bb | 11 | 12 | 13
  2016. cc | | 22 | 23
  2017. dd | | | 33
  2018. ee | | 42 |
  2019. (4 rows)
  2020. SELECT * FROM
  2021. (SELECT name, n as s1_n FROM t1) as s1
  2022. NATURAL FULL JOIN
  2023. (SELECT * FROM
  2024. (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
  2025. NATURAL FULL JOIN
  2026. (SELECT name, n as s3_n FROM t3) as s3
  2027. ) ss2;
  2028. name | s1_n | s2_n | s2_2 | s3_n
  2029. ------+------+------+------+------
  2030. bb | 11 | 12 | 2 | 13
  2031. cc | | 22 | 2 | 23
  2032. dd | | | | 33
  2033. ee | | 42 | 2 |
  2034. (4 rows)
  2035. -- Constants as join keys can also be problematic
  2036. SELECT * FROM
  2037. (SELECT name, n as s1_n FROM t1) as s1
  2038. FULL JOIN
  2039. (SELECT name, 2 as s2_n FROM t2) as s2
  2040. ON (s1_n = s2_n);
  2041. name | s1_n | name | s2_n
  2042. ------+------+------+------
  2043. | | bb | 2
  2044. | | cc | 2
  2045. | | ee | 2
  2046. bb | 11 | |
  2047. (4 rows)
  2048. -- Test for propagation of nullability constraints into sub-joins
  2049. create temp table x (x1 int, x2 int);
  2050. insert into x values (1,11);
  2051. insert into x values (2,22);
  2052. insert into x values (3,null);
  2053. insert into x values (4,44);
  2054. insert into x values (5,null);
  2055. create temp table y (y1 int, y2 int);
  2056. insert into y values (1,111);
  2057. insert into y values (2,222);
  2058. insert into y values (3,333);
  2059. insert into y values (4,null);
  2060. select * from x;
  2061. x1 | x2
  2062. ----+----
  2063. 1 | 11
  2064. 2 | 22
  2065. 3 |
  2066. 4 | 44
  2067. 5 |
  2068. (5 rows)
  2069. select * from y;
  2070. y1 | y2
  2071. ----+-----
  2072. 1 | 111
  2073. 2 | 222
  2074. 3 | 333
  2075. 4 |
  2076. (4 rows)
  2077. select * from x left join y on (x1 = y1 and x2 is not null);
  2078. x1 | x2 | y1 | y2
  2079. ----+----+----+-----
  2080. 1 | 11 | 1 | 111
  2081. 2 | 22 | 2 | 222
  2082. 3 | | |
  2083. 4 | 44 | 4 |
  2084. 5 | | |
  2085. (5 rows)
  2086. select * from x left join y on (x1 = y1 and y2 is not null);
  2087. x1 | x2 | y1 | y2
  2088. ----+----+----+-----
  2089. 1 | 11 | 1 | 111
  2090. 2 | 22 | 2 | 222
  2091. 3 | | 3 | 333
  2092. 4 | 44 | |
  2093. 5 | | |
  2094. (5 rows)
  2095. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2096. on (x1 = xx1);
  2097. x1 | x2 | y1 | y2 | xx1 | xx2
  2098. ----+----+----+-----+-----+-----
  2099. 1 | 11 | 1 | 111 | 1 | 11
  2100. 2 | 22 | 2 | 222 | 2 | 22
  2101. 3 | | 3 | 333 | 3 |
  2102. 4 | 44 | 4 | | 4 | 44
  2103. 5 | | | | 5 |
  2104. (5 rows)
  2105. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2106. on (x1 = xx1 and x2 is not null);
  2107. x1 | x2 | y1 | y2 | xx1 | xx2
  2108. ----+----+----+-----+-----+-----
  2109. 1 | 11 | 1 | 111 | 1 | 11
  2110. 2 | 22 | 2 | 222 | 2 | 22
  2111. 3 | | 3 | 333 | |
  2112. 4 | 44 | 4 | | 4 | 44
  2113. 5 | | | | |
  2114. (5 rows)
  2115. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2116. on (x1 = xx1 and y2 is not null);
  2117. x1 | x2 | y1 | y2 | xx1 | xx2
  2118. ----+----+----+-----+-----+-----
  2119. 1 | 11 | 1 | 111 | 1 | 11
  2120. 2 | 22 | 2 | 222 | 2 | 22
  2121. 3 | | 3 | 333 | 3 |
  2122. 4 | 44 | 4 | | |
  2123. 5 | | | | |
  2124. (5 rows)
  2125. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2126. on (x1 = xx1 and xx2 is not null);
  2127. x1 | x2 | y1 | y2 | xx1 | xx2
  2128. ----+----+----+-----+-----+-----
  2129. 1 | 11 | 1 | 111 | 1 | 11
  2130. 2 | 22 | 2 | 222 | 2 | 22
  2131. 3 | | 3 | 333 | |
  2132. 4 | 44 | 4 | | 4 | 44
  2133. 5 | | | | |
  2134. (5 rows)
  2135. -- these should NOT give the same answers as above
  2136. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2137. on (x1 = xx1) where (x2 is not null);
  2138. x1 | x2 | y1 | y2 | xx1 | xx2
  2139. ----+----+----+-----+-----+-----
  2140. 1 | 11 | 1 | 111 | 1 | 11
  2141. 2 | 22 | 2 | 222 | 2 | 22
  2142. 4 | 44 | 4 | | 4 | 44
  2143. (3 rows)
  2144. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2145. on (x1 = xx1) where (y2 is not null);
  2146. x1 | x2 | y1 | y2 | xx1 | xx2
  2147. ----+----+----+-----+-----+-----
  2148. 1 | 11 | 1 | 111 | 1 | 11
  2149. 2 | 22 | 2 | 222 | 2 | 22
  2150. 3 | | 3 | 333 | 3 |
  2151. (3 rows)
  2152. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  2153. on (x1 = xx1) where (xx2 is not null);
  2154. x1 | x2 | y1 | y2 | xx1 | xx2
  2155. ----+----+----+-----+-----+-----
  2156. 1 | 11 | 1 | 111 | 1 | 11
  2157. 2 | 22 | 2 | 222 | 2 | 22
  2158. 4 | 44 | 4 | | 4 | 44
  2159. (3 rows)
  2160. --
  2161. -- regression test: check for bug with propagation of implied equality
  2162. -- to outside an IN
  2163. --
  2164. select count(*) from tenk1 a where unique1 in
  2165. (select unique1 from tenk1 b join tenk1 c using (unique1)
  2166. where b.unique2 = 42);
  2167. count
  2168. -------
  2169. 1
  2170. (1 row)
  2171. --
  2172. -- regression test: check for failure to generate a plan with multiple
  2173. -- degenerate IN clauses
  2174. --
  2175. select count(*) from tenk1 x where
  2176. x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
  2177. x.unique1 = 0 and
  2178. x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
  2179. count
  2180. -------
  2181. 1
  2182. (1 row)
  2183. -- try that with GEQO too
  2184. begin;
  2185. set geqo = on;
  2186. set geqo_threshold = 2;
  2187. select count(*) from tenk1 x where
  2188. x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
  2189. x.unique1 = 0 and
  2190. x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
  2191. count
  2192. -------
  2193. 1
  2194. (1 row)
  2195. rollback;
  2196. --
  2197. -- regression test: be sure we cope with proven-dummy append rels
  2198. --
  2199. explain (costs off)
  2200. select aa, bb, unique1, unique1
  2201. from tenk1 right join b on aa = unique1
  2202. where bb < bb and bb is null;
  2203. QUERY PLAN
  2204. --------------------------
  2205. Result
  2206. One-Time Filter: false
  2207. (2 rows)
  2208. select aa, bb, unique1, unique1
  2209. from tenk1 right join b on aa = unique1
  2210. where bb < bb and bb is null;
  2211. aa | bb | unique1 | unique1
  2212. ----+----+---------+---------
  2213. (0 rows)
  2214. --
  2215. -- regression test: check handling of empty-FROM subquery underneath outer join
  2216. --
  2217. explain (costs off)
  2218. select * from int8_tbl i1 left join (int8_tbl i2 join
  2219. (select 123 as x) ss on i2.q1 = x) on i1.q2 = i2.q2
  2220. order by 1, 2;
  2221. QUERY PLAN
  2222. -------------------------------------------
  2223. Sort
  2224. Sort Key: i1.q1, i1.q2
  2225. -> Hash Left Join
  2226. Hash Cond: (i1.q2 = i2.q2)
  2227. -> Seq Scan on int8_tbl i1
  2228. -> Hash
  2229. -> Seq Scan on int8_tbl i2
  2230. Filter: (q1 = 123)
  2231. (8 rows)
  2232. select * from int8_tbl i1 left join (int8_tbl i2 join
  2233. (select 123 as x) ss on i2.q1 = x) on i1.q2 = i2.q2
  2234. order by 1, 2;
  2235. q1 | q2 | q1 | q2 | x
  2236. ------------------+-------------------+-----+------------------+-----
  2237. 123 | 456 | 123 | 456 | 123
  2238. 123 | 4567890123456789 | 123 | 4567890123456789 | 123
  2239. 4567890123456789 | -4567890123456789 | | |
  2240. 4567890123456789 | 123 | | |
  2241. 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 123
  2242. (5 rows)
  2243. --
  2244. -- regression test: check a case where join_clause_is_movable_into() gives
  2245. -- an imprecise result, causing an assertion failure
  2246. --
  2247. select count(*)
  2248. from
  2249. (select t3.tenthous as x1, coalesce(t1.stringu1, t2.stringu1) as x2
  2250. from tenk1 t1
  2251. left join tenk1 t2 on t1.unique1 = t2.unique1
  2252. join tenk1 t3 on t1.unique2 = t3.unique2) ss,
  2253. tenk1 t4,
  2254. tenk1 t5
  2255. where t4.thousand = t5.unique1 and ss.x1 = t4.tenthous and ss.x2 = t5.stringu1;
  2256. count
  2257. -------
  2258. 1000
  2259. (1 row)
  2260. --
  2261. -- regression test: check a case where we formerly missed including an EC
  2262. -- enforcement clause because it was expected to be handled at scan level
  2263. --
  2264. explain (costs off)
  2265. select a.f1, b.f1, t.thousand, t.tenthous from
  2266. tenk1 t,
  2267. (select sum(f1)+1 as f1 from int4_tbl i4a) a,
  2268. (select sum(f1) as f1 from int4_tbl i4b) b
  2269. where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
  2270. QUERY PLAN
  2271. -----------------------------------------------------------------------------------------------------------------------
  2272. Nested Loop
  2273. -> Aggregate
  2274. -> Seq Scan on int4_tbl i4b
  2275. -> Nested Loop
  2276. Join Filter: ((sum(i4b.f1)) = ((sum(i4a.f1) + 1)))
  2277. -> Aggregate
  2278. -> Seq Scan on int4_tbl i4a
  2279. -> Index Only Scan using tenk1_thous_tenthous on tenk1 t
  2280. Index Cond: ((thousand = (sum(i4b.f1))) AND (tenthous = ((((sum(i4a.f1) + 1)) + (sum(i4b.f1))) + 999)))
  2281. (9 rows)
  2282. select a.f1, b.f1, t.thousand, t.tenthous from
  2283. tenk1 t,
  2284. (select sum(f1)+1 as f1 from int4_tbl i4a) a,
  2285. (select sum(f1) as f1 from int4_tbl i4b) b
  2286. where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
  2287. f1 | f1 | thousand | tenthous
  2288. ----+----+----------+----------
  2289. (0 rows)
  2290. --
  2291. -- check a case where we formerly got confused by conflicting sort orders
  2292. -- in redundant merge join path keys
  2293. --
  2294. explain (costs off)
  2295. select * from
  2296. j1_tbl full join
  2297. (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k asc) j2_tbl
  2298. on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k;
  2299. QUERY PLAN
  2300. -----------------------------------------------------------------
  2301. Merge Full Join
  2302. Merge Cond: ((j2_tbl.i = j1_tbl.i) AND (j2_tbl.k = j1_tbl.i))
  2303. -> Sort
  2304. Sort Key: j2_tbl.i DESC, j2_tbl.k
  2305. -> Seq Scan on j2_tbl
  2306. -> Sort
  2307. Sort Key: j1_tbl.i DESC
  2308. -> Seq Scan on j1_tbl
  2309. (8 rows)
  2310. select * from
  2311. j1_tbl full join
  2312. (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k asc) j2_tbl
  2313. on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k;
  2314. i | j | t | i | k
  2315. ---+---+-------+---+----
  2316. | | | | 0
  2317. | | | |
  2318. | 0 | zero | |
  2319. | | null | |
  2320. 8 | 8 | eight | |
  2321. 7 | 7 | seven | |
  2322. 6 | 6 | six | |
  2323. | | | 5 | -5
  2324. | | | 5 | -5
  2325. 5 | 0 | five | |
  2326. 4 | 1 | four | |
  2327. | | | 3 | -3
  2328. 3 | 2 | three | |
  2329. 2 | 3 | two | 2 | 2
  2330. | | | 2 | 4
  2331. | | | 1 | -1
  2332. | | | 0 |
  2333. 1 | 4 | one | |
  2334. 0 | | zero | |
  2335. (19 rows)
  2336. --
  2337. -- a different check for handling of redundant sort keys in merge joins
  2338. --
  2339. explain (costs off)
  2340. select count(*) from
  2341. (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
  2342. left join
  2343. (select * from tenk1 y order by y.unique2) y
  2344. on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
  2345. QUERY PLAN
  2346. ----------------------------------------------------------------------------------
  2347. Aggregate
  2348. -> Merge Left Join
  2349. Merge Cond: (x.thousand = y.unique2)
  2350. Join Filter: ((x.twothousand = y.hundred) AND (x.fivethous = y.unique2))
  2351. -> Sort
  2352. Sort Key: x.thousand, x.twothousand, x.fivethous
  2353. -> Seq Scan on tenk1 x
  2354. -> Materialize
  2355. -> Index Scan using tenk1_unique2 on tenk1 y
  2356. (9 rows)
  2357. select count(*) from
  2358. (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
  2359. left join
  2360. (select * from tenk1 y order by y.unique2) y
  2361. on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
  2362. count
  2363. -------
  2364. 10000
  2365. (1 row)
  2366. --
  2367. -- Clean up
  2368. --
  2369. DROP TABLE t1;
  2370. DROP TABLE t2;
  2371. DROP TABLE t3;
  2372. DROP TABLE J1_TBL;
  2373. DROP TABLE J2_TBL;
  2374. -- Both DELETE and UPDATE allow the specification of additional tables
  2375. -- to "join" against to determine which rows should be modified.
  2376. CREATE TEMP TABLE t1 (a int, b int);
  2377. CREATE TEMP TABLE t2 (a int, b int);
  2378. CREATE TEMP TABLE t3 (x int, y int);
  2379. INSERT INTO t1 VALUES (5, 10);
  2380. INSERT INTO t1 VALUES (15, 20);
  2381. INSERT INTO t1 VALUES (100, 100);
  2382. INSERT INTO t1 VALUES (200, 1000);
  2383. INSERT INTO t2 VALUES (200, 2000);
  2384. INSERT INTO t3 VALUES (5, 20);
  2385. INSERT INTO t3 VALUES (6, 7);
  2386. INSERT INTO t3 VALUES (7, 8);
  2387. INSERT INTO t3 VALUES (500, 100);
  2388. DELETE FROM t3 USING t1 table1 WHERE t3.x = table1.a;
  2389. SELECT * FROM t3;
  2390. x | y
  2391. -----+-----
  2392. 6 | 7
  2393. 7 | 8
  2394. 500 | 100
  2395. (3 rows)
  2396. DELETE FROM t3 USING t1 JOIN t2 USING (a) WHERE t3.x > t1.a;
  2397. SELECT * FROM t3;
  2398. x | y
  2399. ---+---
  2400. 6 | 7
  2401. 7 | 8
  2402. (2 rows)
  2403. DELETE FROM t3 USING t3 t3_other WHERE t3.x = t3_other.x AND t3.y = t3_other.y;
  2404. SELECT * FROM t3;
  2405. x | y
  2406. ---+---
  2407. (0 rows)
  2408. -- Test join against inheritance tree
  2409. create temp table t2a () inherits (t2);
  2410. insert into t2a values (200, 2001);
  2411. select * from t1 left join t2 on (t1.a = t2.a);
  2412. a | b | a | b
  2413. -----+------+-----+------
  2414. 5 | 10 | |
  2415. 15 | 20 | |
  2416. 100 | 100 | |
  2417. 200 | 1000 | 200 | 2000
  2418. 200 | 1000 | 200 | 2001
  2419. (5 rows)
  2420. -- Test matching of column name with wrong alias
  2421. select t1.x from t1 join t3 on (t1.a = t3.x);
  2422. ERROR: column t1.x does not exist
  2423. LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
  2424. ^
  2425. HINT: Perhaps you meant to reference the column "t3.x".
  2426. -- Test matching of locking clause with wrong alias
  2427. select t1.*, t2.*, unnamed_join.* from
  2428. t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
  2429. for update of unnamed_join;
  2430. a | b | a | b | x | y
  2431. ---+---+---+---+---+---
  2432. (0 rows)
  2433. select foo.*, unnamed_join.* from
  2434. t1 join t2 using (a) as foo, t3 as unnamed_join
  2435. for update of unnamed_join;
  2436. a | x | y
  2437. ---+---+---
  2438. (0 rows)
  2439. select foo.*, unnamed_join.* from
  2440. t1 join t2 using (a) as foo, t3 as unnamed_join
  2441. for update of foo;
  2442. ERROR: FOR UPDATE cannot be applied to a join
  2443. LINE 3: for update of foo;
  2444. ^
  2445. select bar.*, unnamed_join.* from
  2446. (t1 join t2 using (a) as foo) as bar, t3 as unnamed_join
  2447. for update of foo;
  2448. ERROR: relation "foo" in FOR UPDATE clause not found in FROM clause
  2449. LINE 3: for update of foo;
  2450. ^
  2451. select bar.*, unnamed_join.* from
  2452. (t1 join t2 using (a) as foo) as bar, t3 as unnamed_join
  2453. for update of bar;
  2454. ERROR: FOR UPDATE cannot be applied to a join
  2455. LINE 3: for update of bar;
  2456. ^
  2457. --
  2458. -- regression test for 8.1 merge right join bug
  2459. --
  2460. CREATE TEMP TABLE tt1 ( tt1_id int4, joincol int4 );
  2461. INSERT INTO tt1 VALUES (1, 11);
  2462. INSERT INTO tt1 VALUES (2, NULL);
  2463. CREATE TEMP TABLE tt2 ( tt2_id int4, joincol int4 );
  2464. INSERT INTO tt2 VALUES (21, 11);
  2465. INSERT INTO tt2 VALUES (22, 11);
  2466. set enable_hashjoin to off;
  2467. set enable_nestloop to off;
  2468. -- these should give the same results
  2469. select tt1.*, tt2.* from tt1 left join tt2 on tt1.joincol = tt2.joincol;
  2470. tt1_id | joincol | tt2_id | joincol
  2471. --------+---------+--------+---------
  2472. 1 | 11 | 21 | 11
  2473. 1 | 11 | 22 | 11
  2474. 2 | | |
  2475. (3 rows)
  2476. select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
  2477. tt1_id | joincol | tt2_id | joincol
  2478. --------+---------+--------+---------
  2479. 1 | 11 | 21 | 11
  2480. 1 | 11 | 22 | 11
  2481. 2 | | |
  2482. (3 rows)
  2483. reset enable_hashjoin;
  2484. reset enable_nestloop;
  2485. --
  2486. -- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
  2487. --
  2488. set work_mem to '64kB';
  2489. set enable_mergejoin to off;
  2490. set enable_memoize to off;
  2491. explain (costs off)
  2492. select count(*) from tenk1 a, tenk1 b
  2493. where a.hundred = b.thousand and (b.fivethous % 10) < 10;
  2494. QUERY PLAN
  2495. ------------------------------------------------------------
  2496. Aggregate
  2497. -> Hash Join
  2498. Hash Cond: (a.hundred = b.thousand)
  2499. -> Index Only Scan using tenk1_hundred on tenk1 a
  2500. -> Hash
  2501. -> Seq Scan on tenk1 b
  2502. Filter: ((fivethous % 10) < 10)
  2503. (7 rows)
  2504. select count(*) from tenk1 a, tenk1 b
  2505. where a.hundred = b.thousand and (b.fivethous % 10) < 10;
  2506. count
  2507. --------
  2508. 100000
  2509. (1 row)
  2510. reset work_mem;
  2511. reset enable_mergejoin;
  2512. reset enable_memoize;
  2513. --
  2514. -- regression test for 8.2 bug with improper re-ordering of left joins
  2515. --
  2516. create temp table tt3(f1 int, f2 text);
  2517. insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
  2518. create index tt3i on tt3(f1);
  2519. analyze tt3;
  2520. create temp table tt4(f1 int);
  2521. insert into tt4 values (0),(1),(9999);
  2522. analyze tt4;
  2523. SELECT a.f1
  2524. FROM tt4 a
  2525. LEFT JOIN (
  2526. SELECT b.f1
  2527. FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
  2528. WHERE c.f1 IS NULL
  2529. ) AS d ON (a.f1 = d.f1)
  2530. WHERE d.f1 IS NULL;
  2531. f1
  2532. ------
  2533. 0
  2534. 1
  2535. 9999
  2536. (3 rows)
  2537. --
  2538. -- regression test for proper handling of outer joins within antijoins
  2539. --
  2540. create temp table tt4x(c1 int, c2 int, c3 int);
  2541. explain (costs off)
  2542. select * from tt4x t1
  2543. where not exists (
  2544. select 1 from tt4x t2
  2545. left join tt4x t3 on t2.c3 = t3.c1
  2546. left join ( select t5.c1 as c1
  2547. from tt4x t4 left join tt4x t5 on t4.c2 = t5.c1
  2548. ) a1 on t3.c2 = a1.c1
  2549. where t1.c1 = t2.c2
  2550. );
  2551. QUERY PLAN
  2552. ---------------------------------------------------------
  2553. Hash Anti Join
  2554. Hash Cond: (t1.c1 = t2.c2)
  2555. -> Seq Scan on tt4x t1
  2556. -> Hash
  2557. -> Merge Right Join
  2558. Merge Cond: (t5.c1 = t3.c2)
  2559. -> Merge Join
  2560. Merge Cond: (t4.c2 = t5.c1)
  2561. -> Sort
  2562. Sort Key: t4.c2
  2563. -> Seq Scan on tt4x t4
  2564. -> Sort
  2565. Sort Key: t5.c1
  2566. -> Seq Scan on tt4x t5
  2567. -> Sort
  2568. Sort Key: t3.c2
  2569. -> Merge Left Join
  2570. Merge Cond: (t2.c3 = t3.c1)
  2571. -> Sort
  2572. Sort Key: t2.c3
  2573. -> Seq Scan on tt4x t2
  2574. -> Sort
  2575. Sort Key: t3.c1
  2576. -> Seq Scan on tt4x t3
  2577. (24 rows)
  2578. --
  2579. -- regression test for problems of the sort depicted in bug #3494
  2580. --
  2581. create temp table tt5(f1 int, f2 int);
  2582. create temp table tt6(f1 int, f2 int);
  2583. insert into tt5 values(1, 10);
  2584. insert into tt5 values(1, 11);
  2585. insert into tt6 values(1, 9);
  2586. insert into tt6 values(1, 2);
  2587. insert into tt6 values(2, 9);
  2588. select * from tt5,tt6 where tt5.f1 = tt6.f1 and tt5.f1 = tt5.f2 - tt6.f2;
  2589. f1 | f2 | f1 | f2
  2590. ----+----+----+----
  2591. 1 | 10 | 1 | 9
  2592. (1 row)
  2593. --
  2594. -- regression test for problems of the sort depicted in bug #3588
  2595. --
  2596. create temp table xx (pkxx int);
  2597. create temp table yy (pkyy int, pkxx int);
  2598. insert into xx values (1);
  2599. insert into xx values (2);
  2600. insert into xx values (3);
  2601. insert into yy values (101, 1);
  2602. insert into yy values (201, 2);
  2603. insert into yy values (301, NULL);
  2604. select yy.pkyy as yy_pkyy, yy.pkxx as yy_pkxx, yya.pkyy as yya_pkyy,
  2605. xxa.pkxx as xxa_pkxx, xxb.pkxx as xxb_pkxx
  2606. from yy
  2607. left join (SELECT * FROM yy where pkyy = 101) as yya ON yy.pkyy = yya.pkyy
  2608. left join xx xxa on yya.pkxx = xxa.pkxx
  2609. left join xx xxb on coalesce (xxa.pkxx, 1) = xxb.pkxx;
  2610. yy_pkyy | yy_pkxx | yya_pkyy | xxa_pkxx | xxb_pkxx
  2611. ---------+---------+----------+----------+----------
  2612. 101 | 1 | 101 | 1 | 1
  2613. 201 | 2 | | | 1
  2614. 301 | | | | 1
  2615. (3 rows)
  2616. --
  2617. -- regression test for improper pushing of constants across outer-join clauses
  2618. -- (as seen in early 8.2.x releases)
  2619. --
  2620. create temp table zt1 (f1 int primary key);
  2621. create temp table zt2 (f2 int primary key);
  2622. create temp table zt3 (f3 int primary key);
  2623. insert into zt1 values(53);
  2624. insert into zt2 values(53);
  2625. select * from
  2626. zt2 left join zt3 on (f2 = f3)
  2627. left join zt1 on (f3 = f1)
  2628. where f2 = 53;
  2629. f2 | f3 | f1
  2630. ----+----+----
  2631. 53 | |
  2632. (1 row)
  2633. create temp view zv1 as select *,'dummy'::text AS junk from zt1;
  2634. select * from
  2635. zt2 left join zt3 on (f2 = f3)
  2636. left join zv1 on (f3 = f1)
  2637. where f2 = 53;
  2638. f2 | f3 | f1 | junk
  2639. ----+----+----+------
  2640. 53 | | |
  2641. (1 row)
  2642. --
  2643. -- regression test for improper extraction of OR indexqual conditions
  2644. -- (as seen in early 8.3.x releases)
  2645. --
  2646. select a.unique2, a.ten, b.tenthous, b.unique2, b.hundred
  2647. from tenk1 a left join tenk1 b on a.unique2 = b.tenthous
  2648. where a.unique1 = 42 and
  2649. ((b.unique2 is null and a.ten = 2) or b.hundred = 3);
  2650. unique2 | ten | tenthous | unique2 | hundred
  2651. ---------+-----+----------+---------+---------
  2652. (0 rows)
  2653. --
  2654. -- test proper positioning of one-time quals in EXISTS (8.4devel bug)
  2655. --
  2656. prepare foo(bool) as
  2657. select count(*) from tenk1 a left join tenk1 b
  2658. on (a.unique2 = b.unique1 and exists
  2659. (select 1 from tenk1 c where c.thousand = b.unique2 and $1));
  2660. execute foo(true);
  2661. count
  2662. -------
  2663. 10000
  2664. (1 row)
  2665. execute foo(false);
  2666. count
  2667. -------
  2668. 10000
  2669. (1 row)
  2670. --
  2671. -- test for sane behavior with noncanonical merge clauses, per bug #4926
  2672. --
  2673. begin;
  2674. set enable_mergejoin = 1;
  2675. set enable_hashjoin = 0;
  2676. set enable_nestloop = 0;
  2677. create temp table a (i integer);
  2678. create temp table b (x integer, y integer);
  2679. select * from a left join b on i = x and i = y and x = i;
  2680. i | x | y
  2681. ---+---+---
  2682. (0 rows)
  2683. rollback;
  2684. --
  2685. -- test handling of merge clauses using record_ops
  2686. --
  2687. begin;
  2688. create type mycomptype as (id int, v bigint);
  2689. create temp table tidv (idv mycomptype);
  2690. create index on tidv (idv);
  2691. explain (costs off)
  2692. select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
  2693. QUERY PLAN
  2694. ----------------------------------------------------------
  2695. Merge Join
  2696. Merge Cond: (a.idv = b.idv)
  2697. -> Index Only Scan using tidv_idv_idx on tidv a
  2698. -> Materialize
  2699. -> Index Only Scan using tidv_idv_idx on tidv b
  2700. (5 rows)
  2701. set enable_mergejoin = 0;
  2702. set enable_hashjoin = 0;
  2703. explain (costs off)
  2704. select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
  2705. QUERY PLAN
  2706. ----------------------------------------------------
  2707. Nested Loop
  2708. -> Seq Scan on tidv a
  2709. -> Index Only Scan using tidv_idv_idx on tidv b
  2710. Index Cond: (idv = a.idv)
  2711. (4 rows)
  2712. rollback;
  2713. --
  2714. -- test NULL behavior of whole-row Vars, per bug #5025
  2715. --
  2716. select t1.q2, count(t2.*)
  2717. from int8_tbl t1 left join int8_tbl t2 on (t1.q2 = t2.q1)
  2718. group by t1.q2 order by 1;
  2719. q2 | count
  2720. -------------------+-------
  2721. -4567890123456789 | 0
  2722. 123 | 2
  2723. 456 | 0
  2724. 4567890123456789 | 6
  2725. (4 rows)
  2726. select t1.q2, count(t2.*)
  2727. from int8_tbl t1 left join (select * from int8_tbl) t2 on (t1.q2 = t2.q1)
  2728. group by t1.q2 order by 1;
  2729. q2 | count
  2730. -------------------+-------
  2731. -4567890123456789 | 0
  2732. 123 | 2
  2733. 456 | 0
  2734. 4567890123456789 | 6
  2735. (4 rows)
  2736. select t1.q2, count(t2.*)
  2737. from int8_tbl t1 left join (select * from int8_tbl offset 0) t2 on (t1.q2 = t2.q1)
  2738. group by t1.q2 order by 1;
  2739. q2 | count
  2740. -------------------+-------
  2741. -4567890123456789 | 0
  2742. 123 | 2
  2743. 456 | 0
  2744. 4567890123456789 | 6
  2745. (4 rows)
  2746. select t1.q2, count(t2.*)
  2747. from int8_tbl t1 left join
  2748. (select q1, case when q2=1 then 1 else q2 end as q2 from int8_tbl) t2
  2749. on (t1.q2 = t2.q1)
  2750. group by t1.q2 order by 1;
  2751. q2 | count
  2752. -------------------+-------
  2753. -4567890123456789 | 0
  2754. 123 | 2
  2755. 456 | 0
  2756. 4567890123456789 | 6
  2757. (4 rows)
  2758. --
  2759. -- test incorrect failure to NULL pulled-up subexpressions
  2760. --
  2761. begin;
  2762. create temp table a (
  2763. code char not null,
  2764. constraint a_pk primary key (code)
  2765. );
  2766. create temp table b (
  2767. a char not null,
  2768. num integer not null,
  2769. constraint b_pk primary key (a, num)
  2770. );
  2771. create temp table c (
  2772. name char not null,
  2773. a char,
  2774. constraint c_pk primary key (name)
  2775. );
  2776. insert into a (code) values ('p');
  2777. insert into a (code) values ('q');
  2778. insert into b (a, num) values ('p', 1);
  2779. insert into b (a, num) values ('p', 2);
  2780. insert into c (name, a) values ('A', 'p');
  2781. insert into c (name, a) values ('B', 'q');
  2782. insert into c (name, a) values ('C', null);
  2783. select c.name, ss.code, ss.b_cnt, ss.const
  2784. from c left join
  2785. (select a.code, coalesce(b_grp.cnt, 0) as b_cnt, -1 as const
  2786. from a left join
  2787. (select count(1) as cnt, b.a from b group by b.a) as b_grp
  2788. on a.code = b_grp.a
  2789. ) as ss
  2790. on (c.a = ss.code)
  2791. order by c.name;
  2792. name | code | b_cnt | const
  2793. ------+------+-------+-------
  2794. A | p | 2 | -1
  2795. B | q | 0 | -1
  2796. C | | |
  2797. (3 rows)
  2798. rollback;
  2799. --
  2800. -- test incorrect handling of placeholders that only appear in targetlists,
  2801. -- per bug #6154
  2802. --
  2803. SELECT * FROM
  2804. ( SELECT 1 as key1 ) sub1
  2805. LEFT JOIN
  2806. ( SELECT sub3.key3, sub4.value2, COALESCE(sub4.value2, 66) as value3 FROM
  2807. ( SELECT 1 as key3 ) sub3
  2808. LEFT JOIN
  2809. ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
  2810. ( SELECT 1 as key5 ) sub5
  2811. LEFT JOIN
  2812. ( SELECT 2 as key6, 42 as value1 ) sub6
  2813. ON sub5.key5 = sub6.key6
  2814. ) sub4
  2815. ON sub4.key5 = sub3.key3
  2816. ) sub2
  2817. ON sub1.key1 = sub2.key3;
  2818. key1 | key3 | value2 | value3
  2819. ------+------+--------+--------
  2820. 1 | 1 | 1 | 1
  2821. (1 row)
  2822. -- test the path using join aliases, too
  2823. SELECT * FROM
  2824. ( SELECT 1 as key1 ) sub1
  2825. LEFT JOIN
  2826. ( SELECT sub3.key3, value2, COALESCE(value2, 66) as value3 FROM
  2827. ( SELECT 1 as key3 ) sub3
  2828. LEFT JOIN
  2829. ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
  2830. ( SELECT 1 as key5 ) sub5
  2831. LEFT JOIN
  2832. ( SELECT 2 as key6, 42 as value1 ) sub6
  2833. ON sub5.key5 = sub6.key6
  2834. ) sub4
  2835. ON sub4.key5 = sub3.key3
  2836. ) sub2
  2837. ON sub1.key1 = sub2.key3;
  2838. key1 | key3 | value2 | value3
  2839. ------+------+--------+--------
  2840. 1 | 1 | 1 | 1
  2841. (1 row)
  2842. --
  2843. -- test case where a PlaceHolderVar is used as a nestloop parameter
  2844. --
  2845. EXPLAIN (COSTS OFF)
  2846. SELECT qq, unique1
  2847. FROM
  2848. ( SELECT COALESCE(q1, 0) AS qq FROM int8_tbl a ) AS ss1
  2849. FULL OUTER JOIN
  2850. ( SELECT COALESCE(q2, -1) AS qq FROM int8_tbl b ) AS ss2
  2851. USING (qq)
  2852. INNER JOIN tenk1 c ON qq = unique2;
  2853. QUERY PLAN
  2854. ---------------------------------------------------------------------------------------------------------
  2855. Nested Loop
  2856. -> Hash Full Join
  2857. Hash Cond: ((COALESCE(a.q1, '0'::bigint)) = (COALESCE(b.q2, '-1'::bigint)))
  2858. -> Seq Scan on int8_tbl a
  2859. -> Hash
  2860. -> Seq Scan on int8_tbl b
  2861. -> Index Scan using tenk1_unique2 on tenk1 c
  2862. Index Cond: (unique2 = COALESCE((COALESCE(a.q1, '0'::bigint)), (COALESCE(b.q2, '-1'::bigint))))
  2863. (8 rows)
  2864. SELECT qq, unique1
  2865. FROM
  2866. ( SELECT COALESCE(q1, 0) AS qq FROM int8_tbl a ) AS ss1
  2867. FULL OUTER JOIN
  2868. ( SELECT COALESCE(q2, -1) AS qq FROM int8_tbl b ) AS ss2
  2869. USING (qq)
  2870. INNER JOIN tenk1 c ON qq = unique2;
  2871. qq | unique1
  2872. -----+---------
  2873. 123 | 4596
  2874. 123 | 4596
  2875. 456 | 7318
  2876. (3 rows)
  2877. --
  2878. -- nested nestloops can require nested PlaceHolderVars
  2879. --
  2880. create temp table nt1 (
  2881. id int primary key,
  2882. a1 boolean,
  2883. a2 boolean
  2884. );
  2885. create temp table nt2 (
  2886. id int primary key,
  2887. nt1_id int,
  2888. b1 boolean,
  2889. b2 boolean,
  2890. foreign key (nt1_id) references nt1(id)
  2891. );
  2892. create temp table nt3 (
  2893. id int primary key,
  2894. nt2_id int,
  2895. c1 boolean,
  2896. foreign key (nt2_id) references nt2(id)
  2897. );
  2898. insert into nt1 values (1,true,true);
  2899. insert into nt1 values (2,true,false);
  2900. insert into nt1 values (3,false,false);
  2901. insert into nt2 values (1,1,true,true);
  2902. insert into nt2 values (2,2,true,false);
  2903. insert into nt2 values (3,3,false,false);
  2904. insert into nt3 values (1,1,true);
  2905. insert into nt3 values (2,2,false);
  2906. insert into nt3 values (3,3,true);
  2907. explain (costs off)
  2908. select nt3.id
  2909. from nt3 as nt3
  2910. left join
  2911. (select nt2.*, (nt2.b1 and ss1.a3) AS b3
  2912. from nt2 as nt2
  2913. left join
  2914. (select nt1.*, (nt1.id is not null) as a3 from nt1) as ss1
  2915. on ss1.id = nt2.nt1_id
  2916. ) as ss2
  2917. on ss2.id = nt3.nt2_id
  2918. where nt3.id = 1 and ss2.b3;
  2919. QUERY PLAN
  2920. -----------------------------------------------
  2921. Nested Loop
  2922. -> Nested Loop
  2923. -> Index Scan using nt3_pkey on nt3
  2924. Index Cond: (id = 1)
  2925. -> Index Scan using nt2_pkey on nt2
  2926. Index Cond: (id = nt3.nt2_id)
  2927. -> Index Only Scan using nt1_pkey on nt1
  2928. Index Cond: (id = nt2.nt1_id)
  2929. Filter: (nt2.b1 AND (id IS NOT NULL))
  2930. (9 rows)
  2931. select nt3.id
  2932. from nt3 as nt3
  2933. left join
  2934. (select nt2.*, (nt2.b1 and ss1.a3) AS b3
  2935. from nt2 as nt2
  2936. left join
  2937. (select nt1.*, (nt1.id is not null) as a3 from nt1) as ss1
  2938. on ss1.id = nt2.nt1_id
  2939. ) as ss2
  2940. on ss2.id = nt3.nt2_id
  2941. where nt3.id = 1 and ss2.b3;
  2942. id
  2943. ----
  2944. 1
  2945. (1 row)
  2946. --
  2947. -- test case where a PlaceHolderVar is propagated into a subquery
  2948. --
  2949. explain (costs off)
  2950. select * from
  2951. int8_tbl t1 left join
  2952. (select q1 as x, 42 as y from int8_tbl t2) ss
  2953. on t1.q2 = ss.x
  2954. where
  2955. 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
  2956. order by 1,2;
  2957. QUERY PLAN
  2958. -----------------------------------------------------------
  2959. Sort
  2960. Sort Key: t1.q1, t1.q2
  2961. -> Hash Left Join
  2962. Hash Cond: (t1.q2 = t2.q1)
  2963. Filter: (1 = (SubPlan 1))
  2964. -> Seq Scan on int8_tbl t1
  2965. -> Hash
  2966. -> Seq Scan on int8_tbl t2
  2967. SubPlan 1
  2968. -> Limit
  2969. -> Result
  2970. One-Time Filter: ((42) IS NOT NULL)
  2971. -> Seq Scan on int8_tbl t3
  2972. (13 rows)
  2973. select * from
  2974. int8_tbl t1 left join
  2975. (select q1 as x, 42 as y from int8_tbl t2) ss
  2976. on t1.q2 = ss.x
  2977. where
  2978. 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
  2979. order by 1,2;
  2980. q1 | q2 | x | y
  2981. ------------------+------------------+------------------+----
  2982. 123 | 4567890123456789 | 4567890123456789 | 42
  2983. 123 | 4567890123456789 | 4567890123456789 | 42
  2984. 123 | 4567890123456789 | 4567890123456789 | 42
  2985. 4567890123456789 | 123 | 123 | 42
  2986. 4567890123456789 | 123 | 123 | 42
  2987. 4567890123456789 | 4567890123456789 | 4567890123456789 | 42
  2988. 4567890123456789 | 4567890123456789 | 4567890123456789 | 42
  2989. 4567890123456789 | 4567890123456789 | 4567890123456789 | 42
  2990. (8 rows)
  2991. --
  2992. -- variant where a PlaceHolderVar is needed at a join, but not above the join
  2993. --
  2994. explain (costs off)
  2995. select * from
  2996. int4_tbl as i41,
  2997. lateral
  2998. (select 1 as x from
  2999. (select i41.f1 as lat,
  3000. i42.f1 as loc from
  3001. int8_tbl as i81, int4_tbl as i42) as ss1
  3002. right join int4_tbl as i43 on (i43.f1 > 1)
  3003. where ss1.loc = ss1.lat) as ss2
  3004. where i41.f1 > 0;
  3005. QUERY PLAN
  3006. --------------------------------------------------
  3007. Nested Loop
  3008. -> Nested Loop
  3009. -> Seq Scan on int4_tbl i41
  3010. Filter: (f1 > 0)
  3011. -> Nested Loop
  3012. Join Filter: (i41.f1 = i42.f1)
  3013. -> Seq Scan on int8_tbl i81
  3014. -> Materialize
  3015. -> Seq Scan on int4_tbl i42
  3016. -> Materialize
  3017. -> Seq Scan on int4_tbl i43
  3018. Filter: (f1 > 1)
  3019. (12 rows)
  3020. select * from
  3021. int4_tbl as i41,
  3022. lateral
  3023. (select 1 as x from
  3024. (select i41.f1 as lat,
  3025. i42.f1 as loc from
  3026. int8_tbl as i81, int4_tbl as i42) as ss1
  3027. right join int4_tbl as i43 on (i43.f1 > 1)
  3028. where ss1.loc = ss1.lat) as ss2
  3029. where i41.f1 > 0;
  3030. f1 | x
  3031. ------------+---
  3032. 123456 | 1
  3033. 123456 | 1
  3034. 123456 | 1
  3035. 123456 | 1
  3036. 123456 | 1
  3037. 123456 | 1
  3038. 123456 | 1
  3039. 123456 | 1
  3040. 123456 | 1
  3041. 123456 | 1
  3042. 2147483647 | 1
  3043. 2147483647 | 1
  3044. 2147483647 | 1
  3045. 2147483647 | 1
  3046. 2147483647 | 1
  3047. 2147483647 | 1
  3048. 2147483647 | 1
  3049. 2147483647 | 1
  3050. 2147483647 | 1
  3051. 2147483647 | 1
  3052. (20 rows)
  3053. --
  3054. -- test the corner cases FULL JOIN ON TRUE and FULL JOIN ON FALSE
  3055. --
  3056. select * from int4_tbl a full join int4_tbl b on true;
  3057. f1 | f1
  3058. -------------+-------------
  3059. 0 | 0
  3060. 0 | 123456
  3061. 0 | -123456
  3062. 0 | 2147483647
  3063. 0 | -2147483647
  3064. 123456 | 0
  3065. 123456 | 123456
  3066. 123456 | -123456
  3067. 123456 | 2147483647
  3068. 123456 | -2147483647
  3069. -123456 | 0
  3070. -123456 | 123456
  3071. -123456 | -123456
  3072. -123456 | 2147483647
  3073. -123456 | -2147483647
  3074. 2147483647 | 0
  3075. 2147483647 | 123456
  3076. 2147483647 | -123456
  3077. 2147483647 | 2147483647
  3078. 2147483647 | -2147483647
  3079. -2147483647 | 0
  3080. -2147483647 | 123456
  3081. -2147483647 | -123456
  3082. -2147483647 | 2147483647
  3083. -2147483647 | -2147483647
  3084. (25 rows)
  3085. select * from int4_tbl a full join int4_tbl b on false;
  3086. f1 | f1
  3087. -------------+-------------
  3088. | 0
  3089. | 123456
  3090. | -123456
  3091. | 2147483647
  3092. | -2147483647
  3093. 0 |
  3094. 123456 |
  3095. -123456 |
  3096. 2147483647 |
  3097. -2147483647 |
  3098. (10 rows)
  3099. --
  3100. -- test for ability to use a cartesian join when necessary
  3101. --
  3102. create temp table q1 as select 1 as q1;
  3103. create temp table q2 as select 0 as q2;
  3104. analyze q1;
  3105. analyze q2;
  3106. explain (costs off)
  3107. select * from
  3108. tenk1 join int4_tbl on f1 = twothousand,
  3109. q1, q2
  3110. where q1 = thousand or q2 = thousand;
  3111. QUERY PLAN
  3112. ------------------------------------------------------------------------
  3113. Hash Join
  3114. Hash Cond: (tenk1.twothousand = int4_tbl.f1)
  3115. -> Nested Loop
  3116. -> Nested Loop
  3117. -> Seq Scan on q1
  3118. -> Seq Scan on q2
  3119. -> Bitmap Heap Scan on tenk1
  3120. Recheck Cond: ((q1.q1 = thousand) OR (q2.q2 = thousand))
  3121. -> BitmapOr
  3122. -> Bitmap Index Scan on tenk1_thous_tenthous
  3123. Index Cond: (thousand = q1.q1)
  3124. -> Bitmap Index Scan on tenk1_thous_tenthous
  3125. Index Cond: (thousand = q2.q2)
  3126. -> Hash
  3127. -> Seq Scan on int4_tbl
  3128. (15 rows)
  3129. explain (costs off)
  3130. select * from
  3131. tenk1 join int4_tbl on f1 = twothousand,
  3132. q1, q2
  3133. where thousand = (q1 + q2);
  3134. QUERY PLAN
  3135. --------------------------------------------------------------
  3136. Hash Join
  3137. Hash Cond: (tenk1.twothousand = int4_tbl.f1)
  3138. -> Nested Loop
  3139. -> Nested Loop
  3140. -> Seq Scan on q1
  3141. -> Seq Scan on q2
  3142. -> Bitmap Heap Scan on tenk1
  3143. Recheck Cond: (thousand = (q1.q1 + q2.q2))
  3144. -> Bitmap Index Scan on tenk1_thous_tenthous
  3145. Index Cond: (thousand = (q1.q1 + q2.q2))
  3146. -> Hash
  3147. -> Seq Scan on int4_tbl
  3148. (12 rows)
  3149. --
  3150. -- test ability to generate a suitable plan for a star-schema query
  3151. --
  3152. explain (costs off)
  3153. select * from
  3154. tenk1, int8_tbl a, int8_tbl b
  3155. where thousand = a.q1 and tenthous = b.q1 and a.q2 = 1 and b.q2 = 2;
  3156. QUERY PLAN
  3157. ---------------------------------------------------------------------
  3158. Nested Loop
  3159. -> Seq Scan on int8_tbl b
  3160. Filter: (q2 = 2)
  3161. -> Nested Loop
  3162. -> Seq Scan on int8_tbl a
  3163. Filter: (q2 = 1)
  3164. -> Index Scan using tenk1_thous_tenthous on tenk1
  3165. Index Cond: ((thousand = a.q1) AND (tenthous = b.q1))
  3166. (8 rows)
  3167. --
  3168. -- test a corner case in which we shouldn't apply the star-schema optimization
  3169. --
  3170. explain (costs off)
  3171. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  3172. tenk1 t1
  3173. inner join int4_tbl i1
  3174. left join (select v1.x2, v2.y1, 11 AS d1
  3175. from (select 1,0 from onerow) v1(x1,x2)
  3176. left join (select 3,1 from onerow) v2(y1,y2)
  3177. on v1.x1 = v2.y2) subq1
  3178. on (i1.f1 = subq1.x2)
  3179. on (t1.unique2 = subq1.d1)
  3180. left join tenk1 t2
  3181. on (subq1.y1 = t2.unique1)
  3182. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  3183. QUERY PLAN
  3184. -----------------------------------------------------------------------
  3185. Nested Loop
  3186. -> Nested Loop
  3187. Join Filter: (t1.stringu1 > t2.stringu2)
  3188. -> Nested Loop
  3189. -> Nested Loop
  3190. -> Seq Scan on onerow
  3191. -> Seq Scan on onerow onerow_1
  3192. -> Index Scan using tenk1_unique2 on tenk1 t1
  3193. Index Cond: ((unique2 = (11)) AND (unique2 < 42))
  3194. -> Index Scan using tenk1_unique1 on tenk1 t2
  3195. Index Cond: (unique1 = (3))
  3196. -> Seq Scan on int4_tbl i1
  3197. Filter: (f1 = 0)
  3198. (13 rows)
  3199. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  3200. tenk1 t1
  3201. inner join int4_tbl i1
  3202. left join (select v1.x2, v2.y1, 11 AS d1
  3203. from (select 1,0 from onerow) v1(x1,x2)
  3204. left join (select 3,1 from onerow) v2(y1,y2)
  3205. on v1.x1 = v2.y2) subq1
  3206. on (i1.f1 = subq1.x2)
  3207. on (t1.unique2 = subq1.d1)
  3208. left join tenk1 t2
  3209. on (subq1.y1 = t2.unique1)
  3210. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  3211. unique2 | stringu1 | unique1 | stringu2
  3212. ---------+----------+---------+----------
  3213. 11 | WFAAAA | 3 | LKIAAA
  3214. (1 row)
  3215. -- variant that isn't quite a star-schema case
  3216. select ss1.d1 from
  3217. tenk1 as t1
  3218. inner join tenk1 as t2
  3219. on t1.tenthous = t2.ten
  3220. inner join
  3221. int8_tbl as i8
  3222. left join int4_tbl as i4
  3223. inner join (select 64::information_schema.cardinal_number as d1
  3224. from tenk1 t3,
  3225. lateral (select abs(t3.unique1) + random()) ss0(x)
  3226. where t3.fivethous < 0) as ss1
  3227. on i4.f1 = ss1.d1
  3228. on i8.q1 = i4.f1
  3229. on t1.tenthous = ss1.d1
  3230. where t1.unique1 < i4.f1;
  3231. d1
  3232. ----
  3233. (0 rows)
  3234. -- this variant is foldable by the remove-useless-RESULT-RTEs code
  3235. explain (costs off)
  3236. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  3237. tenk1 t1
  3238. inner join int4_tbl i1
  3239. left join (select v1.x2, v2.y1, 11 AS d1
  3240. from (values(1,0)) v1(x1,x2)
  3241. left join (values(3,1)) v2(y1,y2)
  3242. on v1.x1 = v2.y2) subq1
  3243. on (i1.f1 = subq1.x2)
  3244. on (t1.unique2 = subq1.d1)
  3245. left join tenk1 t2
  3246. on (subq1.y1 = t2.unique1)
  3247. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  3248. QUERY PLAN
  3249. -----------------------------------------------------------------
  3250. Nested Loop
  3251. Join Filter: (t1.stringu1 > t2.stringu2)
  3252. -> Nested Loop
  3253. -> Seq Scan on int4_tbl i1
  3254. Filter: (f1 = 0)
  3255. -> Index Scan using tenk1_unique2 on tenk1 t1
  3256. Index Cond: ((unique2 = (11)) AND (unique2 < 42))
  3257. -> Index Scan using tenk1_unique1 on tenk1 t2
  3258. Index Cond: (unique1 = (3))
  3259. (9 rows)
  3260. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  3261. tenk1 t1
  3262. inner join int4_tbl i1
  3263. left join (select v1.x2, v2.y1, 11 AS d1
  3264. from (values(1,0)) v1(x1,x2)
  3265. left join (values(3,1)) v2(y1,y2)
  3266. on v1.x1 = v2.y2) subq1
  3267. on (i1.f1 = subq1.x2)
  3268. on (t1.unique2 = subq1.d1)
  3269. left join tenk1 t2
  3270. on (subq1.y1 = t2.unique1)
  3271. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  3272. unique2 | stringu1 | unique1 | stringu2
  3273. ---------+----------+---------+----------
  3274. 11 | WFAAAA | 3 | LKIAAA
  3275. (1 row)
  3276. -- Here's a variant that we can't fold too aggressively, though,
  3277. -- or we end up with noplace to evaluate the lateral PHV
  3278. explain (verbose, costs off)
  3279. select * from
  3280. (select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
  3281. lateral (select ss2.y as z limit 1) ss3;
  3282. QUERY PLAN
  3283. ---------------------------
  3284. Nested Loop
  3285. Output: 1, (2), ((2))
  3286. -> Result
  3287. Output: 2
  3288. -> Limit
  3289. Output: ((2))
  3290. -> Result
  3291. Output: (2)
  3292. (8 rows)
  3293. select * from
  3294. (select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
  3295. lateral (select ss2.y as z limit 1) ss3;
  3296. x | y | z
  3297. ---+---+---
  3298. 1 | 2 | 2
  3299. (1 row)
  3300. -- Test proper handling of appendrel PHVs during useless-RTE removal
  3301. explain (costs off)
  3302. select * from
  3303. (select 0 as z) as t1
  3304. left join
  3305. (select true as a) as t2
  3306. on true,
  3307. lateral (select true as b
  3308. union all
  3309. select a as b) as t3
  3310. where b;
  3311. QUERY PLAN
  3312. ---------------------------------------
  3313. Nested Loop
  3314. -> Result
  3315. -> Append
  3316. -> Result
  3317. -> Result
  3318. One-Time Filter: (true)
  3319. (6 rows)
  3320. select * from
  3321. (select 0 as z) as t1
  3322. left join
  3323. (select true as a) as t2
  3324. on true,
  3325. lateral (select true as b
  3326. union all
  3327. select a as b) as t3
  3328. where b;
  3329. z | a | b
  3330. ---+---+---
  3331. 0 | t | t
  3332. 0 | t | t
  3333. (2 rows)
  3334. --
  3335. -- test inlining of immutable functions
  3336. --
  3337. create function f_immutable_int4(i integer) returns integer as
  3338. $$ begin return i; end; $$ language plpgsql immutable;
  3339. -- check optimization of function scan with join
  3340. explain (costs off)
  3341. select unique1 from tenk1, (select * from f_immutable_int4(1) x) x
  3342. where x = unique1;
  3343. QUERY PLAN
  3344. ----------------------------------------------
  3345. Index Only Scan using tenk1_unique1 on tenk1
  3346. Index Cond: (unique1 = 1)
  3347. (2 rows)
  3348. explain (verbose, costs off)
  3349. select unique1, x.*
  3350. from tenk1, (select *, random() from f_immutable_int4(1) x) x
  3351. where x = unique1;
  3352. QUERY PLAN
  3353. -----------------------------------------------------------
  3354. Nested Loop
  3355. Output: tenk1.unique1, (1), (random())
  3356. -> Result
  3357. Output: 1, random()
  3358. -> Index Only Scan using tenk1_unique1 on public.tenk1
  3359. Output: tenk1.unique1
  3360. Index Cond: (tenk1.unique1 = (1))
  3361. (7 rows)
  3362. explain (costs off)
  3363. select unique1 from tenk1, f_immutable_int4(1) x where x = unique1;
  3364. QUERY PLAN
  3365. ----------------------------------------------
  3366. Index Only Scan using tenk1_unique1 on tenk1
  3367. Index Cond: (unique1 = 1)
  3368. (2 rows)
  3369. explain (costs off)
  3370. select unique1 from tenk1, lateral f_immutable_int4(1) x where x = unique1;
  3371. QUERY PLAN
  3372. ----------------------------------------------
  3373. Index Only Scan using tenk1_unique1 on tenk1
  3374. Index Cond: (unique1 = 1)
  3375. (2 rows)
  3376. explain (costs off)
  3377. select unique1 from tenk1, lateral f_immutable_int4(1) x where x in (select 17);
  3378. QUERY PLAN
  3379. --------------------------
  3380. Result
  3381. One-Time Filter: false
  3382. (2 rows)
  3383. explain (costs off)
  3384. select unique1, x from tenk1 join f_immutable_int4(1) x on unique1 = x;
  3385. QUERY PLAN
  3386. ----------------------------------------------
  3387. Index Only Scan using tenk1_unique1 on tenk1
  3388. Index Cond: (unique1 = 1)
  3389. (2 rows)
  3390. explain (costs off)
  3391. select unique1, x from tenk1 left join f_immutable_int4(1) x on unique1 = x;
  3392. QUERY PLAN
  3393. ----------------------------------------------------
  3394. Nested Loop Left Join
  3395. Join Filter: (tenk1.unique1 = 1)
  3396. -> Index Only Scan using tenk1_unique1 on tenk1
  3397. -> Materialize
  3398. -> Result
  3399. (5 rows)
  3400. explain (costs off)
  3401. select unique1, x from tenk1 right join f_immutable_int4(1) x on unique1 = x;
  3402. QUERY PLAN
  3403. ----------------------------------------------------
  3404. Nested Loop Left Join
  3405. -> Result
  3406. -> Index Only Scan using tenk1_unique1 on tenk1
  3407. Index Cond: (unique1 = 1)
  3408. (4 rows)
  3409. explain (costs off)
  3410. select unique1, x from tenk1 full join f_immutable_int4(1) x on unique1 = x;
  3411. QUERY PLAN
  3412. ----------------------------------------------------
  3413. Merge Full Join
  3414. Merge Cond: (tenk1.unique1 = (1))
  3415. -> Index Only Scan using tenk1_unique1 on tenk1
  3416. -> Sort
  3417. Sort Key: (1)
  3418. -> Result
  3419. (6 rows)
  3420. -- check that pullup of a const function allows further const-folding
  3421. explain (costs off)
  3422. select unique1 from tenk1, f_immutable_int4(1) x where x = 42;
  3423. QUERY PLAN
  3424. --------------------------
  3425. Result
  3426. One-Time Filter: false
  3427. (2 rows)
  3428. -- test inlining of immutable functions with PlaceHolderVars
  3429. explain (costs off)
  3430. select nt3.id
  3431. from nt3 as nt3
  3432. left join
  3433. (select nt2.*, (nt2.b1 or i4 = 42) AS b3
  3434. from nt2 as nt2
  3435. left join
  3436. f_immutable_int4(0) i4
  3437. on i4 = nt2.nt1_id
  3438. ) as ss2
  3439. on ss2.id = nt3.nt2_id
  3440. where nt3.id = 1 and ss2.b3;
  3441. QUERY PLAN
  3442. ----------------------------------------------
  3443. Nested Loop Left Join
  3444. Filter: ((nt2.b1 OR ((0) = 42)))
  3445. -> Index Scan using nt3_pkey on nt3
  3446. Index Cond: (id = 1)
  3447. -> Nested Loop Left Join
  3448. Join Filter: (0 = nt2.nt1_id)
  3449. -> Index Scan using nt2_pkey on nt2
  3450. Index Cond: (id = nt3.nt2_id)
  3451. -> Result
  3452. (9 rows)
  3453. drop function f_immutable_int4(int);
  3454. -- test inlining when function returns composite
  3455. create function mki8(bigint, bigint) returns int8_tbl as
  3456. $$select row($1,$2)::int8_tbl$$ language sql;
  3457. create function mki4(int) returns int4_tbl as
  3458. $$select row($1)::int4_tbl$$ language sql;
  3459. explain (verbose, costs off)
  3460. select * from mki8(1,2);
  3461. QUERY PLAN
  3462. ------------------------------------
  3463. Function Scan on mki8
  3464. Output: q1, q2
  3465. Function Call: '(1,2)'::int8_tbl
  3466. (3 rows)
  3467. select * from mki8(1,2);
  3468. q1 | q2
  3469. ----+----
  3470. 1 | 2
  3471. (1 row)
  3472. explain (verbose, costs off)
  3473. select * from mki4(42);
  3474. QUERY PLAN
  3475. -----------------------------------
  3476. Function Scan on mki4
  3477. Output: f1
  3478. Function Call: '(42)'::int4_tbl
  3479. (3 rows)
  3480. select * from mki4(42);
  3481. f1
  3482. ----
  3483. 42
  3484. (1 row)
  3485. drop function mki8(bigint, bigint);
  3486. drop function mki4(int);
  3487. --
  3488. -- test extraction of restriction OR clauses from join OR clause
  3489. -- (we used to only do this for indexable clauses)
  3490. --
  3491. explain (costs off)
  3492. select * from tenk1 a join tenk1 b on
  3493. (a.unique1 = 1 and b.unique1 = 2) or (a.unique2 = 3 and b.hundred = 4);
  3494. QUERY PLAN
  3495. -------------------------------------------------------------------------------------------------
  3496. Nested Loop
  3497. Join Filter: (((a.unique1 = 1) AND (b.unique1 = 2)) OR ((a.unique2 = 3) AND (b.hundred = 4)))
  3498. -> Bitmap Heap Scan on tenk1 b
  3499. Recheck Cond: ((unique1 = 2) OR (hundred = 4))
  3500. -> BitmapOr
  3501. -> Bitmap Index Scan on tenk1_unique1
  3502. Index Cond: (unique1 = 2)
  3503. -> Bitmap Index Scan on tenk1_hundred
  3504. Index Cond: (hundred = 4)
  3505. -> Materialize
  3506. -> Bitmap Heap Scan on tenk1 a
  3507. Recheck Cond: ((unique1 = 1) OR (unique2 = 3))
  3508. -> BitmapOr
  3509. -> Bitmap Index Scan on tenk1_unique1
  3510. Index Cond: (unique1 = 1)
  3511. -> Bitmap Index Scan on tenk1_unique2
  3512. Index Cond: (unique2 = 3)
  3513. (17 rows)
  3514. explain (costs off)
  3515. select * from tenk1 a join tenk1 b on
  3516. (a.unique1 = 1 and b.unique1 = 2) or (a.unique2 = 3 and b.ten = 4);
  3517. QUERY PLAN
  3518. ---------------------------------------------------------------------------------------------
  3519. Nested Loop
  3520. Join Filter: (((a.unique1 = 1) AND (b.unique1 = 2)) OR ((a.unique2 = 3) AND (b.ten = 4)))
  3521. -> Seq Scan on tenk1 b
  3522. Filter: ((unique1 = 2) OR (ten = 4))
  3523. -> Materialize
  3524. -> Bitmap Heap Scan on tenk1 a
  3525. Recheck Cond: ((unique1 = 1) OR (unique2 = 3))
  3526. -> BitmapOr
  3527. -> Bitmap Index Scan on tenk1_unique1
  3528. Index Cond: (unique1 = 1)
  3529. -> Bitmap Index Scan on tenk1_unique2
  3530. Index Cond: (unique2 = 3)
  3531. (12 rows)
  3532. explain (costs off)
  3533. select * from tenk1 a join tenk1 b on
  3534. (a.unique1 = 1 and b.unique1 = 2) or
  3535. ((a.unique2 = 3 or a.unique2 = 7) and b.hundred = 4);
  3536. QUERY PLAN
  3537. ----------------------------------------------------------------------------------------------------------------------
  3538. Nested Loop
  3539. Join Filter: (((a.unique1 = 1) AND (b.unique1 = 2)) OR (((a.unique2 = 3) OR (a.unique2 = 7)) AND (b.hundred = 4)))
  3540. -> Bitmap Heap Scan on tenk1 b
  3541. Recheck Cond: ((unique1 = 2) OR (hundred = 4))
  3542. -> BitmapOr
  3543. -> Bitmap Index Scan on tenk1_unique1
  3544. Index Cond: (unique1 = 2)
  3545. -> Bitmap Index Scan on tenk1_hundred
  3546. Index Cond: (hundred = 4)
  3547. -> Materialize
  3548. -> Bitmap Heap Scan on tenk1 a
  3549. Recheck Cond: ((unique1 = 1) OR (unique2 = 3) OR (unique2 = 7))
  3550. -> BitmapOr
  3551. -> Bitmap Index Scan on tenk1_unique1
  3552. Index Cond: (unique1 = 1)
  3553. -> Bitmap Index Scan on tenk1_unique2
  3554. Index Cond: (unique2 = 3)
  3555. -> Bitmap Index Scan on tenk1_unique2
  3556. Index Cond: (unique2 = 7)
  3557. (19 rows)
  3558. --
  3559. -- test placement of movable quals in a parameterized join tree
  3560. --
  3561. explain (costs off)
  3562. select * from tenk1 t1 left join
  3563. (tenk1 t2 join tenk1 t3 on t2.thousand = t3.unique2)
  3564. on t1.hundred = t2.hundred and t1.ten = t3.ten
  3565. where t1.unique1 = 1;
  3566. QUERY PLAN
  3567. --------------------------------------------------------
  3568. Nested Loop Left Join
  3569. -> Index Scan using tenk1_unique1 on tenk1 t1
  3570. Index Cond: (unique1 = 1)
  3571. -> Nested Loop
  3572. Join Filter: (t1.ten = t3.ten)
  3573. -> Bitmap Heap Scan on tenk1 t2
  3574. Recheck Cond: (t1.hundred = hundred)
  3575. -> Bitmap Index Scan on tenk1_hundred
  3576. Index Cond: (hundred = t1.hundred)
  3577. -> Index Scan using tenk1_unique2 on tenk1 t3
  3578. Index Cond: (unique2 = t2.thousand)
  3579. (11 rows)
  3580. explain (costs off)
  3581. select * from tenk1 t1 left join
  3582. (tenk1 t2 join tenk1 t3 on t2.thousand = t3.unique2)
  3583. on t1.hundred = t2.hundred and t1.ten + t2.ten = t3.ten
  3584. where t1.unique1 = 1;
  3585. QUERY PLAN
  3586. --------------------------------------------------------
  3587. Nested Loop Left Join
  3588. -> Index Scan using tenk1_unique1 on tenk1 t1
  3589. Index Cond: (unique1 = 1)
  3590. -> Nested Loop
  3591. Join Filter: ((t1.ten + t2.ten) = t3.ten)
  3592. -> Bitmap Heap Scan on tenk1 t2
  3593. Recheck Cond: (t1.hundred = hundred)
  3594. -> Bitmap Index Scan on tenk1_hundred
  3595. Index Cond: (hundred = t1.hundred)
  3596. -> Index Scan using tenk1_unique2 on tenk1 t3
  3597. Index Cond: (unique2 = t2.thousand)
  3598. (11 rows)
  3599. explain (costs off)
  3600. select count(*) from
  3601. tenk1 a join tenk1 b on a.unique1 = b.unique2
  3602. left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
  3603. join int4_tbl on b.thousand = f1;
  3604. QUERY PLAN
  3605. -------------------------------------------------------------------------
  3606. Aggregate
  3607. -> Nested Loop Left Join
  3608. Join Filter: (a.unique2 = b.unique1)
  3609. -> Nested Loop
  3610. -> Nested Loop
  3611. -> Seq Scan on int4_tbl
  3612. -> Bitmap Heap Scan on tenk1 b
  3613. Recheck Cond: (thousand = int4_tbl.f1)
  3614. -> Bitmap Index Scan on tenk1_thous_tenthous
  3615. Index Cond: (thousand = int4_tbl.f1)
  3616. -> Index Scan using tenk1_unique1 on tenk1 a
  3617. Index Cond: (unique1 = b.unique2)
  3618. -> Index Only Scan using tenk1_thous_tenthous on tenk1 c
  3619. Index Cond: (thousand = a.thousand)
  3620. (14 rows)
  3621. select count(*) from
  3622. tenk1 a join tenk1 b on a.unique1 = b.unique2
  3623. left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
  3624. join int4_tbl on b.thousand = f1;
  3625. count
  3626. -------
  3627. 10
  3628. (1 row)
  3629. explain (costs off)
  3630. select b.unique1 from
  3631. tenk1 a join tenk1 b on a.unique1 = b.unique2
  3632. left join tenk1 c on b.unique1 = 42 and c.thousand = a.thousand
  3633. join int4_tbl i1 on b.thousand = f1
  3634. right join int4_tbl i2 on i2.f1 = b.tenthous
  3635. order by 1;
  3636. QUERY PLAN
  3637. -----------------------------------------------------------------------------------------
  3638. Sort
  3639. Sort Key: b.unique1
  3640. -> Nested Loop Left Join
  3641. -> Seq Scan on int4_tbl i2
  3642. -> Nested Loop Left Join
  3643. Join Filter: (b.unique1 = 42)
  3644. -> Nested Loop
  3645. -> Nested Loop
  3646. -> Seq Scan on int4_tbl i1
  3647. -> Index Scan using tenk1_thous_tenthous on tenk1 b
  3648. Index Cond: ((thousand = i1.f1) AND (tenthous = i2.f1))
  3649. -> Index Scan using tenk1_unique1 on tenk1 a
  3650. Index Cond: (unique1 = b.unique2)
  3651. -> Index Only Scan using tenk1_thous_tenthous on tenk1 c
  3652. Index Cond: (thousand = a.thousand)
  3653. (15 rows)
  3654. select b.unique1 from
  3655. tenk1 a join tenk1 b on a.unique1 = b.unique2
  3656. left join tenk1 c on b.unique1 = 42 and c.thousand = a.thousand
  3657. join int4_tbl i1 on b.thousand = f1
  3658. right join int4_tbl i2 on i2.f1 = b.tenthous
  3659. order by 1;
  3660. unique1
  3661. ---------
  3662. 0
  3663. (5 rows)
  3664. explain (costs off)
  3665. select * from
  3666. (
  3667. select unique1, q1, coalesce(unique1, -1) + q1 as fault
  3668. from int8_tbl left join tenk1 on (q2 = unique2)
  3669. ) ss
  3670. where fault = 122
  3671. order by fault;
  3672. QUERY PLAN
  3673. --------------------------------------------------------------------------
  3674. Nested Loop Left Join
  3675. Filter: ((COALESCE(tenk1.unique1, '-1'::integer) + int8_tbl.q1) = 122)
  3676. -> Seq Scan on int8_tbl
  3677. -> Index Scan using tenk1_unique2 on tenk1
  3678. Index Cond: (unique2 = int8_tbl.q2)
  3679. (5 rows)
  3680. select * from
  3681. (
  3682. select unique1, q1, coalesce(unique1, -1) + q1 as fault
  3683. from int8_tbl left join tenk1 on (q2 = unique2)
  3684. ) ss
  3685. where fault = 122
  3686. order by fault;
  3687. unique1 | q1 | fault
  3688. ---------+-----+-------
  3689. | 123 | 122
  3690. (1 row)
  3691. explain (costs off)
  3692. select * from
  3693. (values (1, array[10,20]), (2, array[20,30])) as v1(v1x,v1ys)
  3694. left join (values (1, 10), (2, 20)) as v2(v2x,v2y) on v2x = v1x
  3695. left join unnest(v1ys) as u1(u1y) on u1y = v2y;
  3696. QUERY PLAN
  3697. -------------------------------------------------------------
  3698. Nested Loop Left Join
  3699. -> Values Scan on "*VALUES*"
  3700. -> Hash Right Join
  3701. Hash Cond: (u1.u1y = "*VALUES*_1".column2)
  3702. Filter: ("*VALUES*_1".column1 = "*VALUES*".column1)
  3703. -> Function Scan on unnest u1
  3704. -> Hash
  3705. -> Values Scan on "*VALUES*_1"
  3706. (8 rows)
  3707. select * from
  3708. (values (1, array[10,20]), (2, array[20,30])) as v1(v1x,v1ys)
  3709. left join (values (1, 10), (2, 20)) as v2(v2x,v2y) on v2x = v1x
  3710. left join unnest(v1ys) as u1(u1y) on u1y = v2y;
  3711. v1x | v1ys | v2x | v2y | u1y
  3712. -----+---------+-----+-----+-----
  3713. 1 | {10,20} | 1 | 10 | 10
  3714. 2 | {20,30} | 2 | 20 | 20
  3715. (2 rows)
  3716. --
  3717. -- test handling of potential equivalence clauses above outer joins
  3718. --
  3719. explain (costs off)
  3720. select q1, unique2, thousand, hundred
  3721. from int8_tbl a left join tenk1 b on q1 = unique2
  3722. where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
  3723. QUERY PLAN
  3724. --------------------------------------------------------------------------------------
  3725. Nested Loop Left Join
  3726. Filter: ((COALESCE(b.thousand, 123) = a.q1) AND (a.q1 = COALESCE(b.hundred, 123)))
  3727. -> Seq Scan on int8_tbl a
  3728. -> Index Scan using tenk1_unique2 on tenk1 b
  3729. Index Cond: (unique2 = a.q1)
  3730. (5 rows)
  3731. select q1, unique2, thousand, hundred
  3732. from int8_tbl a left join tenk1 b on q1 = unique2
  3733. where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
  3734. q1 | unique2 | thousand | hundred
  3735. ----+---------+----------+---------
  3736. (0 rows)
  3737. explain (costs off)
  3738. select f1, unique2, case when unique2 is null then f1 else 0 end
  3739. from int4_tbl a left join tenk1 b on f1 = unique2
  3740. where (case when unique2 is null then f1 else 0 end) = 0;
  3741. QUERY PLAN
  3742. --------------------------------------------------------------------
  3743. Nested Loop Left Join
  3744. Filter: (CASE WHEN (b.unique2 IS NULL) THEN a.f1 ELSE 0 END = 0)
  3745. -> Seq Scan on int4_tbl a
  3746. -> Index Only Scan using tenk1_unique2 on tenk1 b
  3747. Index Cond: (unique2 = a.f1)
  3748. (5 rows)
  3749. select f1, unique2, case when unique2 is null then f1 else 0 end
  3750. from int4_tbl a left join tenk1 b on f1 = unique2
  3751. where (case when unique2 is null then f1 else 0 end) = 0;
  3752. f1 | unique2 | case
  3753. ----+---------+------
  3754. 0 | 0 | 0
  3755. (1 row)
  3756. --
  3757. -- another case with equivalence clauses above outer joins (bug #8591)
  3758. --
  3759. explain (costs off)
  3760. select a.unique1, b.unique1, c.unique1, coalesce(b.twothousand, a.twothousand)
  3761. from tenk1 a left join tenk1 b on b.thousand = a.unique1 left join tenk1 c on c.unique2 = coalesce(b.twothousand, a.twothousand)
  3762. where a.unique2 < 10 and coalesce(b.twothousand, a.twothousand) = 44;
  3763. QUERY PLAN
  3764. ---------------------------------------------------------------------------------------------
  3765. Nested Loop Left Join
  3766. -> Nested Loop Left Join
  3767. Filter: (COALESCE(b.twothousand, a.twothousand) = 44)
  3768. -> Index Scan using tenk1_unique2 on tenk1 a
  3769. Index Cond: (unique2 < 10)
  3770. -> Bitmap Heap Scan on tenk1 b
  3771. Recheck Cond: (thousand = a.unique1)
  3772. -> Bitmap Index Scan on tenk1_thous_tenthous
  3773. Index Cond: (thousand = a.unique1)
  3774. -> Index Scan using tenk1_unique2 on tenk1 c
  3775. Index Cond: ((unique2 = COALESCE(b.twothousand, a.twothousand)) AND (unique2 = 44))
  3776. (11 rows)
  3777. select a.unique1, b.unique1, c.unique1, coalesce(b.twothousand, a.twothousand)
  3778. from tenk1 a left join tenk1 b on b.thousand = a.unique1 left join tenk1 c on c.unique2 = coalesce(b.twothousand, a.twothousand)
  3779. where a.unique2 < 10 and coalesce(b.twothousand, a.twothousand) = 44;
  3780. unique1 | unique1 | unique1 | coalesce
  3781. ---------+---------+---------+----------
  3782. (0 rows)
  3783. --
  3784. -- check handling of join aliases when flattening multiple levels of subquery
  3785. --
  3786. explain (verbose, costs off)
  3787. select foo1.join_key as foo1_id, foo3.join_key AS foo3_id, bug_field from
  3788. (values (0),(1)) foo1(join_key)
  3789. left join
  3790. (select join_key, bug_field from
  3791. (select ss1.join_key, ss1.bug_field from
  3792. (select f1 as join_key, 666 as bug_field from int4_tbl i1) ss1
  3793. ) foo2
  3794. left join
  3795. (select unique2 as join_key from tenk1 i2) ss2
  3796. using (join_key)
  3797. ) foo3
  3798. using (join_key);
  3799. QUERY PLAN
  3800. --------------------------------------------------------------------------
  3801. Nested Loop Left Join
  3802. Output: "*VALUES*".column1, i1.f1, (666)
  3803. Join Filter: ("*VALUES*".column1 = i1.f1)
  3804. -> Values Scan on "*VALUES*"
  3805. Output: "*VALUES*".column1
  3806. -> Materialize
  3807. Output: i1.f1, (666)
  3808. -> Nested Loop Left Join
  3809. Output: i1.f1, 666
  3810. -> Seq Scan on public.int4_tbl i1
  3811. Output: i1.f1
  3812. -> Index Only Scan using tenk1_unique2 on public.tenk1 i2
  3813. Output: i2.unique2
  3814. Index Cond: (i2.unique2 = i1.f1)
  3815. (14 rows)
  3816. select foo1.join_key as foo1_id, foo3.join_key AS foo3_id, bug_field from
  3817. (values (0),(1)) foo1(join_key)
  3818. left join
  3819. (select join_key, bug_field from
  3820. (select ss1.join_key, ss1.bug_field from
  3821. (select f1 as join_key, 666 as bug_field from int4_tbl i1) ss1
  3822. ) foo2
  3823. left join
  3824. (select unique2 as join_key from tenk1 i2) ss2
  3825. using (join_key)
  3826. ) foo3
  3827. using (join_key);
  3828. foo1_id | foo3_id | bug_field
  3829. ---------+---------+-----------
  3830. 0 | 0 | 666
  3831. 1 | |
  3832. (2 rows)
  3833. --
  3834. -- test successful handling of nested outer joins with degenerate join quals
  3835. --
  3836. explain (verbose, costs off)
  3837. select t1.* from
  3838. text_tbl t1
  3839. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  3840. left join int8_tbl i8
  3841. left join (select *, null::int as d2 from int8_tbl i8b2) b2
  3842. on (i8.q1 = b2.q1)
  3843. on (b2.d2 = b1.q2)
  3844. on (t1.f1 = b1.d1)
  3845. left join int4_tbl i4
  3846. on (i8.q2 = i4.f1);
  3847. QUERY PLAN
  3848. ----------------------------------------------------------------------
  3849. Hash Left Join
  3850. Output: t1.f1
  3851. Hash Cond: (i8.q2 = i4.f1)
  3852. -> Nested Loop Left Join
  3853. Output: t1.f1, i8.q2
  3854. Join Filter: (t1.f1 = '***'::text)
  3855. -> Seq Scan on public.text_tbl t1
  3856. Output: t1.f1
  3857. -> Materialize
  3858. Output: i8.q2
  3859. -> Hash Right Join
  3860. Output: i8.q2
  3861. Hash Cond: ((NULL::integer) = i8b1.q2)
  3862. -> Hash Join
  3863. Output: i8.q2, (NULL::integer)
  3864. Hash Cond: (i8.q1 = i8b2.q1)
  3865. -> Seq Scan on public.int8_tbl i8
  3866. Output: i8.q1, i8.q2
  3867. -> Hash
  3868. Output: i8b2.q1, (NULL::integer)
  3869. -> Seq Scan on public.int8_tbl i8b2
  3870. Output: i8b2.q1, NULL::integer
  3871. -> Hash
  3872. Output: i8b1.q2
  3873. -> Seq Scan on public.int8_tbl i8b1
  3874. Output: i8b1.q2
  3875. -> Hash
  3876. Output: i4.f1
  3877. -> Seq Scan on public.int4_tbl i4
  3878. Output: i4.f1
  3879. (30 rows)
  3880. select t1.* from
  3881. text_tbl t1
  3882. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  3883. left join int8_tbl i8
  3884. left join (select *, null::int as d2 from int8_tbl i8b2) b2
  3885. on (i8.q1 = b2.q1)
  3886. on (b2.d2 = b1.q2)
  3887. on (t1.f1 = b1.d1)
  3888. left join int4_tbl i4
  3889. on (i8.q2 = i4.f1);
  3890. f1
  3891. -------------------
  3892. doh!
  3893. hi de ho neighbor
  3894. (2 rows)
  3895. explain (verbose, costs off)
  3896. select t1.* from
  3897. text_tbl t1
  3898. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  3899. left join int8_tbl i8
  3900. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2) b2
  3901. on (i8.q1 = b2.q1)
  3902. on (b2.d2 = b1.q2)
  3903. on (t1.f1 = b1.d1)
  3904. left join int4_tbl i4
  3905. on (i8.q2 = i4.f1);
  3906. QUERY PLAN
  3907. ----------------------------------------------------------------------------
  3908. Hash Left Join
  3909. Output: t1.f1
  3910. Hash Cond: (i8.q2 = i4.f1)
  3911. -> Nested Loop Left Join
  3912. Output: t1.f1, i8.q2
  3913. Join Filter: (t1.f1 = '***'::text)
  3914. -> Seq Scan on public.text_tbl t1
  3915. Output: t1.f1
  3916. -> Materialize
  3917. Output: i8.q2
  3918. -> Hash Right Join
  3919. Output: i8.q2
  3920. Hash Cond: ((NULL::integer) = i8b1.q2)
  3921. -> Hash Right Join
  3922. Output: i8.q2, (NULL::integer)
  3923. Hash Cond: (i8b2.q1 = i8.q1)
  3924. -> Nested Loop
  3925. Output: i8b2.q1, NULL::integer
  3926. -> Seq Scan on public.int8_tbl i8b2
  3927. Output: i8b2.q1, i8b2.q2
  3928. -> Materialize
  3929. -> Seq Scan on public.int4_tbl i4b2
  3930. -> Hash
  3931. Output: i8.q1, i8.q2
  3932. -> Seq Scan on public.int8_tbl i8
  3933. Output: i8.q1, i8.q2
  3934. -> Hash
  3935. Output: i8b1.q2
  3936. -> Seq Scan on public.int8_tbl i8b1
  3937. Output: i8b1.q2
  3938. -> Hash
  3939. Output: i4.f1
  3940. -> Seq Scan on public.int4_tbl i4
  3941. Output: i4.f1
  3942. (34 rows)
  3943. select t1.* from
  3944. text_tbl t1
  3945. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  3946. left join int8_tbl i8
  3947. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2) b2
  3948. on (i8.q1 = b2.q1)
  3949. on (b2.d2 = b1.q2)
  3950. on (t1.f1 = b1.d1)
  3951. left join int4_tbl i4
  3952. on (i8.q2 = i4.f1);
  3953. f1
  3954. -------------------
  3955. doh!
  3956. hi de ho neighbor
  3957. (2 rows)
  3958. explain (verbose, costs off)
  3959. select t1.* from
  3960. text_tbl t1
  3961. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  3962. left join int8_tbl i8
  3963. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2
  3964. where q1 = f1) b2
  3965. on (i8.q1 = b2.q1)
  3966. on (b2.d2 = b1.q2)
  3967. on (t1.f1 = b1.d1)
  3968. left join int4_tbl i4
  3969. on (i8.q2 = i4.f1);
  3970. QUERY PLAN
  3971. ----------------------------------------------------------------------------
  3972. Hash Left Join
  3973. Output: t1.f1
  3974. Hash Cond: (i8.q2 = i4.f1)
  3975. -> Nested Loop Left Join
  3976. Output: t1.f1, i8.q2
  3977. Join Filter: (t1.f1 = '***'::text)
  3978. -> Seq Scan on public.text_tbl t1
  3979. Output: t1.f1
  3980. -> Materialize
  3981. Output: i8.q2
  3982. -> Hash Right Join
  3983. Output: i8.q2
  3984. Hash Cond: ((NULL::integer) = i8b1.q2)
  3985. -> Hash Right Join
  3986. Output: i8.q2, (NULL::integer)
  3987. Hash Cond: (i8b2.q1 = i8.q1)
  3988. -> Hash Join
  3989. Output: i8b2.q1, NULL::integer
  3990. Hash Cond: (i8b2.q1 = i4b2.f1)
  3991. -> Seq Scan on public.int8_tbl i8b2
  3992. Output: i8b2.q1, i8b2.q2
  3993. -> Hash
  3994. Output: i4b2.f1
  3995. -> Seq Scan on public.int4_tbl i4b2
  3996. Output: i4b2.f1
  3997. -> Hash
  3998. Output: i8.q1, i8.q2
  3999. -> Seq Scan on public.int8_tbl i8
  4000. Output: i8.q1, i8.q2
  4001. -> Hash
  4002. Output: i8b1.q2
  4003. -> Seq Scan on public.int8_tbl i8b1
  4004. Output: i8b1.q2
  4005. -> Hash
  4006. Output: i4.f1
  4007. -> Seq Scan on public.int4_tbl i4
  4008. Output: i4.f1
  4009. (37 rows)
  4010. select t1.* from
  4011. text_tbl t1
  4012. left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
  4013. left join int8_tbl i8
  4014. left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2
  4015. where q1 = f1) b2
  4016. on (i8.q1 = b2.q1)
  4017. on (b2.d2 = b1.q2)
  4018. on (t1.f1 = b1.d1)
  4019. left join int4_tbl i4
  4020. on (i8.q2 = i4.f1);
  4021. f1
  4022. -------------------
  4023. doh!
  4024. hi de ho neighbor
  4025. (2 rows)
  4026. explain (verbose, costs off)
  4027. select * from
  4028. text_tbl t1
  4029. inner join int8_tbl i8
  4030. on i8.q2 = 456
  4031. right join text_tbl t2
  4032. on t1.f1 = 'doh!'
  4033. left join int4_tbl i4
  4034. on i8.q1 = i4.f1;
  4035. QUERY PLAN
  4036. --------------------------------------------------------
  4037. Nested Loop Left Join
  4038. Output: t1.f1, i8.q1, i8.q2, t2.f1, i4.f1
  4039. -> Seq Scan on public.text_tbl t2
  4040. Output: t2.f1
  4041. -> Materialize
  4042. Output: i8.q1, i8.q2, i4.f1, t1.f1
  4043. -> Nested Loop
  4044. Output: i8.q1, i8.q2, i4.f1, t1.f1
  4045. -> Nested Loop Left Join
  4046. Output: i8.q1, i8.q2, i4.f1
  4047. Join Filter: (i8.q1 = i4.f1)
  4048. -> Seq Scan on public.int8_tbl i8
  4049. Output: i8.q1, i8.q2
  4050. Filter: (i8.q2 = 456)
  4051. -> Seq Scan on public.int4_tbl i4
  4052. Output: i4.f1
  4053. -> Seq Scan on public.text_tbl t1
  4054. Output: t1.f1
  4055. Filter: (t1.f1 = 'doh!'::text)
  4056. (19 rows)
  4057. select * from
  4058. text_tbl t1
  4059. inner join int8_tbl i8
  4060. on i8.q2 = 456
  4061. right join text_tbl t2
  4062. on t1.f1 = 'doh!'
  4063. left join int4_tbl i4
  4064. on i8.q1 = i4.f1;
  4065. f1 | q1 | q2 | f1 | f1
  4066. ------+-----+-----+-------------------+----
  4067. doh! | 123 | 456 | doh! |
  4068. doh! | 123 | 456 | hi de ho neighbor |
  4069. (2 rows)
  4070. --
  4071. -- test for appropriate join order in the presence of lateral references
  4072. --
  4073. explain (verbose, costs off)
  4074. select * from
  4075. text_tbl t1
  4076. left join int8_tbl i8
  4077. on i8.q2 = 123,
  4078. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
  4079. where t1.f1 = ss.f1;
  4080. QUERY PLAN
  4081. --------------------------------------------------
  4082. Nested Loop
  4083. Output: t1.f1, i8.q1, i8.q2, (i8.q1), t2.f1
  4084. Join Filter: (t1.f1 = t2.f1)
  4085. -> Nested Loop Left Join
  4086. Output: t1.f1, i8.q1, i8.q2
  4087. -> Seq Scan on public.text_tbl t1
  4088. Output: t1.f1
  4089. -> Materialize
  4090. Output: i8.q1, i8.q2
  4091. -> Seq Scan on public.int8_tbl i8
  4092. Output: i8.q1, i8.q2
  4093. Filter: (i8.q2 = 123)
  4094. -> Memoize
  4095. Output: (i8.q1), t2.f1
  4096. Cache Key: i8.q1
  4097. Cache Mode: binary
  4098. -> Limit
  4099. Output: (i8.q1), t2.f1
  4100. -> Seq Scan on public.text_tbl t2
  4101. Output: i8.q1, t2.f1
  4102. (20 rows)
  4103. select * from
  4104. text_tbl t1
  4105. left join int8_tbl i8
  4106. on i8.q2 = 123,
  4107. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
  4108. where t1.f1 = ss.f1;
  4109. f1 | q1 | q2 | q1 | f1
  4110. ------+------------------+-----+------------------+------
  4111. doh! | 4567890123456789 | 123 | 4567890123456789 | doh!
  4112. (1 row)
  4113. explain (verbose, costs off)
  4114. select * from
  4115. text_tbl t1
  4116. left join int8_tbl i8
  4117. on i8.q2 = 123,
  4118. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
  4119. lateral (select ss1.* from text_tbl t3 limit 1) as ss2
  4120. where t1.f1 = ss2.f1;
  4121. QUERY PLAN
  4122. -------------------------------------------------------------------
  4123. Nested Loop
  4124. Output: t1.f1, i8.q1, i8.q2, (i8.q1), t2.f1, ((i8.q1)), (t2.f1)
  4125. Join Filter: (t1.f1 = (t2.f1))
  4126. -> Nested Loop
  4127. Output: t1.f1, i8.q1, i8.q2, (i8.q1), t2.f1
  4128. -> Nested Loop Left Join
  4129. Output: t1.f1, i8.q1, i8.q2
  4130. -> Seq Scan on public.text_tbl t1
  4131. Output: t1.f1
  4132. -> Materialize
  4133. Output: i8.q1, i8.q2
  4134. -> Seq Scan on public.int8_tbl i8
  4135. Output: i8.q1, i8.q2
  4136. Filter: (i8.q2 = 123)
  4137. -> Memoize
  4138. Output: (i8.q1), t2.f1
  4139. Cache Key: i8.q1
  4140. Cache Mode: binary
  4141. -> Limit
  4142. Output: (i8.q1), t2.f1
  4143. -> Seq Scan on public.text_tbl t2
  4144. Output: i8.q1, t2.f1
  4145. -> Memoize
  4146. Output: ((i8.q1)), (t2.f1)
  4147. Cache Key: (i8.q1), t2.f1
  4148. Cache Mode: binary
  4149. -> Limit
  4150. Output: ((i8.q1)), (t2.f1)
  4151. -> Seq Scan on public.text_tbl t3
  4152. Output: (i8.q1), t2.f1
  4153. (30 rows)
  4154. select * from
  4155. text_tbl t1
  4156. left join int8_tbl i8
  4157. on i8.q2 = 123,
  4158. lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
  4159. lateral (select ss1.* from text_tbl t3 limit 1) as ss2
  4160. where t1.f1 = ss2.f1;
  4161. f1 | q1 | q2 | q1 | f1 | q1 | f1
  4162. ------+------------------+-----+------------------+------+------------------+------
  4163. doh! | 4567890123456789 | 123 | 4567890123456789 | doh! | 4567890123456789 | doh!
  4164. (1 row)
  4165. explain (verbose, costs off)
  4166. select 1 from
  4167. text_tbl as tt1
  4168. inner join text_tbl as tt2 on (tt1.f1 = 'foo')
  4169. left join text_tbl as tt3 on (tt3.f1 = 'foo')
  4170. left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
  4171. lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
  4172. where tt1.f1 = ss1.c0;
  4173. QUERY PLAN
  4174. ----------------------------------------------------------
  4175. Nested Loop
  4176. Output: 1
  4177. -> Nested Loop Left Join
  4178. Output: tt1.f1, tt4.f1
  4179. -> Nested Loop
  4180. Output: tt1.f1
  4181. -> Seq Scan on public.text_tbl tt1
  4182. Output: tt1.f1
  4183. Filter: (tt1.f1 = 'foo'::text)
  4184. -> Seq Scan on public.text_tbl tt2
  4185. Output: tt2.f1
  4186. -> Materialize
  4187. Output: tt4.f1
  4188. -> Nested Loop Left Join
  4189. Output: tt4.f1
  4190. Join Filter: (tt3.f1 = tt4.f1)
  4191. -> Seq Scan on public.text_tbl tt3
  4192. Output: tt3.f1
  4193. Filter: (tt3.f1 = 'foo'::text)
  4194. -> Seq Scan on public.text_tbl tt4
  4195. Output: tt4.f1
  4196. Filter: (tt4.f1 = 'foo'::text)
  4197. -> Memoize
  4198. Output: ss1.c0
  4199. Cache Key: tt4.f1
  4200. Cache Mode: binary
  4201. -> Subquery Scan on ss1
  4202. Output: ss1.c0
  4203. Filter: (ss1.c0 = 'foo'::text)
  4204. -> Limit
  4205. Output: (tt4.f1)
  4206. -> Seq Scan on public.text_tbl tt5
  4207. Output: tt4.f1
  4208. (33 rows)
  4209. select 1 from
  4210. text_tbl as tt1
  4211. inner join text_tbl as tt2 on (tt1.f1 = 'foo')
  4212. left join text_tbl as tt3 on (tt3.f1 = 'foo')
  4213. left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
  4214. lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
  4215. where tt1.f1 = ss1.c0;
  4216. ?column?
  4217. ----------
  4218. (0 rows)
  4219. --
  4220. -- check a case in which a PlaceHolderVar forces join order
  4221. --
  4222. explain (verbose, costs off)
  4223. select ss2.* from
  4224. int4_tbl i41
  4225. left join int8_tbl i8
  4226. join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
  4227. from int4_tbl i42, int4_tbl i43) ss1
  4228. on i8.q1 = ss1.c2
  4229. on i41.f1 = ss1.c1,
  4230. lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
  4231. where ss1.c2 = 0;
  4232. QUERY PLAN
  4233. ------------------------------------------------------------------------
  4234. Nested Loop
  4235. Output: (i41.f1), (i8.q1), (i8.q2), (i42.f1), (i43.f1), ((42))
  4236. -> Hash Join
  4237. Output: i41.f1, i42.f1, i8.q1, i8.q2, i43.f1, 42
  4238. Hash Cond: (i41.f1 = i42.f1)
  4239. -> Nested Loop
  4240. Output: i8.q1, i8.q2, i43.f1, i41.f1
  4241. -> Nested Loop
  4242. Output: i8.q1, i8.q2, i43.f1
  4243. -> Seq Scan on public.int8_tbl i8
  4244. Output: i8.q1, i8.q2
  4245. Filter: (i8.q1 = 0)
  4246. -> Seq Scan on public.int4_tbl i43
  4247. Output: i43.f1
  4248. Filter: (i43.f1 = 0)
  4249. -> Seq Scan on public.int4_tbl i41
  4250. Output: i41.f1
  4251. -> Hash
  4252. Output: i42.f1
  4253. -> Seq Scan on public.int4_tbl i42
  4254. Output: i42.f1
  4255. -> Limit
  4256. Output: (i41.f1), (i8.q1), (i8.q2), (i42.f1), (i43.f1), ((42))
  4257. -> Seq Scan on public.text_tbl
  4258. Output: i41.f1, i8.q1, i8.q2, i42.f1, i43.f1, (42)
  4259. (25 rows)
  4260. select ss2.* from
  4261. int4_tbl i41
  4262. left join int8_tbl i8
  4263. join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
  4264. from int4_tbl i42, int4_tbl i43) ss1
  4265. on i8.q1 = ss1.c2
  4266. on i41.f1 = ss1.c1,
  4267. lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
  4268. where ss1.c2 = 0;
  4269. f1 | q1 | q2 | c1 | c2 | c3
  4270. ----+----+----+----+----+----
  4271. (0 rows)
  4272. --
  4273. -- test successful handling of full join underneath left join (bug #14105)
  4274. --
  4275. explain (costs off)
  4276. select * from
  4277. (select 1 as id) as xx
  4278. left join
  4279. (tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
  4280. on (xx.id = coalesce(yy.id));
  4281. QUERY PLAN
  4282. ---------------------------------------
  4283. Nested Loop Left Join
  4284. -> Result
  4285. -> Hash Full Join
  4286. Hash Cond: (a1.unique1 = (1))
  4287. Filter: (1 = COALESCE((1)))
  4288. -> Seq Scan on tenk1 a1
  4289. -> Hash
  4290. -> Result
  4291. (8 rows)
  4292. select * from
  4293. (select 1 as id) as xx
  4294. left join
  4295. (tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
  4296. on (xx.id = coalesce(yy.id));
  4297. id | unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4 | id
  4298. ----+---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------+----
  4299. 1 | 1 | 2838 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 3 | BAAAAA | EFEAAA | OOOOxx | 1
  4300. (1 row)
  4301. --
  4302. -- test ability to push constants through outer join clauses
  4303. --
  4304. explain (costs off)
  4305. select * from int4_tbl a left join tenk1 b on f1 = unique2 where f1 = 0;
  4306. QUERY PLAN
  4307. -------------------------------------------------
  4308. Nested Loop Left Join
  4309. Join Filter: (a.f1 = b.unique2)
  4310. -> Seq Scan on int4_tbl a
  4311. Filter: (f1 = 0)
  4312. -> Index Scan using tenk1_unique2 on tenk1 b
  4313. Index Cond: (unique2 = 0)
  4314. (6 rows)
  4315. explain (costs off)
  4316. select * from tenk1 a full join tenk1 b using(unique2) where unique2 = 42;
  4317. QUERY PLAN
  4318. -------------------------------------------------
  4319. Merge Full Join
  4320. Merge Cond: (a.unique2 = b.unique2)
  4321. -> Index Scan using tenk1_unique2 on tenk1 a
  4322. Index Cond: (unique2 = 42)
  4323. -> Index Scan using tenk1_unique2 on tenk1 b
  4324. Index Cond: (unique2 = 42)
  4325. (6 rows)
  4326. --
  4327. -- test that quals attached to an outer join have correct semantics,
  4328. -- specifically that they don't re-use expressions computed below the join;
  4329. -- we force a mergejoin so that coalesce(b.q1, 1) appears as a join input
  4330. --
  4331. set enable_hashjoin to off;
  4332. set enable_nestloop to off;
  4333. explain (verbose, costs off)
  4334. select a.q2, b.q1
  4335. from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
  4336. where coalesce(b.q1, 1) > 0;
  4337. QUERY PLAN
  4338. ---------------------------------------------------------
  4339. Merge Left Join
  4340. Output: a.q2, b.q1
  4341. Merge Cond: (a.q2 = (COALESCE(b.q1, '1'::bigint)))
  4342. Filter: (COALESCE(b.q1, '1'::bigint) > 0)
  4343. -> Sort
  4344. Output: a.q2
  4345. Sort Key: a.q2
  4346. -> Seq Scan on public.int8_tbl a
  4347. Output: a.q2
  4348. -> Sort
  4349. Output: b.q1, (COALESCE(b.q1, '1'::bigint))
  4350. Sort Key: (COALESCE(b.q1, '1'::bigint))
  4351. -> Seq Scan on public.int8_tbl b
  4352. Output: b.q1, COALESCE(b.q1, '1'::bigint)
  4353. (14 rows)
  4354. select a.q2, b.q1
  4355. from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
  4356. where coalesce(b.q1, 1) > 0;
  4357. q2 | q1
  4358. -------------------+------------------
  4359. -4567890123456789 |
  4360. 123 | 123
  4361. 123 | 123
  4362. 456 |
  4363. 4567890123456789 | 4567890123456789
  4364. 4567890123456789 | 4567890123456789
  4365. 4567890123456789 | 4567890123456789
  4366. 4567890123456789 | 4567890123456789
  4367. 4567890123456789 | 4567890123456789
  4368. 4567890123456789 | 4567890123456789
  4369. (10 rows)
  4370. reset enable_hashjoin;
  4371. reset enable_nestloop;
  4372. --
  4373. -- test join removal
  4374. --
  4375. begin;
  4376. CREATE TEMP TABLE a (id int PRIMARY KEY, b_id int);
  4377. CREATE TEMP TABLE b (id int PRIMARY KEY, c_id int);
  4378. CREATE TEMP TABLE c (id int PRIMARY KEY);
  4379. CREATE TEMP TABLE d (a int, b int);
  4380. INSERT INTO a VALUES (0, 0), (1, NULL);
  4381. INSERT INTO b VALUES (0, 0), (1, NULL);
  4382. INSERT INTO c VALUES (0), (1);
  4383. INSERT INTO d VALUES (1,3), (2,2), (3,1);
  4384. -- all three cases should be optimizable into a simple seqscan
  4385. explain (costs off) SELECT a.* FROM a LEFT JOIN b ON a.b_id = b.id;
  4386. QUERY PLAN
  4387. ---------------
  4388. Seq Scan on a
  4389. (1 row)
  4390. explain (costs off) SELECT b.* FROM b LEFT JOIN c ON b.c_id = c.id;
  4391. QUERY PLAN
  4392. ---------------
  4393. Seq Scan on b
  4394. (1 row)
  4395. explain (costs off)
  4396. SELECT a.* FROM a LEFT JOIN (b left join c on b.c_id = c.id)
  4397. ON (a.b_id = b.id);
  4398. QUERY PLAN
  4399. ---------------
  4400. Seq Scan on a
  4401. (1 row)
  4402. -- check optimization of outer join within another special join
  4403. explain (costs off)
  4404. select id from a where id in (
  4405. select b.id from b left join c on b.id = c.id
  4406. );
  4407. QUERY PLAN
  4408. ----------------------------
  4409. Hash Join
  4410. Hash Cond: (a.id = b.id)
  4411. -> Seq Scan on a
  4412. -> Hash
  4413. -> Seq Scan on b
  4414. (5 rows)
  4415. -- check that join removal works for a left join when joining a subquery
  4416. -- that is guaranteed to be unique by its GROUP BY clause
  4417. explain (costs off)
  4418. select d.* from d left join (select * from b group by b.id, b.c_id) s
  4419. on d.a = s.id and d.b = s.c_id;
  4420. QUERY PLAN
  4421. ---------------
  4422. Seq Scan on d
  4423. (1 row)
  4424. -- similarly, but keying off a DISTINCT clause
  4425. explain (costs off)
  4426. select d.* from d left join (select distinct * from b) s
  4427. on d.a = s.id and d.b = s.c_id;
  4428. QUERY PLAN
  4429. ---------------
  4430. Seq Scan on d
  4431. (1 row)
  4432. -- join removal is not possible when the GROUP BY contains a column that is
  4433. -- not in the join condition. (Note: as of 9.6, we notice that b.id is a
  4434. -- primary key and so drop b.c_id from the GROUP BY of the resulting plan;
  4435. -- but this happens too late for join removal in the outer plan level.)
  4436. explain (costs off)
  4437. select d.* from d left join (select * from b group by b.id, b.c_id) s
  4438. on d.a = s.id;
  4439. QUERY PLAN
  4440. ------------------------------------------
  4441. Merge Right Join
  4442. Merge Cond: (b.id = d.a)
  4443. -> Group
  4444. Group Key: b.id
  4445. -> Index Scan using b_pkey on b
  4446. -> Sort
  4447. Sort Key: d.a
  4448. -> Seq Scan on d
  4449. (8 rows)
  4450. -- similarly, but keying off a DISTINCT clause
  4451. explain (costs off)
  4452. select d.* from d left join (select distinct * from b) s
  4453. on d.a = s.id;
  4454. QUERY PLAN
  4455. --------------------------------------
  4456. Merge Right Join
  4457. Merge Cond: (b.id = d.a)
  4458. -> Unique
  4459. -> Sort
  4460. Sort Key: b.id, b.c_id
  4461. -> Seq Scan on b
  4462. -> Sort
  4463. Sort Key: d.a
  4464. -> Seq Scan on d
  4465. (9 rows)
  4466. -- check join removal works when uniqueness of the join condition is enforced
  4467. -- by a UNION
  4468. explain (costs off)
  4469. select d.* from d left join (select id from a union select id from b) s
  4470. on d.a = s.id;
  4471. QUERY PLAN
  4472. ---------------
  4473. Seq Scan on d
  4474. (1 row)
  4475. -- check join removal with a cross-type comparison operator
  4476. explain (costs off)
  4477. select i8.* from int8_tbl i8 left join (select f1 from int4_tbl group by f1) i4
  4478. on i8.q1 = i4.f1;
  4479. QUERY PLAN
  4480. -------------------------
  4481. Seq Scan on int8_tbl i8
  4482. (1 row)
  4483. -- check join removal with lateral references
  4484. explain (costs off)
  4485. select 1 from (select a.id FROM a left join b on a.b_id = b.id) q,
  4486. lateral generate_series(1, q.id) gs(i) where q.id = gs.i;
  4487. QUERY PLAN
  4488. -------------------------------------------
  4489. Nested Loop
  4490. -> Seq Scan on a
  4491. -> Function Scan on generate_series gs
  4492. Filter: (a.id = i)
  4493. (4 rows)
  4494. rollback;
  4495. create temp table parent (k int primary key, pd int);
  4496. create temp table child (k int unique, cd int);
  4497. insert into parent values (1, 10), (2, 20), (3, 30);
  4498. insert into child values (1, 100), (4, 400);
  4499. -- this case is optimizable
  4500. select p.* from parent p left join child c on (p.k = c.k);
  4501. k | pd
  4502. ---+----
  4503. 1 | 10
  4504. 2 | 20
  4505. 3 | 30
  4506. (3 rows)
  4507. explain (costs off)
  4508. select p.* from parent p left join child c on (p.k = c.k);
  4509. QUERY PLAN
  4510. ----------------------
  4511. Seq Scan on parent p
  4512. (1 row)
  4513. -- this case is not
  4514. select p.*, linked from parent p
  4515. left join (select c.*, true as linked from child c) as ss
  4516. on (p.k = ss.k);
  4517. k | pd | linked
  4518. ---+----+--------
  4519. 1 | 10 | t
  4520. 2 | 20 |
  4521. 3 | 30 |
  4522. (3 rows)
  4523. explain (costs off)
  4524. select p.*, linked from parent p
  4525. left join (select c.*, true as linked from child c) as ss
  4526. on (p.k = ss.k);
  4527. QUERY PLAN
  4528. ---------------------------------
  4529. Hash Left Join
  4530. Hash Cond: (p.k = c.k)
  4531. -> Seq Scan on parent p
  4532. -> Hash
  4533. -> Seq Scan on child c
  4534. (5 rows)
  4535. -- check for a 9.0rc1 bug: join removal breaks pseudoconstant qual handling
  4536. select p.* from
  4537. parent p left join child c on (p.k = c.k)
  4538. where p.k = 1 and p.k = 2;
  4539. k | pd
  4540. ---+----
  4541. (0 rows)
  4542. explain (costs off)
  4543. select p.* from
  4544. parent p left join child c on (p.k = c.k)
  4545. where p.k = 1 and p.k = 2;
  4546. QUERY PLAN
  4547. ------------------------------------------------
  4548. Result
  4549. One-Time Filter: false
  4550. -> Index Scan using parent_pkey on parent p
  4551. Index Cond: (k = 1)
  4552. (4 rows)
  4553. select p.* from
  4554. (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
  4555. where p.k = 1 and p.k = 2;
  4556. k | pd
  4557. ---+----
  4558. (0 rows)
  4559. explain (costs off)
  4560. select p.* from
  4561. (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
  4562. where p.k = 1 and p.k = 2;
  4563. QUERY PLAN
  4564. --------------------------
  4565. Result
  4566. One-Time Filter: false
  4567. (2 rows)
  4568. -- bug 5255: this is not optimizable by join removal
  4569. begin;
  4570. CREATE TEMP TABLE a (id int PRIMARY KEY);
  4571. CREATE TEMP TABLE b (id int PRIMARY KEY, a_id int);
  4572. INSERT INTO a VALUES (0), (1);
  4573. INSERT INTO b VALUES (0, 0), (1, NULL);
  4574. SELECT * FROM b LEFT JOIN a ON (b.a_id = a.id) WHERE (a.id IS NULL OR a.id > 0);
  4575. id | a_id | id
  4576. ----+------+----
  4577. 1 | |
  4578. (1 row)
  4579. SELECT b.* FROM b LEFT JOIN a ON (b.a_id = a.id) WHERE (a.id IS NULL OR a.id > 0);
  4580. id | a_id
  4581. ----+------
  4582. 1 |
  4583. (1 row)
  4584. rollback;
  4585. -- another join removal bug: this is not optimizable, either
  4586. begin;
  4587. create temp table innertab (id int8 primary key, dat1 int8);
  4588. insert into innertab values(123, 42);
  4589. SELECT * FROM
  4590. (SELECT 1 AS x) ss1
  4591. LEFT JOIN
  4592. (SELECT q1, q2, COALESCE(dat1, q1) AS y
  4593. FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss2
  4594. ON true;
  4595. x | q1 | q2 | y
  4596. ---+------------------+-------------------+------------------
  4597. 1 | 123 | 456 | 123
  4598. 1 | 123 | 4567890123456789 | 123
  4599. 1 | 4567890123456789 | 123 | 42
  4600. 1 | 4567890123456789 | 4567890123456789 | 4567890123456789
  4601. 1 | 4567890123456789 | -4567890123456789 | 4567890123456789
  4602. (5 rows)
  4603. rollback;
  4604. -- another join removal bug: we must clean up correctly when removing a PHV
  4605. begin;
  4606. create temp table uniquetbl (f1 text unique);
  4607. explain (costs off)
  4608. select t1.* from
  4609. uniquetbl as t1
  4610. left join (select *, '***'::text as d1 from uniquetbl) t2
  4611. on t1.f1 = t2.f1
  4612. left join uniquetbl t3
  4613. on t2.d1 = t3.f1;
  4614. QUERY PLAN
  4615. --------------------------
  4616. Seq Scan on uniquetbl t1
  4617. (1 row)
  4618. explain (costs off)
  4619. select t0.*
  4620. from
  4621. text_tbl t0
  4622. left join
  4623. (select case t1.ten when 0 then 'doh!'::text else null::text end as case1,
  4624. t1.stringu2
  4625. from tenk1 t1
  4626. join int4_tbl i4 ON i4.f1 = t1.unique2
  4627. left join uniquetbl u1 ON u1.f1 = t1.string4) ss
  4628. on t0.f1 = ss.case1
  4629. where ss.stringu2 !~* ss.case1;
  4630. QUERY PLAN
  4631. --------------------------------------------------------------------------------------------
  4632. Nested Loop
  4633. Join Filter: (CASE t1.ten WHEN 0 THEN 'doh!'::text ELSE NULL::text END = t0.f1)
  4634. -> Nested Loop
  4635. -> Seq Scan on int4_tbl i4
  4636. -> Index Scan using tenk1_unique2 on tenk1 t1
  4637. Index Cond: (unique2 = i4.f1)
  4638. Filter: (stringu2 !~* CASE ten WHEN 0 THEN 'doh!'::text ELSE NULL::text END)
  4639. -> Materialize
  4640. -> Seq Scan on text_tbl t0
  4641. (9 rows)
  4642. select t0.*
  4643. from
  4644. text_tbl t0
  4645. left join
  4646. (select case t1.ten when 0 then 'doh!'::text else null::text end as case1,
  4647. t1.stringu2
  4648. from tenk1 t1
  4649. join int4_tbl i4 ON i4.f1 = t1.unique2
  4650. left join uniquetbl u1 ON u1.f1 = t1.string4) ss
  4651. on t0.f1 = ss.case1
  4652. where ss.stringu2 !~* ss.case1;
  4653. f1
  4654. ------
  4655. doh!
  4656. (1 row)
  4657. rollback;
  4658. -- test case to expose miscomputation of required relid set for a PHV
  4659. explain (verbose, costs off)
  4660. select i8.*, ss.v, t.unique2
  4661. from int8_tbl i8
  4662. left join int4_tbl i4 on i4.f1 = 1
  4663. left join lateral (select i4.f1 + 1 as v) as ss on true
  4664. left join tenk1 t on t.unique2 = ss.v
  4665. where q2 = 456;
  4666. QUERY PLAN
  4667. -------------------------------------------------------------
  4668. Nested Loop Left Join
  4669. Output: i8.q1, i8.q2, ((i4.f1 + 1)), t.unique2
  4670. -> Nested Loop Left Join
  4671. Output: i8.q1, i8.q2, (i4.f1 + 1)
  4672. -> Seq Scan on public.int8_tbl i8
  4673. Output: i8.q1, i8.q2
  4674. Filter: (i8.q2 = 456)
  4675. -> Seq Scan on public.int4_tbl i4
  4676. Output: i4.f1
  4677. Filter: (i4.f1 = 1)
  4678. -> Index Only Scan using tenk1_unique2 on public.tenk1 t
  4679. Output: t.unique2
  4680. Index Cond: (t.unique2 = ((i4.f1 + 1)))
  4681. (13 rows)
  4682. select i8.*, ss.v, t.unique2
  4683. from int8_tbl i8
  4684. left join int4_tbl i4 on i4.f1 = 1
  4685. left join lateral (select i4.f1 + 1 as v) as ss on true
  4686. left join tenk1 t on t.unique2 = ss.v
  4687. where q2 = 456;
  4688. q1 | q2 | v | unique2
  4689. -----+-----+---+---------
  4690. 123 | 456 | |
  4691. (1 row)
  4692. -- and check a related issue where we miscompute required relids for
  4693. -- a PHV that's been translated to a child rel
  4694. create temp table parttbl (a integer primary key) partition by range (a);
  4695. create temp table parttbl1 partition of parttbl for values from (1) to (100);
  4696. insert into parttbl values (11), (12);
  4697. explain (costs off)
  4698. select * from
  4699. (select *, 12 as phv from parttbl) as ss
  4700. right join int4_tbl on true
  4701. where ss.a = ss.phv and f1 = 0;
  4702. QUERY PLAN
  4703. ------------------------------------
  4704. Nested Loop
  4705. -> Seq Scan on int4_tbl
  4706. Filter: (f1 = 0)
  4707. -> Seq Scan on parttbl1 parttbl
  4708. Filter: (a = 12)
  4709. (5 rows)
  4710. select * from
  4711. (select *, 12 as phv from parttbl) as ss
  4712. right join int4_tbl on true
  4713. where ss.a = ss.phv and f1 = 0;
  4714. a | phv | f1
  4715. ----+-----+----
  4716. 12 | 12 | 0
  4717. (1 row)
  4718. -- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
  4719. select * from
  4720. int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
  4721. ERROR: column reference "f1" is ambiguous
  4722. LINE 2: ..._tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1;
  4723. ^
  4724. select * from
  4725. int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
  4726. ERROR: invalid reference to FROM-clause entry for table "y"
  4727. LINE 2: ...bl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1;
  4728. ^
  4729. HINT: There is an entry for table "y", but it cannot be referenced from this part of the query.
  4730. select * from
  4731. int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
  4732. q1 | q2 | f1 | ff
  4733. ----+----+----+----
  4734. (0 rows)
  4735. --
  4736. -- Test hints given on incorrect column references are useful
  4737. --
  4738. select t1.uunique1 from
  4739. tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t1" suggestion
  4740. ERROR: column t1.uunique1 does not exist
  4741. LINE 1: select t1.uunique1 from
  4742. ^
  4743. HINT: Perhaps you meant to reference the column "t1.unique1".
  4744. select t2.uunique1 from
  4745. tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t2" suggestion
  4746. ERROR: column t2.uunique1 does not exist
  4747. LINE 1: select t2.uunique1 from
  4748. ^
  4749. HINT: Perhaps you meant to reference the column "t2.unique1".
  4750. select uunique1 from
  4751. tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, suggest both at once
  4752. ERROR: column "uunique1" does not exist
  4753. LINE 1: select uunique1 from
  4754. ^
  4755. HINT: Perhaps you meant to reference the column "t1.unique1" or the column "t2.unique1".
  4756. --
  4757. -- Take care to reference the correct RTE
  4758. --
  4759. select atts.relid::regclass, s.* from pg_stats s join
  4760. pg_attribute a on s.attname = a.attname and s.tablename =
  4761. a.attrelid::regclass::text join (select unnest(indkey) attnum,
  4762. indexrelid from pg_index i) atts on atts.attnum = a.attnum where
  4763. schemaname != 'pg_catalog';
  4764. ERROR: column atts.relid does not exist
  4765. LINE 1: select atts.relid::regclass, s.* from pg_stats s join
  4766. ^
  4767. --
  4768. -- Test LATERAL
  4769. --
  4770. select unique2, x.*
  4771. from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
  4772. unique2 | f1
  4773. ---------+----
  4774. 9998 | 0
  4775. (1 row)
  4776. explain (costs off)
  4777. select unique2, x.*
  4778. from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
  4779. QUERY PLAN
  4780. -------------------------------------------------
  4781. Nested Loop
  4782. -> Seq Scan on int4_tbl b
  4783. -> Index Scan using tenk1_unique1 on tenk1 a
  4784. Index Cond: (unique1 = b.f1)
  4785. (4 rows)
  4786. select unique2, x.*
  4787. from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
  4788. unique2 | f1
  4789. ---------+----
  4790. 9998 | 0
  4791. (1 row)
  4792. explain (costs off)
  4793. select unique2, x.*
  4794. from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
  4795. QUERY PLAN
  4796. -----------------------------------------------
  4797. Nested Loop
  4798. -> Seq Scan on int4_tbl x
  4799. -> Index Scan using tenk1_unique1 on tenk1
  4800. Index Cond: (unique1 = x.f1)
  4801. (4 rows)
  4802. explain (costs off)
  4803. select unique2, x.*
  4804. from int4_tbl x cross join lateral (select unique2 from tenk1 where f1 = unique1) ss;
  4805. QUERY PLAN
  4806. -----------------------------------------------
  4807. Nested Loop
  4808. -> Seq Scan on int4_tbl x
  4809. -> Index Scan using tenk1_unique1 on tenk1
  4810. Index Cond: (unique1 = x.f1)
  4811. (4 rows)
  4812. select unique2, x.*
  4813. from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
  4814. unique2 | f1
  4815. ---------+-------------
  4816. 9998 | 0
  4817. | 123456
  4818. | -123456
  4819. | 2147483647
  4820. | -2147483647
  4821. (5 rows)
  4822. explain (costs off)
  4823. select unique2, x.*
  4824. from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
  4825. QUERY PLAN
  4826. -----------------------------------------------
  4827. Nested Loop Left Join
  4828. -> Seq Scan on int4_tbl x
  4829. -> Index Scan using tenk1_unique1 on tenk1
  4830. Index Cond: (unique1 = x.f1)
  4831. (4 rows)
  4832. -- check scoping of lateral versus parent references
  4833. -- the first of these should return int8_tbl.q2, the second int8_tbl.q1
  4834. select *, (select r from (select q1 as q2) x, (select q2 as r) y) from int8_tbl;
  4835. q1 | q2 | r
  4836. ------------------+-------------------+-------------------
  4837. 123 | 456 | 456
  4838. 123 | 4567890123456789 | 4567890123456789
  4839. 4567890123456789 | 123 | 123
  4840. 4567890123456789 | 4567890123456789 | 4567890123456789
  4841. 4567890123456789 | -4567890123456789 | -4567890123456789
  4842. (5 rows)
  4843. select *, (select r from (select q1 as q2) x, lateral (select q2 as r) y) from int8_tbl;
  4844. q1 | q2 | r
  4845. ------------------+-------------------+------------------
  4846. 123 | 456 | 123
  4847. 123 | 4567890123456789 | 123
  4848. 4567890123456789 | 123 | 4567890123456789
  4849. 4567890123456789 | 4567890123456789 | 4567890123456789
  4850. 4567890123456789 | -4567890123456789 | 4567890123456789
  4851. (5 rows)
  4852. -- lateral with function in FROM
  4853. select count(*) from tenk1 a, lateral generate_series(1,two) g;
  4854. count
  4855. -------
  4856. 5000
  4857. (1 row)
  4858. explain (costs off)
  4859. select count(*) from tenk1 a, lateral generate_series(1,two) g;
  4860. QUERY PLAN
  4861. ------------------------------------------------------
  4862. Aggregate
  4863. -> Nested Loop
  4864. -> Seq Scan on tenk1 a
  4865. -> Memoize
  4866. Cache Key: a.two
  4867. Cache Mode: binary
  4868. -> Function Scan on generate_series g
  4869. (7 rows)
  4870. explain (costs off)
  4871. select count(*) from tenk1 a cross join lateral generate_series(1,two) g;
  4872. QUERY PLAN
  4873. ------------------------------------------------------
  4874. Aggregate
  4875. -> Nested Loop
  4876. -> Seq Scan on tenk1 a
  4877. -> Memoize
  4878. Cache Key: a.two
  4879. Cache Mode: binary
  4880. -> Function Scan on generate_series g
  4881. (7 rows)
  4882. -- don't need the explicit LATERAL keyword for functions
  4883. explain (costs off)
  4884. select count(*) from tenk1 a, generate_series(1,two) g;
  4885. QUERY PLAN
  4886. ------------------------------------------------------
  4887. Aggregate
  4888. -> Nested Loop
  4889. -> Seq Scan on tenk1 a
  4890. -> Memoize
  4891. Cache Key: a.two
  4892. Cache Mode: binary
  4893. -> Function Scan on generate_series g
  4894. (7 rows)
  4895. -- lateral with UNION ALL subselect
  4896. explain (costs off)
  4897. select * from generate_series(100,200) g,
  4898. lateral (select * from int8_tbl a where g = q1 union all
  4899. select * from int8_tbl b where g = q2) ss;
  4900. QUERY PLAN
  4901. ------------------------------------------
  4902. Nested Loop
  4903. -> Function Scan on generate_series g
  4904. -> Append
  4905. -> Seq Scan on int8_tbl a
  4906. Filter: (g.g = q1)
  4907. -> Seq Scan on int8_tbl b
  4908. Filter: (g.g = q2)
  4909. (7 rows)
  4910. select * from generate_series(100,200) g,
  4911. lateral (select * from int8_tbl a where g = q1 union all
  4912. select * from int8_tbl b where g = q2) ss;
  4913. g | q1 | q2
  4914. -----+------------------+------------------
  4915. 123 | 123 | 456
  4916. 123 | 123 | 4567890123456789
  4917. 123 | 4567890123456789 | 123
  4918. (3 rows)
  4919. -- lateral with VALUES
  4920. explain (costs off)
  4921. select count(*) from tenk1 a,
  4922. tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
  4923. QUERY PLAN
  4924. ------------------------------------------------------------
  4925. Aggregate
  4926. -> Merge Join
  4927. Merge Cond: (a.unique1 = b.unique2)
  4928. -> Index Only Scan using tenk1_unique1 on tenk1 a
  4929. -> Index Only Scan using tenk1_unique2 on tenk1 b
  4930. (5 rows)
  4931. select count(*) from tenk1 a,
  4932. tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
  4933. count
  4934. -------
  4935. 10000
  4936. (1 row)
  4937. -- lateral with VALUES, no flattening possible
  4938. explain (costs off)
  4939. select count(*) from tenk1 a,
  4940. tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
  4941. QUERY PLAN
  4942. ------------------------------------------------------------------
  4943. Aggregate
  4944. -> Nested Loop
  4945. -> Nested Loop
  4946. -> Index Only Scan using tenk1_unique1 on tenk1 a
  4947. -> Values Scan on "*VALUES*"
  4948. -> Memoize
  4949. Cache Key: "*VALUES*".column1
  4950. Cache Mode: logical
  4951. -> Index Only Scan using tenk1_unique2 on tenk1 b
  4952. Index Cond: (unique2 = "*VALUES*".column1)
  4953. (10 rows)
  4954. select count(*) from tenk1 a,
  4955. tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
  4956. count
  4957. -------
  4958. 10000
  4959. (1 row)
  4960. -- lateral injecting a strange outer join condition
  4961. explain (costs off)
  4962. select * from int8_tbl a,
  4963. int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
  4964. on x.q2 = ss.z
  4965. order by a.q1, a.q2, x.q1, x.q2, ss.z;
  4966. QUERY PLAN
  4967. ------------------------------------------------
  4968. Sort
  4969. Sort Key: a.q1, a.q2, x.q1, x.q2, (a.q1)
  4970. -> Nested Loop
  4971. -> Seq Scan on int8_tbl a
  4972. -> Hash Right Join
  4973. Hash Cond: ((a.q1) = x.q2)
  4974. -> Seq Scan on int4_tbl y
  4975. -> Hash
  4976. -> Seq Scan on int8_tbl x
  4977. (9 rows)
  4978. select * from int8_tbl a,
  4979. int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
  4980. on x.q2 = ss.z
  4981. order by a.q1, a.q2, x.q1, x.q2, ss.z;
  4982. q1 | q2 | q1 | q2 | z
  4983. ------------------+-------------------+------------------+-------------------+------------------
  4984. 123 | 456 | 123 | 456 |
  4985. 123 | 456 | 123 | 4567890123456789 |
  4986. 123 | 456 | 4567890123456789 | -4567890123456789 |
  4987. 123 | 456 | 4567890123456789 | 123 | 123
  4988. 123 | 456 | 4567890123456789 | 123 | 123
  4989. 123 | 456 | 4567890123456789 | 123 | 123
  4990. 123 | 456 | 4567890123456789 | 123 | 123
  4991. 123 | 456 | 4567890123456789 | 123 | 123
  4992. 123 | 456 | 4567890123456789 | 4567890123456789 |
  4993. 123 | 4567890123456789 | 123 | 456 |
  4994. 123 | 4567890123456789 | 123 | 4567890123456789 |
  4995. 123 | 4567890123456789 | 4567890123456789 | -4567890123456789 |
  4996. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  4997. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  4998. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  4999. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5000. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5001. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 |
  5002. 4567890123456789 | -4567890123456789 | 123 | 456 |
  5003. 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5004. 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5005. 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5006. 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5007. 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5008. 4567890123456789 | -4567890123456789 | 4567890123456789 | -4567890123456789 |
  5009. 4567890123456789 | -4567890123456789 | 4567890123456789 | 123 |
  5010. 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5011. 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5012. 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5013. 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5014. 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5015. 4567890123456789 | 123 | 123 | 456 |
  5016. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789
  5017. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789
  5018. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789
  5019. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789
  5020. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789
  5021. 4567890123456789 | 123 | 4567890123456789 | -4567890123456789 |
  5022. 4567890123456789 | 123 | 4567890123456789 | 123 |
  5023. 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5024. 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5025. 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5026. 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5027. 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5028. 4567890123456789 | 4567890123456789 | 123 | 456 |
  5029. 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5030. 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5031. 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5032. 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5033. 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5034. 4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789 |
  5035. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 |
  5036. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5037. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5038. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5039. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5040. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5041. (57 rows)
  5042. -- lateral reference to a join alias variable
  5043. select * from (select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
  5044. lateral (select x) ss2(y);
  5045. x | f1 | y
  5046. ---+----+---
  5047. 0 | 0 | 0
  5048. (1 row)
  5049. select * from (select f1 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
  5050. lateral (values(x)) ss2(y);
  5051. x | f1 | y
  5052. -------------+-------------+-------------
  5053. 0 | 0 | 0
  5054. 123456 | 123456 | 123456
  5055. -123456 | -123456 | -123456
  5056. 2147483647 | 2147483647 | 2147483647
  5057. -2147483647 | -2147483647 | -2147483647
  5058. (5 rows)
  5059. select * from ((select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1) j,
  5060. lateral (select x) ss2(y);
  5061. x | f1 | y
  5062. ---+----+---
  5063. 0 | 0 | 0
  5064. (1 row)
  5065. -- lateral references requiring pullup
  5066. select * from (values(1)) x(lb),
  5067. lateral generate_series(lb,4) x4;
  5068. lb | x4
  5069. ----+----
  5070. 1 | 1
  5071. 1 | 2
  5072. 1 | 3
  5073. 1 | 4
  5074. (4 rows)
  5075. select * from (select f1/1000000000 from int4_tbl) x(lb),
  5076. lateral generate_series(lb,4) x4;
  5077. lb | x4
  5078. ----+----
  5079. 0 | 0
  5080. 0 | 1
  5081. 0 | 2
  5082. 0 | 3
  5083. 0 | 4
  5084. 0 | 0
  5085. 0 | 1
  5086. 0 | 2
  5087. 0 | 3
  5088. 0 | 4
  5089. 0 | 0
  5090. 0 | 1
  5091. 0 | 2
  5092. 0 | 3
  5093. 0 | 4
  5094. 2 | 2
  5095. 2 | 3
  5096. 2 | 4
  5097. -2 | -2
  5098. -2 | -1
  5099. -2 | 0
  5100. -2 | 1
  5101. -2 | 2
  5102. -2 | 3
  5103. -2 | 4
  5104. (25 rows)
  5105. select * from (values(1)) x(lb),
  5106. lateral (values(lb)) y(lbcopy);
  5107. lb | lbcopy
  5108. ----+--------
  5109. 1 | 1
  5110. (1 row)
  5111. select * from (values(1)) x(lb),
  5112. lateral (select lb from int4_tbl) y(lbcopy);
  5113. lb | lbcopy
  5114. ----+--------
  5115. 1 | 1
  5116. 1 | 1
  5117. 1 | 1
  5118. 1 | 1
  5119. 1 | 1
  5120. (5 rows)
  5121. select * from
  5122. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  5123. lateral (values(x.q1,y.q1,y.q2)) v(xq1,yq1,yq2);
  5124. q1 | q2 | q1 | q2 | xq1 | yq1 | yq2
  5125. ------------------+-------------------+------------------+-------------------+------------------+------------------+-------------------
  5126. 123 | 456 | | | 123 | |
  5127. 123 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | -4567890123456789
  5128. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5129. 123 | 4567890123456789 | 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5130. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789
  5131. 4567890123456789 | 123 | 123 | 456 | 4567890123456789 | 123 | 456
  5132. 4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789
  5133. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5134. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 123
  5135. 4567890123456789 | -4567890123456789 | | | 4567890123456789 | |
  5136. (10 rows)
  5137. select * from
  5138. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  5139. lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
  5140. q1 | q2 | q1 | q2 | xq1 | yq1 | yq2
  5141. ------------------+-------------------+------------------+-------------------+------------------+------------------+-------------------
  5142. 123 | 456 | | | 123 | |
  5143. 123 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 123 | 4567890123456789 | -4567890123456789
  5144. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5145. 123 | 4567890123456789 | 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5146. 4567890123456789 | 123 | 123 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789
  5147. 4567890123456789 | 123 | 123 | 456 | 4567890123456789 | 123 | 456
  5148. 4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789
  5149. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5150. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789 | 123
  5151. 4567890123456789 | -4567890123456789 | | | 4567890123456789 | |
  5152. (10 rows)
  5153. select x.* from
  5154. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  5155. lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
  5156. q1 | q2
  5157. ------------------+-------------------
  5158. 123 | 456
  5159. 123 | 4567890123456789
  5160. 123 | 4567890123456789
  5161. 123 | 4567890123456789
  5162. 4567890123456789 | 123
  5163. 4567890123456789 | 123
  5164. 4567890123456789 | 4567890123456789
  5165. 4567890123456789 | 4567890123456789
  5166. 4567890123456789 | 4567890123456789
  5167. 4567890123456789 | -4567890123456789
  5168. (10 rows)
  5169. select v.* from
  5170. (int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1)
  5171. left join int4_tbl z on z.f1 = x.q2,
  5172. lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
  5173. vx | vy
  5174. -------------------+-------------------
  5175. 123 |
  5176. 456 |
  5177. 123 | 4567890123456789
  5178. 4567890123456789 | -4567890123456789
  5179. 123 | 4567890123456789
  5180. 4567890123456789 | 4567890123456789
  5181. 123 | 4567890123456789
  5182. 4567890123456789 | 123
  5183. 4567890123456789 | 123
  5184. 123 | 4567890123456789
  5185. 4567890123456789 | 123
  5186. 123 | 456
  5187. 4567890123456789 | 4567890123456789
  5188. 4567890123456789 | -4567890123456789
  5189. 4567890123456789 | 4567890123456789
  5190. 4567890123456789 | 4567890123456789
  5191. 4567890123456789 | 4567890123456789
  5192. 4567890123456789 | 123
  5193. 4567890123456789 |
  5194. -4567890123456789 |
  5195. (20 rows)
  5196. select v.* from
  5197. (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
  5198. left join int4_tbl z on z.f1 = x.q2,
  5199. lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
  5200. vx | vy
  5201. -------------------+-------------------
  5202. 4567890123456789 | 123
  5203. 123 | 456
  5204. 4567890123456789 | 123
  5205. 123 | 4567890123456789
  5206. 4567890123456789 | 4567890123456789
  5207. 4567890123456789 | 123
  5208. 123 | 4567890123456789
  5209. 4567890123456789 | 123
  5210. 4567890123456789 | 4567890123456789
  5211. 4567890123456789 | 4567890123456789
  5212. 123 | 4567890123456789
  5213. 4567890123456789 | 4567890123456789
  5214. 4567890123456789 | 4567890123456789
  5215. 4567890123456789 | -4567890123456789
  5216. 123 | 4567890123456789
  5217. 4567890123456789 | -4567890123456789
  5218. 123 |
  5219. 456 |
  5220. 4567890123456789 |
  5221. -4567890123456789 |
  5222. (20 rows)
  5223. select v.* from
  5224. (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
  5225. left join int4_tbl z on z.f1 = x.q2,
  5226. lateral (select x.q1,y.q1 from onerow union all select x.q2,y.q2 from onerow) v(vx,vy);
  5227. vx | vy
  5228. -------------------+-------------------
  5229. 4567890123456789 | 123
  5230. 123 | 456
  5231. 4567890123456789 | 123
  5232. 123 | 4567890123456789
  5233. 4567890123456789 | 4567890123456789
  5234. 4567890123456789 | 123
  5235. 123 | 4567890123456789
  5236. 4567890123456789 | 123
  5237. 4567890123456789 | 4567890123456789
  5238. 4567890123456789 | 4567890123456789
  5239. 123 | 4567890123456789
  5240. 4567890123456789 | 4567890123456789
  5241. 4567890123456789 | 4567890123456789
  5242. 4567890123456789 | -4567890123456789
  5243. 123 | 4567890123456789
  5244. 4567890123456789 | -4567890123456789
  5245. 123 |
  5246. 456 |
  5247. 4567890123456789 |
  5248. -4567890123456789 |
  5249. (20 rows)
  5250. explain (verbose, costs off)
  5251. select * from
  5252. int8_tbl a left join
  5253. lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
  5254. QUERY PLAN
  5255. ------------------------------------------
  5256. Nested Loop Left Join
  5257. Output: a.q1, a.q2, b.q1, b.q2, (a.q2)
  5258. -> Seq Scan on public.int8_tbl a
  5259. Output: a.q1, a.q2
  5260. -> Seq Scan on public.int8_tbl b
  5261. Output: b.q1, b.q2, a.q2
  5262. Filter: (a.q2 = b.q1)
  5263. (7 rows)
  5264. select * from
  5265. int8_tbl a left join
  5266. lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
  5267. q1 | q2 | q1 | q2 | x
  5268. ------------------+-------------------+------------------+-------------------+------------------
  5269. 123 | 456 | | |
  5270. 123 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789
  5271. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5272. 123 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 4567890123456789
  5273. 4567890123456789 | 123 | 123 | 456 | 123
  5274. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5275. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789
  5276. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5277. 4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 4567890123456789
  5278. 4567890123456789 | -4567890123456789 | | |
  5279. (10 rows)
  5280. explain (verbose, costs off)
  5281. select * from
  5282. int8_tbl a left join
  5283. lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
  5284. QUERY PLAN
  5285. ------------------------------------------------------------------
  5286. Nested Loop Left Join
  5287. Output: a.q1, a.q2, b.q1, b.q2, (COALESCE(a.q2, '42'::bigint))
  5288. -> Seq Scan on public.int8_tbl a
  5289. Output: a.q1, a.q2
  5290. -> Seq Scan on public.int8_tbl b
  5291. Output: b.q1, b.q2, COALESCE(a.q2, '42'::bigint)
  5292. Filter: (a.q2 = b.q1)
  5293. (7 rows)
  5294. select * from
  5295. int8_tbl a left join
  5296. lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
  5297. q1 | q2 | q1 | q2 | x
  5298. ------------------+-------------------+------------------+-------------------+------------------
  5299. 123 | 456 | | |
  5300. 123 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789
  5301. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5302. 123 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 4567890123456789
  5303. 4567890123456789 | 123 | 123 | 456 | 123
  5304. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5305. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 4567890123456789
  5306. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5307. 4567890123456789 | 4567890123456789 | 4567890123456789 | -4567890123456789 | 4567890123456789
  5308. 4567890123456789 | -4567890123456789 | | |
  5309. (10 rows)
  5310. -- lateral can result in join conditions appearing below their
  5311. -- real semantic level
  5312. explain (verbose, costs off)
  5313. select * from int4_tbl i left join
  5314. lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
  5315. QUERY PLAN
  5316. -------------------------------------------
  5317. Hash Left Join
  5318. Output: i.f1, j.f1
  5319. Hash Cond: (i.f1 = j.f1)
  5320. -> Seq Scan on public.int4_tbl i
  5321. Output: i.f1
  5322. -> Hash
  5323. Output: j.f1
  5324. -> Seq Scan on public.int2_tbl j
  5325. Output: j.f1
  5326. (9 rows)
  5327. select * from int4_tbl i left join
  5328. lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
  5329. f1 | f1
  5330. -------------+----
  5331. 0 | 0
  5332. 123456 |
  5333. -123456 |
  5334. 2147483647 |
  5335. -2147483647 |
  5336. (5 rows)
  5337. explain (verbose, costs off)
  5338. select * from int4_tbl i left join
  5339. lateral (select coalesce(i) from int2_tbl j where i.f1 = j.f1) k on true;
  5340. QUERY PLAN
  5341. -------------------------------------
  5342. Nested Loop Left Join
  5343. Output: i.f1, (COALESCE(i.*))
  5344. -> Seq Scan on public.int4_tbl i
  5345. Output: i.f1, i.*
  5346. -> Seq Scan on public.int2_tbl j
  5347. Output: j.f1, COALESCE(i.*)
  5348. Filter: (i.f1 = j.f1)
  5349. (7 rows)
  5350. select * from int4_tbl i left join
  5351. lateral (select coalesce(i) from int2_tbl j where i.f1 = j.f1) k on true;
  5352. f1 | coalesce
  5353. -------------+----------
  5354. 0 | (0)
  5355. 123456 |
  5356. -123456 |
  5357. 2147483647 |
  5358. -2147483647 |
  5359. (5 rows)
  5360. explain (verbose, costs off)
  5361. select * from int4_tbl a,
  5362. lateral (
  5363. select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
  5364. ) ss;
  5365. QUERY PLAN
  5366. -------------------------------------------------
  5367. Nested Loop
  5368. Output: a.f1, b.f1, c.q1, c.q2
  5369. -> Seq Scan on public.int4_tbl a
  5370. Output: a.f1
  5371. -> Hash Left Join
  5372. Output: b.f1, c.q1, c.q2
  5373. Hash Cond: (b.f1 = c.q1)
  5374. -> Seq Scan on public.int4_tbl b
  5375. Output: b.f1
  5376. -> Hash
  5377. Output: c.q1, c.q2
  5378. -> Seq Scan on public.int8_tbl c
  5379. Output: c.q1, c.q2
  5380. Filter: (a.f1 = c.q2)
  5381. (14 rows)
  5382. select * from int4_tbl a,
  5383. lateral (
  5384. select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
  5385. ) ss;
  5386. f1 | f1 | q1 | q2
  5387. -------------+-------------+----+----
  5388. 0 | 0 | |
  5389. 0 | 123456 | |
  5390. 0 | -123456 | |
  5391. 0 | 2147483647 | |
  5392. 0 | -2147483647 | |
  5393. 123456 | 0 | |
  5394. 123456 | 123456 | |
  5395. 123456 | -123456 | |
  5396. 123456 | 2147483647 | |
  5397. 123456 | -2147483647 | |
  5398. -123456 | 0 | |
  5399. -123456 | 123456 | |
  5400. -123456 | -123456 | |
  5401. -123456 | 2147483647 | |
  5402. -123456 | -2147483647 | |
  5403. 2147483647 | 0 | |
  5404. 2147483647 | 123456 | |
  5405. 2147483647 | -123456 | |
  5406. 2147483647 | 2147483647 | |
  5407. 2147483647 | -2147483647 | |
  5408. -2147483647 | 0 | |
  5409. -2147483647 | 123456 | |
  5410. -2147483647 | -123456 | |
  5411. -2147483647 | 2147483647 | |
  5412. -2147483647 | -2147483647 | |
  5413. (25 rows)
  5414. -- lateral reference in a PlaceHolderVar evaluated at join level
  5415. explain (verbose, costs off)
  5416. select * from
  5417. int8_tbl a left join lateral
  5418. (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
  5419. int8_tbl b cross join int8_tbl c) ss
  5420. on a.q2 = ss.bq1;
  5421. QUERY PLAN
  5422. -------------------------------------------------------------
  5423. Nested Loop Left Join
  5424. Output: a.q1, a.q2, b.q1, c.q1, (LEAST(a.q1, b.q1, c.q1))
  5425. -> Seq Scan on public.int8_tbl a
  5426. Output: a.q1, a.q2
  5427. -> Nested Loop
  5428. Output: b.q1, c.q1, LEAST(a.q1, b.q1, c.q1)
  5429. -> Seq Scan on public.int8_tbl b
  5430. Output: b.q1, b.q2
  5431. Filter: (a.q2 = b.q1)
  5432. -> Seq Scan on public.int8_tbl c
  5433. Output: c.q1, c.q2
  5434. (11 rows)
  5435. select * from
  5436. int8_tbl a left join lateral
  5437. (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
  5438. int8_tbl b cross join int8_tbl c) ss
  5439. on a.q2 = ss.bq1;
  5440. q1 | q2 | bq1 | cq1 | least
  5441. ------------------+-------------------+------------------+------------------+------------------
  5442. 123 | 456 | | |
  5443. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5444. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5445. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5446. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5447. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5448. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5449. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5450. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5451. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5452. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5453. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5454. 123 | 4567890123456789 | 4567890123456789 | 123 | 123
  5455. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5456. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5457. 123 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 123
  5458. 4567890123456789 | 123 | 123 | 123 | 123
  5459. 4567890123456789 | 123 | 123 | 123 | 123
  5460. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5461. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5462. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5463. 4567890123456789 | 123 | 123 | 123 | 123
  5464. 4567890123456789 | 123 | 123 | 123 | 123
  5465. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5466. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5467. 4567890123456789 | 123 | 123 | 4567890123456789 | 123
  5468. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 123
  5469. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 123
  5470. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5471. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5472. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5473. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 123
  5474. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 123
  5475. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5476. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5477. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5478. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 123
  5479. 4567890123456789 | 4567890123456789 | 4567890123456789 | 123 | 123
  5480. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5481. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5482. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5483. 4567890123456789 | -4567890123456789 | | |
  5484. (42 rows)
  5485. -- case requiring nested PlaceHolderVars
  5486. explain (verbose, costs off)
  5487. select * from
  5488. int8_tbl c left join (
  5489. int8_tbl a left join (select q1, coalesce(q2,42) as x from int8_tbl b) ss1
  5490. on a.q2 = ss1.q1
  5491. cross join
  5492. lateral (select q1, coalesce(ss1.x,q2) as y from int8_tbl d) ss2
  5493. ) on c.q2 = ss2.q1,
  5494. lateral (select ss2.y offset 0) ss3;
  5495. QUERY PLAN
  5496. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  5497. Nested Loop
  5498. Output: c.q1, c.q2, a.q1, a.q2, b.q1, (COALESCE(b.q2, '42'::bigint)), d.q1, (COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2)), ((COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2)))
  5499. -> Hash Right Join
  5500. Output: c.q1, c.q2, a.q1, a.q2, b.q1, d.q1, (COALESCE(b.q2, '42'::bigint)), (COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2))
  5501. Hash Cond: (d.q1 = c.q2)
  5502. -> Nested Loop
  5503. Output: a.q1, a.q2, b.q1, d.q1, (COALESCE(b.q2, '42'::bigint)), (COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2))
  5504. -> Hash Left Join
  5505. Output: a.q1, a.q2, b.q1, (COALESCE(b.q2, '42'::bigint))
  5506. Hash Cond: (a.q2 = b.q1)
  5507. -> Seq Scan on public.int8_tbl a
  5508. Output: a.q1, a.q2
  5509. -> Hash
  5510. Output: b.q1, (COALESCE(b.q2, '42'::bigint))
  5511. -> Seq Scan on public.int8_tbl b
  5512. Output: b.q1, COALESCE(b.q2, '42'::bigint)
  5513. -> Seq Scan on public.int8_tbl d
  5514. Output: d.q1, COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2)
  5515. -> Hash
  5516. Output: c.q1, c.q2
  5517. -> Seq Scan on public.int8_tbl c
  5518. Output: c.q1, c.q2
  5519. -> Result
  5520. Output: (COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2))
  5521. (24 rows)
  5522. -- case that breaks the old ph_may_need optimization
  5523. explain (verbose, costs off)
  5524. select c.*,a.*,ss1.q1,ss2.q1,ss3.* from
  5525. int8_tbl c left join (
  5526. int8_tbl a left join
  5527. (select q1, coalesce(q2,f1) as x from int8_tbl b, int4_tbl b2
  5528. where q1 < f1) ss1
  5529. on a.q2 = ss1.q1
  5530. cross join
  5531. lateral (select q1, coalesce(ss1.x,q2) as y from int8_tbl d) ss2
  5532. ) on c.q2 = ss2.q1,
  5533. lateral (select * from int4_tbl i where ss2.y > f1) ss3;
  5534. QUERY PLAN
  5535. ---------------------------------------------------------------------------------------------------------
  5536. Nested Loop
  5537. Output: c.q1, c.q2, a.q1, a.q2, b.q1, d.q1, i.f1
  5538. Join Filter: ((COALESCE((COALESCE(b.q2, (b2.f1)::bigint)), d.q2)) > i.f1)
  5539. -> Hash Right Join
  5540. Output: c.q1, c.q2, a.q1, a.q2, b.q1, d.q1, (COALESCE((COALESCE(b.q2, (b2.f1)::bigint)), d.q2))
  5541. Hash Cond: (d.q1 = c.q2)
  5542. -> Nested Loop
  5543. Output: a.q1, a.q2, b.q1, d.q1, (COALESCE((COALESCE(b.q2, (b2.f1)::bigint)), d.q2))
  5544. -> Hash Right Join
  5545. Output: a.q1, a.q2, b.q1, (COALESCE(b.q2, (b2.f1)::bigint))
  5546. Hash Cond: (b.q1 = a.q2)
  5547. -> Nested Loop
  5548. Output: b.q1, COALESCE(b.q2, (b2.f1)::bigint)
  5549. Join Filter: (b.q1 < b2.f1)
  5550. -> Seq Scan on public.int8_tbl b
  5551. Output: b.q1, b.q2
  5552. -> Materialize
  5553. Output: b2.f1
  5554. -> Seq Scan on public.int4_tbl b2
  5555. Output: b2.f1
  5556. -> Hash
  5557. Output: a.q1, a.q2
  5558. -> Seq Scan on public.int8_tbl a
  5559. Output: a.q1, a.q2
  5560. -> Seq Scan on public.int8_tbl d
  5561. Output: d.q1, COALESCE((COALESCE(b.q2, (b2.f1)::bigint)), d.q2)
  5562. -> Hash
  5563. Output: c.q1, c.q2
  5564. -> Seq Scan on public.int8_tbl c
  5565. Output: c.q1, c.q2
  5566. -> Materialize
  5567. Output: i.f1
  5568. -> Seq Scan on public.int4_tbl i
  5569. Output: i.f1
  5570. (34 rows)
  5571. -- check processing of postponed quals (bug #9041)
  5572. explain (verbose, costs off)
  5573. select * from
  5574. (select 1 as x offset 0) x cross join (select 2 as y offset 0) y
  5575. left join lateral (
  5576. select * from (select 3 as z offset 0) z where z.z = x.x
  5577. ) zz on zz.z = y.y;
  5578. QUERY PLAN
  5579. ----------------------------------------------
  5580. Nested Loop Left Join
  5581. Output: (1), (2), (3)
  5582. Join Filter: (((3) = (1)) AND ((3) = (2)))
  5583. -> Nested Loop
  5584. Output: (1), (2)
  5585. -> Result
  5586. Output: 1
  5587. -> Result
  5588. Output: 2
  5589. -> Result
  5590. Output: 3
  5591. (11 rows)
  5592. -- check dummy rels with lateral references (bug #15694)
  5593. explain (verbose, costs off)
  5594. select * from int8_tbl i8 left join lateral
  5595. (select *, i8.q2 from int4_tbl where false) ss on true;
  5596. QUERY PLAN
  5597. --------------------------------------
  5598. Nested Loop Left Join
  5599. Output: i8.q1, i8.q2, f1, (i8.q2)
  5600. -> Seq Scan on public.int8_tbl i8
  5601. Output: i8.q1, i8.q2
  5602. -> Result
  5603. Output: f1, i8.q2
  5604. One-Time Filter: false
  5605. (7 rows)
  5606. explain (verbose, costs off)
  5607. select * from int8_tbl i8 left join lateral
  5608. (select *, i8.q2 from int4_tbl i1, int4_tbl i2 where false) ss on true;
  5609. QUERY PLAN
  5610. -----------------------------------------
  5611. Nested Loop Left Join
  5612. Output: i8.q1, i8.q2, f1, f1, (i8.q2)
  5613. -> Seq Scan on public.int8_tbl i8
  5614. Output: i8.q1, i8.q2
  5615. -> Result
  5616. Output: f1, f1, i8.q2
  5617. One-Time Filter: false
  5618. (7 rows)
  5619. -- check handling of nested appendrels inside LATERAL
  5620. select * from
  5621. ((select 2 as v) union all (select 3 as v)) as q1
  5622. cross join lateral
  5623. ((select * from
  5624. ((select 4 as v) union all (select 5 as v)) as q3)
  5625. union all
  5626. (select q1.v)
  5627. ) as q2;
  5628. v | v
  5629. ---+---
  5630. 2 | 4
  5631. 2 | 5
  5632. 2 | 2
  5633. 3 | 4
  5634. 3 | 5
  5635. 3 | 3
  5636. (6 rows)
  5637. -- check the number of columns specified
  5638. SELECT * FROM (int8_tbl i cross join int4_tbl j) ss(a,b,c,d);
  5639. ERROR: join expression "ss" has 3 columns available but 4 columns specified
  5640. -- check we don't try to do a unique-ified semijoin with LATERAL
  5641. explain (verbose, costs off)
  5642. select * from
  5643. (values (0,9998), (1,1000)) v(id,x),
  5644. lateral (select f1 from int4_tbl
  5645. where f1 = any (select unique1 from tenk1
  5646. where unique2 = v.x offset 0)) ss;
  5647. QUERY PLAN
  5648. ----------------------------------------------------------------------
  5649. Nested Loop
  5650. Output: "*VALUES*".column1, "*VALUES*".column2, int4_tbl.f1
  5651. -> Values Scan on "*VALUES*"
  5652. Output: "*VALUES*".column1, "*VALUES*".column2
  5653. -> Nested Loop Semi Join
  5654. Output: int4_tbl.f1
  5655. Join Filter: (int4_tbl.f1 = tenk1.unique1)
  5656. -> Seq Scan on public.int4_tbl
  5657. Output: int4_tbl.f1
  5658. -> Materialize
  5659. Output: tenk1.unique1
  5660. -> Index Scan using tenk1_unique2 on public.tenk1
  5661. Output: tenk1.unique1
  5662. Index Cond: (tenk1.unique2 = "*VALUES*".column2)
  5663. (14 rows)
  5664. select * from
  5665. (values (0,9998), (1,1000)) v(id,x),
  5666. lateral (select f1 from int4_tbl
  5667. where f1 = any (select unique1 from tenk1
  5668. where unique2 = v.x offset 0)) ss;
  5669. id | x | f1
  5670. ----+------+----
  5671. 0 | 9998 | 0
  5672. (1 row)
  5673. -- check proper extParam/allParam handling (this isn't exactly a LATERAL issue,
  5674. -- but we can make the test case much more compact with LATERAL)
  5675. explain (verbose, costs off)
  5676. select * from (values (0), (1)) v(id),
  5677. lateral (select * from int8_tbl t1,
  5678. lateral (select * from
  5679. (select * from int8_tbl t2
  5680. where q1 = any (select q2 from int8_tbl t3
  5681. where q2 = (select greatest(t1.q1,t2.q2))
  5682. and (select v.id=0)) offset 0) ss2) ss
  5683. where t1.q1 = ss.q2) ss0;
  5684. QUERY PLAN
  5685. -----------------------------------------------------------------
  5686. Nested Loop
  5687. Output: "*VALUES*".column1, t1.q1, t1.q2, ss2.q1, ss2.q2
  5688. -> Seq Scan on public.int8_tbl t1
  5689. Output: t1.q1, t1.q2
  5690. -> Nested Loop
  5691. Output: "*VALUES*".column1, ss2.q1, ss2.q2
  5692. -> Values Scan on "*VALUES*"
  5693. Output: "*VALUES*".column1
  5694. -> Subquery Scan on ss2
  5695. Output: ss2.q1, ss2.q2
  5696. Filter: (t1.q1 = ss2.q2)
  5697. -> Seq Scan on public.int8_tbl t2
  5698. Output: t2.q1, t2.q2
  5699. Filter: (SubPlan 3)
  5700. SubPlan 3
  5701. -> Result
  5702. Output: t3.q2
  5703. One-Time Filter: $4
  5704. InitPlan 1 (returns $2)
  5705. -> Result
  5706. Output: GREATEST($0, t2.q2)
  5707. InitPlan 2 (returns $4)
  5708. -> Result
  5709. Output: ($3 = 0)
  5710. -> Seq Scan on public.int8_tbl t3
  5711. Output: t3.q1, t3.q2
  5712. Filter: (t3.q2 = $2)
  5713. (27 rows)
  5714. select * from (values (0), (1)) v(id),
  5715. lateral (select * from int8_tbl t1,
  5716. lateral (select * from
  5717. (select * from int8_tbl t2
  5718. where q1 = any (select q2 from int8_tbl t3
  5719. where q2 = (select greatest(t1.q1,t2.q2))
  5720. and (select v.id=0)) offset 0) ss2) ss
  5721. where t1.q1 = ss.q2) ss0;
  5722. id | q1 | q2 | q1 | q2
  5723. ----+------------------+-------------------+------------------+------------------
  5724. 0 | 4567890123456789 | 123 | 4567890123456789 | 4567890123456789
  5725. 0 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789
  5726. 0 | 4567890123456789 | -4567890123456789 | 4567890123456789 | 4567890123456789
  5727. (3 rows)
  5728. -- test some error cases where LATERAL should have been used but wasn't
  5729. select f1,g from int4_tbl a, (select f1 as g) ss;
  5730. ERROR: column "f1" does not exist
  5731. LINE 1: select f1,g from int4_tbl a, (select f1 as g) ss;
  5732. ^
  5733. HINT: There is a column named "f1" in table "a", but it cannot be referenced from this part of the query.
  5734. select f1,g from int4_tbl a, (select a.f1 as g) ss;
  5735. ERROR: invalid reference to FROM-clause entry for table "a"
  5736. LINE 1: select f1,g from int4_tbl a, (select a.f1 as g) ss;
  5737. ^
  5738. HINT: There is an entry for table "a", but it cannot be referenced from this part of the query.
  5739. select f1,g from int4_tbl a cross join (select f1 as g) ss;
  5740. ERROR: column "f1" does not exist
  5741. LINE 1: select f1,g from int4_tbl a cross join (select f1 as g) ss;
  5742. ^
  5743. HINT: There is a column named "f1" in table "a", but it cannot be referenced from this part of the query.
  5744. select f1,g from int4_tbl a cross join (select a.f1 as g) ss;
  5745. ERROR: invalid reference to FROM-clause entry for table "a"
  5746. LINE 1: select f1,g from int4_tbl a cross join (select a.f1 as g) ss...
  5747. ^
  5748. HINT: There is an entry for table "a", but it cannot be referenced from this part of the query.
  5749. -- SQL:2008 says the left table is in scope but illegal to access here
  5750. select f1,g from int4_tbl a right join lateral generate_series(0, a.f1) g on true;
  5751. ERROR: invalid reference to FROM-clause entry for table "a"
  5752. LINE 1: ... int4_tbl a right join lateral generate_series(0, a.f1) g on...
  5753. ^
  5754. DETAIL: The combining JOIN type must be INNER or LEFT for a LATERAL reference.
  5755. select f1,g from int4_tbl a full join lateral generate_series(0, a.f1) g on true;
  5756. ERROR: invalid reference to FROM-clause entry for table "a"
  5757. LINE 1: ...m int4_tbl a full join lateral generate_series(0, a.f1) g on...
  5758. ^
  5759. DETAIL: The combining JOIN type must be INNER or LEFT for a LATERAL reference.
  5760. -- check we complain about ambiguous table references
  5761. select * from
  5762. int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
  5763. ERROR: table reference "x" is ambiguous
  5764. LINE 2: ...cross join (int4_tbl x cross join lateral (select x.f1) ss);
  5765. ^
  5766. -- LATERAL can be used to put an aggregate into the FROM clause of its query
  5767. select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;
  5768. ERROR: aggregate functions are not allowed in FROM clause of their own query level
  5769. LINE 1: select 1 from tenk1 a, lateral (select max(a.unique1) from i...
  5770. ^
  5771. -- check behavior of LATERAL in UPDATE/DELETE
  5772. create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl;
  5773. -- error, can't do this:
  5774. update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
  5775. ERROR: column "x1" does not exist
  5776. LINE 1: ... set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
  5777. ^
  5778. HINT: There is a column named "x1" in table "xx1", but it cannot be referenced from this part of the query.
  5779. update xx1 set x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss;
  5780. ERROR: invalid reference to FROM-clause entry for table "xx1"
  5781. LINE 1: ...t x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss...
  5782. ^
  5783. HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
  5784. -- can't do it even with LATERAL:
  5785. update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss;
  5786. ERROR: invalid reference to FROM-clause entry for table "xx1"
  5787. LINE 1: ...= f1 from lateral (select * from int4_tbl where f1 = x1) ss;
  5788. ^
  5789. HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
  5790. -- we might in future allow something like this, but for now it's an error:
  5791. update xx1 set x2 = f1 from xx1, lateral (select * from int4_tbl where f1 = x1) ss;
  5792. ERROR: table name "xx1" specified more than once
  5793. -- also errors:
  5794. delete from xx1 using (select * from int4_tbl where f1 = x1) ss;
  5795. ERROR: column "x1" does not exist
  5796. LINE 1: ...te from xx1 using (select * from int4_tbl where f1 = x1) ss;
  5797. ^
  5798. HINT: There is a column named "x1" in table "xx1", but it cannot be referenced from this part of the query.
  5799. delete from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss;
  5800. ERROR: invalid reference to FROM-clause entry for table "xx1"
  5801. LINE 1: ...from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss...
  5802. ^
  5803. HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
  5804. delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
  5805. ERROR: invalid reference to FROM-clause entry for table "xx1"
  5806. LINE 1: ...xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
  5807. ^
  5808. HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
  5809. --
  5810. -- test LATERAL reference propagation down a multi-level inheritance hierarchy
  5811. -- produced for a multi-level partitioned table hierarchy.
  5812. --
  5813. create table join_pt1 (a int, b int, c varchar) partition by range(a);
  5814. create table join_pt1p1 partition of join_pt1 for values from (0) to (100) partition by range(b);
  5815. create table join_pt1p2 partition of join_pt1 for values from (100) to (200);
  5816. create table join_pt1p1p1 partition of join_pt1p1 for values from (0) to (100);
  5817. insert into join_pt1 values (1, 1, 'x'), (101, 101, 'y');
  5818. create table join_ut1 (a int, b int, c varchar);
  5819. insert into join_ut1 values (101, 101, 'y'), (2, 2, 'z');
  5820. explain (verbose, costs off)
  5821. select t1.b, ss.phv from join_ut1 t1 left join lateral
  5822. (select t2.a as t2a, t3.a t3a, least(t1.a, t2.a, t3.a) phv
  5823. from join_pt1 t2 join join_ut1 t3 on t2.a = t3.b) ss
  5824. on t1.a = ss.t2a order by t1.a;
  5825. QUERY PLAN
  5826. --------------------------------------------------------------------
  5827. Sort
  5828. Output: t1.b, (LEAST(t1.a, t2.a, t3.a)), t1.a
  5829. Sort Key: t1.a
  5830. -> Nested Loop Left Join
  5831. Output: t1.b, (LEAST(t1.a, t2.a, t3.a)), t1.a
  5832. -> Seq Scan on public.join_ut1 t1
  5833. Output: t1.a, t1.b, t1.c
  5834. -> Hash Join
  5835. Output: t2.a, LEAST(t1.a, t2.a, t3.a)
  5836. Hash Cond: (t3.b = t2.a)
  5837. -> Seq Scan on public.join_ut1 t3
  5838. Output: t3.a, t3.b, t3.c
  5839. -> Hash
  5840. Output: t2.a
  5841. -> Append
  5842. -> Seq Scan on public.join_pt1p1p1 t2_1
  5843. Output: t2_1.a
  5844. Filter: (t1.a = t2_1.a)
  5845. -> Seq Scan on public.join_pt1p2 t2_2
  5846. Output: t2_2.a
  5847. Filter: (t1.a = t2_2.a)
  5848. (21 rows)
  5849. select t1.b, ss.phv from join_ut1 t1 left join lateral
  5850. (select t2.a as t2a, t3.a t3a, least(t1.a, t2.a, t3.a) phv
  5851. from join_pt1 t2 join join_ut1 t3 on t2.a = t3.b) ss
  5852. on t1.a = ss.t2a order by t1.a;
  5853. b | phv
  5854. -----+-----
  5855. 2 |
  5856. 101 | 101
  5857. (2 rows)
  5858. drop table join_pt1;
  5859. drop table join_ut1;
  5860. --
  5861. -- test estimation behavior with multi-column foreign key and constant qual
  5862. --
  5863. begin;
  5864. create table fkest (x integer, x10 integer, x10b integer, x100 integer);
  5865. insert into fkest select x, x/10, x/10, x/100 from generate_series(1,1000) x;
  5866. create unique index on fkest(x, x10, x100);
  5867. analyze fkest;
  5868. explain (costs off)
  5869. select * from fkest f1
  5870. join fkest f2 on (f1.x = f2.x and f1.x10 = f2.x10b and f1.x100 = f2.x100)
  5871. join fkest f3 on f1.x = f3.x
  5872. where f1.x100 = 2;
  5873. QUERY PLAN
  5874. -----------------------------------------------------------
  5875. Nested Loop
  5876. -> Hash Join
  5877. Hash Cond: ((f2.x = f1.x) AND (f2.x10b = f1.x10))
  5878. -> Seq Scan on fkest f2
  5879. Filter: (x100 = 2)
  5880. -> Hash
  5881. -> Seq Scan on fkest f1
  5882. Filter: (x100 = 2)
  5883. -> Index Scan using fkest_x_x10_x100_idx on fkest f3
  5884. Index Cond: (x = f1.x)
  5885. (10 rows)
  5886. alter table fkest add constraint fk
  5887. foreign key (x, x10b, x100) references fkest (x, x10, x100);
  5888. explain (costs off)
  5889. select * from fkest f1
  5890. join fkest f2 on (f1.x = f2.x and f1.x10 = f2.x10b and f1.x100 = f2.x100)
  5891. join fkest f3 on f1.x = f3.x
  5892. where f1.x100 = 2;
  5893. QUERY PLAN
  5894. -----------------------------------------------------
  5895. Hash Join
  5896. Hash Cond: ((f2.x = f1.x) AND (f2.x10b = f1.x10))
  5897. -> Hash Join
  5898. Hash Cond: (f3.x = f2.x)
  5899. -> Seq Scan on fkest f3
  5900. -> Hash
  5901. -> Seq Scan on fkest f2
  5902. Filter: (x100 = 2)
  5903. -> Hash
  5904. -> Seq Scan on fkest f1
  5905. Filter: (x100 = 2)
  5906. (11 rows)
  5907. rollback;
  5908. --
  5909. -- test that foreign key join estimation performs sanely for outer joins
  5910. --
  5911. begin;
  5912. create table fkest (a int, b int, c int unique, primary key(a,b));
  5913. create table fkest1 (a int, b int, primary key(a,b));
  5914. insert into fkest select x/10, x%10, x from generate_series(1,1000) x;
  5915. insert into fkest1 select x/10, x%10 from generate_series(1,1000) x;
  5916. alter table fkest1
  5917. add constraint fkest1_a_b_fkey foreign key (a,b) references fkest;
  5918. analyze fkest;
  5919. analyze fkest1;
  5920. explain (costs off)
  5921. select *
  5922. from fkest f
  5923. left join fkest1 f1 on f.a = f1.a and f.b = f1.b
  5924. left join fkest1 f2 on f.a = f2.a and f.b = f2.b
  5925. left join fkest1 f3 on f.a = f3.a and f.b = f3.b
  5926. where f.c = 1;
  5927. QUERY PLAN
  5928. ------------------------------------------------------------------
  5929. Nested Loop Left Join
  5930. -> Nested Loop Left Join
  5931. -> Nested Loop Left Join
  5932. -> Index Scan using fkest_c_key on fkest f
  5933. Index Cond: (c = 1)
  5934. -> Index Only Scan using fkest1_pkey on fkest1 f1
  5935. Index Cond: ((a = f.a) AND (b = f.b))
  5936. -> Index Only Scan using fkest1_pkey on fkest1 f2
  5937. Index Cond: ((a = f.a) AND (b = f.b))
  5938. -> Index Only Scan using fkest1_pkey on fkest1 f3
  5939. Index Cond: ((a = f.a) AND (b = f.b))
  5940. (11 rows)
  5941. rollback;
  5942. --
  5943. -- test planner's ability to mark joins as unique
  5944. --
  5945. create table j1 (id int primary key);
  5946. create table j2 (id int primary key);
  5947. create table j3 (id int);
  5948. insert into j1 values(1),(2),(3);
  5949. insert into j2 values(1),(2),(3);
  5950. insert into j3 values(1),(1);
  5951. analyze j1;
  5952. analyze j2;
  5953. analyze j3;
  5954. -- ensure join is properly marked as unique
  5955. explain (verbose, costs off)
  5956. select * from j1 inner join j2 on j1.id = j2.id;
  5957. QUERY PLAN
  5958. -----------------------------------
  5959. Hash Join
  5960. Output: j1.id, j2.id
  5961. Inner Unique: true
  5962. Hash Cond: (j1.id = j2.id)
  5963. -> Seq Scan on public.j1
  5964. Output: j1.id
  5965. -> Hash
  5966. Output: j2.id
  5967. -> Seq Scan on public.j2
  5968. Output: j2.id
  5969. (10 rows)
  5970. -- ensure join is not unique when not an equi-join
  5971. explain (verbose, costs off)
  5972. select * from j1 inner join j2 on j1.id > j2.id;
  5973. QUERY PLAN
  5974. -----------------------------------
  5975. Nested Loop
  5976. Output: j1.id, j2.id
  5977. Join Filter: (j1.id > j2.id)
  5978. -> Seq Scan on public.j1
  5979. Output: j1.id
  5980. -> Materialize
  5981. Output: j2.id
  5982. -> Seq Scan on public.j2
  5983. Output: j2.id
  5984. (9 rows)
  5985. -- ensure non-unique rel is not chosen as inner
  5986. explain (verbose, costs off)
  5987. select * from j1 inner join j3 on j1.id = j3.id;
  5988. QUERY PLAN
  5989. -----------------------------------
  5990. Hash Join
  5991. Output: j1.id, j3.id
  5992. Inner Unique: true
  5993. Hash Cond: (j3.id = j1.id)
  5994. -> Seq Scan on public.j3
  5995. Output: j3.id
  5996. -> Hash
  5997. Output: j1.id
  5998. -> Seq Scan on public.j1
  5999. Output: j1.id
  6000. (10 rows)
  6001. -- ensure left join is marked as unique
  6002. explain (verbose, costs off)
  6003. select * from j1 left join j2 on j1.id = j2.id;
  6004. QUERY PLAN
  6005. -----------------------------------
  6006. Hash Left Join
  6007. Output: j1.id, j2.id
  6008. Inner Unique: true
  6009. Hash Cond: (j1.id = j2.id)
  6010. -> Seq Scan on public.j1
  6011. Output: j1.id
  6012. -> Hash
  6013. Output: j2.id
  6014. -> Seq Scan on public.j2
  6015. Output: j2.id
  6016. (10 rows)
  6017. -- ensure right join is marked as unique
  6018. explain (verbose, costs off)
  6019. select * from j1 right join j2 on j1.id = j2.id;
  6020. QUERY PLAN
  6021. -----------------------------------
  6022. Hash Left Join
  6023. Output: j1.id, j2.id
  6024. Inner Unique: true
  6025. Hash Cond: (j2.id = j1.id)
  6026. -> Seq Scan on public.j2
  6027. Output: j2.id
  6028. -> Hash
  6029. Output: j1.id
  6030. -> Seq Scan on public.j1
  6031. Output: j1.id
  6032. (10 rows)
  6033. -- ensure full join is marked as unique
  6034. explain (verbose, costs off)
  6035. select * from j1 full join j2 on j1.id = j2.id;
  6036. QUERY PLAN
  6037. -----------------------------------
  6038. Hash Full Join
  6039. Output: j1.id, j2.id
  6040. Inner Unique: true
  6041. Hash Cond: (j1.id = j2.id)
  6042. -> Seq Scan on public.j1
  6043. Output: j1.id
  6044. -> Hash
  6045. Output: j2.id
  6046. -> Seq Scan on public.j2
  6047. Output: j2.id
  6048. (10 rows)
  6049. -- a clauseless (cross) join can't be unique
  6050. explain (verbose, costs off)
  6051. select * from j1 cross join j2;
  6052. QUERY PLAN
  6053. -----------------------------------
  6054. Nested Loop
  6055. Output: j1.id, j2.id
  6056. -> Seq Scan on public.j1
  6057. Output: j1.id
  6058. -> Materialize
  6059. Output: j2.id
  6060. -> Seq Scan on public.j2
  6061. Output: j2.id
  6062. (8 rows)
  6063. -- ensure a natural join is marked as unique
  6064. explain (verbose, costs off)
  6065. select * from j1 natural join j2;
  6066. QUERY PLAN
  6067. -----------------------------------
  6068. Hash Join
  6069. Output: j1.id
  6070. Inner Unique: true
  6071. Hash Cond: (j1.id = j2.id)
  6072. -> Seq Scan on public.j1
  6073. Output: j1.id
  6074. -> Hash
  6075. Output: j2.id
  6076. -> Seq Scan on public.j2
  6077. Output: j2.id
  6078. (10 rows)
  6079. -- ensure a distinct clause allows the inner to become unique
  6080. explain (verbose, costs off)
  6081. select * from j1
  6082. inner join (select distinct id from j3) j3 on j1.id = j3.id;
  6083. QUERY PLAN
  6084. -----------------------------------------
  6085. Nested Loop
  6086. Output: j1.id, j3.id
  6087. Inner Unique: true
  6088. Join Filter: (j1.id = j3.id)
  6089. -> Unique
  6090. Output: j3.id
  6091. -> Sort
  6092. Output: j3.id
  6093. Sort Key: j3.id
  6094. -> Seq Scan on public.j3
  6095. Output: j3.id
  6096. -> Seq Scan on public.j1
  6097. Output: j1.id
  6098. (13 rows)
  6099. -- ensure group by clause allows the inner to become unique
  6100. explain (verbose, costs off)
  6101. select * from j1
  6102. inner join (select id from j3 group by id) j3 on j1.id = j3.id;
  6103. QUERY PLAN
  6104. -----------------------------------------
  6105. Nested Loop
  6106. Output: j1.id, j3.id
  6107. Inner Unique: true
  6108. Join Filter: (j1.id = j3.id)
  6109. -> Group
  6110. Output: j3.id
  6111. Group Key: j3.id
  6112. -> Sort
  6113. Output: j3.id
  6114. Sort Key: j3.id
  6115. -> Seq Scan on public.j3
  6116. Output: j3.id
  6117. -> Seq Scan on public.j1
  6118. Output: j1.id
  6119. (14 rows)
  6120. drop table j1;
  6121. drop table j2;
  6122. drop table j3;
  6123. -- test more complex permutations of unique joins
  6124. create table j1 (id1 int, id2 int, primary key(id1,id2));
  6125. create table j2 (id1 int, id2 int, primary key(id1,id2));
  6126. create table j3 (id1 int, id2 int, primary key(id1,id2));
  6127. insert into j1 values(1,1),(1,2);
  6128. insert into j2 values(1,1);
  6129. insert into j3 values(1,1);
  6130. analyze j1;
  6131. analyze j2;
  6132. analyze j3;
  6133. -- ensure there's no unique join when not all columns which are part of the
  6134. -- unique index are seen in the join clause
  6135. explain (verbose, costs off)
  6136. select * from j1
  6137. inner join j2 on j1.id1 = j2.id1;
  6138. QUERY PLAN
  6139. ------------------------------------------
  6140. Nested Loop
  6141. Output: j1.id1, j1.id2, j2.id1, j2.id2
  6142. Join Filter: (j1.id1 = j2.id1)
  6143. -> Seq Scan on public.j2
  6144. Output: j2.id1, j2.id2
  6145. -> Seq Scan on public.j1
  6146. Output: j1.id1, j1.id2
  6147. (7 rows)
  6148. -- ensure proper unique detection with multiple join quals
  6149. explain (verbose, costs off)
  6150. select * from j1
  6151. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2;
  6152. QUERY PLAN
  6153. ----------------------------------------------------------
  6154. Nested Loop
  6155. Output: j1.id1, j1.id2, j2.id1, j2.id2
  6156. Inner Unique: true
  6157. Join Filter: ((j1.id1 = j2.id1) AND (j1.id2 = j2.id2))
  6158. -> Seq Scan on public.j2
  6159. Output: j2.id1, j2.id2
  6160. -> Seq Scan on public.j1
  6161. Output: j1.id1, j1.id2
  6162. (8 rows)
  6163. -- ensure we don't detect the join to be unique when quals are not part of the
  6164. -- join condition
  6165. explain (verbose, costs off)
  6166. select * from j1
  6167. inner join j2 on j1.id1 = j2.id1 where j1.id2 = 1;
  6168. QUERY PLAN
  6169. ------------------------------------------
  6170. Nested Loop
  6171. Output: j1.id1, j1.id2, j2.id1, j2.id2
  6172. Join Filter: (j1.id1 = j2.id1)
  6173. -> Seq Scan on public.j1
  6174. Output: j1.id1, j1.id2
  6175. Filter: (j1.id2 = 1)
  6176. -> Seq Scan on public.j2
  6177. Output: j2.id1, j2.id2
  6178. (8 rows)
  6179. -- as above, but for left joins.
  6180. explain (verbose, costs off)
  6181. select * from j1
  6182. left join j2 on j1.id1 = j2.id1 where j1.id2 = 1;
  6183. QUERY PLAN
  6184. ------------------------------------------
  6185. Nested Loop Left Join
  6186. Output: j1.id1, j1.id2, j2.id1, j2.id2
  6187. Join Filter: (j1.id1 = j2.id1)
  6188. -> Seq Scan on public.j1
  6189. Output: j1.id1, j1.id2
  6190. Filter: (j1.id2 = 1)
  6191. -> Seq Scan on public.j2
  6192. Output: j2.id1, j2.id2
  6193. (8 rows)
  6194. -- validate logic in merge joins which skips mark and restore.
  6195. -- it should only do this if all quals which were used to detect the unique
  6196. -- are present as join quals, and not plain quals.
  6197. set enable_nestloop to 0;
  6198. set enable_hashjoin to 0;
  6199. set enable_sort to 0;
  6200. -- create indexes that will be preferred over the PKs to perform the join
  6201. create index j1_id1_idx on j1 (id1) where id1 % 1000 = 1;
  6202. create index j2_id1_idx on j2 (id1) where id1 % 1000 = 1;
  6203. -- need an additional row in j2, if we want j2_id1_idx to be preferred
  6204. insert into j2 values(1,2);
  6205. analyze j2;
  6206. explain (costs off) select * from j1
  6207. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  6208. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1;
  6209. QUERY PLAN
  6210. -----------------------------------------
  6211. Merge Join
  6212. Merge Cond: (j1.id1 = j2.id1)
  6213. Join Filter: (j1.id2 = j2.id2)
  6214. -> Index Scan using j1_id1_idx on j1
  6215. -> Index Scan using j2_id1_idx on j2
  6216. (5 rows)
  6217. select * from j1
  6218. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  6219. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1;
  6220. id1 | id2 | id1 | id2
  6221. -----+-----+-----+-----
  6222. 1 | 1 | 1 | 1
  6223. 1 | 2 | 1 | 2
  6224. (2 rows)
  6225. -- Exercise array keys mark/restore B-Tree code
  6226. explain (costs off) select * from j1
  6227. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  6228. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 = any (array[1]);
  6229. QUERY PLAN
  6230. ----------------------------------------------------
  6231. Merge Join
  6232. Merge Cond: (j1.id1 = j2.id1)
  6233. Join Filter: (j1.id2 = j2.id2)
  6234. -> Index Scan using j1_id1_idx on j1
  6235. -> Index Scan using j2_id1_idx on j2
  6236. Index Cond: (id1 = ANY ('{1}'::integer[]))
  6237. (6 rows)
  6238. select * from j1
  6239. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  6240. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 = any (array[1]);
  6241. id1 | id2 | id1 | id2
  6242. -----+-----+-----+-----
  6243. 1 | 1 | 1 | 1
  6244. 1 | 2 | 1 | 2
  6245. (2 rows)
  6246. -- Exercise array keys "find extreme element" B-Tree code
  6247. explain (costs off) select * from j1
  6248. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  6249. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 >= any (array[1,5]);
  6250. QUERY PLAN
  6251. -------------------------------------------------------
  6252. Merge Join
  6253. Merge Cond: (j1.id1 = j2.id1)
  6254. Join Filter: (j1.id2 = j2.id2)
  6255. -> Index Scan using j1_id1_idx on j1
  6256. -> Index Only Scan using j2_pkey on j2
  6257. Index Cond: (id1 >= ANY ('{1,5}'::integer[]))
  6258. Filter: ((id1 % 1000) = 1)
  6259. (7 rows)
  6260. select * from j1
  6261. inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
  6262. where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 >= any (array[1,5]);
  6263. id1 | id2 | id1 | id2
  6264. -----+-----+-----+-----
  6265. 1 | 1 | 1 | 1
  6266. 1 | 2 | 1 | 2
  6267. (2 rows)
  6268. reset enable_nestloop;
  6269. reset enable_hashjoin;
  6270. reset enable_sort;
  6271. drop table j1;
  6272. drop table j2;
  6273. drop table j3;
  6274. -- check that semijoin inner is not seen as unique for a portion of the outerrel
  6275. explain (verbose, costs off)
  6276. select t1.unique1, t2.hundred
  6277. from onek t1, tenk1 t2
  6278. where exists (select 1 from tenk1 t3
  6279. where t3.thousand = t1.unique1 and t3.tenthous = t2.hundred)
  6280. and t1.unique1 < 1;
  6281. QUERY PLAN
  6282. ---------------------------------------------------------------------------------
  6283. Nested Loop
  6284. Output: t1.unique1, t2.hundred
  6285. -> Hash Join
  6286. Output: t1.unique1, t3.tenthous
  6287. Hash Cond: (t3.thousand = t1.unique1)
  6288. -> HashAggregate
  6289. Output: t3.thousand, t3.tenthous
  6290. Group Key: t3.thousand, t3.tenthous
  6291. -> Index Only Scan using tenk1_thous_tenthous on public.tenk1 t3
  6292. Output: t3.thousand, t3.tenthous
  6293. -> Hash
  6294. Output: t1.unique1
  6295. -> Index Only Scan using onek_unique1 on public.onek t1
  6296. Output: t1.unique1
  6297. Index Cond: (t1.unique1 < 1)
  6298. -> Index Only Scan using tenk1_hundred on public.tenk1 t2
  6299. Output: t2.hundred
  6300. Index Cond: (t2.hundred = t3.tenthous)
  6301. (18 rows)
  6302. -- ... unless it actually is unique
  6303. create table j3 as select unique1, tenthous from onek;
  6304. vacuum analyze j3;
  6305. create unique index on j3(unique1, tenthous);
  6306. explain (verbose, costs off)
  6307. select t1.unique1, t2.hundred
  6308. from onek t1, tenk1 t2
  6309. where exists (select 1 from j3
  6310. where j3.unique1 = t1.unique1 and j3.tenthous = t2.hundred)
  6311. and t1.unique1 < 1;
  6312. QUERY PLAN
  6313. ------------------------------------------------------------------------
  6314. Nested Loop
  6315. Output: t1.unique1, t2.hundred
  6316. -> Nested Loop
  6317. Output: t1.unique1, j3.tenthous
  6318. -> Index Only Scan using onek_unique1 on public.onek t1
  6319. Output: t1.unique1
  6320. Index Cond: (t1.unique1 < 1)
  6321. -> Index Only Scan using j3_unique1_tenthous_idx on public.j3
  6322. Output: j3.unique1, j3.tenthous
  6323. Index Cond: (j3.unique1 = t1.unique1)
  6324. -> Index Only Scan using tenk1_hundred on public.tenk1 t2
  6325. Output: t2.hundred
  6326. Index Cond: (t2.hundred = j3.tenthous)
  6327. (13 rows)
  6328. drop table j3;