select.sql 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. --
  11. -- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
  12. --
  13. SELECT onek.unique1, onek.stringu1 FROM onek
  14. WHERE onek.unique1 < 20
  15. ORDER BY unique1 using >;
  16. --
  17. -- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
  18. --
  19. SELECT onek.unique1, onek.stringu1 FROM onek
  20. WHERE onek.unique1 > 980
  21. ORDER BY stringu1 using <;
  22. --
  23. -- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
  24. -- sort +1d -2 +0nr -1
  25. --
  26. SELECT onek.unique1, onek.string4 FROM onek
  27. WHERE onek.unique1 > 980
  28. ORDER BY string4 using <, unique1 using >;
  29. --
  30. -- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
  31. -- sort +1dr -2 +0n -1
  32. --
  33. SELECT onek.unique1, onek.string4 FROM onek
  34. WHERE onek.unique1 > 980
  35. ORDER BY string4 using >, unique1 using <;
  36. --
  37. -- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
  38. -- sort +0nr -1 +1d -2
  39. --
  40. SELECT onek.unique1, onek.string4 FROM onek
  41. WHERE onek.unique1 < 20
  42. ORDER BY unique1 using >, string4 using <;
  43. --
  44. -- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
  45. -- sort +0n -1 +1dr -2
  46. --
  47. SELECT onek.unique1, onek.string4 FROM onek
  48. WHERE onek.unique1 < 20
  49. ORDER BY unique1 using <, string4 using >;
  50. --
  51. -- test partial btree indexes
  52. --
  53. -- As of 7.2, planner probably won't pick an indexscan without stats,
  54. -- so ANALYZE first. Also, we want to prevent it from picking a bitmapscan
  55. -- followed by sort, because that could hide index ordering problems.
  56. --
  57. ANALYZE onek2;
  58. SET enable_seqscan TO off;
  59. SET enable_bitmapscan TO off;
  60. SET enable_sort TO off;
  61. --
  62. -- awk '{if($1<10){print $0;}else{next;}}' onek.data | sort +0n -1
  63. --
  64. SELECT onek2.* FROM onek2 WHERE onek2.unique1 < 10;
  65. --
  66. -- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
  67. --
  68. SELECT onek2.unique1, onek2.stringu1 FROM onek2
  69. WHERE onek2.unique1 < 20
  70. ORDER BY unique1 using >;
  71. --
  72. -- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
  73. --
  74. SELECT onek2.unique1, onek2.stringu1 FROM onek2
  75. WHERE onek2.unique1 > 980;
  76. RESET enable_seqscan;
  77. RESET enable_bitmapscan;
  78. RESET enable_sort;
  79. SELECT two, stringu1, ten, string4
  80. INTO TABLE tmp
  81. FROM onek;
  82. --
  83. -- awk '{print $1,$2;}' person.data |
  84. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
  85. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
  86. -- awk 'BEGIN{FS=" ";}{if(NF!=2){print $4,$5;}else{print;}}' - stud_emp.data
  87. --
  88. -- SELECT name, age FROM person*; ??? check if different
  89. SELECT p.name, p.age FROM person* p;
  90. --
  91. -- awk '{print $1,$2;}' person.data |
  92. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
  93. -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
  94. -- awk 'BEGIN{FS=" ";}{if(NF!=1){print $4,$5;}else{print;}}' - stud_emp.data |
  95. -- sort +1nr -2
  96. --
  97. SELECT p.name, p.age FROM person* p ORDER BY age using >, name;
  98. --
  99. -- Test some cases involving whole-row Var referencing a subquery
  100. --
  101. select foo from (select 1 offset 0) as foo;
  102. select foo from (select null offset 0) as foo;
  103. select foo from (select 'xyzzy',1,null offset 0) as foo;
  104. --
  105. -- Test VALUES lists
  106. --
  107. select * from onek, (values(147, 'RFAAAA'), (931, 'VJAAAA')) as v (i, j)
  108. WHERE onek.unique1 = v.i and onek.stringu1 = v.j;
  109. -- a more complex case
  110. -- looks like we're coding lisp :-)
  111. select * from onek,
  112. (values ((select i from
  113. (values(10000), (2), (389), (1000), (2000), ((select 10029))) as foo(i)
  114. order by i asc limit 1))) bar (i)
  115. where onek.unique1 = bar.i;
  116. -- try VALUES in a subquery
  117. select * from onek
  118. where (unique1,ten) in (values (1,1), (20,0), (99,9), (17,99))
  119. order by unique1;
  120. -- VALUES is also legal as a standalone query or a set-operation member
  121. VALUES (1,2), (3,4+4), (7,77.7);
  122. VALUES (1,2), (3,4+4), (7,77.7)
  123. UNION ALL
  124. SELECT 2+2, 57
  125. UNION ALL
  126. TABLE int8_tbl;
  127. -- corner case: VALUES with no columns
  128. CREATE TEMP TABLE nocols();
  129. INSERT INTO nocols DEFAULT VALUES;
  130. SELECT * FROM nocols n, LATERAL (VALUES(n.*)) v;
  131. --
  132. -- Test ORDER BY options
  133. --
  134. CREATE TEMP TABLE foo (f1 int);
  135. INSERT INTO foo VALUES (42),(3),(10),(7),(null),(null),(1);
  136. SELECT * FROM foo ORDER BY f1;
  137. SELECT * FROM foo ORDER BY f1 ASC; -- same thing
  138. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  139. SELECT * FROM foo ORDER BY f1 DESC;
  140. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  141. -- check if indexscans do the right things
  142. CREATE INDEX fooi ON foo (f1);
  143. SET enable_sort = false;
  144. SELECT * FROM foo ORDER BY f1;
  145. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  146. SELECT * FROM foo ORDER BY f1 DESC;
  147. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  148. DROP INDEX fooi;
  149. CREATE INDEX fooi ON foo (f1 DESC);
  150. SELECT * FROM foo ORDER BY f1;
  151. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  152. SELECT * FROM foo ORDER BY f1 DESC;
  153. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  154. DROP INDEX fooi;
  155. CREATE INDEX fooi ON foo (f1 DESC NULLS LAST);
  156. SELECT * FROM foo ORDER BY f1;
  157. SELECT * FROM foo ORDER BY f1 NULLS FIRST;
  158. SELECT * FROM foo ORDER BY f1 DESC;
  159. SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
  160. --
  161. -- Test planning of some cases with partial indexes
  162. --
  163. -- partial index is usable
  164. explain (costs off)
  165. select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  166. select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  167. -- actually run the query with an analyze to use the partial index
  168. explain (costs off, analyze on, timing off, summary off)
  169. select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  170. explain (costs off)
  171. select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  172. select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
  173. -- partial index predicate implies clause, so no need for retest
  174. explain (costs off)
  175. select * from onek2 where unique2 = 11 and stringu1 < 'B';
  176. select * from onek2 where unique2 = 11 and stringu1 < 'B';
  177. explain (costs off)
  178. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  179. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  180. -- but if it's an update target, must retest anyway
  181. explain (costs off)
  182. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
  183. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
  184. -- partial index is not applicable
  185. explain (costs off)
  186. select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
  187. select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
  188. -- partial index implies clause, but bitmap scan must recheck predicate anyway
  189. SET enable_indexscan TO off;
  190. explain (costs off)
  191. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  192. select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
  193. RESET enable_indexscan;
  194. -- check multi-index cases too
  195. explain (costs off)
  196. select unique1, unique2 from onek2
  197. where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
  198. select unique1, unique2 from onek2
  199. where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
  200. explain (costs off)
  201. select unique1, unique2 from onek2
  202. where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
  203. select unique1, unique2 from onek2
  204. where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
  205. --
  206. -- Test some corner cases that have been known to confuse the planner
  207. --
  208. -- ORDER BY on a constant doesn't really need any sorting
  209. SELECT 1 AS x ORDER BY x;
  210. -- But ORDER BY on a set-valued expression does
  211. create function sillysrf(int) returns setof int as
  212. 'values (1),(10),(2),($1)' language sql immutable;
  213. select sillysrf(42);
  214. select sillysrf(-1) order by 1;
  215. drop function sillysrf(int);
  216. -- X = X isn't a no-op, it's effectively X IS NOT NULL assuming = is strict
  217. -- (see bug #5084)
  218. select * from (values (2),(null),(1)) v(k) where k = k order by k;
  219. select * from (values (2),(null),(1)) v(k) where k = k;
  220. -- Test partitioned tables with no partitions, which should be handled the
  221. -- same as the non-inheritance case when expanding its RTE.
  222. create table list_parted_tbl (a int,b int) partition by list (a);
  223. create table list_parted_tbl1 partition of list_parted_tbl
  224. for values in (1) partition by list(b);
  225. explain (costs off) select * from list_parted_tbl;
  226. drop table list_parted_tbl;