limit.sql 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. --
  2. -- LIMIT
  3. -- Check the LIMIT/OFFSET feature of SELECT
  4. --
  5. SELECT ''::text AS two, unique1, unique2, stringu1
  6. FROM onek WHERE unique1 > 50
  7. ORDER BY unique1 LIMIT 2;
  8. SELECT ''::text AS five, unique1, unique2, stringu1
  9. FROM onek WHERE unique1 > 60
  10. ORDER BY unique1 LIMIT 5;
  11. SELECT ''::text AS two, unique1, unique2, stringu1
  12. FROM onek WHERE unique1 > 60 AND unique1 < 63
  13. ORDER BY unique1 LIMIT 5;
  14. SELECT ''::text AS three, unique1, unique2, stringu1
  15. FROM onek WHERE unique1 > 100
  16. ORDER BY unique1 LIMIT 3 OFFSET 20;
  17. SELECT ''::text AS zero, unique1, unique2, stringu1
  18. FROM onek WHERE unique1 < 50
  19. ORDER BY unique1 DESC LIMIT 8 OFFSET 99;
  20. SELECT ''::text AS eleven, unique1, unique2, stringu1
  21. FROM onek WHERE unique1 < 50
  22. ORDER BY unique1 DESC LIMIT 20 OFFSET 39;
  23. SELECT ''::text AS ten, unique1, unique2, stringu1
  24. FROM onek
  25. ORDER BY unique1 OFFSET 990;
  26. SELECT ''::text AS five, unique1, unique2, stringu1
  27. FROM onek
  28. ORDER BY unique1 OFFSET 990 LIMIT 5;
  29. SELECT ''::text AS five, unique1, unique2, stringu1
  30. FROM onek
  31. ORDER BY unique1 LIMIT 5 OFFSET 900;
  32. -- Test null limit and offset. The planner would discard a simple null
  33. -- constant, so to ensure executor is exercised, do this:
  34. select * from int8_tbl limit (case when random() < 0.5 then null::bigint end);
  35. select * from int8_tbl offset (case when random() < 0.5 then null::bigint end);
  36. -- Test assorted cases involving backwards fetch from a LIMIT plan node
  37. begin;
  38. declare c1 cursor for select * from int8_tbl limit 10;
  39. fetch all in c1;
  40. fetch 1 in c1;
  41. fetch backward 1 in c1;
  42. fetch backward all in c1;
  43. fetch backward 1 in c1;
  44. fetch all in c1;
  45. declare c2 cursor for select * from int8_tbl limit 3;
  46. fetch all in c2;
  47. fetch 1 in c2;
  48. fetch backward 1 in c2;
  49. fetch backward all in c2;
  50. fetch backward 1 in c2;
  51. fetch all in c2;
  52. declare c3 cursor for select * from int8_tbl offset 3;
  53. fetch all in c3;
  54. fetch 1 in c3;
  55. fetch backward 1 in c3;
  56. fetch backward all in c3;
  57. fetch backward 1 in c3;
  58. fetch all in c3;
  59. declare c4 cursor for select * from int8_tbl offset 10;
  60. fetch all in c4;
  61. fetch 1 in c4;
  62. fetch backward 1 in c4;
  63. fetch backward all in c4;
  64. fetch backward 1 in c4;
  65. fetch all in c4;
  66. declare c5 cursor for select * from int8_tbl order by q1 fetch first 2 rows with ties;
  67. fetch all in c5;
  68. fetch 1 in c5;
  69. fetch backward 1 in c5;
  70. fetch backward 1 in c5;
  71. fetch all in c5;
  72. fetch backward all in c5;
  73. fetch all in c5;
  74. fetch backward all in c5;
  75. rollback;
  76. -- Stress test for variable LIMIT in conjunction with bounded-heap sorting
  77. SELECT
  78. (SELECT n
  79. FROM (VALUES (1)) AS x,
  80. (SELECT n FROM generate_series(1,10) AS n
  81. ORDER BY n LIMIT 1 OFFSET s-1) AS y) AS z
  82. FROM generate_series(1,10) AS s;
  83. --
  84. -- Test behavior of volatile and set-returning functions in conjunction
  85. -- with ORDER BY and LIMIT.
  86. --
  87. create temp sequence testseq;
  88. explain (verbose, costs off)
  89. select unique1, unique2, nextval('testseq')
  90. from tenk1 order by unique2 limit 10;
  91. select unique1, unique2, nextval('testseq')
  92. from tenk1 order by unique2 limit 10;
  93. select currval('testseq');
  94. explain (verbose, costs off)
  95. select unique1, unique2, nextval('testseq')
  96. from tenk1 order by tenthous limit 10;
  97. select unique1, unique2, nextval('testseq')
  98. from tenk1 order by tenthous limit 10;
  99. select currval('testseq');
  100. explain (verbose, costs off)
  101. select unique1, unique2, generate_series(1,10)
  102. from tenk1 order by unique2 limit 7;
  103. select unique1, unique2, generate_series(1,10)
  104. from tenk1 order by unique2 limit 7;
  105. explain (verbose, costs off)
  106. select unique1, unique2, generate_series(1,10)
  107. from tenk1 order by tenthous limit 7;
  108. select unique1, unique2, generate_series(1,10)
  109. from tenk1 order by tenthous limit 7;
  110. -- use of random() is to keep planner from folding the expressions together
  111. explain (verbose, costs off)
  112. select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2;
  113. select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2;
  114. explain (verbose, costs off)
  115. select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2
  116. order by s2 desc;
  117. select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2
  118. order by s2 desc;
  119. -- test for failure to set all aggregates' aggtranstype
  120. explain (verbose, costs off)
  121. select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
  122. from tenk1 group by thousand order by thousand limit 3;
  123. select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
  124. from tenk1 group by thousand order by thousand limit 3;
  125. --
  126. -- FETCH FIRST
  127. -- Check the WITH TIES clause
  128. --
  129. SELECT thousand
  130. FROM onek WHERE thousand < 5
  131. ORDER BY thousand FETCH FIRST 2 ROW WITH TIES;
  132. SELECT thousand
  133. FROM onek WHERE thousand < 5
  134. ORDER BY thousand FETCH FIRST ROWS WITH TIES;
  135. SELECT thousand
  136. FROM onek WHERE thousand < 5
  137. ORDER BY thousand FETCH FIRST 1 ROW WITH TIES;
  138. SELECT thousand
  139. FROM onek WHERE thousand < 5
  140. ORDER BY thousand FETCH FIRST 2 ROW ONLY;
  141. -- SKIP LOCKED and WITH TIES are incompatible
  142. SELECT thousand
  143. FROM onek WHERE thousand < 5
  144. ORDER BY thousand FETCH FIRST 1 ROW WITH TIES FOR UPDATE SKIP LOCKED;
  145. -- should fail
  146. SELECT ''::text AS two, unique1, unique2, stringu1
  147. FROM onek WHERE unique1 > 50
  148. FETCH FIRST 2 ROW WITH TIES;
  149. -- test ruleutils
  150. CREATE VIEW limit_thousand_v_1 AS SELECT thousand FROM onek WHERE thousand < 995
  151. ORDER BY thousand FETCH FIRST 5 ROWS WITH TIES OFFSET 10;
  152. \d+ limit_thousand_v_1
  153. CREATE VIEW limit_thousand_v_2 AS SELECT thousand FROM onek WHERE thousand < 995
  154. ORDER BY thousand OFFSET 10 FETCH FIRST 5 ROWS ONLY;
  155. \d+ limit_thousand_v_2
  156. CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
  157. ORDER BY thousand FETCH FIRST NULL ROWS WITH TIES; -- fails
  158. CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
  159. ORDER BY thousand FETCH FIRST (NULL+1) ROWS WITH TIES;
  160. \d+ limit_thousand_v_3
  161. CREATE VIEW limit_thousand_v_4 AS SELECT thousand FROM onek WHERE thousand < 995
  162. ORDER BY thousand FETCH FIRST NULL ROWS ONLY;
  163. \d+ limit_thousand_v_4
  164. -- leave these views