limit.out 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. two | unique1 | unique2 | stringu1
  9. -----+---------+---------+----------
  10. | 51 | 76 | ZBAAAA
  11. | 52 | 985 | ACAAAA
  12. (2 rows)
  13. SELECT ''::text AS five, unique1, unique2, stringu1
  14. FROM onek WHERE unique1 > 60
  15. ORDER BY unique1 LIMIT 5;
  16. five | unique1 | unique2 | stringu1
  17. ------+---------+---------+----------
  18. | 61 | 560 | JCAAAA
  19. | 62 | 633 | KCAAAA
  20. | 63 | 296 | LCAAAA
  21. | 64 | 479 | MCAAAA
  22. | 65 | 64 | NCAAAA
  23. (5 rows)
  24. SELECT ''::text AS two, unique1, unique2, stringu1
  25. FROM onek WHERE unique1 > 60 AND unique1 < 63
  26. ORDER BY unique1 LIMIT 5;
  27. two | unique1 | unique2 | stringu1
  28. -----+---------+---------+----------
  29. | 61 | 560 | JCAAAA
  30. | 62 | 633 | KCAAAA
  31. (2 rows)
  32. SELECT ''::text AS three, unique1, unique2, stringu1
  33. FROM onek WHERE unique1 > 100
  34. ORDER BY unique1 LIMIT 3 OFFSET 20;
  35. three | unique1 | unique2 | stringu1
  36. -------+---------+---------+----------
  37. | 121 | 700 | REAAAA
  38. | 122 | 519 | SEAAAA
  39. | 123 | 777 | TEAAAA
  40. (3 rows)
  41. SELECT ''::text AS zero, unique1, unique2, stringu1
  42. FROM onek WHERE unique1 < 50
  43. ORDER BY unique1 DESC LIMIT 8 OFFSET 99;
  44. zero | unique1 | unique2 | stringu1
  45. ------+---------+---------+----------
  46. (0 rows)
  47. SELECT ''::text AS eleven, unique1, unique2, stringu1
  48. FROM onek WHERE unique1 < 50
  49. ORDER BY unique1 DESC LIMIT 20 OFFSET 39;
  50. eleven | unique1 | unique2 | stringu1
  51. --------+---------+---------+----------
  52. | 10 | 520 | KAAAAA
  53. | 9 | 49 | JAAAAA
  54. | 8 | 653 | IAAAAA
  55. | 7 | 647 | HAAAAA
  56. | 6 | 978 | GAAAAA
  57. | 5 | 541 | FAAAAA
  58. | 4 | 833 | EAAAAA
  59. | 3 | 431 | DAAAAA
  60. | 2 | 326 | CAAAAA
  61. | 1 | 214 | BAAAAA
  62. | 0 | 998 | AAAAAA
  63. (11 rows)
  64. SELECT ''::text AS ten, unique1, unique2, stringu1
  65. FROM onek
  66. ORDER BY unique1 OFFSET 990;
  67. ten | unique1 | unique2 | stringu1
  68. -----+---------+---------+----------
  69. | 990 | 369 | CMAAAA
  70. | 991 | 426 | DMAAAA
  71. | 992 | 363 | EMAAAA
  72. | 993 | 661 | FMAAAA
  73. | 994 | 695 | GMAAAA
  74. | 995 | 144 | HMAAAA
  75. | 996 | 258 | IMAAAA
  76. | 997 | 21 | JMAAAA
  77. | 998 | 549 | KMAAAA
  78. | 999 | 152 | LMAAAA
  79. (10 rows)
  80. SELECT ''::text AS five, unique1, unique2, stringu1
  81. FROM onek
  82. ORDER BY unique1 OFFSET 990 LIMIT 5;
  83. five | unique1 | unique2 | stringu1
  84. ------+---------+---------+----------
  85. | 990 | 369 | CMAAAA
  86. | 991 | 426 | DMAAAA
  87. | 992 | 363 | EMAAAA
  88. | 993 | 661 | FMAAAA
  89. | 994 | 695 | GMAAAA
  90. (5 rows)
  91. SELECT ''::text AS five, unique1, unique2, stringu1
  92. FROM onek
  93. ORDER BY unique1 LIMIT 5 OFFSET 900;
  94. five | unique1 | unique2 | stringu1
  95. ------+---------+---------+----------
  96. | 900 | 913 | QIAAAA
  97. | 901 | 931 | RIAAAA
  98. | 902 | 702 | SIAAAA
  99. | 903 | 641 | TIAAAA
  100. | 904 | 793 | UIAAAA
  101. (5 rows)
  102. -- Test null limit and offset. The planner would discard a simple null
  103. -- constant, so to ensure executor is exercised, do this:
  104. select * from int8_tbl limit (case when random() < 0.5 then null::bigint end);
  105. q1 | q2
  106. ------------------+-------------------
  107. 123 | 456
  108. 123 | 4567890123456789
  109. 4567890123456789 | 123
  110. 4567890123456789 | 4567890123456789
  111. 4567890123456789 | -4567890123456789
  112. (5 rows)
  113. select * from int8_tbl offset (case when random() < 0.5 then null::bigint end);
  114. q1 | q2
  115. ------------------+-------------------
  116. 123 | 456
  117. 123 | 4567890123456789
  118. 4567890123456789 | 123
  119. 4567890123456789 | 4567890123456789
  120. 4567890123456789 | -4567890123456789
  121. (5 rows)
  122. -- Test assorted cases involving backwards fetch from a LIMIT plan node
  123. begin;
  124. rollback;
  125. select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
  126. from tenk1 group by thousand order by thousand limit 3;
  127. s1 | s2
  128. -------+-------
  129. 45000 | 45000
  130. 45010 | 45010
  131. 45020 | 45020
  132. (3 rows)
  133. SELECT thousand
  134. FROM onek WHERE thousand < 5
  135. ORDER BY thousand FETCH FIRST 2 ROW ONLY;
  136. thousand
  137. ----------
  138. 0
  139. 0
  140. (2 rows)
  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. ERROR: SKIP LOCKED and WITH TIES options cannot be used together
  146. -- should fail
  147. SELECT ''::text AS two, unique1, unique2, stringu1
  148. FROM onek WHERE unique1 > 50
  149. FETCH FIRST 2 ROW WITH TIES;
  150. ERROR: WITH TIES cannot be specified without ORDER BY clause
  151. -- leave these views