select.out 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. --
  2. -- SELECT
  3. --
  4. -- btree index
  5. -- awk '{if($1<10){print;}else{next;}}' onek.data | sort +0n -1
  6. --
  7. SELECT * FROM onek
  8. WHERE onek.unique1 < 10
  9. ORDER BY onek.unique1;
  10. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
  11. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
  12. 0 | 998 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | AAAAAA | KMBAAA | OOOOxx
  13. 1 | 214 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 3 | BAAAAA | GIAAAA | OOOOxx
  14. 2 | 326 | 0 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 4 | 5 | CAAAAA | OMAAAA | OOOOxx
  15. 3 | 431 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 6 | 7 | DAAAAA | PQAAAA | VVVVxx
  16. 4 | 833 | 0 | 0 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 8 | 9 | EAAAAA | BGBAAA | HHHHxx
  17. 5 | 541 | 1 | 1 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 10 | 11 | FAAAAA | VUAAAA | HHHHxx
  18. 6 | 978 | 0 | 2 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 12 | 13 | GAAAAA | QLBAAA | OOOOxx
  19. 7 | 647 | 1 | 3 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 14 | 15 | HAAAAA | XYAAAA | VVVVxx
  20. 8 | 653 | 0 | 0 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 16 | 17 | IAAAAA | DZAAAA | HHHHxx
  21. 9 | 49 | 1 | 1 | 9 | 9 | 9 | 9 | 9 | 9 | 9 | 18 | 19 | JAAAAA | XBAAAA | HHHHxx
  22. (10 rows)
  23. --
  24. -- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
  25. --
  26. SELECT onek.unique1, onek.stringu1 FROM onek
  27. WHERE onek.unique1 < 20
  28. ORDER BY unique1 using >;
  29. unique1 | stringu1
  30. ---------+----------
  31. 19 | TAAAAA
  32. 18 | SAAAAA
  33. 17 | RAAAAA
  34. 16 | QAAAAA
  35. 15 | PAAAAA
  36. 14 | OAAAAA
  37. 13 | NAAAAA
  38. 12 | MAAAAA
  39. 11 | LAAAAA
  40. 10 | KAAAAA
  41. 9 | JAAAAA
  42. 8 | IAAAAA
  43. 7 | HAAAAA
  44. 6 | GAAAAA
  45. 5 | FAAAAA
  46. 4 | EAAAAA
  47. 3 | DAAAAA
  48. 2 | CAAAAA
  49. 1 | BAAAAA
  50. 0 | AAAAAA
  51. (20 rows)
  52. --
  53. -- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
  54. --
  55. SELECT onek.unique1, onek.stringu1 FROM onek
  56. WHERE onek.unique1 > 980
  57. ORDER BY stringu1 using <;
  58. unique1 | stringu1
  59. ---------+----------
  60. 988 | AMAAAA
  61. 989 | BMAAAA
  62. 990 | CMAAAA
  63. 991 | DMAAAA
  64. 992 | EMAAAA
  65. 993 | FMAAAA
  66. 994 | GMAAAA
  67. 995 | HMAAAA
  68. 996 | IMAAAA
  69. 997 | JMAAAA
  70. 998 | KMAAAA
  71. 999 | LMAAAA
  72. 981 | TLAAAA
  73. 982 | ULAAAA
  74. 983 | VLAAAA
  75. 984 | WLAAAA
  76. 985 | XLAAAA
  77. 986 | YLAAAA
  78. 987 | ZLAAAA
  79. (19 rows)
  80. --
  81. -- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
  82. -- sort +1d -2 +0nr -1
  83. --
  84. SELECT onek.unique1, onek.string4 FROM onek
  85. WHERE onek.unique1 > 980
  86. ORDER BY string4 using <, unique1 using >;
  87. unique1 | string4
  88. ---------+---------
  89. 999 | AAAAxx
  90. 995 | AAAAxx
  91. 983 | AAAAxx
  92. 982 | AAAAxx
  93. 981 | AAAAxx
  94. 998 | HHHHxx
  95. 997 | HHHHxx
  96. 993 | HHHHxx
  97. 990 | HHHHxx
  98. 986 | HHHHxx
  99. 996 | OOOOxx
  100. 991 | OOOOxx
  101. 988 | OOOOxx
  102. 987 | OOOOxx
  103. 985 | OOOOxx
  104. 994 | VVVVxx
  105. 992 | VVVVxx
  106. 989 | VVVVxx
  107. 984 | VVVVxx
  108. (19 rows)
  109. --
  110. -- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
  111. -- sort +1dr -2 +0n -1
  112. --
  113. SELECT onek.unique1, onek.string4 FROM onek
  114. WHERE onek.unique1 > 980
  115. ORDER BY string4 using >, unique1 using <;
  116. unique1 | string4
  117. ---------+---------
  118. 984 | VVVVxx
  119. 989 | VVVVxx
  120. 992 | VVVVxx
  121. 994 | VVVVxx
  122. 985 | OOOOxx
  123. 987 | OOOOxx
  124. 988 | OOOOxx
  125. 991 | OOOOxx
  126. 996 | OOOOxx
  127. 986 | HHHHxx
  128. 990 | HHHHxx
  129. 993 | HHHHxx
  130. 997 | HHHHxx
  131. 998 | HHHHxx
  132. 981 | AAAAxx
  133. 982 | AAAAxx
  134. 983 | AAAAxx
  135. 995 | AAAAxx
  136. 999 | AAAAxx
  137. (19 rows)
  138. --
  139. -- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
  140. -- sort +0nr -1 +1d -2
  141. --
  142. SELECT onek.unique1, onek.string4 FROM onek
  143. WHERE onek.unique1 < 20
  144. ORDER BY unique1 using >, string4 using <;
  145. unique1 | string4
  146. ---------+---------
  147. 19 | OOOOxx
  148. 18 | VVVVxx
  149. 17 | HHHHxx
  150. 16 | OOOOxx
  151. 15 | VVVVxx
  152. 14 | AAAAxx
  153. 13 | OOOOxx
  154. 12 | AAAAxx
  155. 11 | OOOOxx
  156. 10 | AAAAxx
  157. 9 | HHHHxx
  158. 8 | HHHHxx
  159. 7 | VVVVxx
  160. 6 | OOOOxx
  161. 5 | HHHHxx
  162. 4 | HHHHxx
  163. 3 | VVVVxx
  164. 2 | OOOOxx
  165. 1 | OOOOxx
  166. 0 | OOOOxx
  167. (20 rows)
  168. --
  169. -- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
  170. -- sort +0n -1 +1dr -2
  171. --
  172. SELECT onek.unique1, onek.string4 FROM onek
  173. WHERE onek.unique1 < 20
  174. ORDER BY unique1 using <, string4 using >;
  175. unique1 | string4
  176. ---------+---------
  177. 0 | OOOOxx
  178. 1 | OOOOxx
  179. 2 | OOOOxx
  180. 3 | VVVVxx
  181. 4 | HHHHxx
  182. 5 | HHHHxx
  183. 6 | OOOOxx
  184. 7 | VVVVxx
  185. 8 | HHHHxx
  186. 9 | HHHHxx
  187. 10 | AAAAxx
  188. 11 | OOOOxx
  189. 12 | AAAAxx
  190. 13 | OOOOxx
  191. 14 | AAAAxx
  192. 15 | VVVVxx
  193. 16 | OOOOxx
  194. 17 | HHHHxx
  195. 18 | VVVVxx
  196. 19 | OOOOxx
  197. (20 rows)
  198. --
  199. -- test partial btree indexes
  200. --
  201. -- As of 7.2, planner probably won't pick an indexscan without stats,
  202. -- so ANALYZE first. Also, we want to prevent it from picking a bitmapscan
  203. -- followed by sort, because that could hide index ordering problems.
  204. --
  205. ANALYZE onek2;
  206. SET enable_seqscan TO off;
  207. SET enable_bitmapscan TO off;
  208. SET enable_sort TO off;
  209. --
  210. -- awk '{if($1<10){print $0;}else{next;}}' onek.data | sort +0n -1
  211. --
  212. SELECT onek2.* FROM onek2 WHERE onek2.unique1 < 10;
  213. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
  214. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
  215. 0 | 998 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | AAAAAA | KMBAAA | OOOOxx
  216. 1 | 214 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 3 | BAAAAA | GIAAAA | OOOOxx
  217. 2 | 326 | 0 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 4 | 5 | CAAAAA | OMAAAA | OOOOxx
  218. 3 | 431 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 6 | 7 | DAAAAA | PQAAAA | VVVVxx
  219. 4 | 833 | 0 | 0 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 8 | 9 | EAAAAA | BGBAAA | HHHHxx
  220. 5 | 541 | 1 | 1 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 10 | 11 | FAAAAA | VUAAAA | HHHHxx
  221. 6 | 978 | 0 | 2 | 6 | 6 | 6 | 6 | 6 | 6 | 6 | 12 | 13 | GAAAAA | QLBAAA | OOOOxx
  222. 7 | 647 | 1 | 3 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 14 | 15 | HAAAAA | XYAAAA | VVVVxx
  223. 8 | 653 | 0 | 0 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 16 | 17 | IAAAAA | DZAAAA | HHHHxx
  224. 9 | 49 | 1 | 1 | 9 | 9 | 9 | 9 | 9 | 9 | 9 | 18 | 19 | JAAAAA | XBAAAA | HHHHxx
  225. (10 rows)
  226. --
  227. -- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
  228. --
  229. SELECT onek2.unique1, onek2.stringu1 FROM onek2
  230. WHERE onek2.unique1 < 20
  231. ORDER BY unique1 using >;
  232. unique1 | stringu1
  233. ---------+----------
  234. 19 | TAAAAA
  235. 18 | SAAAAA
  236. 17 | RAAAAA
  237. 16 | QAAAAA
  238. 15 | PAAAAA
  239. 14 | OAAAAA
  240. 13 | NAAAAA
  241. 12 | MAAAAA
  242. 11 | LAAAAA
  243. 10 | KAAAAA
  244. 9 | JAAAAA
  245. 8 | IAAAAA
  246. 7 | HAAAAA
  247. 6 | GAAAAA
  248. 5 | FAAAAA
  249. 4 | EAAAAA
  250. 3 | DAAAAA
  251. 2 | CAAAAA
  252. 1 | BAAAAA
  253. 0 | AAAAAA
  254. (20 rows)
  255. --
  256. -- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
  257. --
  258. SELECT onek2.unique1, onek2.stringu1 FROM onek2
  259. WHERE onek2.unique1 > 980;
  260. unique1 | stringu1
  261. ---------+----------
  262. 981 | TLAAAA
  263. 982 | ULAAAA
  264. 983 | VLAAAA
  265. 984 | WLAAAA
  266. 985 | XLAAAA
  267. 986 | YLAAAA
  268. 987 | ZLAAAA
  269. 988 | AMAAAA
  270. 989 | BMAAAA
  271. 990 | CMAAAA
  272. 991 | DMAAAA
  273. 992 | EMAAAA
  274. 993 | FMAAAA
  275. 994 | GMAAAA
  276. 995 | HMAAAA
  277. 996 | IMAAAA
  278. 997 | JMAAAA
  279. 998 | KMAAAA
  280. 999 | LMAAAA
  281. (19 rows)
  282. RESET enable_seqscan;
  283. RESET enable_bitmapscan;
  284. RESET enable_sort;
  285. SELECT two, stringu1, ten, string4
  286. INTO TABLE tmp
  287. FROM onek;
  288. --
  289. -- awk '{print $1,$2;}' person.data |
  290. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
  291. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
  292. -- awk 'BEGIN{FS=" ";}{if(NF!=2){print $4,$5;}else{print;}}' - stud_emp.data
  293. --
  294. -- SELECT name, age FROM person*; ??? check if different
  295. SELECT p.name, p.age FROM person* p;
  296. name | age
  297. ---------+-----
  298. mike | 40
  299. joe | 20
  300. sally | 34
  301. sandra | 19
  302. alex | 30
  303. sue | 50
  304. denise | 24
  305. sarah | 88
  306. teresa | 38
  307. nan | 28
  308. leah | 68
  309. wendy | 78
  310. melissa | 28
  311. joan | 18
  312. mary | 8
  313. jane | 58
  314. liza | 38
  315. jean | 28
  316. jenifer | 38
  317. juanita | 58
  318. susan | 78
  319. zena | 98
  320. martie | 88
  321. chris | 78
  322. pat | 18
  323. zola | 58
  324. louise | 98
  325. edna | 18
  326. bertha | 88
  327. sumi | 38
  328. koko | 88
  329. gina | 18
  330. rean | 48
  331. sharon | 78
  332. paula | 68
  333. julie | 68
  334. belinda | 38
  335. karen | 48
  336. carina | 58
  337. diane | 18
  338. esther | 98
  339. trudy | 88
  340. fanny | 8
  341. carmen | 78
  342. lita | 25
  343. pamela | 48
  344. sandy | 38
  345. trisha | 88
  346. uma | 78
  347. velma | 68
  348. sharon | 25
  349. sam | 30
  350. bill | 20
  351. fred | 28
  352. larry | 60
  353. jeff | 23
  354. cim | 30
  355. linda | 19
  356. (58 rows)
  357. --
  358. -- awk '{print $1,$2;}' person.data |
  359. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
  360. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
  361. -- awk 'BEGIN{FS=" ";}{if(NF!=1){print $4,$5;}else{print;}}' - stud_emp.data |
  362. -- sort +1nr -2
  363. --
  364. SELECT p.name, p.age FROM person* p ORDER BY age using >, name;
  365. name | age
  366. ---------+-----
  367. esther | 98
  368. louise | 98
  369. zena | 98
  370. bertha | 88
  371. koko | 88
  372. martie | 88
  373. sarah | 88
  374. trisha | 88
  375. trudy | 88
  376. carmen | 78
  377. chris | 78
  378. sharon | 78
  379. susan | 78
  380. uma | 78
  381. wendy | 78
  382. julie | 68
  383. leah | 68
  384. paula | 68
  385. velma | 68
  386. larry | 60
  387. carina | 58
  388. jane | 58
  389. juanita | 58
  390. zola | 58
  391. sue | 50
  392. karen | 48
  393. pamela | 48
  394. rean | 48
  395. mike | 40
  396. belinda | 38
  397. jenifer | 38
  398. liza | 38
  399. sandy | 38
  400. sumi | 38
  401. teresa | 38
  402. sally | 34
  403. alex | 30
  404. cim | 30
  405. sam | 30
  406. fred | 28
  407. jean | 28
  408. melissa | 28
  409. nan | 28
  410. lita | 25
  411. sharon | 25
  412. denise | 24
  413. jeff | 23
  414. bill | 20
  415. joe | 20
  416. linda | 19
  417. sandra | 19
  418. diane | 18
  419. edna | 18
  420. gina | 18
  421. joan | 18
  422. pat | 18
  423. fanny | 8
  424. mary | 8
  425. (58 rows)
  426. --
  427. -- Test some cases involving whole-row Var referencing a subquery
  428. --
  429. select foo from (select 1 offset 0) as foo;
  430. foo
  431. -----
  432. (1)
  433. (1 row)
  434. select foo from (select null offset 0) as foo;
  435. foo
  436. -----
  437. ()
  438. (1 row)
  439. select foo from (select 'xyzzy',1,null offset 0) as foo;
  440. foo
  441. ------------
  442. (xyzzy,1,)
  443. (1 row)
  444. --
  445. -- Test VALUES lists
  446. --
  447. select * from onek, (values(147, 'RFAAAA'), (931, 'VJAAAA')) as v (i, j)
  448. WHERE onek.unique1 = v.i and onek.stringu1 = v.j;
  449. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4 | i | j
  450. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------+-----+--------
  451. 147 | 0 | 1 | 3 | 7 | 7 | 7 | 47 | 147 | 147 | 147 | 14 | 15 | RFAAAA | AAAAAA | AAAAxx | 147 | RFAAAA
  452. 931 | 1 | 1 | 3 | 1 | 11 | 1 | 31 | 131 | 431 | 931 | 2 | 3 | VJAAAA | BAAAAA | HHHHxx | 931 | VJAAAA
  453. (2 rows)
  454. -- a more complex case
  455. -- looks like we're coding lisp :-)
  456. select * from onek,
  457. (values ((select i from
  458. (values(10000), (2), (389), (1000), (2000), ((select 10029))) as foo(i)
  459. order by i asc limit 1))) bar (i)
  460. where onek.unique1 = bar.i;
  461. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4 | i
  462. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------+---
  463. 2 | 326 | 0 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 4 | 5 | CAAAAA | OMAAAA | OOOOxx | 2
  464. (1 row)
  465. -- try VALUES in a subquery
  466. select * from onek
  467. where (unique1,ten) in (values (1,1), (20,0), (99,9), (17,99))
  468. order by unique1;
  469. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
  470. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
  471. 1 | 214 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 3 | BAAAAA | GIAAAA | OOOOxx
  472. 20 | 306 | 0 | 0 | 0 | 0 | 0 | 20 | 20 | 20 | 20 | 0 | 1 | UAAAAA | ULAAAA | OOOOxx
  473. 99 | 101 | 1 | 3 | 9 | 19 | 9 | 99 | 99 | 99 | 99 | 18 | 19 | VDAAAA | XDAAAA | HHHHxx
  474. (3 rows)
  475. -- VALUES is also legal as a standalone query or a set-operation member
  476. VALUES (1,2), (3,4+4), (7,77.7);
  477. column1 | column2
  478. ---------+---------
  479. 1 | 2
  480. 3 | 8
  481. 7 | 77.7
  482. (3 rows)
  483. VALUES (1,2), (3,4+4), (7,77.7)
  484. UNION ALL
  485. SELECT 2+2, 57
  486. UNION ALL
  487. TABLE int8_tbl;
  488. column1 | column2
  489. ------------------+-------------------
  490. 1 | 2
  491. 3 | 8
  492. 7 | 77.7
  493. 4 | 57
  494. 123 | 456
  495. 123 | 4567890123456789
  496. 4567890123456789 | 123
  497. 4567890123456789 | 4567890123456789
  498. 4567890123456789 | -4567890123456789
  499. (9 rows)
  500. -- corner case: VALUES with no columns
  501. CREATE TEMP TABLE nocols();
  502. INSERT INTO nocols DEFAULT VALUES;
  503. SELECT * FROM nocols n, LATERAL (VALUES(n.*)) v;
  504. --
  505. (1 row)
  506. --
  507. -- Test ORDER BY options
  508. --
  509. CREATE TEMP TABLE foo (f1 int);
  510. INSERT INTO foo VALUES (42),(3),(10),(7),(null),(null),(1);
  511. SELECT * FROM foo ORDER BY f1;
  512. f1
  513. ----
  514. 1
  515. 3
  516. 7
  517. 10
  518. 42
  519. (7 rows)
  520. SELECT * FROM foo ORDER BY f1 ASC; -- same thing
  521. f1
  522. ----
  523. 1
  524. 3
  525. 7
  526. 10
  527. 42
  528. (7 rows)
  529. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  530. f1
  531. ----
  532. 1
  533. 3
  534. 7
  535. 10
  536. 42
  537. (7 rows)
  538. SELECT * FROM foo ORDER BY f1 DESC;
  539. f1
  540. ----
  541. 42
  542. 10
  543. 7
  544. 3
  545. 1
  546. (7 rows)
  547. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  548. f1
  549. ----
  550. 42
  551. 10
  552. 7
  553. 3
  554. 1
  555. (7 rows)
  556. -- check if indexscans do the right things
  557. CREATE INDEX fooi ON foo (f1);
  558. SET enable_sort = false;
  559. SELECT * FROM foo ORDER BY f1;
  560. f1
  561. ----
  562. 1
  563. 3
  564. 7
  565. 10
  566. 42
  567. (7 rows)
  568. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  569. f1
  570. ----
  571. 1
  572. 3
  573. 7
  574. 10
  575. 42
  576. (7 rows)
  577. SELECT * FROM foo ORDER BY f1 DESC;
  578. f1
  579. ----
  580. 42
  581. 10
  582. 7
  583. 3
  584. 1
  585. (7 rows)
  586. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  587. f1
  588. ----
  589. 42
  590. 10
  591. 7
  592. 3
  593. 1
  594. (7 rows)
  595. DROP INDEX fooi;
  596. CREATE INDEX fooi ON foo (f1 DESC);
  597. SELECT * FROM foo ORDER BY f1;
  598. f1
  599. ----
  600. 1
  601. 3
  602. 7
  603. 10
  604. 42
  605. (7 rows)
  606. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  607. f1
  608. ----
  609. 1
  610. 3
  611. 7
  612. 10
  613. 42
  614. (7 rows)
  615. SELECT * FROM foo ORDER BY f1 DESC;
  616. f1
  617. ----
  618. 42
  619. 10
  620. 7
  621. 3
  622. 1
  623. (7 rows)
  624. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  625. f1
  626. ----
  627. 42
  628. 10
  629. 7
  630. 3
  631. 1
  632. (7 rows)
  633. DROP INDEX fooi;
  634. CREATE INDEX fooi ON foo (f1 DESC NULLS LAST);
  635. SELECT * FROM foo ORDER BY f1;
  636. f1
  637. ----
  638. 1
  639. 3
  640. 7
  641. 10
  642. 42
  643. (7 rows)
  644. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  645. f1
  646. ----
  647. 1
  648. 3
  649. 7
  650. 10
  651. 42
  652. (7 rows)
  653. SELECT * FROM foo ORDER BY f1 DESC;
  654. f1
  655. ----
  656. 42
  657. 10
  658. 7
  659. 3
  660. 1
  661. (7 rows)
  662. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  663. f1
  664. ----
  665. 42
  666. 10
  667. 7
  668. 3
  669. 1
  670. (7 rows)
  671. --
  672. -- Test planning of some cases with partial indexes
  673. --
  674. -- partial index is usable
  675. explain (costs off)
  676. select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  677. QUERY PLAN
  678. -----------------------------------------
  679. Index Scan using onek2_u2_prtl on onek2
  680. Index Cond: (unique2 = 11)
  681. Filter: (stringu1 = 'ATAAAA'::name)
  682. (3 rows)
  683. select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  684. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
  685. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
  686. 494 | 11 | 0 | 2 | 4 | 14 | 4 | 94 | 94 | 494 | 494 | 8 | 9 | ATAAAA | LAAAAA | VVVVxx
  687. (1 row)
  688. -- actually run the query with an analyze to use the partial index
  689. explain (costs off, analyze on, timing off, summary off)
  690. select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  691. QUERY PLAN
  692. -----------------------------------------------------------------
  693. Index Scan using onek2_u2_prtl on onek2 (actual rows=1 loops=1)
  694. Index Cond: (unique2 = 11)
  695. Filter: (stringu1 = 'ATAAAA'::name)
  696. (3 rows)
  697. explain (costs off)
  698. select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  699. QUERY PLAN
  700. -----------------------------------------
  701. Index Scan using onek2_u2_prtl on onek2
  702. Index Cond: (unique2 = 11)
  703. Filter: (stringu1 = 'ATAAAA'::name)
  704. (3 rows)
  705. select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  706. unique2
  707. ---------
  708. 11
  709. (1 row)
  710. -- partial index predicate implies clause, so no need for retest
  711. explain (costs off)
  712. select * from onek2 where unique2 = 11 and stringu1 < 'B';
  713. QUERY PLAN
  714. -----------------------------------------
  715. Index Scan using onek2_u2_prtl on onek2
  716. Index Cond: (unique2 = 11)
  717. (2 rows)
  718. select * from onek2 where unique2 = 11 and stringu1 < 'B';
  719. unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
  720. ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
  721. 494 | 11 | 0 | 2 | 4 | 14 | 4 | 94 | 94 | 494 | 494 | 8 | 9 | ATAAAA | LAAAAA | VVVVxx
  722. (1 row)
  723. explain (costs off)
  724. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  725. QUERY PLAN
  726. ----------------------------------------------
  727. Index Only Scan using onek2_u2_prtl on onek2
  728. Index Cond: (unique2 = 11)
  729. (2 rows)
  730. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  731. unique2
  732. ---------
  733. 11
  734. (1 row)
  735. -- but if it's an update target, must retest anyway
  736. explain (costs off)
  737. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
  738. QUERY PLAN
  739. -----------------------------------------------
  740. LockRows
  741. -> Index Scan using onek2_u2_prtl on onek2
  742. Index Cond: (unique2 = 11)
  743. Filter: (stringu1 < 'B'::name)
  744. (4 rows)
  745. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
  746. unique2
  747. ---------
  748. 11
  749. (1 row)
  750. -- partial index is not applicable
  751. explain (costs off)
  752. select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
  753. QUERY PLAN
  754. -------------------------------------------------------
  755. Seq Scan on onek2
  756. Filter: ((stringu1 < 'C'::name) AND (unique2 = 11))
  757. (2 rows)
  758. select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
  759. unique2
  760. ---------
  761. 11
  762. (1 row)
  763. -- partial index implies clause, but bitmap scan must recheck predicate anyway
  764. SET enable_indexscan TO off;
  765. explain (costs off)
  766. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  767. QUERY PLAN
  768. -------------------------------------------------------------
  769. Bitmap Heap Scan on onek2
  770. Recheck Cond: ((unique2 = 11) AND (stringu1 < 'B'::name))
  771. -> Bitmap Index Scan on onek2_u2_prtl
  772. Index Cond: (unique2 = 11)
  773. (4 rows)
  774. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  775. unique2
  776. ---------
  777. 11
  778. (1 row)
  779. RESET enable_indexscan;
  780. -- check multi-index cases too
  781. explain (costs off)
  782. select unique1, unique2 from onek2
  783. where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
  784. QUERY PLAN
  785. --------------------------------------------------------------------------------
  786. Bitmap Heap Scan on onek2
  787. Recheck Cond: (((unique2 = 11) AND (stringu1 < 'B'::name)) OR (unique1 = 0))
  788. Filter: (stringu1 < 'B'::name)
  789. -> BitmapOr
  790. -> Bitmap Index Scan on onek2_u2_prtl
  791. Index Cond: (unique2 = 11)
  792. -> Bitmap Index Scan on onek2_u1_prtl
  793. Index Cond: (unique1 = 0)
  794. (8 rows)
  795. select unique1, unique2 from onek2
  796. where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
  797. unique1 | unique2
  798. ---------+---------
  799. 494 | 11
  800. 0 | 998
  801. (2 rows)
  802. explain (costs off)
  803. select unique1, unique2 from onek2
  804. where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
  805. QUERY PLAN
  806. --------------------------------------------------------------------------------
  807. Bitmap Heap Scan on onek2
  808. Recheck Cond: (((unique2 = 11) AND (stringu1 < 'B'::name)) OR (unique1 = 0))
  809. -> BitmapOr
  810. -> Bitmap Index Scan on onek2_u2_prtl
  811. Index Cond: (unique2 = 11)
  812. -> Bitmap Index Scan on onek2_u1_prtl
  813. Index Cond: (unique1 = 0)
  814. (7 rows)
  815. select unique1, unique2 from onek2
  816. where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
  817. unique1 | unique2
  818. ---------+---------
  819. 494 | 11
  820. 0 | 998
  821. (2 rows)
  822. --
  823. -- Test some corner cases that have been known to confuse the planner
  824. --
  825. -- ORDER BY on a constant doesn't really need any sorting
  826. SELECT 1 AS x ORDER BY x;
  827. x
  828. ---
  829. 1
  830. (1 row)
  831. -- But ORDER BY on a set-valued expression does
  832. create function sillysrf(int) returns setof int as
  833. 'values (1),(10),(2),($1)' language sql immutable;
  834. select sillysrf(42);
  835. sillysrf
  836. ----------
  837. 1
  838. 10
  839. 2
  840. 42
  841. (4 rows)
  842. select sillysrf(-1) order by 1;
  843. sillysrf
  844. ----------
  845. -1
  846. 1
  847. 2
  848. 10
  849. (4 rows)
  850. drop function sillysrf(int);
  851. -- X = X isn't a no-op, it's effectively X IS NOT NULL assuming = is strict
  852. -- (see bug #5084)
  853. select * from (values (2),(null),(1)) v(k) where k = k order by k;
  854. k
  855. ---
  856. 1
  857. 2
  858. (2 rows)
  859. select * from (values (2),(null),(1)) v(k) where k = k;
  860. k
  861. ---
  862. 2
  863. 1
  864. (2 rows)
  865. -- Test partitioned tables with no partitions, which should be handled the
  866. -- same as the non-inheritance case when expanding its RTE.
  867. create table list_parted_tbl (a int,b int) partition by list (a);
  868. create table list_parted_tbl1 partition of list_parted_tbl
  869. for values in (1) partition by list(b);
  870. explain (costs off) select * from list_parted_tbl;
  871. QUERY PLAN
  872. --------------------------
  873. Result
  874. One-Time Filter: false
  875. (2 rows)
  876. drop table list_parted_tbl;